-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpanded_terminal.tac
More file actions
456 lines (451 loc) · 13.2 KB
/
Copy pathexpanded_terminal.tac
File metadata and controls
456 lines (451 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#-------------------------------------------------0x00 SEGMENT START------------
#-------------------------------------------------0x00 SEGMENT INDEX START------
# Segment index, responsible for "routing" calls to other segments
# Expects:
# GRA = jump address in new segment
# GRB = new segment address
index@00: lni 0xFF,0x00
seg
sps # the segment address location is 0xFF on MMIO
lbs # push our current segment to the stack
sb GRB # set the segment address we should be in
# the next instruction will be at the same PC address but in
# the segment we set. e.g. 0x0004 -> 0xFF04 if GRB = 0xFF
#
# Since this is in segment 0x00, we must test if we are just starting
sop_and
cmp GRA GRA # test if the jump pointer is null
jmp begin@00 # jump to the actual beginning of the program
goto # jump to the pointer if it isn't 0
#-------------------------------------------------0x00 SEGMENT INDEX END--------
#
begin@00: mnb_incr
mnb_store
lni 0x01,0x01,0x10
sps
mnb 2
getlabel init
push
li 3
mov GRA GRB
getlabel matrix@03
pcr
jmp index@00
pop
# terminal_test - Interactive terminal program allowing you to type commands to
# call specific methods.
# Uses the ExampleConfigurationROM circuit
###################### Print the welcome string
string Welcome!\n\0
pcr
jmp print
jmp term_char
###################### Set up our buffer pointer to live in GRC
poll_setup: lni 32,0x00
seg
pop
mov GRA GRC
###################### Set up our polling loop
poll: sop_xor
lni 128,252 # Load the bitmask for available data + the polling address
sps # Set the polling address
pop # Pop the bitmask for available data into GRA
###################### Poll until data is available
p_loop: lb GRB
cmp GRB GRA
jmp p_loop
###################### Handle buffering the data and echo
###################### Clears the byte out of the buffer in the keyboard
sb GRA
###################### Did we read a backspace character? If so, handle that
li 8 # ascii BS
cmp GRA GRB
jmp bcksp
###################### Echo the byte
sb GRB
###################### Write the byte in the buffer in RAM
sp GRC
sb GRB
###################### Increment the buffer pointer
incr GRC GRC
###################### If we have reached the end of the buffer, restart
li 110 # max buffer size = 78, 32 + 78 = 110
cmp GRA GRC
jmp term_char
###################### If the user pressed the enter key, output the buffer
li 10 # ascii LF
cmp GRB GRA
jmp cmpr
jmp poll
###################### Handle backspaces
bcksp: li 32 # buffer pointer
###################### Are we at the start of the buffer? If so, we can return
cmp GRA GRC
jmp poll
###################### Echo the byte
sb GRB
###################### Decrement the buffer pointer and start polling again
li 1
sop_sub
op GRC GRA GRC
jmp poll
############ Print routine that expects a null-terminated string on the stack
print: pop
mov GRA GRB
sop_and
li 252 # MMIO TTY address
sp GRA
print_loop: pop
cmp GRA GRA # test for a null byte
jmp print_done # if we get a null byte, return
sb GRA
jmp print_loop
print_done: mov GRB GRA
goto
##### first off, store our buffer end pointer in RAM
cmpr: li 32 # buffer pointer
push
# compute the buffer size
sop_sub
op GRC GRA GRA
push
li 0x01
sp GRA
mnb_store
mnb 2
# read if we are going to copy the buffer from the "set" command
li 0x10
sp GRA
lb GRA
or GRA GRA
jmp copy
getlabel do_comp@01
push
li 0x1
mov GRA GRB
getlabel matrix@01
pcr
jmp index@00
pop
li 0x11
sp GRA
lb GRA
and GRA GRA
jmp poll_setup
###################### Print the terminal character '$ '
term_char: string $ \0
pcr
jmp print
jmp poll_setup
########### copy routine
copy: li 0x01
sp GRA
lb GRC
incr GRA GRA
sp GRA
lb GRB
sp GRB
one GRB
mnb_load
sop_sub
mov GRC GRA
copy_l: cmp GRA GRB
jmp copy_w
mnb 1
op GRA GRB GRA
jmp copy_l
copy_w: lni 0x3F,0x01
seg
sps
mov GRC GRA
push
mnb_store
mnb 1
copy_w_l: cmp GRA GRB
jmp copy_d
mnb 1
op GRA GRB GRA
jmp copy_w_l
copy_d: lni 0x1,0x1,0x10,0x00
seg
sps
mnb 2
jmp term_char
#-------------------------------------------------0x00 SEGMENT END--------------
#-------------------------------------------------0x01 SEGMENT START------------
#-------------------------------------------------0x01 SEGMENT INDEX START------
space 0x0100
index@01: lni 0xFF,0x00
seg
sps
lbs
sb GRB # set the segment address we should be in
goto
#-------------------------------------------------0x01 MATRIX START-------------
matrix@01: li 0x3 # Our pointer in RAM for the return segment and address
sp GRA
mnb_store
mnb 2
pop # pop the target function to GRA
goto # goto will jump to the target function in GRA
return@01: li 0x3 # we will return from the function here
sp GRA # The pointer in RAM for the return segment and address
lb GRB # load the return segment
incr GRA GRA
sp GRA
lb GRA # load the return address
jmp index@01 # go route us!
###################### Compare the buffer to our commands
###################### Specify the string we will be comparing, and the length.
###################### This is in reverse order, as the bytes will be pushed to
###################### the stack. The last byte is the first byte out.
###################### The last byte is the length of the string.
do_comp@01: sop_add
############ Respond to 'testing123' with 'horray!'
# "testing123\n\0", length 11
n0: lni 0x1D,0x01
seg
sps
mnb_load
mnb 13
pcr
jmp callcmp
pop
and GRA GRA
############ If the strings match, print our response, else go handle input
jmp n1
############ load the null-terminated string
############ "horray!\n\0"
lni 0x2A,0x01
seg
sps
mnb_load
mnb 9
pcr
jmp print@01
jmp return@01
############# Respond to 'ping' with 'pong'
## "ping\n\0", length 5
n1: lni 0x10,0x01
seg
sps
mnb_load
mnb 7
pcr
jmp callcmp
pop
and GRA GRA
############ If the strings match, print our response, else continue to the next
jmp n2
############ load the null-terminated string to print
############ "pong\n\0"
lni 0x17,0x01
seg
sps
mnb_load
mnb 6
pcr
jmp print@01
jmp return@01
############ Respond to 'exit' by exiting, yo
# "exit\n", length 5
n2: string exit\n\0
li 5 #length
push
pcr
jmp callcmp
pop
and GRA GRA
jmp n3
########## clear the TTY
exit: jmp exit
jmp return@01 // shouldn't return
############ Respond to 'set' by copying the next entered string into memory
n3: string set\n\0
li 4 #length
push
pcr
jmp callcmp
pop
and GRA GRA
jmp n4
######### set the copy flag to true, don't do setup
lni 0x00,0x11,0x00,0x10,0x00
seg
sps
sbs
sps
sbs
jmp return@01
############ Respond to 'print' by printing the string stored in memory by "set"
n4: string print\n\0
li 6 #length
push
pcr
jmp callcmp
pop
and GRA GRA
jmp return@01
######## echo our copy buf
lni 0x0,0x3F,0x01
seg
sps
mnb_load
mnb 1
pop
incr GRA GRA
echo_r: decr GRA GRA
jmp echo_d
mnb 1
jmp echo_r
echo_d: pcr
jmp print@01
jmp return@01
callcmp: lni 0xF,0x0
seg
sps
sbs
getlabel cmpr_f@02
push
li 0x2
mov GRA GRB
getlabel matrix@01
pcr
jmp index@01
pop
li 0xF
sp GRA
lb GRA
goto
print@01: lni 252,0x0
seg
sps
pop
mov GRA GRB
sop_and
print_loop@01: pop
cmp GRA GRA # test for a null byte
jmp print_done@01 # if we get a null byte, return
sb GRA
jmp print_loop@01
print_done@01: mov GRB GRA
goto
#-------------------------------------------------0x01 SEGMENT END--------------
#-------------------------------------------------0x02 SEGMENT START------------
#-------------------------------------------------0x02 SEGMENT INDEX START------
space 0x0200
index@02: lni 0xFF,0x00
seg
sps
lbs
sb GRB # set the segment address we should be in
goto
#-------------------------------------------------0x02 MATRIX START-------------
matrix@02: li 0x5 # Our pointer in RAM for the return segment and address
sp GRA
mnb_store
mnb 2
pop # pop our target function to GRA
goto # goto will jump to the target function in GRA
return@02: li 0x5 # Our pointer in RAm for the return segment and address
# we will return from the function here
sp GRA
lb GRB # load the return segment
incr GRA GRA
sp GRA
lb GRA # load the return address
jmp index@02 # go route us!
#-------------------------------------------------0x02 SEGMENT INDEX END--------
######### compare function, compares the null-terminated string on the stack to
# the contents of the buffer and returns a boolean in GRB.
# Expects:
# Stack: size of compare string, null-terminated compare string
# Memory:
# 0x1 - buffer length
# 0x2 - buffer pointer
cmpr_f@02: li 0x01 # buffer length / pointer
sp GRA
######## Get the buffer length + start address
lb GRC
incr GRA GRA
sp GRA
lb GRB
sp GRB
######## If the length of our string does not match our buffer,
######## just exit immediately.
pop
sop_xor
cmp GRC GRA
jmp c_cmpr
jmp c_fail
c_cmpr: mov GRB GRC
c_loop: pop
######## Test if we've reached the end of our comparision buffer.
######## If we've reached the end and haven't branched out, then
######## it is a match!
cmp GRA NIL # test for a null byte
jmp c_match
lb GRB
######## Increment our buffer pointer
incr GRC GRC
sp GRC
######## If the bytes match, continue looping
cmp GRB GRA
jmp c_loop
# Return false
c_fail: pop # Flush the stack until we get the null byte
cmp GRA NIL # test for a null byte
jmp c_r # if null, return
jmp c_fail # else continue flushing
# Return true
c_match: li 0x01
c_r: push
jmp return@02
#-------------------------------------------------0x02 SEGMENT END--------------
#-------------------------------------------------0x03 SEGMENT START------------
#-------------------------------------------------0x03 SEGMENT INDEX START------
space 0x0300
index@03: lni 0xFF,0x00
seg
sps
lbs
sb GRB # set the segment address we should be in
goto
#-------------------------------------------------0x03 MATRIX START-------------
matrix@03: li 0x8 # Our pointer in RAM for the return segment and address
sp GRA
mnb_store
mnb 2
pop # pop our target function to GRA
goto # goto will jump to the target function in GRA
return@03: li 0x8 # Our pointer in RAM for the return segment and address
# we will return from the function here
sp GRA
lb GRB # load the return segment
incr GRA GRA
sp GRA
lb GRA # load the return address
jmp index@03 # go route us!
########## static strings
init: mnb_store
lni 0x05,0x10,0x01 # string length, address, segment
seg
sps
string_r ping\n\0
mnb 7
li 0x17 # address
sp GRA
string_r pong\n\0
mnb 6 #
lni 0x0B,0x1D # string length, address
sps
string_r testing123\n\0
mnb 13
string_r horray!\n\0
li 0x2A
sp GRA
mnb 9
li 0
seg
trap@03: jmp return@03
#-------------------------------------------------0x03 SEGMENT INDEX END--------