-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlimePhysics.asm
320 lines (271 loc) · 8.97 KB
/
SlimePhysics.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
#include p18f87k22.inc
#include constants.inc
global slime_0_x, slime_0_y, slime_0_vx, slime_0_vy
global slime_1_x, slime_1_y, slime_1_vx, slime_1_vy
global Slime_Step
extern Mul_16_16
extern Compare_2B, compare_2B_1, compare_2B_2
acs0 udata_acs
; Positions are stored in 2s complement to aid calculations
; Known to be positive (and capped at ~4000) - can be used directly in DAC.
slime_0_x res 2
slime_0_y res 2
slime_1_x res 2
slime_1_y res 2
; Velocities are -32768 to 32767 in 2's complement
slime_0_vx res 2
slime_0_vy res 2
slime_1_vx res 2
slime_1_vy res 2
constant slime_0_left_limit = wall_x_lower + slime_radius
constant slime_0_right_limit = net_x - slime_radius
constant slime_1_left_limit = net_x + slime_radius
constant slime_1_right_limit = wall_x_higher - slime_radius
SlimePhysics code
;;;;; SLIME SETUP ;;;;;
; Sets PORTC to use as controls ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Slime_Setup
setf TRISC ; Set PORTC as all inputs
return
;;;;; SLIME STEP ;;;;;
; Does 1 physics tick, ;
; gets and uses controls ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Slime_Step
call Update_With_Controls
call Slime_0_Propagate
call Slime_1_Propagate
call Clip_Slime_Positions
return
;;;;; UPDATE WITH CONTROLS ;;;;;
; Updates slime velocities ;
; by looking at controls ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Update_With_Controls
; Slime 0:
; PORTC 0 1 2 = UP LEFT RIGHT
check_0_up
btfss PORTC, 0
bra check_0_left
tstfsz slime_0_y
bra check_0_left
tstfsz slime_0_y + 1
bra check_0_left
movlw slime_jump_vy
movwf slime_0_vy
movlw 0
movwf slime_0_vy + 1
check_0_left
btfss PORTC, 1
bra check_0_right_no_left
btfss PORTC, 2 ; Know that left is pressed, check if right is also pressed
bra Set_slime_0_vx_To_Negative ; Right not pressed, move left.
bra Set_slime_0_vx_To_0 ; Right also pressed
check_0_right_no_left ; Know that left is not pressed.
btfss PORTC, 2
bra Set_slime_0_vx_To_0 ; Right not pressed, do not move
bra Set_slime_0_vx_To_Positive ; Right pressed, move right.
Set_slime_0_vx_To_0
movlw 0
movwf slime_0_vx
movwf slime_0_vx + 1
bra Set_slime_0_vx_end
Set_slime_0_vx_To_Positive
movlw slime_move_vx
movwf slime_0_vx
movlw 0
movwf slime_0_vx + 1
bra Set_slime_0_vx_end
Set_slime_0_vx_To_Negative
movlw slime_move_vx
comf WREG, W
addlw 1
movwf slime_0_vx
movlw 0xff
movwf slime_0_vx + 1
Set_slime_0_vx_end
; Slime 1:
; PORTC 3 4 5 = UP LEFT RIGHT
check_1_up
btfss PORTC, 3
bra check_1_left
tstfsz slime_1_y
bra check_1_left
tstfsz slime_1_y + 1
bra check_1_left
movlw slime_jump_vy
movwf slime_1_vy
movlw 0
movwf slime_1_vy + 1
check_1_left
btfss PORTC, 4
bra check_1_right_no_left
btfss PORTC, 5 ; Know that left is pressed, check if right is also pressed
bra Set_slime_1_vx_To_Negative ; Right not pressed, move left.
bra Set_slime_1_vx_To_0 ; Right also pressed
check_1_right_no_left ; Know that left is not pressed.
btfss PORTC, 5
bra Set_slime_1_vx_To_0 ; Right not pressed, do not move
bra Set_slime_1_vx_To_Positive ; Right pressed, move right.
Set_slime_1_vx_To_0
movlw 0
movwf slime_1_vx
movwf slime_1_vx + 1
bra Set_slime_1_vx_end
Set_slime_1_vx_To_Positive
movlw slime_move_vx
movwf slime_1_vx
movlw 0
movwf slime_1_vx + 1
bra Set_slime_1_vx_end
Set_slime_1_vx_To_Negative
movlw slime_move_vx
comf WREG, W
addlw 1
movwf slime_1_vx
movlw 0xff
movwf slime_1_vx + 1
Set_slime_1_vx_end
return
;;;;;;;;; SLIME 0 PROPAGATE ;;;;;;;;;
; Propagate current position by 1 frame ;
; Applies gravity ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Slime_0_Propagate
; x = x + v, t = 1
movf slime_0_vx, W
addwf slime_0_x, f
movf slime_0_vx + 1, W
addwfc slime_0_x + 1, f
movf slime_0_vy, W
addwf slime_0_y, f
movf slime_0_vy + 1, W
addwfc slime_0_y + 1, f
; Gravity
; Only apply when in the air - y > 0
btfsc slime_0_y + 1, 7 ; Skip if positive
bra post_gravity_0 ; If slime y < 0
movlw slime_gravity
subwf slime_0_vy, f
movlw 0
subwfb slime_0_vy + 1, f
post_gravity_0
; If y < 0, set y = 0, vy = 0
btfsc slime_0_y + 1, 7 ; Skip if positive
call Set_Slime_0_To_0
return
;;;;;;;;; SLIME 1 PROPAGATE ;;;;;;;;;
; Propagate current position by 1 frame ;
; Applies gravity ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Slime_1_Propagate
; x = x + v, t = 1
movf slime_1_vx, W
addwf slime_1_x, f
movf slime_1_vx + 1, W
addwfc slime_1_x + 1, f
movf slime_1_vy, W
addwf slime_1_y, f
movf slime_1_vy + 1, W
addwfc slime_1_y + 1, f
; Gravity
; Only apply when in the air - y > 0
btfsc slime_1_y + 1, 7 ; Skip if positive
bra post_gravity_1 ; If slime y < 0
movlw slime_gravity
subwf slime_1_vy, f
movlw 0
subwfb slime_1_vy + 1, f
post_gravity_1
; If y < 0, set y = 0, vy = 0
btfsc slime_1_y + 1, 7 ; Skip if positive
call Set_Slime_1_To_0
return
; Set Slime 0's y and vy to 0
Set_Slime_0_To_0
movlw 0
movwf slime_0_y
movwf slime_0_y + 1
movwf slime_0_vy
movwf slime_0_vy + 1
return
; Set Slime 1's y and vy to 0
Set_Slime_1_To_0
movlw 0
movwf slime_1_y
movwf slime_1_y + 1
movwf slime_1_vy
movwf slime_1_vy + 1
return
;;;;; CLIP SLIME POSITIONS ;;;;;
; Clips slime positions to provided ;
; constants ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Clip_Slime_Positions
; Ensure slime_0_left_limit <= slime_0_x <= slime_0_right_limit
movff slime_0_x, compare_2B_1
movff slime_0_x + 1, compare_2B_1 + 1
movlw low(slime_0_left_limit)
movwf compare_2B_2
movlw high(slime_0_left_limit)
movwf compare_2B_2 + 1
call Compare_2B ; (slime_0_x > slime_0_left_limit) in W
tstfsz WREG ; Skip if slime_0_x < slime_0_left_limit
bra slime_0_right_limit_check
; slime_0_x < slime_0_left_limits, set to slime_0_left_limit
movlw low(slime_0_left_limit)
movwf slime_0_x
movlw high(slime_0_left_limit)
movwf slime_0_x + 1
bra slime_0_check_end
slime_0_right_limit_check
movlw low(slime_0_right_limit)
movwf compare_2B_1
movlw high(slime_0_right_limit)
movwf compare_2B_1 + 1
movff slime_0_x, compare_2B_2
movff slime_0_x + 1, compare_2B_2 + 1
call Compare_2B ; (slime_0_x < slime_0_right_limit) in W
tstfsz WREG ; Skip if slime_0_x > slime_0_right_limit
bra slime_0_check_end
; slime_0_x > slime_0_right_limit_check, set to slime_0_right_limit_check
movlw low(slime_0_right_limit)
movwf slime_0_x
movlw high(slime_0_right_limit)
movwf slime_0_x + 1
slime_0_check_end
; Ensure slime_1_left_limit <= slime_1_x <= slime_1_right_limit
movff slime_1_x, compare_2B_1
movff slime_1_x + 1, compare_2B_1 + 1
movlw low(slime_1_left_limit)
movwf compare_2B_2
movlw high(slime_1_left_limit)
movwf compare_2B_2 + 1
call Compare_2B ; (slime_1_x > slime_1_left_limit) in W
tstfsz WREG ; Skip if slime_1_x < slime_1_left_limit
bra slime_1_right_limit_check
; slime_1_x < slime_1_left_limits, set to slime_1_left_limit
movlw low(slime_1_left_limit)
movwf slime_1_x
movlw high(slime_1_left_limit)
movwf slime_1_x + 1
bra slime_1_check_end
slime_1_right_limit_check
movlw low(slime_1_right_limit)
movwf compare_2B_1
movlw high(slime_1_right_limit)
movwf compare_2B_1 + 1
movff slime_1_x, compare_2B_2
movff slime_1_x + 1, compare_2B_2 + 1
call Compare_2B ; (slime_1_x < slime_1_right_limit) in W
tstfsz WREG ; Skip if slime_1_x > slime_1_right_limit
bra slime_1_check_end
; slime_1_x > slime_1_right_limit_check, set to slime_1_right_limit_check
movlw low(slime_1_right_limit)
movwf slime_1_x
movlw high(slime_1_right_limit)
movwf slime_1_x + 1
slime_1_check_end
return
end