diff options
| author | Julian Blake Kongslie | 2021-07-06 09:44:36 -0700 |
|---|---|---|
| committer | Julian Blake Kongslie | 2021-07-06 09:44:36 -0700 |
| commit | 60e1775b874015a3451e4bde10a8eb30701b1165 (patch) | |
| tree | 477a2835c0f7e616bdeeabe6aee85f8af8b79650 /sim/alu.sv | |
| download | breadboarding-60e1775b874015a3451e4bde10a8eb30701b1165.tar.xz | |
Initial commit.
Diffstat (limited to '')
| -rw-r--r-- | sim/alu.sv | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/sim/alu.sv b/sim/alu.sv new file mode 100644 index 0000000..407b083 --- /dev/null +++ b/sim/alu.sv | |||
| @@ -0,0 +1,76 @@ | |||
| 1 | module alu | ||
| 2 | #( parameter UROM = "<no file specified>" | ||
| 3 | , parameter UIP_BITS = 15 | ||
| 4 | , parameter UROM_BITS = 8 | ||
| 5 | , parameter BUS_BITS = 16 | ||
| 6 | ) | ||
| 7 | ( input bit clk | ||
| 8 | , input bit reset | ||
| 9 | , input bit [UIP_BITS-1:0] uip | ||
| 10 | , inout bit [BUS_BITS-1:0] abus | ||
| 11 | , inout bit [BUS_BITS-1:0] dbus | ||
| 12 | ); | ||
| 13 | |||
| 14 | bit [BUS_BITS-1:0] x; | ||
| 15 | |||
| 16 | typedef enum | ||
| 17 | { OP | ||
| 18 | , OP_SEL0 | ||
| 19 | , OP_SEL1 | ||
| 20 | , OP_SEL2 | ||
| 21 | , OUTADDR | ||
| 22 | , OUTDATA | ||
| 23 | } CtrlBit; | ||
| 24 | |||
| 25 | bit [UROM_BITS-1:0] ctrl; | ||
| 26 | urom#(UROM, UIP_BITS, UROM_BITS) urom(uip, ctrl); | ||
| 27 | |||
| 28 | bit [2:0] sel; | ||
| 29 | assign sel = {ctrl[OP_SEL2], ctrl[OP_SEL1], ctrl[OP_SEL0]}; | ||
| 30 | |||
| 31 | bit [BUS_BITS-1:0] and_result; | ||
| 32 | bit [BUS_BITS-1:0] or_result; | ||
| 33 | bit [BUS_BITS-1:0] xor_result; | ||
| 34 | bit [BUS_BITS-1:0] add_result; | ||
| 35 | bit [BUS_BITS-1:0] sub_result; | ||
| 36 | bit [BUS_BITS-1:0] cmp_result; | ||
| 37 | bit [BUS_BITS-1:0] lshift_result; | ||
| 38 | bit [BUS_BITS-1:0] rshift_result; | ||
| 39 | |||
| 40 | assign and_result = abus & dbus; | ||
| 41 | assign or_result = abus | dbus; | ||
| 42 | assign xor_result = abus ^ dbus; | ||
| 43 | assign add_result = abus + dbus; | ||
| 44 | assign sub_result = abus - dbus; | ||
| 45 | assign cmp_result = {{(BUS_BITS-6){1'b0}}, | ||
| 46 | (abus != 0) & (dbus != 0), | ||
| 47 | (abus != 0) | (dbus != 0), | ||
| 48 | (abus != 0) ^ (dbus != 0), | ||
| 49 | abus > dbus, | ||
| 50 | abus == dbus, | ||
| 51 | abus < dbus}; | ||
| 52 | assign lshift_result = (dbus >= BUS_BITS) ? 0 : (abus << dbus); | ||
| 53 | assign rshift_result = (dbus >= BUS_BITS) ? 0 : (abus >> dbus); | ||
| 54 | |||
| 55 | bit [BUS_BITS-1:0] newx; | ||
| 56 | assign newx = | ||
| 57 | (sel == 0) ? and_result : | ||
| 58 | (sel == 1) ? or_result : | ||
| 59 | (sel == 2) ? xor_result : | ||
| 60 | (sel == 3) ? add_result : | ||
| 61 | (sel == 4) ? sub_result : | ||
| 62 | (sel == 5) ? cmp_result : | ||
| 63 | (sel == 6) ? lshift_result : | ||
| 64 | (sel == 7) ? rshift_result : | ||
| 65 | {(BUS_BITS){1'bX}}; | ||
| 66 | |||
| 67 | assign abus = ctrl[OUTADDR] ? x : {(BUS_BITS){1'bZ}}; | ||
| 68 | assign dbus = ctrl[OUTDATA] ? x : {(BUS_BITS){1'bZ}}; | ||
| 69 | |||
| 70 | always @(posedge clk) begin | ||
| 71 | if (ctrl[OP]) begin | ||
| 72 | x <= newx; | ||
| 73 | end | ||
| 74 | end | ||
| 75 | |||
| 76 | endmodule | ||
