-
Notifications
You must be signed in to change notification settings - Fork 0
/
cart.asm
222 lines (185 loc) · 4.76 KB
/
cart.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
processor 6502
include "vcs.h"
include "macro.h"
;;;; start constant declarations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
P0HEIGHT equ 9
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; end constant declarations
;;; $80 to $FF for variables, minus some at end if using stack
seg.u variables
org $80
;;;; start variable declarations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
P0x byte ; P0 x
P0y byte ; P0 y
P0spritePtr ds ; y-adjusted sprite pointer
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; end variables
;;; Begin code segment in ROM at $F000
seg code
org $F000
Start:
CLEAN_START
;;;; start variable initialization
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
lda #50
sta P0x
sta P0y
;;; Set high byte of P0spritePtr (low byte updated per frame)
lda #>P0bitmap
sta P0spritePtr+1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; end variable initialization
StartFrame:
lda #2
sta VSYNC
;;; 3 lines of VSYNC
sta WSYNC ; store halts until scanline complete
sta WSYNC ; 2nd
sta WSYNC ; 3rd
;;;; set timer for VBLANK
LDA #44
STA TIM64T
lda #0
sta VSYNC ; turn off VSYNC
;;;; start game vblank logic
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; P0 horizontal position
ldx #0
lda P0x
jsr PosObject
;;; P0 y pointer
lda #<P0bitmap+P0HEIGHT
sec
sbc P0y
sta P0spritePtr
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; end game vblank logic
;;;; Wait for rest of VBLANK
.VblankWaitLoop
lda INTIM ; load timer interrupt
bne .VblankWaitLoop
sta WSYNC ; wait for next wsync
sta VBLANK ; turn off VBlank
;;;; kernel (192 visible scan lines)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ldy #192 ; counter
.LoopVisible:
;;; for rainbow background
sty COLUBK ; set bg color to loop var
;;; draw P0
sec ; 2 set carry
tya ; 2
sbc P0y ; 3
adc P0HEIGHT ; 2
bcs .DrawP0
nop ; 2
nop ; 2
sec ; 2
bcs .NoDrawP0 ; 3
.DrawP0
lda (P0spritePtr),Y ; 5
sta GRP0 ; 3
.NoDrawP0
sta WSYNC ; wait for next scanline
dey ; y--
bne .LoopVisible ; go back until x = 0
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; end kernel
;;;; set timer for OVERSCAN
lda #2
sta WSYNC
sta VBLANK
lda #36
sta TIM64T
;;;; start game overscan logic
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; end game overscan logic
;;;; Wait for rest of OVERSCAN
.OverscanWaitLoop:
lda INTIM
bne .OverscanWaitLoop
lda #2
sta WSYNC
;;; new frame
jmp StartFrame
;;;; start subroutines
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; PosObject from https://www.biglist.com/lists/stella/archives/200403/msg00260.html
; Positions an object horizontally
; Inputs: A = Desired position.
; X = Desired object to be positioned (0-5). *jh* (P0, P1, M0, M1, Ball)
; scanlines: If control comes on or before cycle 73 then 1 scanline is consumed.
; If control comes after cycle 73 then 2 scanlines are consumed.
; Outputs: X = unchanged
; A = Fine Adjustment value.
; Y = the "remainder" of the division by 15 minus an additional 15.
; control is returned on cycle 6 of the next scanline.
PosObject SUBROUTINE
STA WSYNC ; 00 Sync to start of scanline.
SEC ; 02 Set the carry flag so no borrow will be applied during the division.
.divideby15
SBC #15 ; 04 ; Waste the necessary amount of time dividing X-pos by 15!
BCS .divideby15 ; 06/07 - 11/16/21/26/31/36/41/46/51/56/61/66
TAY ; 08 ; At this point the value in A is -1 to -15. In this code I use a table
; to quickly convert that value to the fine adjust value needed.
LDA fineAdjustTable,Y ; 13 -> Consume 5 cycles by guaranteeing we cross a page boundary
; In your own code you may wish to consume only 4.
STA HMP0,X ; 17 Store the fine adjustment value.
STA RESP0,X ; 21/ 26/31/36/41/46/51/56/61/66/71 - Set the rough position.
STA WSYNC
RTS
;;; end PosObject from https://www.biglist.com/lists/stella/archives/200403/msg00260.html
;;; (see link for alternate way without lookup table)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; end subroutines
;;;; start ROM lookup tables
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
P0bitmap:
byte #%00000000
byte #%00101001
byte #%01101001
byte #%10101001
byte #%00101001
byte #%00101111
byte #%00101001
byte #%00101001
byte #%11101001
P0color:
byte #$00
byte #$00
byte #$00
byte #$00
byte #$00
byte #$00
byte #$00
byte #$00
byte #$00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; end ROM lookup tables
;;; fine adjustment for PosObject
;;; some explanation on "negative index" is here:
;;; - https://www.randomterrain.com/atari-2600-memories-tutorial-andrew-davie-24.html
fineAdjustBegin
DC.B %01110000; Left 7
DC.B %01100000; Left 6
DC.B %01010000; Left 5
DC.B %01000000; Left 4
DC.B %00110000; Left 3
DC.B %00100000; Left 2
DC.B %00010000; Left 1
DC.B %00000000; No movement.
DC.B %11110000; Right 1
DC.B %11100000; Right 2
DC.B %11010000; Right 3
DC.B %11000000; Right 4
DC.B %10110000; Right 5
DC.B %10100000; Right 6
DC.B %10010000; Right 7
fineAdjustTable EQU fineAdjustBegin - %11110001 ; NOTE: %11110001 = -15
;;; Complete to 4kB
org $FFFC
.word Start
.word Start