diff options
| author | Julian Blake Kongslie | 2021-03-23 12:22:38 -0700 |
|---|---|---|
| committer | Julian Blake Kongslie | 2021-03-23 12:22:38 -0700 |
| commit | 5d95607192380be5cc62592efac52814a0e090df (patch) | |
| tree | 656bc3ec79180a89f355681e454808ebce6eb6d9 /top.sv | |
| download | toycpu-5d95607192380be5cc62592efac52814a0e090df.tar.xz | |
Initial commit.
Diffstat (limited to 'top.sv')
| -rw-r--r-- | top.sv | 145 |
1 files changed, 145 insertions, 0 deletions
| @@ -0,0 +1,145 @@ | |||
| 1 | module top | ||
| 2 | #( FIB_BITS = 16 | ||
| 3 | , ROM_BITS = 8 | ||
| 4 | ) | ||
| 5 | ( input bit clk // verilator public | ||
| 6 | , input bit reset_n // verilator public | ||
| 7 | ); | ||
| 8 | |||
| 9 | bit reset; | ||
| 10 | assign reset = !reset_n; | ||
| 11 | |||
| 12 | bit [7:0] rom [0:(1<<ROM_BITS)-1]; | ||
| 13 | initial $readmemh("rom.hex", rom); | ||
| 14 | |||
| 15 | bit [ROM_BITS-1:0] addr; | ||
| 16 | |||
| 17 | bit rx_ready; | ||
| 18 | bit rx_valid; | ||
| 19 | bit [7:0] rx_data; | ||
| 20 | |||
| 21 | bit tx_ready; | ||
| 22 | bit tx_valid; | ||
| 23 | bit [7:0] tx_data; | ||
| 24 | |||
| 25 | jtag_uart | ||
| 26 | #( .INSTANCE(0) | ||
| 27 | ) uart0 | ||
| 28 | ( .clk(clk) | ||
| 29 | , .reset(reset) | ||
| 30 | |||
| 31 | , .rx_ready(rx_ready) | ||
| 32 | , .rx_valid(rx_valid) `define rx_valid $past(rx_valid) | ||
| 33 | , .rx_data(rx_data) `define rx_data $past(rx_data) | ||
| 34 | |||
| 35 | , .tx_ready(tx_ready) `define tx_ready $past(tx_ready) | ||
| 36 | , .tx_valid(tx_valid) | ||
| 37 | , .tx_data(tx_data) | ||
| 38 | ); | ||
| 39 | |||
| 40 | bit fib_ready; | ||
| 41 | bit fib_valid; | ||
| 42 | bit [FIB_BITS-1:0] fib_data; | ||
| 43 | |||
| 44 | fibseq | ||
| 45 | #( .BITS(FIB_BITS) | ||
| 46 | ) fib | ||
| 47 | ( .clk(clk) | ||
| 48 | , .reset(reset) | ||
| 49 | |||
| 50 | , .ready(fib_ready) | ||
| 51 | , .valid(fib_valid) | ||
| 52 | , .data(fib_data) | ||
| 53 | ); | ||
| 54 | |||
| 55 | bit fib_a_ready; | ||
| 56 | bit fib_a_valid; | ||
| 57 | bit [7:0] fib_a_data; | ||
| 58 | |||
| 59 | ntoa | ||
| 60 | #( .BITS(FIB_BITS) | ||
| 61 | ) fib_ntoa | ||
| 62 | ( .clk(clk) | ||
| 63 | , .reset(reset) | ||
| 64 | |||
| 65 | , .n_ready(fib_ready) | ||
| 66 | , .n_valid(fib_valid) | ||
| 67 | , .n_data(fib_data) | ||
| 68 | |||
| 69 | , .a_ready(fib_a_ready) | ||
| 70 | , .a_valid(fib_a_valid) `define fib_a_valid $past(fib_a_valid) | ||
| 71 | , .a_data(fib_a_data) `define fib_a_data $past(fib_a_data) | ||
| 72 | ); | ||
| 73 | |||
| 74 | enum | ||
| 75 | { INTRO_ECHO | ||
| 76 | , ECHO | ||
| 77 | , INTRO_FIB | ||
| 78 | , FIB | ||
| 79 | } state; | ||
| 80 | |||
| 81 | bit tmp_valid; | ||
| 82 | bit [7:0] tmp_data; | ||
| 83 | |||
| 84 | always_ff @(posedge clk) begin | ||
| 85 | if (reset) begin | ||
| 86 | addr = 0; | ||
| 87 | rx_ready = 0; | ||
| 88 | tx_valid = 0; | ||
| 89 | fib_a_ready = 0; | ||
| 90 | state = state.first; | ||
| 91 | tmp_valid = 0; | ||
| 92 | end else unique0 case (state) | ||
| 93 | |||
| 94 | INTRO_ECHO, INTRO_FIB: begin | ||
| 95 | automatic bit [7:0] data = rom[addr]; | ||
| 96 | if (`tx_ready) tx_valid = 0; | ||
| 97 | if (!tx_valid && (data != 0)) begin | ||
| 98 | tx_valid = 1; | ||
| 99 | tx_data = data; | ||
| 100 | addr = addr + 1; | ||
| 101 | end else if (data == 0) begin | ||
| 102 | addr = addr + 1; | ||
| 103 | state = state.next; | ||
| 104 | end | ||
| 105 | end | ||
| 106 | |||
| 107 | ECHO: begin | ||
| 108 | if (`tx_ready && tx_valid && tx_data == "\n") begin | ||
| 109 | // FIXME race; we aren't going to consume input this cycle, but we might have tmp_valid or rx_ready asserted | ||
| 110 | rx_ready = 0; | ||
| 111 | tx_valid = 0; | ||
| 112 | state = INTRO_FIB; | ||
| 113 | end else begin | ||
| 114 | if (`tx_ready) tx_valid = 0; | ||
| 115 | if (rx_ready && `rx_valid) begin | ||
| 116 | tmp_valid = 1; | ||
| 117 | tmp_data = `rx_data; | ||
| 118 | end | ||
| 119 | if (!tx_valid && tmp_valid) begin | ||
| 120 | tx_valid = 1; | ||
| 121 | tx_data = tmp_data; | ||
| 122 | tmp_valid = 0; | ||
| 123 | end | ||
| 124 | rx_ready = !tmp_valid; | ||
| 125 | end | ||
| 126 | end | ||
| 127 | |||
| 128 | FIB: begin | ||
| 129 | if (`tx_ready) tx_valid = 0; | ||
| 130 | if (fib_a_ready && `fib_a_valid) begin | ||
| 131 | tmp_valid = 1; | ||
| 132 | tmp_data = `fib_a_data; | ||
| 133 | end | ||
| 134 | if (!tx_valid && tmp_valid) begin | ||
| 135 | tx_valid = 1; | ||
| 136 | tx_data = tmp_data; | ||
| 137 | tmp_valid = 0; | ||
| 138 | end | ||
| 139 | fib_a_ready = !tmp_valid; | ||
| 140 | end | ||
| 141 | |||
| 142 | endcase | ||
| 143 | end | ||
| 144 | |||
| 145 | endmodule | ||
