1 /*
2  * Copyright (c) 2016 Helmut Neemann
3  * Use of this source code is governed by the GPL v3 license
4  * that can be found in the LICENSE file.
5  */
6 package de.neemann.digital.core.wiring;
7 
8 import de.neemann.digital.core.Node;
9 import de.neemann.digital.core.NodeException;
10 import de.neemann.digital.core.ObservableValue;
11 import de.neemann.digital.core.ObservableValues;
12 import de.neemann.digital.core.element.Element;
13 import de.neemann.digital.core.element.ElementAttributes;
14 import de.neemann.digital.core.element.ElementTypeDescription;
15 import de.neemann.digital.core.element.Keys;
16 import de.neemann.digital.core.stats.Countable;
17 import de.neemann.digital.lang.Lang;
18 
19 import java.util.ArrayList;
20 
21 import static de.neemann.digital.core.element.PinInfo.input;
22 
23 
24 /**
25  * The decoder
26  */
27 public class Decoder extends Node implements Element, Countable {
28 
29     private final int selectorBits;
30     private final ObservableValues output;
31     private ObservableValue selector;
32 
33     private int oldSelectorValue;
34     private int selectorValue;
35 
36     /**
37      * The Decoder description
38      */
39     public static final ElementTypeDescription DESCRIPTION = new ElementTypeDescription(Decoder.class,
40             input("sel"))
41             .addAttribute(Keys.ROTATE)
42             .addAttribute(Keys.SELECTOR_BITS)
43             .addAttribute(Keys.FLIP_SEL_POSITON)
44             .supportsHDL();
45 
46     /**
47      * Creates a new instance
48      *
49      * @param attributes the attributes
50      */
Decoder(ElementAttributes attributes)51     public Decoder(ElementAttributes attributes) {
52         this.selectorBits = attributes.get(Keys.SELECTOR_BITS);
53         int outputs = 1 << selectorBits;
54         ArrayList<ObservableValue> o = new ArrayList<>(outputs);
55         for (int i = 0; i < outputs; i++)
56             o.add(new ObservableValue("out_" + i, 1).setValue(0).setDescription(Lang.get("elem_Decoder_output", i)));
57         output = new ObservableValues(o);
58     }
59 
60     @Override
getOutputs()61     public ObservableValues getOutputs() {
62         return output;
63     }
64 
65     @Override
readInputs()66     public void readInputs() throws NodeException {
67         selectorValue = (int) selector.getValue();
68     }
69 
70     @Override
writeOutputs()71     public void writeOutputs() throws NodeException {
72         output.get(oldSelectorValue).setValue(0);
73         output.get(selectorValue).setValue(1);
74         oldSelectorValue = selectorValue;
75     }
76 
77     @Override
setInputs(ObservableValues inputs)78     public void setInputs(ObservableValues inputs) throws NodeException {
79         selector = inputs.get(0).addObserverToValue(this).checkBits(selectorBits, this);
80     }
81 
82     @Override
getDataBits()83     public int getDataBits() {
84         return 1;
85     }
86 
87     @Override
getAddrBits()88     public int getAddrBits() {
89         return selectorBits;
90     }
91 }
92