Improve game slightly. Add foldRoutine*, use in checker.
Cat's Eye Technologies
8 years ago
182 | 182 |
* `jsr (vector)`
|
183 | 183 |
* `jmp routine`
|
184 | 184 |
* insist on EOL after each instruction. need spacesWOEOL production
|
|
185 |
* `copy immediate word`
|
17 | 17 |
; ---------
|
18 | 18 |
|
19 | 19 |
reserve vector save_cinv
|
|
20 |
|
20 | 21 |
assign word position $fb
|
|
22 |
assign word new_position $fd
|
|
23 |
|
21 | 24 |
reserve word delta
|
22 | 25 |
reserve byte value
|
23 | 26 |
reserve word compare_target
|
|
44 | 47 |
sta (position), y
|
45 | 48 |
jsr read_stick
|
46 | 49 |
jsr advance_pos
|
|
50 |
jsr check_new_position_in_bounds
|
|
51 |
if bcs {
|
|
52 |
jsr install_new_position
|
|
53 |
} else { }
|
47 | 54 |
|
48 | |
lda #$07 ; just past bottom of screen
|
49 | |
sta >compare_target
|
50 | |
lda #$e8
|
51 | |
sta <compare_target
|
52 | |
jsr compare_pos
|
53 | |
|
54 | |
if bcs {
|
55 | |
jsr reset_position
|
56 | |
} else {
|
57 | |
|
58 | |
lda #$04
|
59 | |
sta >compare_target
|
60 | |
lda #$00
|
61 | |
sta <compare_target
|
62 | |
jsr compare_pos
|
63 | |
|
64 | |
if bcc {
|
65 | |
jsr reset_position
|
66 | |
} else { }
|
67 | |
}
|
68 | 55 |
jmp (save_cinv)
|
69 | 56 |
}
|
70 | 57 |
|
|
79 | 66 |
clc
|
80 | 67 |
lda <position
|
81 | 68 |
adc <delta
|
82 | |
sta <position
|
|
69 |
sta <new_position
|
83 | 70 |
lda >position
|
84 | 71 |
adc >delta
|
|
72 |
sta >new_position
|
|
73 |
}
|
|
74 |
|
|
75 |
routine install_new_position {
|
|
76 |
lda <new_position
|
|
77 |
sta <position
|
|
78 |
lda >new_position
|
85 | 79 |
sta >position
|
86 | 80 |
}
|
87 | 81 |
|
88 | |
routine compare_pos {
|
89 | |
lda >position
|
|
82 |
routine check_new_position_in_bounds {
|
|
83 |
lda #$07 ; just past bottom of screen
|
|
84 |
sta >compare_target
|
|
85 |
lda #$e8
|
|
86 |
sta <compare_target
|
|
87 |
jsr compare_new_pos
|
|
88 |
|
|
89 |
if bcs {
|
|
90 |
clc
|
|
91 |
} else {
|
|
92 |
|
|
93 |
lda #$04
|
|
94 |
sta >compare_target
|
|
95 |
lda #$00
|
|
96 |
sta <compare_target
|
|
97 |
jsr compare_new_pos
|
|
98 |
|
|
99 |
if bcc {
|
|
100 |
clc
|
|
101 |
} else {
|
|
102 |
sec
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
routine compare_new_pos {
|
|
108 |
lda >new_position
|
90 | 109 |
cmp >compare_target
|
91 | 110 |
if beq {
|
92 | |
lda <position
|
|
111 |
lda <new_position
|
93 | 112 |
cmp <compare_target
|
94 | 113 |
} else {
|
95 | 114 |
}
|
37 | 37 |
|
38 | 38 |
noUseOfUndeclaredRoutines p@(Program decls routines) =
|
39 | 39 |
let
|
40 | |
mappedProgram = mapProgramRoutines (checkInstr) p
|
|
40 |
undeclaredRoutines = foldProgramRoutines (checkInstr) 0 p
|
41 | 41 |
in
|
42 | |
mappedProgram == p
|
|
42 |
undeclaredRoutines == 0
|
43 | 43 |
where
|
44 | 44 |
routineNames = declaredRoutineNames p
|
45 | 45 |
-- TODO also check COPYROUTINE here
|
46 | |
checkInstr j@(JSR routName) =
|
|
46 |
checkInstr j@(JSR routName) acc =
|
47 | 47 |
case routName `elem` routineNames of
|
48 | |
True -> j
|
49 | |
False -> (COPY A A)
|
50 | |
checkInstr other = other
|
|
48 |
True -> acc
|
|
49 |
False -> error ("undeclared routine '" ++ routName ++ "'") -- acc + 1
|
|
50 |
checkInstr other acc = acc
|
51 | 51 |
|
52 | 52 |
-- -- -- -- -- --
|
53 | 53 |
|
124 | 124 |
routineDeclared routName p =
|
125 | 125 |
elem routName (declaredRoutineNames p)
|
126 | 126 |
|
|
127 |
--
|
|
128 |
|
127 | 129 |
mapBlock :: (Instruction -> Instruction) -> [Instruction] -> [Instruction]
|
128 | 130 |
mapBlock = map
|
129 | 131 |
|
|
136 | 138 |
(mapRoutine f rout):(mapRoutines f routs)
|
137 | 139 |
|
138 | 140 |
mapProgramRoutines :: (Instruction -> Instruction) -> Program -> Program
|
139 | |
mapProgramRoutines f (Program decls routs) = Program decls $ mapRoutines f routs
|
|
141 |
mapProgramRoutines f (Program decls routs) =
|
|
142 |
Program decls $ mapRoutines f routs
|
|
143 |
|
|
144 |
--
|
|
145 |
|
|
146 |
foldBlock :: (Instruction -> a -> a) -> a -> [Instruction] -> a
|
|
147 |
foldBlock = foldr
|
|
148 |
|
|
149 |
foldRoutine :: (Instruction -> a -> a) -> a -> Routine -> a
|
|
150 |
foldRoutine f a (Routine name instrs) =
|
|
151 |
foldBlock f a instrs
|
|
152 |
|
|
153 |
foldRoutines :: (Instruction -> a -> a) -> a -> [Routine] -> a
|
|
154 |
foldRoutines f a [] = a
|
|
155 |
foldRoutines f a (rout:routs) =
|
|
156 |
let
|
|
157 |
z = foldRoutine f a rout
|
|
158 |
in
|
|
159 |
foldRoutines f z routs
|
|
160 |
|
|
161 |
foldProgramRoutines :: (Instruction -> a -> a) -> a -> Program -> a
|
|
162 |
foldProgramRoutines f a (Program decls routs) =
|
|
163 |
foldRoutines f a routs
|
|
164 |
|
|
165 |
--
|
140 | 166 |
|
141 | 167 |
lookupDecl (Program decls _) name =
|
142 | 168 |
lookupDecl' (filter (isLocationDecl) decls) name
|