summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Blake Kongslie2021-04-04 14:16:27 -0700
committerJulian Blake Kongslie2021-04-04 14:16:27 -0700
commit6fb37d5f47629817a5092da40217bde5ca0ed4fa (patch)
treea4f176f62cce9ca66806445ab9a0ca4e7543def7
parentRemove unneeded .exe suffixes in bat scripts. (diff)
downloadnoncpu-6fb37d5f47629817a5092da40217bde5ca0ed4fa.tar.xz
Very fancy improved Fibonacci machine, with HDL convert-to-ASCII functionality
Diffstat (limited to '')
-rwxr-xr-xasm.rb4
-rw-r--r--hdl/top.sv11
-rw-r--r--mem/mem.hex78
3 files changed, 80 insertions, 13 deletions
diff --git a/asm.rb b/asm.rb
index 80bbd67..b883510 100755
--- a/asm.rb
+++ b/asm.rb
@@ -2,10 +2,11 @@
2 2
3OPCODES = { 3OPCODES = {
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+)$/
diff --git a/hdl/top.sv b/hdl/top.sv
index 35279d4..4cb5e30 100644
--- a/hdl/top.sv
+++ b/hdl/top.sv
@@ -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
3000 // loop: acc= 0 3// Reset state is acc=0 idx=0
4140 // load 0x40 4
5000 // loop: acc= 0 # Print from 0x70 until we find a 0x00 byte
6170 // ladd 0x70
5300 // ifeq 0 7300 // ifeq 0
6402 // jmp fib 8402 // jmp fib
7f44 // ++idx tx 9f44 // ++idx tx
84fa // jmp loop 104fa // jmp loop
9f20 // fib: idx 11f20 // fib: idx # N.B. acc is 0 at this point (we got here from ifeq 0)
10240 // store 0x40 12270 // store 0x70 # 0x70 is now beginning of Fibonacci sequence (0)
11001 // acc= 1 13001 // acc= 1
12241 // store 0x41 14271 // store 0x71 # 0x71 is 1
13000 // fibloop: acc= 0 15000 // fibloop: acc= 0
14140 // load 0x40 16f10 // swap
15f44 // tx ++idx 17332 // ifeq 50
16140 // load 0x40 1842b // jmp fullydone
17241 // store 0x41 1926f // store 0x6f # 0x6f is current index into Fibonacci sequence
184fa // jmp fibloop 20f10 // swap
21170 // ladd 0x70 # Load next unprinted number in Fibonacci sequence
22f10 // swap
23000 // acc= 0
24f10 // swap
2556b // ascii 0x6b # LSB of ASCII rep
2656c // ascii 0x6c
2756d // ascii 0x6d
2856e // ascii 0x6e # MSB of ASCII rep
29004 // acc= 4
30f10 // swap
31f08 // scan: --idx
32000 // acc= 0
3316b // ladd 0x6b
34f10 // swap
35300 // ifeq 0
36412 // jmp allzeroes
37f10 // swap
38330 // ifeq 0x30
394f7 // jmp scan
40f40 // done: tx
41f10 // swap
42300 // ifeq 0
43404 // jmp space
44f12 // --acc swap
45000 // acc= 0
4616b // ladd 0x6b
474f8 // jmp done
48020 // space: acc= 0x20
49f40 // tx
50000 // acc= 0
51f20 // idx
5216f // ladd 0x6f
53f10 // swap
54402 // jmp nextfib
55f10 // allzeroes: swap
564ef // jmp done
57170 // nextfib: ladd 0x70 # Load just-printed Fibonacci number
58171 // ladd 0x71 # Add about-to-print Fibonacci number
59272 // store 0x72 # Store n+2 Fibonacci number
60f04 // ++idx
614d1 // jmp fibloop
62021 // fullydone: acc= 0x21
63f40 // tx
64f80 // halt
65
66@6c
67
68000 // 6b: LSB of ASCII rep
69000 // 6c: USB of ASCII rep
70000 // 6d: USB of ASCII rep
71000 // 6e: MSB of ASCII rep
72000 // 6f: next index to store in memory image of Fibonacci sequence
19 73
20@40 74@70
21 75
22048 // H 76048 // H # later: start of in-memory Fib sequence
23065 // e 77065 // e
2406c // l 7806c // l
2506c // l 7906c // l