/
bios.s
3056 lines (2729 loc) · 50.8 KB
/
bios.s
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
.out ""
.ifdef PARALLELPORT
.out "Assembling Parallel port version of the bios"
.else
.out "Assembling USB version of the bios"
.endif
;protocol:
;
;3ah : read CPU space
;xxxx : start address
;yy : # pages
;a3h : confirm
;<sends data>
;4bh : write CPU space
;xxxx : start address
;yyyy : # pages
;b4h : confirm
;<store data bytes
;5ch : read PPU space
;xxxx : start address
;yyyy : # of pages
;c5h : confirm
;<sends data>
;6dh : write PPU space
;xxxx : start address
;yyyy : # of pages
;d6h : confirm
;<stores data bytes>
;7eh : execute code
;xxxx : start address
;e7h : confirm
;from above code, NES can send its own data packets. format:
;8fh : incoming data packet
;nn : bitwise:
; 0 - 0 = Horiz mirror, 1 = Vert mirror
; 1 - 0 = no WRAM, 1= WRAM
; 2 - 0 = no 4 screen, 1 = 4 screen
; 3 - 0 = normal, 1 = CPU ROM only
;mm : Mapper #
;xxxxxx : # of bytes of CPU
;xxxxxx : # of bytes of PPU
;f8h : confirm
;nn... : data sent
;MAJOR CHANGES FOR USB
; all lcd code removed
; handshaking changed, handshaking pinouts changed
; bios vers 4
.segment "ZEROPAGE"
bc:
c: .res 1
b: .res 1
joy_pad: .res 1
;80 = A
;40 = B
;20 = select
;10 = start
;08 = U
;04 = D
;02 = L
;01 = R
old_but: .res 1
char_ctr: .res 1
temp: .res 1
baton_c: .res 1
tempbank: .res 1
mtype: .res 1
addl: .res 1
addh: .res 1
npage: .res 1
mtype2: .res 1
temp_x: .res 1
temp1:
temp1_lo: .res 1
temp1_hi: .res 1
temp2:
temp2_lo: .res 1
temp2_hi: .res 1
temp_byte: .res 1
type_4016: .res 1
;nsf header info:
load: .res 2
init: .res 2
play: .res 2
len: .res 3
banks: .res 8
initsng: .res 2
.ifndef PARALLELPORT
nmicounter: .res 1
irqcounter: .res 1
.endif
crc0 := $80
crc1 := $81
crc2 := $82
crc3 := $83
s_init := $01fc
s_play := $01fe
;was $4800, some mappers use this location.
port := $4a00
topostack := $ec
realstack := $f4
;1 frame = 262 scanlines
;
;a) rendering:
; 1 garbage scanline
; 240 visible
; 1 unusable
;
;b) vblank:
; 20 usable
;
render := 27507 ;# of cycles during rendering phase
vblank := 2273 ;# of cycles during vblank phase
; 1ed - instruction counter for vblank
; 1ee - PPU 2005 1st write
; 1ef - PPU 2005 2nd write
;
; 1f0 - emu temp
; 1f1 - cycle count low
; 1f2 - cycle count high
; 1f3 - stack level 1
; 1f4 - stack level 0
; 1f5 - Acc
; 1f6 - X reg
; 1f7 - Y reg
; 1f8 - flags
; 1f9 - stack pointer
; 1fa - pcl
; 1fb - pch
; 1fc - instruction
; 1fd - address low
; 1fe - address high
; 1ff - rts
;ppustat bits:
;
;0 - if we read 2007 before or not
;1 - interrupt enable (when set, NMIs will be performed)
;2 - PPU increment quantity
;3 - rendering enabled
;4 - sprites enabled
;5 - PPU enable (when set, we will worry about the PPU)
;6 - which load of 2005/2006 we are on
;7 - NMI enable (1 = yes, 0 = no)
icount :=$01ed ;# of instructions to perform in vblank
p2005 :=$01ee ;PPU scrolling registers
emutemp :=$01f0
cyclecount :=$01f1 ;2 bytes to hold the current cycle count
; :=$01f2 ;stack level 1
; :=$01f3 ;stack level 0
reg_a :=$01f5
reg_x :=$01f6
reg_y :=$01f7
reg_p :=$01f8
reg_s :=$01f9
reg_pcl :=$01fa
reg_pch :=$01fb
ramloc :=$01fc
controlbus :=port+$00 ;port B data
; port+$01 ;port A data
; port+$02 ;port B direction register
; port+$03 ;port A direction register
; port+$04 ;timer 1 low byte
; port+$05 ;timer 1 high byte
brkpt :=port+$06 ;timer 1 period low (store breakpoint lo here!)
; port+$07 ;timer 1 period high (store breakpoint hi here!)
; port+$08 ;timer 2 low byte
; port+$09 ;timer 2 high byte
ppustat :=port+$0a ;shift register (store PPU status here!)
nothreg1 :=port+$0b ;mode control reg 1 (use bit 6 only!)
p2006lo :=port+$0c ;mode control reg 2 (2006 low)
; port+$0d ;IRQ status
p2006hi :=port+$0e ;use bits 0-6 (2006 high)
; port+$0f ;port A mirror
.segment "STACK"
.segment "RAM"
.segment "MAIN"
rts
start:
sei
cld
ldx #$fb
txs
.ifdef PARALLELPORT
lda char_ctr
sta $1fe ;do not trash the reset ram anymore than we have to.
jsr set_in ;input mode
ldx #$00
stx type_4016
stx $4016
lda #$20
bit port+$01
beq :+
lda #$02
sta type_4016
sta $4016
:
.endif
jsr init_port
lda port+$00
sta $1ff
.ifdef PARALLELPORT
jsr init_lcd
jsr load_chars
jsr init_ppu
jsr lcd_clr
lda #0 ;message 0: "welcome message"
jsr sho_msg
.else
jsr init_ppu
.endif
lda port+$00
and #$40
beq :++
.ifdef PARALLELPORT
lda #3 ;message 3: "Playing Game"
jsr sho_msg
lda $1fe
sta char_ctr ;restore uninitialized value.
.endif
ldx #7
:
lda ram_dat,x
sta $1f8,x
dex
bpl :-
jmp $1f8
:
.ifdef PARALLELPORT
lda char_ctr
sta $1fe ;stash char_ctr in temp location
.endif
ldx #0
txa
:
sta 0, x
.BYTE $9D, $FC, $00 ;absolute write
sta $200, x
sta $300, x
sta $400, x
sta $500, x
sta $600, x
sta $700, x
dex
bne :-
lda $1ff
sta temp_byte
.ifdef PARALLELPORT
lda $1fe
sta char_ctr
.endif
ldx #0
ldy #0
:
lda #$4c
sta $200,x
inx
lda vec_tab,y
iny
sta $200,x
inx
lda vec_tab,y
iny
sta $200,x
inx
cmp #$ff
bne :-
main:
.ifdef PARALLELPORT
jsr set_out
jsr lcd_clr
lda #4
jsr sho_msg ;message 4: "Waiting for Host"
.endif
jsr set_in ;set 6522 input mode
jsr read_byte ;get mode byte
cmp #$3a
beq mode_1 ;read CPU space
cmp #$4b
beq mode_2 ;write CPU space
cmp #$5c
beq mde_3 ;read PPU space
cmp #$6d
beq mde_4 ;write PPU space
cmp #$7e
beq mde_5 ;execute code
cmp #$8e
beq mde_6 ;load nsf
cmp #$9f
beq mde_7 ;run nsf
cmp #$a0
beq mde_8 ;run emulator
cmp #$a1
beq mde_9 ;send identifier string
cmp #$a2
beq mde_10 ;send version #
jmp main
mde_3: jmp mode_3
mde_4: jmp mode_4
mde_5: jmp mode_5
mde_6: jmp loadnsf
mde_7: jmp runnsf
mde_8: lda temp_byte
sta emutemp
jmp main2 ;run the emulator
mde_9: jmp identify
mde_10: jmp identify2
;read CPU space
mode_1: jsr read_pack
lda mtype2
cmp #$a3
bne main
jsr set_out ;set 6522 output mode
.ifdef PARALLELPORT
lda #5
jsr sho_msg ;message 5: "Transferring..."
.endif
ldy #0
:
lda (addl),y
jsr write_byte
iny
bne :-
inc addh
.ifdef PARALLELPORT
jsr baton
.endif
dec npage
bne :-
.ifdef PARALLELPORT
lda #6 ;message 6: "Transfer Done!"
jsr sho_msg
.endif
lda #120
jsr wait_vbl
jmp main
;write CPU space
mode_2:
jsr read_pack
lda mtype2
cmp #$b4
beq :+
j_main: jmp main
:
.ifdef PARALLELPORT
;lda #5
;jsr sho_msg
.endif
ldy #0
:
jsr read_byte
sta (addl),y
iny
bne :-
inc addh
.ifdef PARALLELPORT
;jsr baton
.endif
dec npage
bne :-
.ifdef PARALLELPORT
jsr set_out
lda #$80
jsr lcd_ins
lda $400
jsr sho_hex
lda $401
jsr sho_hex
; lda #6
; jsr sho_msg
.endif
lda #6
jsr wait_vbl
jmp main
;run code
mode_5:
jsr read_pack
lda mtype2
cmp #$e7
bne j_main
jsr set_out
.ifdef PARALLELPORT
lda #5
jsr sho_msg
.endif
lda #>(back_rd-1)
pha
lda #<(back_rd-1)
pha
jmp (addl)
back_rd:
.ifdef PARALLELPORT
lda #6
jsr sho_msg
.endif
lda #60
jsr wait_vbl
jmp main
mode_3:
mode_4: jmp main
;---------------------------------------
;NSF player stuff
loadnsf:
lda #$1f
sta temp2
:
ldx #$10
lda #0
sta temp1_lo
lda #$80
sta temp1_hi
lda temp2
sta $5ff8
ldy #0
tya
:
sta (temp1),y
iny
bne :-
inc temp1_hi
dex
bne :-
dec temp2
bpl :--
ldy #0
:
jsr read_byte
sta load,y
iny
cpy #19
bne :- ;read header
jsr work_bank
lda load
sta temp1_lo
lda load+1
and #$0f
ora #$80
sta temp1_hi ;adjust to get offset into bank
ldy #0
:
jsr read_byte
sta (temp1),y
inc temp1_lo
bne :+ ;load 256 bytes
inc temp1_hi
lda #$90
cmp temp1_hi
bne :+ ;load 4K banks
inc temp2
lda temp2
sta $5ff8
lda #$80
sta temp1_hi ;inc bank and reset pointers
:
dec len
lda #$ff
cmp len
bne :--
dec len+1
cmp len+1
bne :--
dec len+2
cmp len+2
bne :-- ;dec length counter
ldx #3
:
lda init,x
sta s_init,x
dex
bpl :- ;save vectors
replaynsf:
lda banks
sta $5ff8
lda #$60
sta temp1_hi
ldx #$20
lda #0
sta temp1_lo
tay
:
sta (temp1),y
iny
bne :-
inc temp1_hi
dex
bne :-
ldx #0
:
lda init_sound,x
sta $4000,x
inx
cpx #$14
bne :-
lda #$0f
sta $4015
.ifdef PARALLELPORT
jsr set_out
lda #7
jsr sho_msg
lda initsng+1
jsr sho_hex
lda #8
jsr sho_msg
lda initsng
jsr sho_hex
.endif
ldy initsng+1
ldx #0
txa
:
.byte $9d, $01, $00 ;don't overwrite $00
.byte $9d, $fc, $00 ; sta $00fc,x (absolute!!! ZP does not work)
sta $200,x
sta $300,x
sta $400,x
sta $500,x
sta $600,x
sta $700,x
dex
bne :-
lda #>(back_rd2-1)
pha
lda #<(back_rd2-1)
pha
tya
clc
sbc #0
tax
tay
jmp (s_init)
back_rd2:
lda #$85
sta port+$04 ;timer value
lda #$74
sta port+$05 ;timer value ;timer value
lda #$40
sta port+$0b ;timer interrupts continuous ;timer mode
sta port+$0e ;enable interrupts ;timer enable
: bit port+$0d
bvc :- ;wait for timer 1
lda #>(back_rd3-1)
pha
lda #<(back_rd3-1)
pha
jmp (s_play) ;JSR play routine
back_rd3:
lda port+$04 ;reset irq flag
jmp :-
init_sound:
.byte 0,0,0,0
.byte 0,0,0,0
.byte 0,0,0,0
.byte $10,0,0,0
.byte 0,0,0,0
runnsf:
ldy #0
:
jsr read_byte
sta banks,y
iny
cpy #10
bne :-
jsr work_bank
lda banks
sta $5ff8
jmp replaynsf
work_bank:
ldx #7
lda #0
sta $5ff7
:
ora banks, x ;check to see if all bank bytes are $00
pha
txa
sta $5ff8, x ;and set banks up to 0,1,2,3,4,5,6,7
pla
dex
bpl :-
cmp #0
beq :++
ldx #7
:
lda banks,x
sta $5ff8,x
dex
bpl :-
lda #0
sta $5ff8
sta temp2
rts ;if the banks were non-zero, load them up
:
lda load+1
lsr a
lsr a
lsr a
lsr a
and #$07
sta $5ff8
sta temp2 ;start loading at proper bank if non-banked
rts
;--------------------------------------------------------------------------
;Main bulk of the emulator code goes here
;--------------------------------------------------------------------------
;The first 256 bytes of the emulator code needs to be aligned to a page
;boundary.
.align 256
jmp main2
mode_indx:
jsr pcfetch7 ;clear carry here
adc reg_x
jsr indydo
jmp fixaddy
mode_indy:
jsr pcfetch
jsr indydo
jmp indycon
mode_zerx:
jsr pcfetch7 ;clear carry here
adc reg_x
jmp zpcon
mode_zery:
jsr pcfetch7 ;clear carry here
adc reg_y
jmp zpcon
mode_zero:
jsr pcfetch
zpcon:
sta ramloc+1
lda #$00
sta ramloc+2
jmp idone
mode_absx:
jsr pcfetch4 ;not a whole lot I can do.. only 2 stack levels
jsr pcfetch5
jsr pcfetch6
lda reg_x
jmp absocon
mode_absy:
jsr pcfetch4
jsr pcfetch5
jsr pcfetch6
indycon:
lda reg_y
absocon:
adc ramloc+1 ;carry cleared in pcfetch6
sta ramloc+1
bcc fixaddy
jsr pagecross
bcs fixaddy ;carry will always be set
mode_abso:
jsr pcfetch4
jsr pcfetch5
jsr pcfetch6
fixaddy:
jmp fixaddy2
mode_immd:
jsr pcfetch
ldx #$60
stx ramloc+2
.byte $2c ;skip with a BIT
mode_impl:
lda #$60
sta ramloc+1
tya
jmp execute
mode_nop3:
jsr pcfetch
mode_nop2:
jsr pcfetch
jmp edone
mode_op08:
lda reg_p
ora #$30
jmp m08cont
mode_op48:
lda reg_a
m08cont:
jsr safepush
jmp edone
mode_op78:
lda #$04
ora reg_p
bne m78cont
mode_op58:
lda #$fb
and reg_p
jmp m78cont
mode_op28:
jsr safepop
ora #$30 ;turn on B and unimplemented flags
m78cont:
sta reg_p
jmp edone
mode_op68:
jsr safepop
sta reg_a
lda #<(reg_a)
sta ramloc+1
lda #>(reg_a)
sta ramloc+2
lda #$ad
jmp execute
mode_op20:
jsr pcfetch
tay
lda reg_pch
jsr safepush
lda reg_pcl
jsr safepush
jmp m4ccont
mode_op4c:
jsr pcfetch
tay
m4ccont:
jsr pcfetch
sta reg_pch
sty reg_pcl
jmp edone
mode_op00:
jmp op_00
mode_op40:
jmp op_40
mode_op60:
jmp op_60
mode_op6c:
jmp op_6c
mode_op9a:
jmp op_9a
mode_opba:
jmp op_ba
mode_halt:
jmp ehalt
mode_bran:
tya
;--- end of critical 256 byte code block
rol a
rol a
rol a
and #$03
tax
tya
and #$20
bne rels
relc:
lda reltab,x
bit reg_p
beq dorel
bne norel
reltab:
.byte $80, $40, $01, $02
rels:
lda reltab,x
bit reg_p
bne dorel
norel:
jsr pcfetch
jmp edone
dorel:
jsr pagecross2
jsr pcfetch
bpl relpos
clc
adc reg_pcl
sta reg_pcl
bcs reldone
dec reg_pch
reldone:
jmp edone
relpos:
clc
adc reg_pcl
sta reg_pcl
bcc reldone
inc reg_pch
jsr pagecross2
jmp edone
indydo:
tax
lda 0,x
sta ramloc+1
lda 1,x
sta ramloc+2 ;pull address from zeropage
clc
rts
;-------------------------
;opcode routines, one per
op_00:
jsr pcfetch
lda reg_pch
jsr safepush
lda reg_pcl
jsr safepush
lda reg_p
ora #$24 ;set unimplemented and I flag
and #$ef ;clear break flag
jsr safepush
lda $fffe
sta reg_pcl
lda $ffff
sta reg_pch
jmp edone
op_40:
jsr safepop
ora #$30
sta reg_p
jsr safepop
sta reg_pcl
jsr safepop
sta reg_pch
jmp edone
op_60:
jsr safepop
sta reg_pcl
jsr safepop
sta reg_pch
jsr pcfetch2 ;do increment
jmp edone
op_6c:
jsr pcfetch4 ;do address mode
jsr pcfetch5
jsr pcfetch6
jsr ramloc
sta reg_pcl
inc ramloc+1
jsr ramloc
sta reg_pch
jmp edone
op_9a:
lda reg_x
sta reg_s
lda #topostack
cmp reg_s
bcs :+
lda #topostack
sta reg_s
:
jmp edone
op_ba:
lda reg_s
sta reg_x
lda #<(reg_x)
sta ramloc+1
lda #>(reg_x)
sta ramloc+2
lda #$ae
jmp execute ;ldx absolute
fixaddy2:
lda ramloc+2 ;filter out bad read/writes
bmi idone ;if in ROM, we're done automatically
cmp #$01
beq stackstuff
cmp #$09
beq stackstuff ;Somebody could otherwise make life difficult. :)
and #$f8
cmp #$48
beq portwrite ;port chip at 4800-4fff
cmp #$40
beq chksprite
and #$e0
cmp #$20
bne idone
jmp ppuaddy
portwrite:
lda #$42 ;force bogus addresses to 4200-42ff
sta ramloc+2
bne idone