This repository has been archived by the owner on May 29, 2018. It is now read-only.
/
library.dasm16
540 lines (464 loc) · 8.74 KB
/
library.dasm16
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
; prints a text to stdout
; A: start address of the text
:text_out
SET PUSH, A
SET PUSH, B
SET PUSH, C
SET PUSH, X
SET PUSH, I
SET B, [video_col]
SET I, [video_cur]
SET X, [video_width]
MUL X, [video_height]
ADD X, [video_mem]
:text_out_loop
SET C, [A]
IFE C, 0x0000
SET PC, text_out_end
IFE C, 0x00A0
SET PC, text_out_nl
AND C, 0x00FF
BOR C, B
SET [I], C
ADD A, 1
ADD I, 1
IFE I, X
SET PC, text_out_scroll
SET PC, text_out_loop
:text_out_scroll
SET [video_cur], I
JSR scroll
SET I, [video_cur]
SET PC, text_out_loop
:text_out_nl
SET [video_cur], I
JSR newline
SET I, [video_cur]
ADD A, 1
SET PC, text_out_loop
:text_out_end
SET [video_cur], I
SET I, POP
SET X, POP
SET C, POP
SET B, POP
SET A, POP
SET PC, POP
; Linefeed
:newline
SET PUSH, A
SET PUSH, B
SET PUSH, X
SET X, [video_width]
MUL X, [video_height]
ADD X, [video_mem]
SET A, 0x0020
SET B, [video_cur]
MOD B, A
SUB A, B
ADD [video_cur], A
IFE [video_cur], X
JSR scroll
SET X, POP
SET B, POP
SET A, POP
SET PC, POP
; Scrolls the screen one line
:scroll
SET PUSH, A
SET PUSH, X
SET PUSH, Y
SET X, [video_mem] ; Set X to the video memory
SET Y, [video_mem]
ADD Y, 0x0020 ; Set Y to the second line in the video memory
SET A, [video_width]
MUL A, [video_height]
ADD A, [video_mem]
:scroll_loop1
SET [X], [Y]
SET [1+X], [1+Y]
SET [2+X], [2+Y]
SET [3+X], [3+Y]
ADD X, 4
ADD Y, 4
IFN Y, A
SET PC, scroll_loop1
:scroll_loop2
SET [X], [video_clear]
ADD X, 1
IFE X, A
SET PC, scroll_end
SET PC, scroll_loop2
:scroll_end
SUB [video_cur], 0x20
SET Y, POP
SET X, POP
SET A, POP
SET PC, POP
; Clears the screen and sets the cursor to the first line (working)
:clear
SET PUSH, A
SET PUSH, B
SET PUSH, C
SET A, [video_mem]
SET B, [video_clear]
SET C, [video_width]
MUL C, [video_height]
ADD C, [video_mem]
:clear_loop
SET [A], B
SET [1+A], B
SET [2+A], B
SET [3+A], B
ADD A, 4
IFN A, C
SET PC, clear_loop
:clear_end
SET [video_cur], [video_mem]
SET C, POP
SET B, POP
SET A, POP
SET PC, POP
; Clamps the value to within the screen memory map
; A -> Value input
; A <- Clamped value
:clamp_to_screen
SET PUSH, B
SET B, [video_width]
MUL B, [video_height]
ADD B, [video_mem]
IFG A, B
SET A, B
SET B, POP
SET PC, POP
; Puts a single character onto the screen
; A -> X coordinate
; B -> Y coordinate
; C -> Character
:char_put
SET PUSH, A
SET PUSH, B
MUL B, [video_width]
ADD A, B
ADD A, [video_mem]
JSR clamp_to_screen
SET [A], C
:char_put_end
SET B, POP
SET A, POP
SET PC, POP
; Gets a single character from the screen
; A -> X coordinate
; B -> Y coordinate
; C <- Character
:char_get
SET PUSH, A
SET PUSH, B
MUL B, [video_width]
ADD A, B
ADD A, [video_mem]
JSR clamp_to_screen
SET C, [A]
:char_get_end
SET B, POP
SET A, POP
SET PC, POP
; converts a number to a string in the given radix
; e.g. 2 is binary, 10 is decimal and 16 is hex
; A = input number
; B = buffer location
; C = radix
:itoa
SET PUSH, I
SET PUSH, B
SET PUSH, A
SET I, 0
;count how long it is
:itoa_0
ADD I, 1
DIV A, C
IFN A, 0
SET PC, itoa_0
;itoa proper
ADD B, I
SET [B], 0
SET A, PEEK
:itoa_1
SUB B, 1
SET I, A
MOD I, C
DIV A, C
IFG I, 9
ADD I, 7
ADD I, 0x30
SET [B], I
IFN A, 0
SET PC, itoa_1
SET A, POP
SET B, POP
SET I, POP
SET PC, POP
; Takes a text buffer containing an integer and converts it to an integer
; A: Address of text buffer
:atoi
SET PUSH, A
SET PUSH, B
SET C, 0
:atoi_loop
IFE [A], 0
SET PC, atoi_end
; Capture the first digit and subtract 48 so our ASCII code for the digit becomes the numeric value of the digit
SET B, [A]
SUB B, 48
; Add the value of the digit to the accumulator
ADD C, B
; Increment our address and multiply the accumulator
ADD A, 1
IFE [A], 0
SET PC, atoi_end
MUL C, 10
SET PC, atoi_loop
:atoi_end
SET B, POP
SET A, POP
SET PC, POP
; PUSHes all registers to the stack
:pusha
SET [pushpop_buffer], POP ; Save jump-back-address
SET PUSH, A
SET PUSH, B
SET PUSH, C
SET PUSH, X
SET PUSH, Y
SET PUSH, Z
SET PUSH, I
SET PUSH, J
SET PUSH, EX
SET PC, [pushpop_buffer] ; jump back
; POPs all registers from the stack
:popa
SET [pushpop_buffer], POP ; Save jump-back-address
SET EX, POP
SET J, POP
SET I, POP
SET Z, POP
SET Y, POP
SET X, POP
SET C, POP
SET B, POP
SET A, POP
SET PC, [pushpop_buffer] ; jump back
:pushpop_buffer dat 0x0000
; Copies a string from a source to a destination
; A -> source address
; B -> destination address
:strcpy
SET PUSH, A
SET PUSH, B
:strcpy_loop
SET [B], [A]
ADD A, 1
ADD B, 1
IFN [A], 0
SET PC, strcpy_loop
:strcpy_end
SET B, POP
SET A, POP
SET PC, POP
; Copies a string from a source to a destination with length limitation
; Takes:
; A: source
; B: destination
; C: length
:strncpy
SET PUSH, A
SET PUSH, B
SET PUSH, C
ADD C, B
:strncpy_loop1
IFE [A], 0
SET PC, strncpy_loop2
SET [B], [A]
ADD A, 1
ADD B, 1
IFE B, C
SET PC, strncpy_end
SET PC, strncpy_loop1
:strncpy_loop2
SET [B], 0
ADD B, 1
IFN B, C
SET PC, strncpy_loop2
:strncpy_end
SET C, POP
SET B, POP
SET A, POP
SET PC, POP
; Compares strings and stores the result in C and EX
; A=B => C=0, EX=0
; A>B => C>0, EX=0
; A<B => C>0, EX>0
; Takes:
; A -> source #1
; B -> source #2
; C <- result
; EX <- result part B
:strcmp
SET PUSH, A
SET PUSH, B
SET EX, 0
SET C, 0
:strcmp_loop
IFE [A], 0
SET PC, strcmp_end
IFN [A], [B]
SET PC, strcmp_end
ADD A, 1
ADD B, 1
SET PC, strcmp_loop
:strcmp_end
SET C, [A]
SUB C, [B]
SET B, POP
SET A, POP
SET PC, POP
; strlen
; A -> Address of the string buffer
; B <- Length of string
:strlen
SET PUSH, A
SET B, 0
:strlen_loop
ADD A, 1
IFN [A], 0
SET PC, strlen_loop
:strlen_end
SET B, A
SUB B, PEEK
SET A, POP
SET PC, POP
; Reads a line of chars from the keyboard
; A: String buffer address
; B: Length
; C: Keybuffer
:read_line
SET PUSH, C
SET PUSH, B
SET PUSH, A
JSR mem_clear ; Clear the buffer
ADD B, A
:read_line_loop
JSR proc_suspend
;INT [clock_address]
IFE [C], 0x11 ; 13 -> 17 per keyboard HW (enter)
SET PC, read_line_end
IFE [C], 0x10 ; 8 -> 16 per keyboard HW (backspace)
SET PC, read_line_backspace
IFG 0x20, [C]
SET PC, read_line_skip
IFG [C], 0x7F
SET PC, read_line_skip
IFE A, B
SET PC, read_line_skip
SET [A], [C]
; Put the character on-screen so the user can see what is being typed
; Maybe have this toggleable?
SET PUSH, A
SET PUSH, B
SET B, [A]
BOR B, 0x7400
SET A, B
SET B, [video_cur]
SET [B], A
ADD [video_cur], 1
SET A, [video_width]
MUL A, [video_height]
ADD A, [video_mem]
IFE [video_cur], A
JSR scroll
SET B, POP
SET A, POP
ADD A, 1
:read_line_skip
; Display the blinking cursor
SET PUSH, A
SET PUSH, B
SET PUSH, C
SET A, [video_cur]
SUB A, [video_mem]
SET B, 0
SET C, 0x709F
JSR char_put
SET C, POP
SET B, POP
SET A, POP
SET PC, read_line_loop
:read_line_backspace
; Remove the blinking cursor
SET PUSH, A
SET PUSH, B
SET PUSH, C
SET A, [video_cur]
SUB A, [video_mem]
SET B, 0
SET C, [video_clear]
JSR char_put
SET C, POP
SET B, POP
SET A, POP
; Ensure we don't backspace past the beginning
IFE A, PEEK
SET PC, read_line_skip
SET PUSH, A
SET PUSH, B
SUB [video_cur], 1
SET B, [video_cur]
SET [B], 0
SET B, POP
SET A, POP
SUB A, 1
SET PC, read_line_skip
:read_line_end
; Remove the blinking cursor
SET PUSH, A
SET PUSH, B
SET PUSH, C
SET A, [video_cur]
SUB A, [video_mem]
SET B, 0
SET C, [video_clear]
JSR char_put
SET C, POP
SET B, POP
SET A, POP
; Add the null terminator
SET [A], 0
; Pop everything back out
SET A, POP
SET B, POP
SET C, POP
SET PC, POP
; Sleeps for some cycles
; Max of 3000 ms otherwise B will overflow
; CAUTION! This sleep is blocking. It does not release to other processes during the sleep
; A -> number of milliseconds to wait
:sleep
MUL A, 20
:sleep_loop
SUB A, 1
IFN A, 0
SET PC, sleep_loop
SET PC, POP
; Returns a randomized number in A
:rand
MUL [entropy], 52265
ADD [entropy], 135
SET A, [entropy]
SET PC, POP
; Takes a seed in A
:srand
MUL A, 49763
SHL A, 2
XOR A, 1273
SET [entropy], A
SET PC, POP