You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: hardware.md
+74-1Lines changed: 74 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -188,7 +188,80 @@ These are 2 independent 8-bit general purpose registers, primarily used in combi
188
188
189
189
## Arithmetic Logic Unit (ALU)
190
190
191
-
TODO
191
+
An 8-bit ALU that can do addition and subtraction based on the values in the A- and B-registers, and output the result to the bus.
192
+
193
+
Addition is performed as `A-register + B-register` and stored as soon as any of the registers change value, without waiting for the clock to tick.
194
+
195
+
Subtraction can be invoked using the `S-` control line to perform a recalculation as `A-register - B-register`. Subtraction is a one off operation and not a state change, so the result will be overwritten using addition when `S-` turns off.
196
+
197
+
Both types of calculations result in some status bits being set.
198
+
199
+
The bits are:
200
+
- Carry: whether the calculation results in a number larger than 8 bit (255) and has wrapped around.
201
+
- Zero: whether the calculation results in 0.
202
+
203
+
The bits change immediately after a calculation. The carry bit is part of the adder chips, while the zero bit is calculated using additional circuitry that was added to support the flags register.
204
+
205
+
Subtraction happens using two's compliment.
206
+
207
+
Example:
208
+
```
209
+
30 - 12
210
+
30 = 0001 1110
211
+
12 = 0000 1100
212
+
```
213
+
214
+
Since the computer only does addition, we can convert 12 to -12 using two's compliment, and then think of the calculation as 30 + -12.
215
+
216
+
Two's compliment of 12 is done by inverting the bits and adding 1.
217
+
218
+
```
219
+
Inverted 12 = 1111 0011
220
+
+1 = 1111 0100
221
+
= 244
222
+
```
223
+
224
+
The calculation then becomes:
225
+
226
+
```
227
+
30 + 244 = 274
228
+
274 = 1 0001 0010
229
+
```
230
+
231
+
Or 18 (`0001 0010`) + the carry bit
232
+
233
+
This is why the carry bit LED is often on when subtracting.
234
+
235
+
The carry bit is not set when the result is 255 and less.
236
+
Example:
237
+
238
+
```
239
+
0 - 1
240
+
0 = 0000 0000
241
+
1 = 0000 0001
242
+
Inverted 1 = 1111 1110
243
+
+1 = 1111 1111
244
+
= 255
245
+
0 + 255 = 255 (no carry needed)
246
+
```
247
+
248
+
Technically this is solved using the XOR gates and `S-`. The B register is connected to one of the sets of inputs, and `S-` to the other sets of inputs. When `S-` is enabled, the XOR gates will output the inverted value of the B register, and when it's disabled it will output the original value of the B register. That output goes into the adders. To get the +1 we need for two's compliment we send `S-` to carry in on the adders as well.
249
+
250
+
* Chips
251
+
* 2x 74LS283 adder: to support 8-bit addition.
252
+
* 2x 74LS86 XOR gate: to invert the value in the B register when `S-` is enabled, to support subtraction.
253
+
* 74LS245 buffer: to control when the result is outputted to the bus.
254
+
* Inputs
255
+
* Current value from both A and B registers.
256
+
* Outputs
257
+
* Carry bit: goes to the flags register.
258
+
* Result: goes to the flags register circuitry for the zero bit.
259
+
* LEDs
260
+
* 8x Red: shows the result of the calculation.
261
+
* Blue: shows if there is a carry in the result.
262
+
* Control lines
263
+
* SO: put the 8-bit result onto the bus.
264
+
* S-: calculate using subtraction instead of addition.
0 commit comments