Skip to content

Commit c53321a

Browse files
committed
Ping pong is ready
1 parent 99b95c1 commit c53321a

File tree

6 files changed

+178
-37
lines changed

6 files changed

+178
-37
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ OUTPUT_DIR=output
77
all: all_programs_binary all_programs_resolved
88

99
clean:
10-
rm -r $(BUILD_DIR)
11-
rm -r $(OUTPUT_DIR)
10+
rm -rf $(BUILD_DIR)
11+
rm -rf $(OUTPUT_DIR)
1212

1313
include emulator/Makefile.mk
1414

@@ -30,4 +30,4 @@ $(OUTPUT_DIR)/programs/%_resolved.asm: programs/%.asm
3030
python3 -m planner asm -r $^ > $@
3131

3232
run_ping_pong:
33-
python3 -m planner -v compile_and_execute ping_pong
33+
python3 -m planner compile_and_execute ping_pong

README.md

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,18 @@
55

66
The eventual goal(?) is to build a general-purpose processor integrated with simple input (e.g. buttons) and output devices (8x8 LED display).
77

8-
## Verification
8+
## Sample Programs
99

10-
### Emulation
11-
12-
```
13-
# Build ROM[boot]
14-
$ python3 -rb assembler/assembler.py programs/boot_sequence.asm | tee build/boot_rom.txt.
15-
16-
# Write your program in custom assembly.
17-
$ cat programs/ping_pong.asm # we can proceeding with this
18-
19-
# Use assembler to translate the instructions into machine code.
20-
$ mkdir -p build
21-
$ python3 assembler/assembler.py -r programs/ping_pong.asm | tee build/ping_pong_resolved.txt # optional
22-
$ python3 assembler/assembler.py -rb programs/ping_pong.asm | tee build/ping_pong_rom.txt
23-
24-
# Use emulater the run the machine code.
25-
$ python3 emulator/emulate.py build/ping_pong_rom.txt
26-
```
10+
* Ping Pong
11+
* Source: [ping_pong.asm](programs/ping_pong.asm)
12+
* Generate resolved assembly: `python3 -m planner asm -r programs/ping_pong.asm`
13+
* Generate binary: `python3 -m planner asm -b programs/ping_pong.asm`
14+
* Run on emulator: `python3 -m planner compile_and_execute ping_pong`
2715

2816
## Design
2917

18+
This section is not up-to date.
19+
3020
### Specs
3121

3222
* Address Line: 16-bits

