1 |
2 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: zparser.h
|
4 |
|
|
//
|
5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU core
|
6 |
|
|
//
|
7 |
|
|
// Purpose: The beginnings of a parser for Zip CPU assembly.
|
8 |
|
|
//
|
9 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
10 |
|
|
// Gisselquist Tecnology, LLC
|
11 |
|
|
//
|
12 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
13 |
|
|
//
|
14 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
15 |
|
|
//
|
16 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
17 |
|
|
// modify it under the terms of the GNU General Public License as published
|
18 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
19 |
|
|
// your option) any later version.
|
20 |
|
|
//
|
21 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
22 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
23 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
24 |
|
|
// for more details.
|
25 |
|
|
//
|
26 |
|
|
// You should have received a copy of the GNU General Public License along
|
27 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
28 |
|
|
// target there if the PDF file isn't present.) If not, see
|
29 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
30 |
|
|
//
|
31 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
32 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
33 |
|
|
//
|
34 |
|
|
//
|
35 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
36 |
|
|
|
37 |
|
|
#ifndef ZPARSER_H
|
38 |
|
|
#define ZPARSER_H
|
39 |
|
|
|
40 |
|
|
class ZPARSER {
|
41 |
|
|
public:
|
42 |
|
|
typedef unsigned int ZIPI, ZIPA;
|
43 |
|
|
typedef int ZIPIMM;
|
44 |
|
|
typedef enum {
|
45 |
|
|
ZIP_R0, ZIP_R1, ZIP_R2, ZIP_R3, ZIP_R4, ZIP_R5, ZIP_R6, ZIP_R7,
|
46 |
|
|
ZIP_R8, ZIP_R9, ZIP_R10, ZIP_R11, ZIP_R12,
|
47 |
|
|
ZIP_SP, ZIP_CC, ZIP_PC,
|
48 |
|
|
ZIP_uR0, ZIP_uR1, ZIP_uR2, ZIP_uR3, ZIP_uR4, ZIP_uR5, ZIP_uR6, ZIP_uR7,
|
49 |
|
|
ZIP_uR8, ZIP_uR9, ZIP_uR10, ZIP_uR11, ZIP_uR12,
|
50 |
|
|
ZIP_uSP, ZIP_uCC, ZIP_uPC,
|
51 |
|
|
ZIP_Rnone
|
52 |
|
|
} ZIPREG;
|
53 |
|
|
|
54 |
|
|
typedef enum {
|
55 |
|
|
ZIPC_ALWAYS, ZIPC_Z, ZIPC_NZ, ZIPC_GE, ZIPC_GT, ZIPC_LT,
|
56 |
|
|
ZIPC_C, ZIPC_V
|
57 |
|
|
} ZIPCOND;
|
58 |
|
|
|
59 |
|
|
bool iscomment(const char *line) const;
|
60 |
|
|
bool islabel(const char *line) const;
|
61 |
|
|
bool parse_op(const char *line, ZIPA pc, ZIPI &instruction, const unsigned lineno) const;
|
62 |
|
|
bool parse(const char *line, ZIPA &pc, ZIPI &instruction, const unsigned lineno);
|
63 |
|
|
|
64 |
|
|
ZIPI op_cmp(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
65 |
|
|
ZIPI op_cmp(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
66 |
|
|
ZIPI op_cmp(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
67 |
|
|
{ return op_cmp(ZIPC_ALWAYS, imm, b, a); }
|
68 |
|
|
ZIPI op_cmp(ZIPIMM imm, ZIPREG a) const
|
69 |
|
|
{ return op_cmp(ZIPC_ALWAYS, imm, a); }
|
70 |
|
|
|
71 |
|
|
ZIPI op_tst(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
72 |
|
|
ZIPI op_tst(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
73 |
|
|
ZIPI op_tst(ZIPIMM imm, ZIPREG a) const
|
74 |
|
|
{ return op_tst(ZIPC_ALWAYS, imm, a); }
|
75 |
|
|
ZIPI op_tst(ZIPCOND cnd, ZIPREG a) const
|
76 |
|
|
{ return op_tst(cnd, -1, a); }
|
77 |
|
|
|
78 |
|
|
ZIPI op_mov(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
79 |
|
|
ZIPI op_mov(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
80 |
|
|
{ return op_mov(ZIPC_ALWAYS, imm, b, a); }
|
81 |
|
|
ZIPI op_mov(ZIPREG b, ZIPREG a) const
|
82 |
|
|
{ return op_mov(ZIPC_ALWAYS, 0, b, a); }
|
83 |
|
|
|
84 |
|
|
ZIPI op_ldi(ZIPIMM imm, ZIPREG a) const;
|
85 |
|
|
ZIPI op_trap(ZIPCOND cnd, ZIPIMM imm) const;
|
86 |
|
|
ZIPI op_trap(ZIPIMM imm) const
|
87 |
|
|
{ return op_trap(ZIPC_ALWAYS, imm); }
|
88 |
|
|
ZIPI op_clr(ZIPREG a) const { return op_ldi(0, a); }
|
89 |
|
|
|
90 |
|
|
ZIPI op_noop(void) const;
|
91 |
|
|
ZIPI op_break(void) const;
|
92 |
|
|
|
93 |
|
|
ZIPI op_ldihi(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
94 |
|
|
ZIPI op_ldilo(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
95 |
|
|
ZIPI op_ldihi(ZIPIMM imm, ZIPREG a) const
|
96 |
|
|
{ return op_ldihi(ZIPC_ALWAYS, imm, a); }
|
97 |
|
|
ZIPI op_ldilo(ZIPIMM imm, ZIPREG a) const
|
98 |
|
|
{ return op_ldilo(ZIPC_ALWAYS, imm, a); }
|
99 |
|
|
|
100 |
8 |
dgisselq |
ZIPI op_mpy(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
101 |
|
|
ZIPI op_mpy(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
102 |
|
|
ZIPI op_mpy(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
103 |
|
|
{ return op_mpy(ZIPC_ALWAYS, imm, b, a); }
|
104 |
|
|
ZIPI op_mpy(ZIPIMM imm, ZIPREG a) const
|
105 |
|
|
{ return op_mpy(ZIPC_ALWAYS, imm, a); }
|
106 |
|
|
|
107 |
|
|
ZIPI op_rol(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
108 |
|
|
ZIPI op_rol(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
109 |
|
|
ZIPI op_rol(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
110 |
|
|
{ return op_rol(ZIPC_ALWAYS, imm, b, a); }
|
111 |
|
|
ZIPI op_rol(ZIPIMM imm, ZIPREG a) const
|
112 |
|
|
{ return op_rol(ZIPC_ALWAYS, imm, a); }
|
113 |
|
|
|
114 |
2 |
dgisselq |
ZIPI op_lod(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
115 |
|
|
ZIPI op_lod(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
116 |
|
|
ZIPI op_lod(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
117 |
|
|
{ return op_lod(ZIPC_ALWAYS, imm, b, a); }
|
118 |
|
|
ZIPI op_lod(ZIPIMM imm, ZIPREG a) const
|
119 |
|
|
{ return op_lod(ZIPC_ALWAYS, imm, a); }
|
120 |
|
|
|
121 |
|
|
ZIPI op_sto(ZIPCOND cnd, ZIPREG v, ZIPIMM imm, ZIPREG b) const;
|
122 |
|
|
ZIPI op_sto(ZIPCOND cnd, ZIPREG v, ZIPIMM imm) const;
|
123 |
|
|
ZIPI op_sto(ZIPREG v, ZIPIMM imm, ZIPREG b) const
|
124 |
|
|
{ return op_sto(ZIPC_ALWAYS, v, imm, b); }
|
125 |
|
|
ZIPI op_sto(ZIPREG v, ZIPIMM imm) const
|
126 |
|
|
{ return op_sto(ZIPC_ALWAYS, v, imm); }
|
127 |
|
|
|
128 |
|
|
ZIPI op_sub(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
129 |
|
|
ZIPI op_sub(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
130 |
|
|
ZIPI op_sub(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
131 |
|
|
{ return op_sub(ZIPC_ALWAYS, imm, b, a); }
|
132 |
|
|
ZIPI op_sub(ZIPIMM imm, ZIPREG a) const
|
133 |
|
|
{ return op_sub(ZIPC_ALWAYS, imm, a); }
|
134 |
|
|
|
135 |
|
|
ZIPI op_and(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
136 |
|
|
ZIPI op_and(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
137 |
|
|
ZIPI op_and(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
138 |
|
|
{ return op_and(ZIPC_ALWAYS, imm, b, a); }
|
139 |
|
|
ZIPI op_and(ZIPIMM imm, ZIPREG a) const
|
140 |
|
|
{ return op_and(ZIPC_ALWAYS, imm, a); }
|
141 |
|
|
|
142 |
|
|
ZIPI op_add(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
143 |
|
|
ZIPI op_add(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
144 |
|
|
ZIPI op_add(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
145 |
|
|
{ return op_add(ZIPC_ALWAYS, imm, b, a); }
|
146 |
|
|
ZIPI op_add(ZIPIMM imm, ZIPREG a) const // GOOD
|
147 |
|
|
{ return op_add(ZIPC_ALWAYS, imm, a); }
|
148 |
|
|
|
149 |
|
|
ZIPI op_or(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
150 |
|
|
ZIPI op_or(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
151 |
|
|
ZIPI op_or(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
152 |
|
|
{ return op_or(ZIPC_ALWAYS, imm, b, a); }
|
153 |
|
|
ZIPI op_or(ZIPIMM imm, ZIPREG a) const
|
154 |
|
|
{ return op_or(ZIPC_ALWAYS, imm, a); }
|
155 |
|
|
|
156 |
|
|
ZIPI op_xor(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
157 |
|
|
ZIPI op_xor(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
158 |
|
|
ZIPI op_xor(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
159 |
|
|
{ return op_xor(ZIPC_ALWAYS, imm, b, a); }
|
160 |
|
|
ZIPI op_xor(ZIPIMM imm, ZIPREG a) const
|
161 |
|
|
{ return op_xor(ZIPC_ALWAYS, imm, a); }
|
162 |
|
|
|
163 |
|
|
ZIPI op_lsl(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
164 |
|
|
ZIPI op_lsl(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
165 |
|
|
ZIPI op_lsl(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
166 |
|
|
{ return op_lsl(ZIPC_ALWAYS, imm, b, a); }
|
167 |
|
|
ZIPI op_lsl(ZIPIMM imm, ZIPREG a) const
|
168 |
|
|
{ return op_lsl(ZIPC_ALWAYS, imm, a); }
|
169 |
|
|
ZIPI op_asl(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
170 |
|
|
{ return op_lsl(cnd, imm, b, a); }
|
171 |
|
|
ZIPI op_asl(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const
|
172 |
|
|
{ return op_lsl(cnd, imm, a); }
|
173 |
|
|
ZIPI op_asl(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
174 |
|
|
{ return op_lsl(ZIPC_ALWAYS, imm, b, a); }
|
175 |
|
|
ZIPI op_asl(ZIPIMM imm, ZIPREG a) const
|
176 |
|
|
{ return op_lsl(ZIPC_ALWAYS, imm, a); }
|
177 |
|
|
|
178 |
|
|
ZIPI op_asr(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
179 |
|
|
ZIPI op_asr(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
180 |
|
|
ZIPI op_asr(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
181 |
|
|
{ return op_asr(ZIPC_ALWAYS, imm, b, a); }
|
182 |
|
|
ZIPI op_asr(ZIPIMM imm, ZIPREG a) const
|
183 |
|
|
{ return op_asr(ZIPC_ALWAYS, imm, a); }
|
184 |
|
|
|
185 |
|
|
ZIPI op_lsr(ZIPCOND cnd, ZIPIMM imm, ZIPREG b, ZIPREG a) const;
|
186 |
|
|
ZIPI op_lsr(ZIPCOND cnd, ZIPIMM imm, ZIPREG a) const;
|
187 |
|
|
ZIPI op_lsr(ZIPIMM imm, ZIPREG b, ZIPREG a) const
|
188 |
|
|
{ return op_lsr(ZIPC_ALWAYS, imm, b, a); }
|
189 |
|
|
ZIPI op_lsr(ZIPIMM imm, ZIPREG a) const
|
190 |
|
|
{ return op_lsr(ZIPC_ALWAYS, imm, a); }
|
191 |
|
|
|
192 |
|
|
ZIPI op_bra(ZIPCOND cnd, ZIPIMM imm) const
|
193 |
|
|
{ return op_mov(cnd, imm, ZIP_PC, ZIP_PC); }
|
194 |
|
|
ZIPI op_bra(ZIPIMM imm) const
|
195 |
|
|
{ return op_mov(ZIPC_ALWAYS, imm, ZIP_PC, ZIP_PC); }
|
196 |
|
|
ZIPI op_brz(ZIPIMM imm) const
|
197 |
|
|
{ return op_mov(ZIPC_Z, imm, ZIP_PC, ZIP_PC); }
|
198 |
|
|
ZIPI op_bnz(ZIPIMM imm) const
|
199 |
|
|
{ return op_mov(ZIPC_NZ, imm, ZIP_PC, ZIP_PC); }
|
200 |
|
|
ZIPI op_bge(ZIPIMM imm) const
|
201 |
|
|
{ return op_mov(ZIPC_GE, imm, ZIP_PC, ZIP_PC); }
|
202 |
|
|
ZIPI op_bgt(ZIPIMM imm) const
|
203 |
|
|
{ return op_mov(ZIPC_GT, imm, ZIP_PC, ZIP_PC); }
|
204 |
|
|
ZIPI op_blt(ZIPIMM imm) const
|
205 |
|
|
{ return op_mov(ZIPC_LT, imm, ZIP_PC, ZIP_PC); }
|
206 |
|
|
ZIPI op_brc(ZIPIMM imm) const
|
207 |
|
|
{ return op_mov(ZIPC_C, imm, ZIP_PC, ZIP_PC); }
|
208 |
|
|
ZIPI op_brv(ZIPIMM imm) const
|
209 |
|
|
{ return op_mov(ZIPC_V, imm, ZIP_PC, ZIP_PC); }
|
210 |
|
|
ZIPI op_bv(ZIPIMM imm) const
|
211 |
|
|
{ return op_brv(imm); }
|
212 |
|
|
|
213 |
|
|
ZIPI op_clrf(ZIPCOND cnd, ZIPREG a) const
|
214 |
|
|
{ return op_xor(cnd, 0, a, a); }
|
215 |
|
|
ZIPI op_clrf(ZIPREG a) const
|
216 |
|
|
{ return op_xor(ZIPC_ALWAYS, 0, a, a); }
|
217 |
|
|
ZIPI op_halt(ZIPCOND c) const {
|
218 |
|
|
return op_or(c, 0x10, ZIP_CC); }
|
219 |
|
|
ZIPI op_wait(ZIPCOND c) const {
|
220 |
|
|
return op_or(c, 0x10, ZIP_CC); }
|
221 |
|
|
ZIPI op_halt(void) const {
|
222 |
|
|
return op_or(ZIPC_ALWAYS, 0x10, ZIP_CC); }
|
223 |
|
|
ZIPI op_wait(void) const {
|
224 |
|
|
return op_or(ZIPC_ALWAYS, 0x10, ZIP_CC); }
|
225 |
|
|
ZIPI op_busy(ZIPCOND c) const {
|
226 |
|
|
return op_mov(c, -1, ZIP_PC, ZIP_PC); }
|
227 |
|
|
ZIPI op_busy(void) const {
|
228 |
|
|
return op_mov(ZIPC_ALWAYS, -1, ZIP_PC, ZIP_PC); }
|
229 |
|
|
|
230 |
|
|
ZIPI op_rtu(void) const {
|
231 |
|
|
return op_or(ZIPC_ALWAYS, 0x20, ZIP_CC); }
|
232 |
|
|
ZIPI op_rtu(ZIPCOND cnd) const {
|
233 |
|
|
return op_or(cnd, 0x20, ZIP_CC); }
|
234 |
|
|
|
235 |
|
|
ZIPI op_jmp(ZIPCOND c, ZIPIMM imm, ZIPREG r) const
|
236 |
|
|
{ return op_mov(ZIPC_ALWAYS, imm, r, ZIP_PC); }
|
237 |
|
|
|
238 |
|
|
ZIPI op_ljmp(ZIPCOND c, ZIPIMM imm) const {
|
239 |
|
|
return op_add(ZIPC_ALWAYS, imm, ZIP_PC); }
|
240 |
|
|
|
241 |
|
|
ZIPI op_not(ZIPCOND c, ZIPREG r) const
|
242 |
|
|
{ return op_xor(c, -1, r); }
|
243 |
|
|
ZIPI op_not(ZIPREG r) const
|
244 |
|
|
{ return op_xor(ZIPC_ALWAYS, -1, r); }
|
245 |
|
|
|
246 |
|
|
};
|
247 |
|
|
|
248 |
|
|
#endif
|