-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsnip.z80
3248 lines (2927 loc) · 52.4 KB
/
parsnip.z80
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;Parsnip
; A parser for the TI-83+/84+/SE calculators.
; --> https://github.com/Zeda/Parsnip
#include "parsnip.inc"
#define speed
#define ALIGN_COMMAND_TABLE ;Comment this to potentially save some bytes, uncomment to save some clock cycles
#define STACK_SAFE ;Makes the stack routines verify that they aren't overflowing
.org $4000
.db $80,$0F, 0,0,0,0
.db $80,$12, $01,$04 ;signing key ID
.db $80,$47, "Parsnip" ;change the $47 according to name len.
.db $80,$81, 2 ;num pages
.db $80,$90 ;no splash
.db $03,$22,$09,$00 ;date stamp
.db $02,$00 ;date stamp signature
.db $80,$70 ;final field
jp main
jumptable:
#include "jmptable.z80"
SelectedProg:
bcall(_OP5ToOP1)
SelectedProgOP1:
ld hl,gbuf
ld (buf_ptr),hl
bcall(_ChkFindSym)
ret c
ld hl,cmdShadow+2
ld a,$BB
cp (hl)
jr nz,+_
inc l
ld a,$6D
cp (hl)
jp z,EndHook_prepush
_:
ld a,(temp_word_3)
or a
jr z,ExecOP1
;Here we need to move the code to RAM.
;Put the name in OP1
ld hl,GramTempName
rst rMov9ToOP1
;Now we create the var
ld a,16h
ld hl,(temp_word_4)
bcall(_CreateVar)
inc de
inc de
;Copy the source to RAM
;If size is 0, just exit.
ld bc,(temp_word_4)
ld a,b
or c
jr nz,+_
pop hl
ret
_:
ld hl,(temp_word_5)
ld a,(temp_word_3)
call nz,ReadArc
;Now we copy the temp var's name back to OP1 and call it good :)
ld hl,OP4
rst rMov9ToOP1
ExecOP1:
ld hl,OP1
ld de,basic_prog
call mov9_p0
bcall(_ChkFindSym)
ld a,b
ret c
or a
ret nz
ex de,hl
ld c,(hl)
inc hl
ld b,(hl)
inc hl
parse_via_ptr:
;HL points to code
;BC is the size
ld (parsePtr),hl
ld (progStart),hl
add hl,bc
ld (progEnd),hl
grazh:
call SetUpData
ld a,2
ld (text_mode),a
call heap_init
di
ld hl,BreakProgram
push hl
ParserNext:
bit OnBlockFlag,(iy+UserFlags)
call z,CheckStatus
call z,err_ON
ld de,ParserNext
push de
ParseArg:
ld de,(parsePtr)
ld hl,(progEnd)
scf
sbc hl,de
jr c,end_of_program
ex de,hl
ParseArg2:
ld a,(hl)
inc hl
ld (parsePtr),hl
ex de,hl
#ifdef ALIGN_COMMAND_TABLE
ld h,CommandJumpTable>>8
add a,a
ld l,a
jr nc,+_
inc h
_:
rra
#else
ld c,a
ld hl,CommandJumpTable
ld b,0
add hl,bc
add hl,bc
#endif
ld c,(hl)
inc hl
ld b,(hl)
push bc
ex de,hl
ret
end_of_program:
call exec_newline
jp StopToken
_:
dec a
call opstackprecedence
exec_newline:
;need to pop and evaluate the stack
ld hl,(opstack_ptr)
xor a
cp (hl)
jr nz,-_
_:
;Either 0 or 1 operands should be left on the stack!
;Return if there are no operands left
call isoperandstack_empty
ret z
;There is an operand, so pop it into Ans
call popoperand_ui16
ld (Ans),de
;Now if there are any more operands, throw an error
call isoperandstack_empty
ret z
call err_too_many_args
isoperandstack_empty:
or a
ld hl,(operandstack_ptr)
ld de,(operandstack_bottom)
sbc hl,de
ret
exec_goto:
;WARNING:
; This routine needs to verify that the stack and opstack don't have anything.
ld de,(progStart)
;HL points to the label name
;DE points to where to begin searching
push hl
ld hl,(progEnd)
or a
sbc hl,de
ld b,h
ld c,l
pop hl
;HL points to the label name
;DE points to where to begin searching
;BC is the number of bytes to search
ex de,hl
call searchlabel
jp nz,err_label_not_found
ld (parsePtr),hl
ret
exec_else:
ld hl,(parsePtr)
ld a,(hl)
cp $CE ;If
jr nz,+_
inc hl
_:
call seek_end_from_HL
exec_end:
; First verify that we have a 0 on the opstack
ld hl,(opstack_ptr)
ld a,(hl)
or a
jp nz,err_syntax
inc hl
ld a,(hl)
cp $CE
jr z,end_if
cp $D1 ;While token
jr z,end_while
cp $D2 ;Repeat token
jr z,end_repeat
jp err_fatal
end_if:
inc hl
ld (opstack_ptr),hl
ret
end_repeat:
call eval_condition
ret z
ld (opstack_ptr),hl
ld (parsePtr),de
ret
end_while:
call eval_condition
ret nz
ld (opstack_ptr),hl
ld (parsePtr),de
ret
eval_condition:
inc hl
ld e,(hl)
inc hl
ld d,(hl)
inc hl
push hl ;points to the top of the OP stack if done
call parse_condition
pop hl
ret
parse_condition:
ld hl,(parsePtr)
push hl
ld (parsePtr),de ;points to the condition
_:
; Parse the condition
call parseArg
ld hl,(parsePtr)
ld a,(hl)
sub $3E
and $FE
jr nz,-_
inc hl
ld (parsePtr),hl
;now make sure process the stuff on the opstack
ld a,254
_:
call opstackprecedence
jr nc,-_
call popoperand_bool
pop de
ret
exec_lbl:
seek_EOL:
ld a,(hl)
inc hl
sub $3E ; colon
and $FE
jr nz,exec_lbl
ld (parsePtr),hl
ret
exec_sqr:
ld hl,(operandstack_ptr)
dec hl
ld a,(hl)
ld c,a
cp type_var
call z,peekvar
ld a,c
ld hl,eval_sqr_LUT
jp jumptableindex
exec_inv:
ld hl,(operandstack_ptr)
dec hl
ld a,(hl)
ld c,a
cp type_var
call z,peekvar
ld a,c
ld hl,eval_inv_LUT
jp jumptableindex
exec_cubed:
ld hl,(operandstack_ptr)
dec hl
ld a,(hl)
ld c,a
cp type_var
call z,peekvar
ld a,c
ld hl,eval_cubed_LUT
jp jumptableindex
exec_while:
ex de,hl
call parse_condition
jp z,seek_end
ex de,hl
ld (parsePtr),hl
ld a,$D1
exec_repeat:
; Need to push HL to the operator stack, this points to the expression
; need to push the repeat token to the operator stack
; need to push a 0 to the operator stack
; skip to the end of the line
; execute as normal
ex de,hl
ld hl,(opstack_ptr)
dec hl
ld (hl),d
dec hl
ld (hl),e
dec hl
ld (hl),a
dec hl
ld (hl),$00
ld (opstack_ptr),hl
ex de,hl
jr seek_EOL
exec_comma:
;evaluate up to the function on the stack
call +_
; ld hl,(opstack_ptr)
inc hl
ld (opstack_ptr),hl
ret
_:
exec_multibyte:
exec_open_parens:
exec_open_list:
exec_if:
exec_for:
exec_raw:
exec_full:
exec_clrdraw:
exec_clrhome:
exec_dispgraph:
exec_text:
exec_putsprite:
exec_getkey:
exec_pixelon:
exec_pixeloff:
exec_pixeltoggle:
exec_pixeltest:
exec_int:
exec_ln:
exec_exp:
exec_sqrt:
exec_pause:
exec_disp:
exec_fix:
exec_sto:
exec_eq:
exec_neq:
exec_le:
exec_ge:
exec_lt:
exec_gt:
exec_add:
exec_sub:
exec_mul:
exec_div:
exec_neg:
exec_pow:
;pop off ops and evaluate until op has lower precedence
push af
call getprecedence
jr z,opeval
;here we have a function
;So we need to push it to the operator stack
;And then we need to push a null byte to the operand stack to indicate the end
;of the arguments for the function (for when we need to evaluate the function).
#ifdef STACK_SAFE
ld hl,(operandstack_top)
ld bc,(operandstack_ptr)
scf
sbc hl,bc
jp c,err_operand_stack_overflow
ld a,type_param_end
ld (bc),a
inc bc
ld (operandstack_ptr),bc
#else
ld hl,(operandstack_ptr)
ld (hl),type_param_end
inc hl
ld (operandstack_ptr),hl
#endif
ld hl,(opstack_ptr)
dec hl
ld (opstack_ptr),hl
pop af
ld (hl),a
ret
exec_close_parens:
;need to evaluate up to the open parens or function
call end_arg
jr eval_func
end_arg:
ld hl,(opstack_ptr)
ld a,(hl)
inc hl
call getprecedence
cp 254
call c,eval_func
jr c,end_arg
ret
opeval:
call opstackprecedence
jr nc,opeval
;push the operator
dec hl
dec hl
ld (opstack_ptr),hl
pop af
ld (hl),a
ret
opstackprecedence:
;Input:
; A is the precedence of the input operator.
; HL points to the first byte of the operator to compare to.
;Output:
; nc if the input operator is of greater or equal precedence
; HL points to the next operator
push af
ld hl,(opstack_ptr)
ld a,(hl)
inc hl
call getprecedence
ld b,a
pop af
cp b
ret c
eval_func:
push af
ld de,(opstack_ptr)
ld a,(de)
or a
scf
ret z
ld (opstack_ptr),hl
ld c,a
#ifdef ALIGN_COMMAND_TABLE
ld h,evalLUT>>8
add a,a
ld l,a
jr nc,+_
inc h
_:
#else
ld hl,evalLUT
ld b,0
add hl,bc
add hl,bc
#endif
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
ld a,c
call call_hl
pop af
ret
call_hl:
jp (hl)
getprecedence:
;A is the first byte of the operator
;HL points to the next byte(s), if any
push hl
ld bc,(precedenceLUT_end-precedenceLUT)/2
ld hl,precedenceLUT
_:
cpi
jr z,+_
inc hl
jp pe,-_
_:
;we have a match
ld a,(hl)
pop hl
ret
precedenceLUT:
.db 0,255 ;highest precedence
.db $2B,-3 ;comma
.db $6A,30 ;=
.db $6F,30 ;!=
.db $6B,32 ;<
.db $6C,32 ;>
.db $6D,32 ;<=
.db $6E,32 ;>=
.db $B0,41 ;(-)
.db $F0,47 ;^
.db $83,48 ;division
.db $82,49 ;multiplication
.db $71,50 ;subtraction
.db $70,51 ;addition
.db $04,-2 ;sto
precedenceLUT_end:
.db -2
type_size_LUT:
.db 1 ; 00 end of params
.db 2 ; 01 ui8
.db 3 ; 02 ui16
.db 5 ; 03 ui32
.db 3 ; 04 fixed88
.db 5 ; 05 fixed1616
.db 2 ; 06 var
.db 1 ; 07 true
.db 1 ; 08 false
.db 5 ; 09 tstr_ref
.db 0 ; 0A raw
.db 0 ; 0B str
.db 5 ; 0C str_ref
.db 5 ; 0D single
.db 11 ; 0E xfloat
.db 0 ; 0F list
.db 5 ; 10 list_ref
.db 2 ; 11 char
.db 3 ; 12 ref
.db 1 ; 13 display
type_size_LUT_end:
exec_close_list:
; There is a special case to take care of!
; In the event of [stuff][index], we need to check if the next char to be parsed
; is a '['. If so, need to store t_open_index instead of t_open_list
;
; As well, ']' can match '[' or t_open_index.
; If it is an index:
; If the previous arg was a temp list, replace it with the sublist.
; Else replace the previous arg with an index variable, {t_index,t_var,.dw #, .dw upper, .dw lower}
; Else need to allocate space for the list
; '[' is stored like a function, so find the start of the "function"
; arguments in the operandstack.
; Have to work backwards, also get the size of each element (needs to be
; evaluated)
call end_arg
; Make sure the opstack has a `[` on it
ld hl,(opstack_ptr)
ld a,(hl)
inc hl
ld (opstack_ptr),hl
cp tok_index
jp z,exec_close_index
cp $06
jp nz,err_syntax
; Initialize the list field (and set it to size 0)
ld hl,(heap_ptr)
ld (hl),type_list
inc hl
xor a
ld (hl),a
inc hl
ld (hl),a
inc hl
ld (hl),a
inc hl
ld (hl),a
inc hl
ld (heap_ptr),hl
ld h,a
ld l,a
ld (temp_word_1),hl
ld hl,(heap_top)
push hl
jr write_list_start
write_list_loop:
ld hl,temp_word_1
inc (hl)
jr nz,+_
inc hl
inc (hl)
_:
call pop_operand
pop hl
or a
sbc hl,bc
push hl
;make sure HL>=heap_ptr
push de
ld de,(heap_ptr)
add a,1 ;resets carry flag and sets A to the type
sbc hl,de
jp c,err_mem
add hl,de
pop de
ld (hl),a
push hl
ld hl,type_size_LUT
add a,l
ld l,a
jr nc,$+3
inc h
ld a,(hl)
pop hl
or a
jr nz,+_
;need to write the size bytes still
pop hl
ld a,(hl)
dec hl
dec hl
dec hl
ld (hl),a
push hl
inc hl
ld (hl),c
inc hl
ld (hl),b
inc bc
_:
cpi
jp po,write_list_start
ex de,hl
ldir
write_list_start:
call is_end_of_args
jr nz,write_list_loop
;pop off the end-of-args
ld (operandstack_ptr),hl
pop hl ;start of the list data
ld de,(temp_word_1) ;number of elements
dec hl
ld (hl),d
dec hl
ld (hl),e
ex de,hl
ld hl,(heap_top)
or a
sbc hl,de
ld b,h
ld c,l
ld hl,(heap_ptr)
dec hl
dec hl
dec hl
dec hl
push hl
push bc
ld (hl),c
inc hl
ld (hl),b
inc hl
ex de,hl
ldir
ex de,hl
ld (heap_ptr),hl
pop bc
pop de
dec de
ld a,type_list_ref
call pushoperand_ref
jp check_if_index
exec_close_index:
call popoperand_ui16
push de
call is_end_of_args
jp nz,err_syntax
ld (operandstack_ptr),hl
call pop_operand
pop hl ;the index
cp type_str-1
jr z,exec_str_index
cp type_str_ref-1
jr z,exec_str_ref_index
cp type_list-1
jr z,exec_list_index
cp type_list_ref-1
jr z,exec_list_ref_index
jp err_syntax
exec_list_ref_index:
ld b,h
ld c,l
ex de,hl
ld e,(hl)
inc hl
ld d,(hl)
ld h,b
ld l,c
inc de
inc de
inc de
exec_list_index:
ex de,hl
ld c,(hl)
inc hl
ld b,(hl)
inc hl
ex de,hl
;make sure HL<BC
; or a
sbc hl,bc
jp nc,err_bad_index
add hl,bc
ld b,h
ld c,l
ex de,hl
jr list_index_start
list_index_loop:
call next_list_element
dec bc
list_index_start:
ld a,b
or c
jr nz,list_index_loop
;HL points to the var
call pushoperand_type_ref
jp check_if_index
exec_str_ref_index:
push hl
ex de,hl
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld c,(hl)
inc hl
ld b,(hl)
pop hl
exec_str_index:
;make sure HL<BC-1
dec bc
; or a
sbc hl,bc
jp nc,err_bad_index
add hl,bc
add hl,de
ld a,(hl)
jp pushoperand_char
exec_dec:
;If numeric, jump to exec_num
ld a,(hl)
cp '0'
jr c,+_
cp '9'+1
jr c,exec_num
_:
;First, get the type of the previous element
ld hl,(operandstack_ptr)
dec hl
ld a,(hl)
cp type_ref
call z,get_ref_type
cp type_var
call z,get_var_type
push af ;type
ld hl,type_methods_LUT
add a,a
add a,l
ld l,a
jr nc,$+3
inc h
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
or h
jp z,err_bad_name
ld e,(hl)
inc hl
;HL points to the LUT
ld (temp_word_2),hl
ld hl,(parsePtr)
ld a,(hl)
inc hl
call tok_to_alnum
ld b,h
ld c,l
ld (temp_word_1),a
;E is the number of elements
ld l,e
ld h,0
ld ix,methods_LUT_search
call binarysearch
jp nz,err_bad_name
ld hl,(temp_word_2)
ld a,(hl)
cp $10 ;(
ld a,(temp_word_1+1) ;function number or var number
rla
jr nc,exec_method_var
jp nz,err_missing_open_parens
inc hl
ld (parsePtr),hl
ld hl,(opstack_ptr)
dec hl
ld (hl),a ;function number*2
pop af
dec hl
ld (hl),a ;type
dec hl
ld (hl),d ;0
ld(opstack_ptr),hl
ld a,tok_method*2
push af
jp push_2byte_op
exec_method_var:
ld (parsePtr),hl
ld hl,(temp_word_3)
jp (hl)
exec_num:
ex de,hl
dec de
call atoui32
;make sure it isn't supposed to be a float!
jr z,+_
;HLIX
ld (parsePtr),de
push ix
pop de
ld a,h
or l
jp z,pushoperand_ui16
ex de,hl
jp pushoperand_ui32
_:
;If it got this far, we actually have a float or a fixed-point number
;BC points to the number
;Default to an xfloat
ld h,b
ld l,c
ld bc,OP1
call strtox_p0
;save the parsePtr
ld hl,(strtox_ptr)
ld (parsePtr),hl
; allocate space on the stack for an xfloat
pushoperand_xfloat:
;BC points to the float
#ifdef STACK_SAFE
ld de,(operandstack_ptr)
ld hl,(operandstack_top)
or a
sbc hl,de
;if HL<11, oh no!
ld a,l
sub 11
ld a,h
sbc a,0
jp c,err_operand_stack_overflow
#endif
ld h,b
ld l,c
call mov10_p0
ex de,hl
ld (hl),type_xfloat
inc hl
ld (operandstack_ptr),hl
ret
exec_rand:
ld bc,xOP1
call xrand_p0
jp pushoperand_xfloat
exec_pi:
ld de,(operandstack_ptr)
ld a,X_CONST_PI
call xconst_to_p0
ex de,hl
ld (hl),type_xfloat
inc hl
ld (operandstack_ptr),hl
ret
exec_BB:
ld a,(hl)
cp $31 ;e token
jr z,exec_e
; Check if it is lowercase
cp $B0
jr c,+_
cp $CB
jp c,exec_var_pre
_:
jp ErrBadToken
exec_e:
inc hl
ld (parsePtr),hl
ld de,(operandstack_ptr)
ld a,X_CONST_e
call xconst_to_p0
ex de,hl
ld (hl),type_xfloat
inc hl
ld (operandstack_ptr),hl
ret
eval_internal:
ld hl,(opstack_ptr)
ld a,(hl)
inc hl
ld (opstack_ptr),hl
;A is the actual function to execute
ld hl,eval_func_p1
ld (next_page_call_address),hl
jp next_page_call
eval_dispgraph:
;might have 0 or 1 argument
;assume 0 for now
call pop_end_of_params
jp GraphToLCD
eval_clrdraw:
;might have 0 or 1 argument
;assume 0 for now
call pop_end_of_params
ld hl,gbuf
jp zeromem768
eval_clrhome:
call pop_end_of_params
bcall(_HomeUp)
bcall(_ClrScrnFull)
ret
eval_full:
call pop_end_of_params
in a,(2)
add a,a