planner/asm/line_parser.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ def parse_line(line: str) -> Tuple[Optional[str], Optional[List[Tuple[unit.Opera
4747
if op.upper() == memory.TOKEN_ESP:
4848
optype = address_of(optype)
4949
op = str(memory.ESP)
50+
if op.upper() == memory.TOKEN_ESB:
51+
optype = address_of(optype)
52+
op = str(memory.ESB)
5053
elif op in memory.TOKEN_GENERAL_REGISTERS:
5154
optype = address_of(optype)
5255
try:

planner/asm/program_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def parse_data(self, tokens: List[str]):
157157
raise ValueError("unsupported data size provided")
158158

159159
# unsigned integer only for now
160-
val = int(tokens[2])
160+
val = int(tokens[2], 0)
161161
assert val >= 0 and val < (2**(8*sz))
162162

163163
for _ in range(times):

planner/memory.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ def get_register_address(index):
99

1010
# Stack Registers
1111
TOKEN_ESP = "ESP"
12+
TOKEN_ESB = "ESB"
1213
ESP = 0x20
13-
MSI = 0x24 # multi-step instruction state
14+
ESB = 0x24
15+
MSI = 0x28 # multi-step instruction state
1416

1517
# free
1618

programs/ping_pong.asm

Lines changed: 160 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ OUTPUT_WIDTH equ 6
3333
OUTPUT_HEGHT equ 7
3434
INPUT_DEVICE equ 1
3535

36+
BALL_STEP_SIZE equ 2
37+
GAME_OVER_BLINK_STEP equ 5
38+
39+
BALL_DIR_RIGHT equ 1
40+
BALL_DIR_LEFT equ 2
41+
# BALL_DIR_RIGHT|BALL_DIR_LEFT
42+
BALL_DIR_FLIP_HOR equ 3
43+
# if not up or down, ball goes horizontally
44+
BALL_DIR_UP equ 4
45+
BALL_DIR_DOWN equ 8
46+
# BALL_DIR_UP|BALL_DIR_DOWN
47+
BALL_DIR_FLIP_VER equ 12
48+
49+
BALL_MAXX equ 14
50+
BALL_MINX equ 1
51+
BALL_MAXY equ 7
52+
BALL_MINY equ 0
53+
54+
3655
section .text
3756
main:
3857
movc ESP, 0xFF
@@ -45,69 +64,110 @@ section .data
4564
bat2_y dd 2
4665
ball_x dd 7
4766
ball_y dd 2
67+
mask16f dd 0xFFFF
68+
ball_step_counter dd 0
69+
ball_dir dd 6
70+
71+
game_over_blink_counter dd 0
72+
game_over_blink dd 1
4873

4974
section .text
5075
game:
5176
call print
5277
call read_input_p1_up
53-
call sleep
78+
call step
5479
jmp game
5580

81+
game_over:
82+
call game_over_step
83+
call print
84+
jmp game_over
85+
86+
game_over_step:
87+
cmpc [game_over_blink_counter], 0
88+
jnz _game_over_step_end
89+
xorc [game_over_blink], 1
90+
movc [game_over_blink_counter], GAME_OVER_BLINK_STEP
91+
_game_over_step_end:
92+
subc [game_over_blink_counter], 1
93+
ret
5694

57-
# call step
58-
# jmp step
59-
game_after_step:
60-
# jmp game
61-
# hlt_marker:
62-
# jmp hlt_marker
6395

6496
read_input_p1_up:
6597
IN R0, INPUT_DEVICE
6698
movc R1, 0x1
6799
and R1, R0
68100
jz read_input_p1_down
101+
cmpc [bat1_y], 0
102+
jz read_input_p1_down
69103
subc [bat1_y], 1
70104
read_input_p1_down:
71105
movc R1, 0x02
72106
and R1, R0
73107
jz read_input_p2_up
108+
cmpc [bat1_y], BAT_MAXY
109+
jz read_input_p2_up
74110
addc [bat1_y], 1
75111
read_input_p2_up:
76112
movc R1, 0x04
77113
and R1, R0
78114
jz read_input_p2_down
115+
cmpc [bat2_y], 0
116+
jz read_input_p2_down
79117
subc [bat2_y], 1
80118
read_input_p2_down:
81119
movc R1, 0x08
82120
and R1, R0
83121
jz read_input_end
122+
cmpc [bat2_y], BAT_MAXY
123+
jz read_input_end
84124
addc [bat2_y], 1
85125
read_input_end:
86126
ret
87127

88128
print:
129+
# if game over draw blink bat animation
130+
cmpc [game_over_blink], 0
131+
jz _draw_ball
132+
89133
## Player 1
90134
# anode col
91135
movc R0, 0x7F
92136
shlc R0, 8
93137
xorc R0, 0xFF
94138
# cathode row
95-
movc R1, 0x3
139+
movc R1, 0x7
96140
shl R1, [bat1_y]
97141
OUT OUTPUT_WIDTH, R0
98142
OUT OUTPUT_HEGHT, R1
99-
100143
call sleep
144+
101145
## Player 2
102146
# anode col
103147
movc R0, 0xFF
104148
shlc R0, 8
105149
xorc R0, 0xFE
106150
# cathode row
107-
movc R1, 0x3
151+
movc R1, 0x7
108152
shl R1, [bat2_y]
109153
OUT OUTPUT_WIDTH, R0
110154
OUT OUTPUT_HEGHT, R1
155+
call sleep
156+
157+
_draw_ball:
158+
## Ball
159+
mov R0, [ball_x]
160+
movc R1, 0x80
161+
shlc R1, 8
162+
shr R1, R0
163+
xor R1, [mask16f]
164+
mov R0, [ball_y]
165+
movc R2, 1
166+
shl R2, R0
167+
OUT OUTPUT_WIDTH, R1
168+
OUT OUTPUT_HEGHT, R2
169+
call sleep
170+
111171
ret
112172

113173
sleep:
@@ -118,9 +178,95 @@ section .text
118178
jnz _sleep
119179
ret
120180

121-
#step:
122-
# addc [ball_x], 1
123-
# andc [ball_x], 0x07
124-
# jmp game_after_step
181+
step:
182+
cmpc [ball_step_counter], 0
183+
jnz step_over
184+
movc [ball_step_counter], BALL_STEP_SIZE
185+
call step_ball_move
186+
step_over:
187+
subc [ball_step_counter], 1
188+
ret
189+
190+
step_ball_move:
191+
cmpc [ball_x], BALL_MAXX
192+
jz _step_ball_collision_hor_right
193+
cmpc [ball_x], BALL_MINX
194+
jz _step_ball_collision_hor_left
195+
_step_ball_collision_hor_over:
196+
cmpc [ball_y], BALL_MAXY
197+
jz _step_ball_collision_ver
198+
cmpc [ball_y], BALL_MINY
199+
jz _step_ball_collision_ver
200+
_step_ball_collision_ver_over:
201+
202+
mov R0, [ball_dir]
203+
andc R0, BALL_DIR_RIGHT
204+
jz _step_ball_left
205+
# move_right
206+
addc [ball_x], 1
207+
jmp _step_ball_up
208+
_step_ball_left:
209+
# move left
210+
subc [ball_x], 1
211+
_step_ball_up:
212+
mov R0, [ball_dir]
213+
andc R0, BALL_DIR_UP
214+
jz _step_ball_down
215+
# move up
216+
subc [ball_y], 1
217+
_step_ball_down:
218+
mov R0, [ball_dir]
219+
andc R0, BALL_DIR_DOWN
220+
jz _step_ball_move_over
221+
# move down
222+
addc [ball_y], 1
223+
_step_ball_move_over:
224+
ret
125225

226+
_step_ball_collision_hor_right:
227+
push [bat2_y]
228+
call _step_ball_collision_hor
229+
pop R7
230+
jmp _step_ball_collision_hor_over
231+
232+
_step_ball_collision_hor_left:
233+
push [bat1_y]
234+
call _step_ball_collision_hor
235+
pop R7
236+
jmp _step_ball_collision_hor_over
237+
238+
_step_ball_collision_hor:
239+
# Argument
240+
# bat_y
241+
mov esb, esp
242+
addc esb, 4
243+
load R1, [esb] # bat_y
244+
245+
xorc [ball_dir], BALL_DIR_FLIP_HOR
246+
movc R0, BALL_DIR_FLIP_VER
247+
xorc R0, 0xFF
248+
and [ball_dir], R0
249+
cmp [ball_y], R1
250+
jz _step_ball_collision_hor_right_go_up
251+
addc R1, 1
252+
cmp [ball_y], R1
253+
jz _step_ball_collision_hor_right_go_hor
254+
addc R1, 1
255+
cmp [ball_y], R1
256+
jz _step_ball_collision_hor_right_go_down
257+
jmp game_over
258+
_step_ball_collision_hor_right_go_up:
259+
orc [ball_dir], BALL_DIR_UP
260+
ret
261+
_step_ball_collision_hor_right_go_down:
262+
orc [ball_dir], BALL_DIR_DOWN
263+
ret
264+
_step_ball_collision_hor_right_go_hor:
265+
ret
126266

267+
_step_ball_collision_ver:
268+
mov R0, [ball_dir]
269+
andc R0, BALL_DIR_FLIP_VER
270+
jz _step_ball_collision_ver_over
271+
xorc [ball_dir], BALL_DIR_FLIP_VER
272+
jmp _step_ball_collision_ver_over

0 commit comments

Comments
 (0)