forked from johanberntsson/ozmoo
/
zmachine.asm
2118 lines (1940 loc) · 39.7 KB
/
zmachine.asm
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
z_extended_opcode !byte 0
z_operand_count !byte 0
z_canonical_opcode !byte 0
z_operand_type_arr !byte 0, 0, 0, 0, 0, 0, 0, 0
z_operand_value_high_arr !byte 0, 0, 0, 0, 0, 0, 0, 0
z_operand_value_low_arr !byte 0, 0, 0, 0, 0, 0, 0, 0
z_local_var_count !byte 0
z_temp !byte 0, 0, 0, 0, 0
z_rnd_a !byte 123
z_rnd_b !byte 75
z_rnd_c !byte 93
z_rnd_x !byte 1
z_rnd_mode !byte 0
z_test !byte 0
z_test_mode_print = 1
z_test_mode_print_and_store = 2
z_font !byte 1, 1
z_window_lower = 0
z_window_upper = 1
z_window !byte z_window_lower
z_interrupt_return_value !byte 0,0
; opcount0 = 0
; opcount1 = 16
; opcount2 = 32
; opcountvar = 64
; opcountext = 96
; =========================================== Highbytes of jump table
z_jump_high_arr
; 0OP
!byte >z_ins_rtrue
!byte >z_ins_rfalse
!byte >z_ins_print
!byte >z_ins_print_ret
!byte >z_ins_nop
!ifndef Z5PLUS {
!byte >z_ins_save
!byte >z_not_implemented
} else {
!byte >z_not_implemented
!byte >z_not_implemented
}
!byte >z_not_implemented
!byte >z_ins_ret_popped
!ifndef Z5PLUS {
!byte >stack_pull ; z_ins_pop
} else {
!byte >z_ins_catch
}
!byte >z_ins_quit
!byte >z_ins_new_line
!ifdef Z3 {
!byte >z_ins_show_status
} else {
!byte >z_ins_nop ; should be nop according to show_status/spec 1.0
}
!byte >make_branch_true ; z_ins_verify
!ifdef Z5PLUS {
!byte >z_not_implemented
!byte >make_branch_true ; z_ins_piracy
} else {
!byte >z_not_implemented
!byte >z_not_implemented
}
; 1OP
!byte >z_ins_jz
!byte >z_ins_get_sibling
!byte >z_ins_get_child
!byte >z_ins_get_parent
!byte >z_ins_get_prop_len
!byte >z_ins_inc
!byte >z_ins_dec
!byte >z_ins_print_addr
!byte >z_ins_call_xs
!byte >z_ins_remove_obj
!byte >z_ins_print_obj
!byte >z_ins_ret
!byte >z_ins_jump
!byte >z_ins_print_paddr
!byte >z_ins_load
!ifndef Z5PLUS {
!byte >z_ins_not
} else {
!byte >z_ins_call_xn
}
; 2OP
!byte >z_not_implemented
!byte >z_ins_je
!byte >z_ins_jl
!byte >z_ins_jg
!byte >z_ins_dec_chk
!byte >z_ins_inc_chk
!byte >z_ins_jin
!byte >z_ins_test
!byte >z_ins_or
!byte >z_ins_and
!byte >z_ins_test_attr
!byte >z_ins_set_attr
!byte >z_ins_clear_attr
!byte >z_ins_store
!byte >z_ins_insert_obj
!byte >z_ins_loadw_and_storew
!byte >z_ins_loadb
!byte >z_ins_get_prop
!byte >z_ins_get_prop_addr
!byte >z_ins_get_next_prop
!byte >z_ins_add
!byte >z_ins_sub
!byte >z_ins_mul
!byte >z_ins_div
!byte >z_ins_mod
!ifndef Z4PLUS {
!byte >z_not_implemented
} else {
!byte >z_ins_call_xs
}
!ifndef Z5PLUS {
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
} else {
!byte >z_ins_call_xn
!byte >z_not_implemented
!byte >z_ins_throw
}
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_ins_call_xs
!byte >z_ins_loadw_and_storew
!byte >z_ins_storeb
!byte >z_ins_put_prop
!ifndef Z5PLUS {
!byte >z_ins_sread
} else {
!byte >z_ins_aread
}
!byte >z_ins_print_char
!byte >z_ins_print_num
!byte >z_ins_random
!byte >z_ins_push
!byte >z_ins_pull
!byte >z_ins_split_window
!byte >z_ins_set_window
!byte >z_ins_call_xs
!ifdef Z4PLUS {
!byte >z_ins_erase_window
!byte >z_ins_erase_line
!byte >z_ins_set_cursor
!byte >z_ins_get_cursor
!byte >z_ins_set_text_style
!byte >z_ins_buffer_mode
} else {
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
}
!byte >z_ins_output_stream
!byte >z_not_implemented
!byte >z_ins_sound_effect
!ifdef Z4PLUS {
!byte >z_ins_read_char
!byte >z_ins_scan_table
} else {
!byte >z_not_implemented
!byte >z_not_implemented
}
!ifdef Z5PLUS {
!byte >z_ins_not
!byte >z_ins_call_xn
!byte >z_ins_call_xn
!byte >z_ins_tokenise_text
!byte >z_ins_encode_text
!byte >z_not_implemented
!byte >z_ins_print_table
!byte >z_ins_check_arg_count
} else {
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
}
; EXT
z_opcount_ext_jump_high_arr
!ifdef Z5PLUS {
!byte >z_ins_save
!byte >z_not_implemented
!byte >z_ins_log_shift
!byte >z_ins_art_shift
!byte >z_ins_set_font
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_ins_save_restore_undo
!byte >z_ins_save_restore_undo
!byte >z_not_implemented
!byte >z_not_implemented
!byte >z_ins_set_true_colour
}
; =========================================== Lowbytes of jump table
z_jump_low_arr
!byte <z_ins_rtrue
!byte <z_ins_rfalse
!byte <z_ins_print
!byte <z_ins_print_ret
!byte <z_ins_nop
!ifndef Z5PLUS {
!byte <z_ins_save
!byte <z_not_implemented
} else {
!byte <z_not_implemented
!byte <z_not_implemented
}
!byte <z_not_implemented
!byte <z_ins_ret_popped
!ifndef Z5PLUS {
!byte <stack_pull ; z_ins_pop
} else {
!byte <z_ins_catch
}
!byte <z_ins_quit
!byte <z_ins_new_line
!ifdef Z3 {
!byte <z_ins_show_status
} else {
!byte <z_ins_nop ; should be nop according to show_status/spec 1.0
}
!byte <make_branch_true ; z_ins_verify
!ifdef Z5PLUS {
!byte <z_not_implemented
!byte <make_branch_true ; z_ins_piracy
} else {
!byte <z_not_implemented
!byte <z_not_implemented
}
; 1OP
!byte <z_ins_jz
!byte <z_ins_get_sibling
!byte <z_ins_get_child
!byte <z_ins_get_parent
!byte <z_ins_get_prop_len
!byte <z_ins_inc
!byte <z_ins_dec
!byte <z_ins_print_addr
!byte <z_ins_call_xs
!byte <z_ins_remove_obj
!byte <z_ins_print_obj
!byte <z_ins_ret
!byte <z_ins_jump
!byte <z_ins_print_paddr
!byte <z_ins_load
!ifndef Z5PLUS {
!byte <z_ins_not
} else {
!byte <z_ins_call_xn
}
; 2OP
!byte <z_not_implemented
!byte <z_ins_je
!byte <z_ins_jl
!byte <z_ins_jg
!byte <z_ins_dec_chk
!byte <z_ins_inc_chk
!byte <z_ins_jin
!byte <z_ins_test
!byte <z_ins_or
!byte <z_ins_and
!byte <z_ins_test_attr
!byte <z_ins_set_attr
!byte <z_ins_clear_attr
!byte <z_ins_store
!byte <z_ins_insert_obj
!byte <z_ins_loadw_and_storew
!byte <z_ins_loadb
!byte <z_ins_get_prop
!byte <z_ins_get_prop_addr
!byte <z_ins_get_next_prop
!byte <z_ins_add
!byte <z_ins_sub
!byte <z_ins_mul
!byte <z_ins_div
!byte <z_ins_mod
!ifndef Z4PLUS {
!byte <z_not_implemented
} else {
!byte <z_ins_call_xs
}
!ifndef Z5PLUS {
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
} else {
!byte <z_ins_call_xn
!byte <z_not_implemented
!byte <z_ins_throw
}
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
; VAR
!byte <z_ins_call_xs
!byte <z_ins_loadw_and_storew
!byte <z_ins_storeb
!byte <z_ins_put_prop
!ifndef Z5PLUS {
!byte <z_ins_sread
} else {
!byte <z_ins_aread
}
!byte <z_ins_print_char
!byte <z_ins_print_num
!byte <z_ins_random
!byte <z_ins_push
!byte <z_ins_pull
!byte <z_ins_split_window
!byte <z_ins_set_window
!byte <z_ins_call_xs
!ifdef Z4PLUS {
!byte <z_ins_erase_window
!byte <z_ins_erase_line
!byte <z_ins_set_cursor
!byte <z_ins_get_cursor
!byte <z_ins_set_text_style
!byte <z_ins_buffer_mode
} else {
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
}
!byte <z_ins_output_stream
!byte <z_not_implemented
!byte <z_ins_sound_effect
!ifdef Z4PLUS {
!byte <z_ins_read_char
!byte <z_ins_scan_table
} else {
!byte <z_not_implemented
!byte <z_not_implemented
}
!ifdef Z5PLUS {
!byte <z_ins_not
!byte <z_ins_call_xn
!byte <z_ins_call_xn
!byte <z_ins_tokenise_text
!byte <z_ins_encode_text
!byte <z_not_implemented
!byte <z_ins_print_table
!byte <z_ins_check_arg_count
} else {
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
}
; EXT
z_opcount_ext_jump_low_arr
!ifdef Z5PLUS {
!byte <z_ins_save
!byte <z_not_implemented
!byte <z_ins_log_shift
!byte <z_ins_art_shift
!byte <z_ins_set_font
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_ins_save_restore_undo
!byte <z_ins_save_restore_undo
!byte <z_not_implemented
!byte <z_not_implemented
!byte <z_ins_set_true_colour
}
z_number_of_ext_opcodes_implemented = * - z_opcount_ext_jump_low_arr
z_number_of_opcodes_implemented = * - z_jump_low_arr
; These get zeropage addresses in constants.asm:
; z_opcode
; z_opcode_number
; z_opcode_opcount ; 0 = 0OP, 1=1OP, 2=2OP, 3=VAR
z_opcode_extended = 190
z_opcode_call_vs2 = 236
z_opcode_call_vn2 = 250
z_opcode_opcount_0op = 0
z_opcode_opcount_1op = 16
z_opcode_opcount_2op = 32
z_opcode_opcount_var = 64
z_opcode_opcount_ext = 96
z_exe_mode_normal = $0
z_exe_mode_return_from_read_interrupt = $80
z_init
!zone {
!ifdef DEBUG {
!ifdef VMEM_OPTIMIZE {
jsr print_following_string
!pet "*** vmem optimization mode ***",13,13,0
}
}
ldy #1
sty z_pc_mempointer_is_unsafe
dey
sty z_exe_mode ; 0 = Normal
!ifdef TRACE {
; Setup trace
lda #0
sta z_trace_index
tay
- sta z_trace_page,y
iny
bne -
}
; Modify header to tell game about terp capabilities
!ifdef Z3 {
lda story_start + 1
and #(255 - 16 - 32 - 64) ; Screen-splitting not available, variable-pitch font is not default
sta story_start + 1
} else {
!ifdef Z4 {
lda story_start + 1
and #(255 - 4 - 8) ; bold font, italic font, timed input not available
ora #(16 + 128) ; Fixed-space style, timed input available
sta story_start + 1
} else { ; Z5PLUS
lda story_start + 1
and #(255 - 1 - 4 - 8) ; colours, bold font, italic font
ora #(16 + 128) ; Fixed-space style, timed input available
sta story_start + 1
lda story_start + $11
and #(255 - 8 - 16 - 32 - 128) ; pictures, undo, mouse, sound effect not available
sta story_start + $11
}
}
!ifdef Z4PLUS {
lda #8
sta story_start + $1e ; Interpreter number (8 = C64)
lda #64
sta story_start + $1f ; Interpreter number. Usually ASCII code for a capital letter (We use @ until the terp is ready for release)
lda #25
sta story_start + $20 ; Screen lines
lda #40
sta story_start + $21 ; Screen columns
}
!ifdef Z5PLUS {
lda #>320
sta story_start + $22 ; Screen width in units
lda #<320
sta story_start + $23 ; Screen width in units
lda #>200
sta story_start + $24 ; Screen height in units
lda #<200
sta story_start + $25 ; Screen height in units
lda #8
sta story_start + $26 ; Font width in units
sta story_start + $27 ; Font height in units
; TODO: Store default background and foreground color in 2c, 2d (or comply to game's wish?)
}
lda #0
sta story_start + $32 ; major standard revision number which this terp complies to
sta story_start + $33 ; minor standard revision number which this terp complies to
; Copy z_pc from header
sta z_pc
lda story_start + header_initial_pc
sta z_pc + 1
lda story_start + header_initial_pc + 1
sta z_pc + 2
; Setup globals pointer
lda story_start + header_globals + 1
clc
adc #<(story_start - 32)
sta z_global_vars_start
lda story_start + header_globals
adc #>(story_start - 32)
sta z_global_vars_start + 1
; Init sound
lda #0
ldx #$18
- sta $d400,x
dex
bpl -
lda #$f
sta $d418
lda #$00
sta $d405
lda #$f2
sta $d406
; Init randomization
lda #$ff
sta $d40e
sta $d40f
ldx #$80
stx $d412
!ifdef BENCHMARK {
ldy #1
jmp z_rnd_init
} else {
jmp z_rnd_init_random
}
}
!zone z_execute {
!ifdef Z4PLUS {
.return_from_interrupt
lda #z_exe_mode_normal
sta z_exe_mode
; jsr read_routine_callback
rts
}
z_execute
!ifdef DEBUG {
; Play high-pitched beep
; lda #1
; sta z_operand_value_low_arr
; jsr z_ins_sound_effect
}
.main_loop
!ifdef Z4PLUS {
lda z_exe_mode
bne .return_from_interrupt
}
!ifdef TRACE {
; Store z_pc to trace page
ldx #0
ldy z_trace_index
- lda z_pc,x
sta z_trace_page,y
iny
inx
cpx #3
bne -
sty z_trace_index
}
+read_next_byte_at_z_pc
sta z_opcode
!ifdef DEBUG {
;jsr print_following_string
;!pet "opcode: ",0
;ldx z_opcode
;jsr printx
;jsr print_following_string
;!pet " @ ",0
;ldx z_pc + 2
;lda z_pc + 1
;jsr printinteger
;jsr newline
;lda z_opcode
}
lda #z_opcode_opcount_2op
sta z_opcode_opcount ; This is the most common case. Adjust value when other case is found.
lda z_opcode
bit z_opcode
bpl .top_bits_are_0x
bvc .top_bits_are_10
; Top bits are 11. Form = Variable
tax
and #%00011111
sta z_opcode_number
txa
and #%00100000
beq + ; This is a 2OP instruction, with up to 4 operands
asl z_opcode_opcount ; Set to VAR
+ jmp .get_4_ops ; Always branch
.top_bits_are_10
!ifdef Z5PLUS {
cmp #z_opcode_extended
bne .short_form
; Form = Extended
lda #z_opcode_opcount_ext
sta z_opcode_opcount ; Set to EXT
+read_next_byte_at_z_pc
sta z_extended_opcode
sta z_opcode_number
jmp .get_4_ops
}
.short_form
; Form = Short
and #%00001111
sta z_opcode_number
ldx #0
stx z_operand_type_arr
inx
lda z_opcode
asl
asl
asl
rol z_operand_type_arr
asl
rol z_operand_type_arr
lsr z_opcode_opcount ; Set to 1OP
lda z_operand_type_arr
cmp #%11
bne +
dex
lda #z_opcode_opcount_0op
sta z_opcode_opcount ; Set to 0OP
+
stx z_operand_count
jmp .read_operands
.top_bits_are_0x
; Form = Long
asl
sta zp_temp
ldx #0
- asl zp_temp
lda #%10
bcs +
lda #%01
+ sta z_operand_type_arr,x
inx
cpx #2
bne -
stx z_operand_count
dex
lda z_opcode
and #%00011111
sta z_opcode_number
jmp .read_operands
; Get another byte of operand types
.get_4_more_ops
ldy #8
sty z_operand_count
ldy #4
ldx #4
jsr z_get_op_types
jmp .read_operands
.get_4_ops
ldy #4
sty z_operand_count
ldx #0
jsr z_get_op_types
lda z_opcode
cmp #z_opcode_call_vs2
beq .get_4_more_ops
cmp #z_opcode_call_vn2
beq .get_4_more_ops
.read_operands
ldy z_operand_count
beq .op_is_omitted
ldy #0
.read_next_operand
lda z_operand_type_arr,y
bne .operand_is_not_large_constant
sty z_temp
+read_next_byte_at_z_pc
pha
+read_next_byte_at_z_pc
ldy z_temp
tax
pla
jmp .store_operand
.operand_is_not_large_constant
; cmp #%11
; beq .op_is_omitted ; Should no longer be needed!
tax
sty z_temp
+read_next_byte_at_z_pc
ldy z_temp
cpx #%10
beq .operand_is_var
tax
lda #0
beq .store_operand ; Always branch
.operand_is_var
tax
jsr z_get_variable_value
.store_operand
sta z_operand_value_high_arr,y
txa
sta z_operand_value_low_arr,y
iny
cpy z_operand_count
bcc .read_next_operand
.op_is_omitted
sty z_operand_count
.perform_instruction
lda z_opcode_opcount
clc
adc z_opcode_number
sta z_canonical_opcode
!ifdef TRACE {
ldy z_trace_index
sta z_trace_page,y
inc z_trace_index
}
cmp #z_number_of_opcodes_implemented
bcs z_not_implemented
;.have_stored_canonical
tax
lda z_jump_low_arr,x
sta .jsr_perform + 1
lda z_jump_high_arr,x
sta .jsr_perform + 2
.jsr_perform
jsr $8000
jmp .main_loop
z_not_implemented
!ifdef DEBUG {
jsr print_following_string
!pet "opcode: ",0
ldx z_opcode
jsr printx
jsr print_following_string
!pet " @ ",0
ldx z_pc + 2
lda z_pc + 1
jsr printinteger
jsr newline
}
lda #ERROR_OPCODE_NOT_IMPLEMENTED
jsr fatalerror
}
z_get_op_types
; x = index of first operand (0 or 4), y = number of operands (1-4)
!zone {
sty z_temp
+read_next_byte_at_z_pc
sta z_temp + 1
ldy z_temp
.get_next_op_type
lda #0
asl z_temp + 1
rol
asl z_temp + 1
rol
cmp #%11
beq .done
sta z_operand_type_arr,x
inx
dey
bne .get_next_op_type
.done
stx z_operand_count
rts
}
; These instructions use variable references: inc, dec, inc_chk, dec_chk, store, pull, load
!zone {
z_get_variable_reference
; input: Variable in x
; output: Address is returned in a,x
; affects registers: p
sty zp_temp + 3
cpx #0
bne +
; Find on stack
jsr stack_get_ref_to_top_value
ldy zp_temp + 3
rts
+ txa
cmp #16
bcs .find_global_var
; Local variable
tay
dey
cpy z_local_var_count
bcs .nonexistent_local
asl
clc
adc z_local_vars_ptr
tax
lda z_local_vars_ptr + 1
adc #0
ldy zp_temp + 3
rts
.find_global_var
ldx #0
stx zp_temp + 1
asl
rol zp_temp + 1
clc
adc z_global_vars_start
tax
lda zp_temp + 1
adc z_global_vars_start + 1
ldy zp_temp + 3
rts
.nonexistent_local
lda #ERROR_USED_NONEXISTENT_LOCAL_VAR
jsr fatalerror
z_get_variable_value
; Variable in x
; Returns value in a,x
sty zp_temp + 3
cpx #0
beq .read_from_stack
txa
cmp #16
bcs .read_global_var
; Local variable
tay
dey
cpy z_local_var_count
bcs .nonexistent_local
asl
tay
iny
lda (z_local_vars_ptr),y
tax
dey
lda (z_local_vars_ptr),y
ldy zp_temp + 3
rts
.read_from_stack
jsr stack_pull
ldy zp_temp + 3
rts
.read_global_var
cmp #128
bcs .read_high_global_var
z_get_low_global_variable_value
; Read global var 0-111
; input: a = variable# + 16 (16-127)
asl
tay
iny
lda (z_global_vars_start),y
tax
dey
lda (z_global_vars_start),y
ldy zp_temp + 3
rts
.read_high_global_var
inc z_global_vars_start + 1
and #$7f ; Change variable# 128->0, 129->1 ... 255 -> 127
asl
tay
iny
lda (z_global_vars_start),y
tax
dey
lda (z_global_vars_start),y
dec z_global_vars_start + 1
ldy zp_temp + 3
rts
z_set_variable
; Value in a,x
; Variable in y
; affects: a, x, y
cpy #0
beq .write_to_stack
sta zp_temp + 2
stx zp_temp + 3
tya
cmp #16
bcs .write_global_var
; Local variable
tay
dey
cpy z_local_var_count
bcs .nonexistent_local
asl
tay
lda zp_temp + 2
sta (z_local_vars_ptr),y
iny
lda zp_temp + 3
sta (z_local_vars_ptr),y
rts
.write_to_stack
jsr stack_push
rts
.write_global_var
cmp #128
bcs .write_high_global_var
asl
tay
lda zp_temp + 2
sta (z_global_vars_start),y
iny
lda zp_temp + 3
sta (z_global_vars_start),y
rts
.write_high_global_var
inc z_global_vars_start + 1
and #$7f ; Change variable# 128->0, 129->1 ... 255 -> 127
asl
tay
lda zp_temp + 2
sta (z_global_vars_start),y
iny
lda zp_temp + 3
sta (z_global_vars_start),y
dec z_global_vars_start + 1
rts
}
!zone {
check_for_routine_0
; If value in argument 0 is 0, set status flag Z to 1 and return
lda z_operand_value_high_arr
ora z_operand_value_low_arr
rts
check_for_routine_0_and_store
; If value in argument 0 is 0, store 0 in the variable in byte at Z_PC, then set status flag Z to 1 and return
jsr check_for_routine_0
bne .not_0
lda #0
tax
jsr z_store_result
lda #0
.not_0
rts
}
!zone {
}
!zone {
z_store_result
; input: a,x hold result
; affected: a,x,y
+ pha
+read_next_byte_at_z_pc
tay
pla
jmp z_set_variable
}
!zone z_division {
z_divide
; input: Dividend in arg 0, divisor in arg 1, y = signed? 0 = unsigned, $ff = signed
; output: result in division_result (low byte, high byte)
cpy #0
beq .div_unsigned
lda z_operand_value_high_arr
eor z_operand_value_high_arr + 1
sta zp_temp + 2 ; Top bit: 1 = Result is negative, other bits must be ignored
; Get 2-complement of dividend, if negative
lda z_operand_value_low_arr