diff options
| author | Julian Blake Kongslie | 2021-04-04 14:16:27 -0700 |
|---|---|---|
| committer | Julian Blake Kongslie | 2021-04-04 14:16:27 -0700 |
| commit | 6fb37d5f47629817a5092da40217bde5ca0ed4fa (patch) | |
| tree | a4f176f62cce9ca66806445ab9a0ca4e7543def7 | |
| parent | Remove unneeded .exe suffixes in bat scripts. (diff) | |
| download | noncpu-6fb37d5f47629817a5092da40217bde5ca0ed4fa.tar.xz | |
Very fancy improved Fibonacci machine, with HDL convert-to-ASCII functionality
Diffstat (limited to '')
| -rwxr-xr-x | asm.rb | 4 | ||||
| -rw-r--r-- | hdl/top.sv | 11 | ||||
| -rw-r--r-- | mem/mem.hex | 78 |
3 files changed, 80 insertions, 13 deletions
| @@ -2,10 +2,11 @@ | |||
| 2 | 2 | ||
| 3 | OPCODES = { | 3 | OPCODES = { |
| 4 | "acc=" => 0x000, | 4 | "acc=" => 0x000, |
| 5 | "load" => 0x100, | 5 | "ladd" => 0x100, |
| 6 | "store" => 0x200, | 6 | "store" => 0x200, |
| 7 | "ifeq" => 0x300, | 7 | "ifeq" => 0x300, |
| 8 | "jmp" => 0x400, | 8 | "jmp" => 0x400, |
| 9 | "ascii" => 0x500, | ||
| 9 | "++acc" => 0xf01, | 10 | "++acc" => 0xf01, |
| 10 | "--acc" => 0xf02, | 11 | "--acc" => 0xf02, |
| 11 | "++idx" => 0xf04, | 12 | "++idx" => 0xf04, |
| @@ -27,6 +28,7 @@ ARGF.each_line() do | line | | |||
| 27 | op = 0x000 | 28 | op = 0x000 |
| 28 | refs = [] | 29 | refs = [] |
| 29 | line.scan(/\S+/).each() do | word | | 30 | line.scan(/\S+/).each() do | word | |
| 31 | break if word =~ /^#/ | ||
| 30 | if word =~ /^0(\d+)$/ | 32 | if word =~ /^0(\d+)$/ |
| 31 | op |= $1.to_i(8) | 33 | op |= $1.to_i(8) |
| 32 | elsif word =~ /^-0(\d+)$/ | 34 | elsif word =~ /^-0(\d+)$/ |
| @@ -120,6 +120,11 @@ always_ff @(posedge clk) begin | |||
| 120 | 'h2: state = AGEN; | 120 | 'h2: state = AGEN; |
| 121 | 'h3: if (acc != sign_extended_operand) ++pc; | 121 | 'h3: if (acc != sign_extended_operand) ++pc; |
| 122 | 'h4: pc = pc + sign_extended_operand; | 122 | 'h4: pc = pc + sign_extended_operand; |
| 123 | 'h5: begin | ||
| 124 | mem_write_data = acc % 10 + 'h30; | ||
| 125 | acc = acc / 10; | ||
| 126 | state = AGEN; | ||
| 127 | end | ||
| 123 | 'hf: begin | 128 | 'hf: begin |
| 124 | if (operand[0]) ++acc; | 129 | if (operand[0]) ++acc; |
| 125 | if (operand[1]) --acc; | 130 | if (operand[1]) --acc; |
| @@ -152,6 +157,12 @@ always_ff @(posedge clk) begin | |||
| 152 | mem_write_data = acc; | 157 | mem_write_data = acc; |
| 153 | state = `mem_ready ? FETCH : AGEN; | 158 | state = `mem_ready ? FETCH : AGEN; |
| 154 | end | 159 | end |
| 160 | 'h5: begin | ||
| 161 | mem_valid = 1; | ||
| 162 | mem_address = {2'b0, idx + sign_extended_operand}; | ||
| 163 | mem_write = 1; | ||
| 164 | state = `mem_ready ? FETCH : AGEN; | ||
| 165 | end | ||
| 155 | endcase | 166 | endcase |
| 156 | end | 167 | end |
| 157 | 168 | ||
diff --git a/mem/mem.hex b/mem/mem.hex index 63038e3..d30c5b6 100644 --- a/mem/mem.hex +++ b/mem/mem.hex | |||
| @@ -1,25 +1,79 @@ | |||
| 1 | @0 | 1 | @0 |
| 2 | 2 | ||
| 3 | 000 // loop: acc= 0 | 3 | // Reset state is acc=0 idx=0 |
| 4 | 140 // load 0x40 | 4 | |
| 5 | 000 // loop: acc= 0 # Print from 0x70 until we find a 0x00 byte | ||
| 6 | 170 // ladd 0x70 | ||
| 5 | 300 // ifeq 0 | 7 | 300 // ifeq 0 |
| 6 | 402 // jmp fib | 8 | 402 // jmp fib |
| 7 | f44 // ++idx tx | 9 | f44 // ++idx tx |
| 8 | 4fa // jmp loop | 10 | 4fa // jmp loop |
| 9 | f20 // fib: idx | 11 | f20 // fib: idx # N.B. acc is 0 at this point (we got here from ifeq 0) |
| 10 | 240 // store 0x40 | 12 | 270 // store 0x70 # 0x70 is now beginning of Fibonacci sequence (0) |
| 11 | 001 // acc= 1 | 13 | 001 // acc= 1 |
| 12 | 241 // store 0x41 | 14 | 271 // store 0x71 # 0x71 is 1 |
| 13 | 000 // fibloop: acc= 0 | 15 | 000 // fibloop: acc= 0 |
| 14 | 140 // load 0x40 | 16 | f10 // swap |
| 15 | f44 // tx ++idx | 17 | 332 // ifeq 50 |
| 16 | 140 // load 0x40 | 18 | 42b // jmp fullydone |
| 17 | 241 // store 0x41 | 19 | 26f // store 0x6f # 0x6f is current index into Fibonacci sequence |
| 18 | 4fa // jmp fibloop | 20 | f10 // swap |
| 21 | 170 // ladd 0x70 # Load next unprinted number in Fibonacci sequence | ||
| 22 | f10 // swap | ||
| 23 | 000 // acc= 0 | ||
| 24 | f10 // swap | ||
| 25 | 56b // ascii 0x6b # LSB of ASCII rep | ||
| 26 | 56c // ascii 0x6c | ||
| 27 | 56d // ascii 0x6d | ||
| 28 | 56e // ascii 0x6e # MSB of ASCII rep | ||
| 29 | 004 // acc= 4 | ||
| 30 | f10 // swap | ||
| 31 | f08 // scan: --idx | ||
| 32 | 000 // acc= 0 | ||
| 33 | 16b // ladd 0x6b | ||
| 34 | f10 // swap | ||
| 35 | 300 // ifeq 0 | ||
| 36 | 412 // jmp allzeroes | ||
| 37 | f10 // swap | ||
| 38 | 330 // ifeq 0x30 | ||
| 39 | 4f7 // jmp scan | ||
| 40 | f40 // done: tx | ||
| 41 | f10 // swap | ||
| 42 | 300 // ifeq 0 | ||
| 43 | 404 // jmp space | ||
| 44 | f12 // --acc swap | ||
| 45 | 000 // acc= 0 | ||
| 46 | 16b // ladd 0x6b | ||
| 47 | 4f8 // jmp done | ||
| 48 | 020 // space: acc= 0x20 | ||
| 49 | f40 // tx | ||
| 50 | 000 // acc= 0 | ||
| 51 | f20 // idx | ||
| 52 | 16f // ladd 0x6f | ||
| 53 | f10 // swap | ||
| 54 | 402 // jmp nextfib | ||
| 55 | f10 // allzeroes: swap | ||
| 56 | 4ef // jmp done | ||
| 57 | 170 // nextfib: ladd 0x70 # Load just-printed Fibonacci number | ||
| 58 | 171 // ladd 0x71 # Add about-to-print Fibonacci number | ||
| 59 | 272 // store 0x72 # Store n+2 Fibonacci number | ||
| 60 | f04 // ++idx | ||
| 61 | 4d1 // jmp fibloop | ||
| 62 | 021 // fullydone: acc= 0x21 | ||
| 63 | f40 // tx | ||
| 64 | f80 // halt | ||
| 65 | |||
| 66 | @6c | ||
| 67 | |||
| 68 | 000 // 6b: LSB of ASCII rep | ||
| 69 | 000 // 6c: USB of ASCII rep | ||
| 70 | 000 // 6d: USB of ASCII rep | ||
| 71 | 000 // 6e: MSB of ASCII rep | ||
| 72 | 000 // 6f: next index to store in memory image of Fibonacci sequence | ||
| 19 | 73 | ||
| 20 | @40 | 74 | @70 |
| 21 | 75 | ||
| 22 | 048 // H | 76 | 048 // H # later: start of in-memory Fib sequence |
| 23 | 065 // e | 77 | 065 // e |
| 24 | 06c // l | 78 | 06c // l |
| 25 | 06c // l | 79 | 06c // l |
