git @ Cat's Eye Technologies SixtyPical / 5af7c45
Machine model is a bit fuller now. Cat's Eye Technologies 11 years ago
6 changed file(s) with 69 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
9191 take a block. The natural symmetrical opcode is inserted at the end of the
9292 block.
9393
94 ### Loops ###
95
96 Still need to figure this out.
97
98 Typical `repeat` loop looks like:
99
100 ldy #0
101 _loop:
102 lda #65
103 sta screen, y
104 iny
105 cpy #250
106 bne _loop
107
108 This might be
109
110 routine blah {
111 ldy# 0
112 repeat bne {
113 lda# 65
114 sta,y screen
115 iny
116 cpy# 250
117 }
118 }
119
120 Note, `screen` must be a `byte table` here.
121
94122 TODO
95123 ----
96124
97125 * Parse HEX values like $40A3
98 * Full machine model
126 * Fuller machine model
99127 * Addressing modes; rename instructions to match
100128
101129 Tests
88 -- -- -- -- data-flow-analysis context -- -- -- --
99
1010 data Usage = Unknown
11 | Value LocationName -- obviously a bit daft for now
12 | Retained Register
11 | Value StorageLocation -- obviously a bit daft for now
12 | Retained StorageLocation
1313 deriving (Show, Ord, Eq)
1414
15 type RoutineContext = Map.Map Register Usage
15 type RoutineContext = Map.Map StorageLocation Usage
1616
1717 type ProgramContext = Map.Map RoutineName RoutineContext
1818
2020 elem locName (map (getDeclLocationName) decls)
2121 where
2222
23 -- in the following, we mean Named locations
24
2325 routineUsedLocations (Routine _ instrs) = blockUsedLocations instrs
2426
2527 blockUsedLocations [] = []
2628 blockUsedLocations (instr:instrs) =
2729 (instrUsedLocations instr) ++ blockUsedLocations instrs
2830
29 instrUsedLocations (LOAD reg loc) = [loc]
30 instrUsedLocations (CMP reg loc) = [loc]
31 instrUsedLocations (LOAD reg (NamedLocation loc)) = [loc]
32 instrUsedLocations (CMP reg (NamedLocation loc)) = [loc]
3133 -- TODO: JSR...
3234 instrUsedLocations (IFEQ b1 b2) =
3335 blockUsedLocations b1 ++ blockUsedLocations b2
3434 emitInstrs p r (instr:instrs) =
3535 " " ++ emitInstr p r instr ++ "\n" ++ emitInstrs p r instrs
3636
37 emitInstr p r (LOAD A label) = "lda " ++ label
38 emitInstr p r (LOAD X label) = "ldx " ++ label
39 emitInstr p r (LOAD Y label) = "ldy " ++ label
40 emitInstr p r (CMP A label) = "cmp " ++ label
37 emitInstr p r (LOAD A (NamedLocation label)) = "lda " ++ label
38 emitInstr p r (LOAD X (NamedLocation label)) = "ldx " ++ label
39 emitInstr p r (LOAD Y (NamedLocation label)) = "ldy " ++ label
40 emitInstr p r (CMP A (NamedLocation label)) = "cmp " ++ label
4141
4242 emitInstr p r (COPY A X) = "tax"
4343 emitInstr p r (COPY A Y) = "tay"
77
88 type LocationName = String
99
10 data Register = A | X | Y -- | MemLoc LocationName
10 -- We do not include the PC as it of course changes constantly.
11 -- We do not include the stack pointer, as it should not change over
12 -- the lifetime of a single routine. (Always pop what you pushed.)
13 -- Ditto the I flag. (always enable interrupts after disabling them.)
14 -- We do not include the B flag, because for us, BRK is game over, man.
15
16 -- One of these should never refer to the program code. We can only police
17 -- this up to a point.
18
19 data StorageLocation = A
20 | Y
21 | X
22 | FlagN
23 | FlagV
24 | FlagD
25 | FlagZ
26 | FlagC
27 | NamedLocation LocationName
1128 deriving (Show, Ord, Eq)
1229
13 allRegisters = [A, X, Y]
30 -- this is bunk, man. if a location does not appear in an analysis
31 -- map the meaning should be taken to be "preserved".
32
33 allRegisters = [A, X, Y, FlagN, FlagV, FlagD, FlagZ, FlagC]
1434
1535 -- -- -- -- program model -- -- -- --
1636
2444
2545 type RoutineName = String
2646
27 data Instruction = LOAD Register LocationName
28 | COPY Register Register
29 | CMP Register LocationName
47 data Instruction = LOAD StorageLocation StorageLocation
48 | COPY StorageLocation StorageLocation
49 | CMP StorageLocation StorageLocation
3050 | JSR RoutineName
3151 | IFEQ [Instruction] [Instruction]
3252 | NOP
8787 string "cmp"
8888 spaces
8989 l <- locationName
90 return (CMP A l)
90 return (CMP A (NamedLocation l))
9191
9292 lda :: Parser Instruction
9393 lda = do
9494 string "lda"
9595 spaces
9696 l <- locationName
97 return (LOAD A l)
97 return (LOAD A (NamedLocation l))
9898
9999 ldx :: Parser Instruction
100100 ldx = do
101101 string "ldx"
102102 spaces
103103 l <- locationName
104 return (LOAD X l)
104 return (LOAD X (NamedLocation l))
105105
106106 ldy :: Parser Instruction
107107 ldy = do
108108 string "ldy"
109109 spaces
110110 l <- locationName
111 return (LOAD Y l)
111 return (LOAD Y (NamedLocation l))
112112
113113 txa :: Parser Instruction
114114 txa = do