Skip to content

Commit 5dedd51

Browse files
committed
FPGA asm programs
1 parent 8ba1b97 commit 5dedd51

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

asm/fpga_bootloader.asm

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
.text
2+
.global main:
3+
4+
; (blocking) reads one char from uart to the D register
5+
; pollutes A, B, C, D
6+
; needs 1 stack space
7+
uart_read_char:
8+
; push address of misc.in (which contains if uart is writeable/readable), and read option
9+
LDV B, (0b110)
10+
PSH B
11+
SYS
12+
; pop read value in B
13+
POP B
14+
; get second-to-last bit: AND with 2
15+
LDV A, 2
16+
AND A, B
17+
; if A now contains 2, that means we can read from uart
18+
LDV C, uart_read_char:
19+
LDV B, 2
20+
; if A == 0, then A < B and we will retry
21+
JLT B, C
22+
23+
; read from uart to D register
24+
25+
LDV A, (0b1010)
26+
PSH A
27+
SYS
28+
POP D
29+
RET
30+
31+
; (blocking) write the D register to uart
32+
; pollutes A, B, C
33+
; needs 2 stack space
34+
uart_send_char:
35+
LDV B, (0b110)
36+
PSH B
37+
SYS
38+
; pop read value in B
39+
POP B
40+
; get last bit: AND with 1
41+
LDV A, 1
42+
AND A, B
43+
; if A now contains 1, that means we can write to uart
44+
LDV C, uart_send_char:
45+
LDV B, 1
46+
; if A == 0, then A < B and we will retry
47+
JLT B, C
48+
49+
; else write to uart
50+
PSH D
51+
LDV A, (0b1001)
52+
PSH A
53+
SYS
54+
RET
55+
56+
; blocking, reads two chars from uart into a 16-bit value into the D register
57+
; the first character is the most significant byte, the second character the least
58+
; pollutes A, B, C, D
59+
read_16_bits_from_uart:
60+
LDV A, uart_read_char:
61+
CAL A
62+
; D contains the first 8 bits of the address, shift it and push it onto the stack
63+
LSH D, 7
64+
LSH D, 1
65+
PSH D
66+
LDV A, uart_read_char:
67+
CAL A
68+
; D now contains the last 8 bits of the address
69+
70+
POP C
71+
; C now contains the first 8 bits of the address
72+
73+
OR D, C
74+
; now D contains the full address
75+
RET
76+
77+
main:
78+
; send a single '>'
79+
LDV A, uart_send_char:
80+
LDV D, 62
81+
CAL A
82+
83+
LDV A, read_16_bits_from_uart:
84+
CAL A
85+
; push the address onto the stack
86+
PSH D
87+
LDV A, read_16_bits_from_uart:
88+
CAL A
89+
; the value to load is now in the D register
90+
POP C
91+
LDP C, D
92+
; The first ever instruction will be a jump to main
93+
; this makes it easy to transfer control flow to the now-loaded program:
94+
; you just need to overwrite the jump in address 0 with a jump to your loaded program
95+
JMP 0

asm/fpga_sys_debug.asm

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.text
2+
.global main:
3+
4+
test:
5+
LDV A, 20
6+
PSH A
7+
POP D
8+
RET
9+
10+
main:
11+
LDV A, test:
12+
CAL A
13+
HLT

asm/fpga_uart_recv_send.asm

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
.text
2+
.global main:
3+
4+
; (blocking) reads one char from uart to the D register
5+
; pollutes A, B, C, D
6+
uart_read_char:
7+
; push address of misc.in (which contains if uart is writeable/readable), and read option
8+
LDV B, (0b110)
9+
PSH B
10+
SYS
11+
; pop read value in B
12+
POP B
13+
; get second-to-last bit: AND with 2
14+
LDV A, 2
15+
AND A, B
16+
; if A now contains 2, that means we can read from uart
17+
LDV C, uart_read_char:
18+
LDV B, 2
19+
; if A == 0, then A < B and we will retry
20+
JLT B, C
21+
22+
; read from uart to D register
23+
24+
LDV A, (0b1010)
25+
PSH A
26+
SYS
27+
POP D
28+
RET
29+
30+
; (blocking) write the D register to uart
31+
; pollutes A, B, C
32+
uart_send_char:
33+
LDV B, (0b110)
34+
PSH B
35+
SYS
36+
; pop read value in B
37+
POP B
38+
; get last bit: AND with 1
39+
LDV A, 1
40+
AND A, B
41+
; if A now contains 1, that means we can write to uart
42+
LDV C, uart_send_char:
43+
LDV B, 1
44+
; if A == 0, then A < B and we will retry
45+
JLT B, C
46+
47+
; else write to uart
48+
PSH D
49+
LDV A, (0b1001)
50+
PSH A
51+
SYS
52+
RET
53+
54+
main:
55+
LDV A, uart_send_char:
56+
; '|'
57+
LDV D, 124
58+
CAL A
59+
LDV A, uart_send_char:
60+
; '>'
61+
LDV D, 62
62+
CAL A
63+
LDV A, uart_read_char:
64+
CAL A
65+
; D contains the read character
66+
INC D
67+
LDV A, uart_send_char:
68+
CAL A
69+
JMP main:

0 commit comments

Comments
 (0)