Arithmetic Logic Unit (ALU)
Note
This microsim is a work-in-progress. The ALU works in some cases but has bugs and the layout is not optimized.
Prompt:
Create a single file ps.js sketch on a 400x400 canvas.
The sketch is a simulation of an Arithmetic Logic Unit (ALU).
Place a title "Arithmetic Logic Unit" on the top center of the canvas of text size 16.
The inputs are two registers: A and B.
Place the labels for the registers to the left of text inputs.
Place the input registers so the align vertically.
Place buttons for the operations below the registers.
The operation are: ADD, SUBTRACT, AND, OR, XOR.
Display the value of the output below the operations.
Label the output value "Output" in blue text.
Create an input toggle button to display all values in either binary or decimal.
Make the default value be decimal.
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 |
|
Challenges
Real ALUs also keep track of several bits if information about the result of the arithmetic such as if there were overflows.
Challenges
- Add a bit to indicate the result is non-zero.
- Add a sign flag to indicate the number is non-negative
- Add a carry flag to indicate that the result fits in 16 bits
- Add an overflow flag to indicate that the sum of two numbers is also positive
References
I love this small simulator from CircuitVerse:
Four Bit ALU on CircuitVerse - This shows the process of using an OpCode which is a number for an operation. The carry bit does not work in this example.
A much better example is here:
University of New Brunswick EE3221 Course
That version is a binary only but it includes the following flags:
Flags The Zero flag (Z) is 0 because the result is non-zero
The Sign flag (N) is 0 because the result appears non-negative (the MSB is 0)
The Carry flag (C) is 0 (good) because the addition result did not exceed 16-bits. This would be of interest if (RA,RB) represent unsigned integers (0..65535).
The Overflow flag (V) is 0 (good) because adding two positive numbers gave a positive result. This would be of interest if (RA,RB) represent signed integers (-32768..+32767).