Commit MetaInfo

Revisionae6e9946c36e69631a091e198a34951c7f93c484 (tree)
Time2021-09-16 07:52:29
Authoringlorion <homemicro@ingl...>
Commiteringlorion

Log Message

rom: implemented savecart

Change Summary

Incremental Difference

diff -r 82a0ecb626f6 -r ae6e9946c36e rom/rom.s
--- a/rom/rom.s Fri Sep 03 11:11:23 2021 -0700
+++ b/rom/rom.s Wed Sep 15 23:52:29 2021 +0100
@@ -12,6 +12,7 @@
1212 SAVEY = $ff
1313 IRQHNDLR = $0200
1414 NMIHNDLR = $0204
15+ CARTBSZ = $0206
1516 RAMTOP = $021e
1617 KBDSTATE = $0220
1718 BUF = $0240
@@ -694,7 +695,153 @@
694695 rts
695696
696697 savecart:
697- ;; TODO: Implement.
698+;;; Saves data to a cartridge.
699+;;; In:
700+;;; $a0..$a3 Location (on cartridge) to save to.
701+;;; $a4..$a5 Number of bytes to save.
702+;;; $a6..$a7 Address of first byte to save.
703+;;; Out:
704+;;; a 0 if successful. Any other value indicates an error.
705+;;; Clobbers x.
706+ ;; We save data in blocks of CARTBSZ bytes.
707+ ;; Compute the size of the first. The computation
708+ ;; requires that CARTBSZ be a power of 2 and computes
709+ ;; the maximum number of bytes that can be written from
710+ ;; start without crossing into the next block:
711+ ;;
712+ ;; start and (CARTBSZ - 1)
713+ ;;
714+ ;; Then, it sets the size to be written to the minimum of
715+ ;; that value and the size that was actually passed in.
716+ lda CARTBSZ
717+ sec
718+ sbc #1
719+ and $a0
720+ sta $a8
721+ lda CARTBSZ + 1
722+ sbc #0
723+ and $a1
724+ sta $a9
725+ lda CARTBSZ
726+ sec
727+ sbc $a8
728+ sta $a8
729+ lda CARTBSZ + 1
730+ sbc $a9
731+ sta $a9
732+savecart0:
733+ lda $a4
734+ cmp $a8
735+ lda $a5
736+ sbc $a9
737+ ;; At this point, carry clear means that the number of bytes
738+ ;; we were asked to write ($a4..$a5) is less than the number
739+ ;; of bytes that fit in the block ($a8..$a9). We set $a8..$a9
740+ ;; to the lesser value.
741+ bcs savecart1
742+ lda $a4
743+ sta $a8
744+ lda $a5
745+ sta $a9
746+savecart1:
747+ lda $a8
748+ sta $aa
749+ lda $a9
750+ sta $ab
751+ jsr saveblk
752+ bne savecart2
753+ ;; Compute new start address.
754+ lda $a0
755+ clc
756+ adc $aa
757+ sta $a0
758+ lda $a1
759+ adc $ab
760+ sta $a1
761+ lda $a2
762+ adc #0
763+ sta $a2
764+ lda $a3
765+ adc #0
766+ sta $a3
767+ ;; Compute remaining bytes to write.
768+ lda $a4
769+ sec
770+ sbc $aa
771+ sta $a4
772+ lda $a5
773+ sbc $ab
774+ sta $a5
775+ ;; If no bytes left to write, we're done.
776+ ora $a4
777+ beq savecart2
778+ ;; Now that we are aligned to a block boundary,
779+ ;; write up to CARTBSZ bytes at a time.
780+ lda CARTBSZ
781+ sta $a8
782+ lda CARTBSZ + 1
783+ sta $a9
784+ jmp savecart0
785+savecart2:
786+ rts
787+
788+;;; Saves a single block of data to the cartridge.
789+;;; In:
790+;;; $a0..$a3 Location (on cartridge) to save to.
791+;;; $a6..$a7 Address of first byte to save.
792+;;; $a8..$a9 Number of bytes to save.
793+;;; Out:
794+;;; a 0 if successful. Any other value indicates an error.
795+saveblk:
796+ jsr cartnext
797+ cmp #0
798+ bne saveblk2
799+saveblk0:
800+ ldy #0
801+ lda ($a6), y
802+ jsr twisendb
803+ cmp #0
804+ bne saveblk2
805+ inc $a6
806+ bne saveblk1
807+ inc $a7
808+saveblk1:
809+ dec $a8
810+ bne saveblk0
811+ lda $a9
812+ beq saveblk2
813+ dec $a9
814+ jmp saveblk0
815+saveblk2:
816+ tax
817+ jsr twistop
818+ txa
819+ rts
820+
821+cartnext:
822+;;; Waits until the cartridge is ready and sets the
823+;;; next address. If more than 10ms elapse and the cartridge
824+;;; does not acknowledge the new address, the routine
825+;;; returns with the a register set to a nonzero value.
826+;;;
827+;;; Postconditions:
828+;;; - If a is 0: The cartridge is ready to receive bytes to be
829+;;; written.
830+;;; - Otherwise: The cartridge did not acknowledge the new
831+;;; address.
832+;;;
833+;;; In:
834+;;; $a0..$a3 Cartridge location to set.
835+ ldx #$96
836+cartnext0:
837+ jsr twistop
838+ jsr twistart
839+ jsr cart_set_location
840+ cmp #0
841+ beq cartnextd
842+ dex
843+ bne cartnext0
844+cartnextd:
698845 rts
699846
700847 sub16:
Show on old repository browser