-
Notifications
You must be signed in to change notification settings - Fork 3
/
z80.peg
232 lines (183 loc) · 9.25 KB
/
z80.peg
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
package zog
type PegAssembler Peg {
Current
}
Program <- Line+ !.
Line <- ws* LabelDefn? ws* Statement? ws? Comment? ws? ("\r"?"\n" / ":") { p.Emit() }
Statement <- Directive / Instruction
Directive <- (Title / Aseg / Org / Defb / Defs / Defw)
Title <- '.'? 'title' ws "'" [^']* "'"
Aseg <- 'aseg'
Org <- "org" ws nn { p.Org() }
Defb <- ("defb" / "db") ws n { p.DefByte() }
Defw <- ("defw" / "dw") ws nn { p.DefWord() }
Defs <- ("defs" / "ds") ws n { p.DefSpace() }
LabelDefn <- LabelText ":" ws? { p.LabelDefn(buffer[begin:end])}
LabelText <- <alphaund alphaundnum alphaundnum+>
alphaundnum <- alphaund / num
alphaund <- [[a-z]] / "_"
num <- [0-9]
Comment <- (";" / "#") [^\n]*
Instruction <- ( Assignment / Inc / Dec / Alu16 / Alu / BitOp / EDSimple / Simple / Jump / IO )
Assignment <- Load / Push / Pop / Ex
Load <- Load16 / Load8
Load8 <- "LD" ws Dst8 sep Src8 { p.LD8() }
Load16 <- "LD" ws Dst16 sep Src16 { p.LD16() }
Push <- "PUSH" ws Src16 { p.Push() }
Pop <- "POP" ws Dst16 { p.Pop() }
Ex <- "EX" ws Dst16 sep Src16 { p.Ex() }
# Annoying parse ordering case, we can't have 8->16 or 16->8
# "INC IXY" and "INC IX"
# "INC C" and "INC BC"
# Ditto DEC
Inc <- Inc16Indexed8 / Inc16 / Inc8
Inc16Indexed8 <- "INC" ws ILoc8 { p.Inc8() }
Inc8 <- "INC" ws Loc8 { p.Inc8() }
Inc16 <- "INC" ws Loc16 { p.Inc16() }
Dec <- Dec16Indexed8 / Dec16 / Dec8
Dec16Indexed8 <- "DEC" ws ILoc8 { p.Dec8() }
Dec8 <- "DEC" ws Loc8 { p.Dec8() }
Dec16 <- "DEC" ws Loc16 { p.Dec16() }
Alu16 <- Add16 / Adc16 / Sbc16
Add16 <- "ADD" ws Dst16 sep Src16 { p.Add16() }
Adc16 <- "ADC" ws Dst16 sep Src16 { p.Adc16() }
Sbc16 <- "SBC" ws Dst16 sep Src16 { p.Sbc16() }
Dst8 <- (Reg8 / Reg16Contents / nn_contents) { p.Dst8() }
Src8 <- (n / Reg8 / Reg16Contents / nn_contents) { p.Src8() }
Loc8 <- (Reg8 / Reg16Contents) { p.Loc8() }
Copy8 <- Reg8 { p.Copy8() }
ILoc8 <- IReg8 { p.Loc8() }
Reg8 <- <A / F / B / C / D / E / H / L / IReg8 / I / R> { p.R8(buffer[begin:end]) }
IReg8 <- <IXH / IXL / IYH / IYL> { p.R8(buffer[begin:end]) }
Dst16 <- (Reg16 / nn_contents / Reg16Contents) { p.Dst16() }
Src16 <- (Reg16 / nn / nn_contents) { p.Src16() }
Loc16 <- Reg16 { p.Loc16() }
Reg16 <- <AF_PRIME / AF / BC / DE / HL / SP / IReg16> { p.R16(buffer[begin:end]) }
IReg16 <- <IX / IY> { p.R16(buffer[begin:end]) }
Reg16Contents <- ( IndexedR16C / PlainR16C )
PlainR16C <- '(' Reg16 ')' { p.R16Contents() }
IndexedR16C <- '(' IReg16 ws? disp ws? ')' { p.IR16Contents() }
n <- hexByteH / hexByte0x / decimalByte
nn <- LabelNN / hexWordH / hexWord0x
disp <- signedHexByteH / signedHexByte0x / signedDecimalByte
signedDecimalByte <- <[-+]?[0-9]+> { p.DispDecimal(buffer[begin:end]) }
signedHexByteH <- <[-+]?[0-9a-fA-F]+> "h" { p.DispHex(buffer[begin:end]) }
signedHexByte0x <- <[-+]? "0x" [0-9a-fA-F]+> { p.Disp0xHex(buffer[begin:end]) }
hexByteH <- <hexdigit hexdigit> "h" { p.Nhex(buffer[begin:end]) }
hexByte0x <- "0x" <hexdigit hexdigit> { p.Nhex(buffer[begin:end]) }
decimalByte <- <[0-9]+> { p.Ndec(buffer[begin:end]) }
LabelNN <- <LabelText> { p.NNLabel(buffer[begin:end]) }
hexWordH <- ( zeroHexWord / hexWord ) "h"
hexWord0x <- "0x" ( zeroHexWord / hexWord )
hexWord <- <hexdigit hexdigit hexdigit hexdigit> { p.NNhex(buffer[begin:end]) }
zeroHexWord <- '0' hexWord
nn_contents <- '(' nn ')' { p.NNContents() }
Alu <- Add / Adc / Sub / Sbc / And / Xor / Or / Cp
Add <- "ADD" ws "A" sep Src8 { p.Accum("ADD") }
Adc <- "ADC" ws "A" sep Src8 { p.Accum("ADC") }
Sub <- "SUB" ws Src8 { p.Accum("SUB") }
Sbc <- "SBC" ws "A" sep Src8 { p.Accum("SBC") }
And <- "AND" ws ("A" sep)? Src8 { p.Accum("AND") }
Xor <- "XOR" ws Src8 { p.Accum("XOR") }
Or <- "OR" ws Src8 { p.Accum("OR") }
Cp <- "CP" ws Src8 { p.Accum("CP") }
BitOp <- Rot / Bit / Res / Set
Rot <- Rlc / Rrc / Rl / Rr / Sla / Sra / Sll / Srl
Rlc <- "RLC" ws Loc8 (sep Copy8)? { p.Rot("RLC") }
Rrc <- "RRC" ws Loc8 (sep Copy8)? { p.Rot("RRC") }
Rl <- "RL" ws Loc8 (sep Copy8)? { p.Rot("RL") }
Rr <- "RR" ws Loc8 (sep Copy8)? { p.Rot("RR") }
Sla <- "SLA" ws Loc8 (sep Copy8)? { p.Rot("SLA") }
Sra <- "SRA" ws Loc8 (sep Copy8)? { p.Rot("SRA") }
Sll <- "SLL" ws Loc8 (sep Copy8)? { p.Rot("SLL") }
Srl <- "SRL" ws Loc8 (sep Copy8)? { p.Rot("SRL") }
Bit <- "BIT" ws octaldigit sep Loc8 { p.Bit() }
Res <- "RES" ws octaldigit sep Loc8 (sep Copy8)? { p.Res() }
Set <- "SET" ws octaldigit sep Loc8 (sep Copy8)? { p.Set() }
Simple <- Nop / Halt / Rlca / Rrca / Rla / Rra / Daa / Cpl / Scf / Ccf / Exx / Di / Ei
Nop <- <"NOP"> { p.Simple(buffer[begin:end]) }
Halt <- <"HALT"> { p.Simple(buffer[begin:end]) }
Rlca <- <"RLCA"> { p.Simple(buffer[begin:end]) }
Rrca <- <"RRCA"> { p.Simple(buffer[begin:end]) }
Rla <- <"RLA"> { p.Simple(buffer[begin:end]) }
Rra <- <"RRA"> { p.Simple(buffer[begin:end]) }
Daa <- <"DAA"> { p.Simple(buffer[begin:end]) }
Cpl <- <"CPL"> { p.Simple(buffer[begin:end]) }
Scf <- <"SCF"> { p.Simple(buffer[begin:end]) }
Ccf <- <"CCF"> { p.Simple(buffer[begin:end]) }
Exx <- <"EXX"> { p.Simple(buffer[begin:end]) }
Di <- <"DI"> { p.Simple(buffer[begin:end]) }
Ei <- <"EI"> { p.Simple(buffer[begin:end]) }
EDSimple <- Neg / Retn / Reti / Rrd / Rld / Im0 / Im1 / Im2 / Blit / BlitIO
Neg <- <"NEG"> { p.EDSimple(buffer[begin:end]) }
Retn <- <"RETN"> { p.EDSimple(buffer[begin:end]) }
Reti <- <"RETI"> { p.EDSimple(buffer[begin:end]) }
Rrd <- <"RRD"> { p.EDSimple(buffer[begin:end]) }
Rld <- <"RLD"> { p.EDSimple(buffer[begin:end]) }
Im0 <- <"IM 0"> { p.EDSimple(buffer[begin:end]) }
Im1 <- <"IM 1"> { p.EDSimple(buffer[begin:end]) }
Im2 <- <"IM 2"> { p.EDSimple(buffer[begin:end]) }
Blit <- Ldir / Ldi / Cpir / Cpi / Lddr / Ldd / Cpdr / Cpd
BlitIO <- Inir / Ini / Otir / Outi / Indr / Ind / Otdr / Outd
Ldi <- <"LDI"> { p.EDSimple(buffer[begin:end]) }
Cpi <- <"CPI"> { p.EDSimple(buffer[begin:end]) }
Ini <- <"INI"> { p.EDSimple(buffer[begin:end]) }
Outi <- <"OUTI"> { p.EDSimple(buffer[begin:end]) }
Ldd <- <"LDD"> { p.EDSimple(buffer[begin:end]) }
Cpd <- <"CPD"> { p.EDSimple(buffer[begin:end]) }
Ind <- <"IND"> { p.EDSimple(buffer[begin:end]) }
Outd <- <"OUTD"> { p.EDSimple(buffer[begin:end]) }
Ldir <- <"LDIR"> { p.EDSimple(buffer[begin:end]) }
Cpir <- <"CPIR"> { p.EDSimple(buffer[begin:end]) }
Inir <- <"INIR"> { p.EDSimple(buffer[begin:end]) }
Otir <- <"OTIR"> { p.EDSimple(buffer[begin:end]) }
Lddr <- <"LDDR"> { p.EDSimple(buffer[begin:end]) }
Cpdr <- <"CPDR"> { p.EDSimple(buffer[begin:end]) }
Indr <- <"INDR"> { p.EDSimple(buffer[begin:end]) }
Otdr <- <"OTDR"> { p.EDSimple(buffer[begin:end]) }
Jump <- Rst / Call / Ret / Jp / Jr / Djnz
Rst <- "RST" ws n { p.Rst() }
Call <- "CALL" ws (cc sep)? Src16 { p.Call() }
Ret <- "RET" (ws cc)? { p.Ret() }
Jp <- "JP" ws (cc sep)? Src16 { p.Jp() }
Jr <- "JR" ws (cc sep)? disp { p.Jr() }
Djnz <- "DJNZ" ws disp { p.Djnz() }
IO <- IN / OUT
IN <- "IN" ws Reg8 sep Port { p.In() }
OUT <- "OUT" ws Port sep Reg8 { p.Out() }
Port <- "(C)" / '(' n ')'
sep <- ws? ',' ws?
ws <- [ \t]+
A <- "A"
F <- "F"
B <- "B"
C <- "C"
D <- "D"
E <- "E"
H <- "H"
L <- "L"
IXH <- "IXH"
IXL <- "IXL"
IYH <- "IYH"
IYL <- "IYL"
I <- "I"
R <- "R"
AF <- "AF"
AF_PRIME <- "AF'"
BC <- "BC"
DE <- "DE"
HL <- "HL"
IX <- "IX"
IY <- "IY"
SP <- "SP"
hexdigit <- [0-9] / [[a-f]]
octaldigit <- <[0-7]> { p.ODigit(buffer[begin:end]) }
cc <- FT_NZ / FT_Z / FT_NC / FT_C / FT_PO / FT_PE / FT_P / FT_M
FT_NZ <- "NZ" { p.Conditional(Not{FT_Z}) }
FT_Z <- "Z" { p.Conditional(FT_Z) }
FT_NC <- "NC" { p.Conditional(Not{FT_C}) }
FT_C <- "C" { p.Conditional(FT_C) }
FT_PO <- "PO" { p.Conditional(FT_PO) }
FT_PE <- "PE" { p.Conditional(FT_PE) }
FT_P <- "P" { p.Conditional(FT_P) }
FT_M <- "M" { p.Conditional(FT_M) }