summaryrefslogtreecommitdiffhomepage
path: root/hdl.html.markdown
blob: cad0781775a62b65a4b646c126c502795c92ea4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
---
language: hdl
filename: learnhdl.hdl
contributors:
  - ["Jack Smith", "https://github.com/JSmithTech2019"]
---

HDL (hardware description language) is a specialized language used to describe the structure/behavior of real world circuits. 

It is used by circuit designers to simulate circuits and logic prior to wiring and fabricating a hardware circuit.

HDL allows circuit designers to simulate circuits at a high level without being connected to specific components.

## Basic building blocks & introduction to the language---
This programming language is built by simulating hardware chips and wiring. Normal programming functions are replaced with specialized chips that are added to the current wiring desing. Every base chip must be written as it's own file and imported to be used in the current chip, though they may be reused as often as desired.

```verilog
// Single line comments start with two forward slashes.

/*
 * Multiline comments can be written using '/*' and 'star/'.
 * These are often used as comments.
 *
 * Note that they cannot be nested and will end at the first 'star/'.
 */

////////////////////////////////////////////////////
// 1. Chips & Components
////////////////////////////////////////////////////
/* 
 * Unlike other languages HDL creates an individual chip (function) per file
 * These are defined with a name, input arguments, output arguments
 * and finally the parts/logic of that specific chip.
 */

// Note CHIP is capitalized, the chip name does not need to be.
CHIP Ex {
    IN  a,  // Single bit (0 or 1) variable.
        c[16];  // 16 bit variable bus of single bit values.

    OUT out[16],  // 16 bit variable bus output.
        carry;  // Single bit output variable

    PARTS:
    // The functional components of the chip.
}

// Lines are ended with semicolons but can be continued using commas. The
// whitespace is ignored.



////////////////////////////////////////////////////
// 2. Inputs, Outputs, & Variables
////////////////////////////////////////////////////
/*
 * Variables and IO are treated as pins/wires and can carry a single bit
 * of data (0 or 1).
 */

// Hardware works on low level 0's and 1's, in order to use a constant
// high or low we use the terms true and false.
a=false; // This is a 0 value.
b=true; // This is a 1 value.

// Inputs and outputs can be defined as single bits
IN a, b; // Creates two single bit inputs

// They can also be defined as busses act as arrays where each
// index can contain a single bit value.
OUT c[16]; // Creates a 16 bit output array.

// Bussed values can be accessed using brackets
a[0] // The first indexed value in the bus a.
a[0..3] // The first 4 values in the a bus.
// Values can also be passed in entirety. For example if the function 
// foo() takes an 8 bit input bus and outputs a 2 bit bus:
foo(in=a[0..7], out=c); // C is now a 2 bit internal bus


// Note that internally defined busses cannot be subbussed!
// To access these elements, output or input them seperately:
foo(in[0]=false, in[1..7]=a[0..6], out[0]=out1, out[1]=out2);
// out1 and out2 can then be passed into other circuits within the design.



////////////////////////////////////////////////////
// Combining Subsystems
////////////////////////////////////////////////////
/*
 * HDL relies heavily on using smaller "building block" chips to then be
 * added into larger and more complex designs. Creating the smaller components
 * and then adding them to the larger circuit allows for fewer lines of code
 * as well as reduction in total rewriting of code.
 */

// We are writing the function AND that checks if inputs I and K are both one.
// To implement this chip we will use the built in NAND gate as well as design
// a custom NOT gate to invert a single input.

// First we construct the Negation (not) chip. We will use the logically
// complete gate NAND that is built in for this task.
CHIP Not {
    IN i; // Not gates only take one single bit input.
    OUT o; // The negated value of a.

    PARTS:
    // Add the input to the built in chip, which then sends output to the NOT
    // output. This effectively negates the given value.
    Nand(a=i, b=i, out=o);
}

// By using the built in NAND gate we were able to construct a NOT gate
// that works like a real world hardware logic chip. Now we must construct
// the AND gate using these two gate primitives.

// We define a two input, single output AND gate:
CHIP And {
    IN i, k; // Two single bit inputs.
    OUT o; // One single bit output.

    PARTS:
    // Insert I and K into the nand gate and store the output in an internal
    // wire called notOut.
    Nand(a=i,b=b,out=notOut);

    // Use the not gate we constructed to invert notOut and send to the AND
    // output.
    Not(in=notOut,out=o);
}

// Easy! Now we can use Nand, And, and Not gates in higher level circuits.
// Many of these low level components are built in to HDL but any chip can
// be written as a submodule and used in larger designs.
```

## Test Files
When working with the nand2tetris hardware simulator chips written using HDL will
then be processed against test and comparison files to test functionality of the
simulated chip versus the expected output. To do this a test file will be loaded
into the hardware simulator and run against the simulated hardware.

```verilog
// First the chip the test file is written for is loaded
load <chip name>.hdl

// We set the output file for the simulated chip output as well as the comparison
// file that it will be tested against. We also specify what the output is
// expected to look like. In this case there will be two output columns, each
// will be buffered by a single space on either side and 4 binary values in
// the center of each column.
output-file <chip name>.out,
compare-to <chip name>.cmp,
output-list in%B1.4.1 out%B1.4.1;

// Then we set initial values for inputs to the chip. For example
set enable1 1, // set input enable1 to 1
set enable2 0, // set input enable2 to 0

// The clock is also controlled in the test file using tick and tock. Tick is a
// positive pulse and tock takes the clock back to 0. Clock cycles can be run
// multiple times in a row with no other changes to inputs or outputs.
tick,
tock,

// Finally we output the first expected value (from the test file) which is then
// compared with the first line of real output from our HDL circuit. This output
// can be viewed in the <chip name>.out file.
output;

// An example of <chip name>, a chip that takes in a 4 bit value as input and
// adds 1 to that value could have the following as test code:

// Set the input value to 0000, clock pulse, compare output from cmp file to actual out.
set in %B0000,
tick,
tock,
output;

// Set the input value to 0110, clock pulse, compare output from cmp file to actual out.
set in %B0110,
tick,
tock,
output;

// The expected output for case 1 should be 0001 and case 2 expects 0111, lets
// learn a little more about comparison files before finalizing our lesson.
```

## Comparison Files
Now lets take a look at comparison files, the files that hold what the test file
compares with the actual output of an HDL chip in the hardware simulator!

```verilog
// Like the <chip name> example above, the structure of the comparison file
// would look something like this
|  in  | out  |
| 0000 | 0001 |
| 0110 | 0111 |

// Notice how the input values specified in the test case are equivalent to the
// `in` column of the comparison file, and that the space buffer is 1 on either side.

// If the output from the HDL code we not this, such as the output below, then the
// test will fail and the user will know that the simulated chip is not correctly designed.
|  in  | out  |
| 0000 | 0001 |
| 0110 | 0110 | // Error! The chip did not add 1 here, something went wrong.


```

This is incredibly useful as it allows designers to simulate chip logic prior to
fabricating real life hardware and identify problems in their designs. Be warned that
errors in the test or comparison files can lead to both false positives and also
the more damaging false negatives so ensure that the logic is sound behind the test
creation.


Good luck and happy coding!

## Resources

* [From Nand To Tetris](https://www.nand2tetris.org)

## Further Reading

* [Hardware Description Language](https://en.wikipedia.org/wiki/Hardware_description_language)

* [HDL Programming Fundamentals](https://www.electronicdesign.com/products/hdl-programming-fundamentals)