2 * Simulator of microcontrollers (m6800cl.h)
4 * Copyright (C) 2020 Drotos Daniel
6 * To contact author send email to dr.dkdb@gmail.com
10 /* This file is part of microcontroller simulator: ucsim.
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 #ifndef M6800CL_HEADER
29 #define M6800CL_HEADER
40 extern instruction_wrapper_fn itab
[256];
46 #ifdef WORDS_BIGENDIAN
61 #ifdef WORDS_BIGENDIAN
72 #define rA (acc.DAB.a8.Ar)
73 #define A (acc.DAB.a8.Ar)
74 #define rB (acc.DAB.a8.Br)
75 #define B (acc.DAB.a8.Br)
76 #define rD (acc.DAB.Dr)
77 #define D (acc.DAB.Dr)
78 #define rCC (CC.cc16.cc8.ccl)
79 #define rF (CC.cc16.cc8.ccl)
109 #define ifCC (!(rF&mC))
111 #define ifPL (!(rF&mN))
113 #define ifVC (!(rF&mV))
115 #define ifNE (!(rF&mZ))
116 #define ifLT ( (rF&mN) ^ ((rF&mV)?mN:0) )
117 #define ifLE ( (rF&mZ) | (((rF&mN)?mZ:0) ^ ((rF&mV)?mZ:0)) )
120 #define ifLS ( ((rF&mC)?mZ:0) | (rF&mZ) )
125 extern i8_t p0ticks
[256];
127 class cl_idx16
: public cl_cell16
132 cl_idx16(): cl_cell16() { name
= ""; }
136 class cl_mop16
: public cl_cell16
139 cl_memory_cell
*l
, *h
;
141 class cl_address_space
*as
;
144 cl_mop16(): cl_cell16() {}
145 virtual void set_uc(class cl_uc
*iuc
);
146 virtual void a(u16_t iaddr
);
147 virtual void r(u16_t iaddr
);
148 virtual t_mem
read(void) { return h
->R()*256 + l
->R(); }
149 virtual t_mem
get(void) { return h
->get()*256 + l
->get(); }
150 virtual t_mem
write(t_mem val
) { return (h
->W(val
>>8))*256 + l
->W(val
); }
151 virtual t_mem
set(t_mem val
) { h
->set(val
>>8); l
->set(val
); return val
; }
156 * Base of M6800 processor
159 class cl_m6800
: public cl_uc
165 class cl_cell8 cA
, cB
, cCC
;
168 class cl_it_src
*src_irq
, *src_nmi
, *src_swi
;
170 class cl_mop16 mop16
;
172 u16_t IRQ_AT
, SWI_AT
, NMI_AT
, RESET_AT
;
174 cl_m6800(class cl_sim
*asim
);
175 virtual int init(void);
176 virtual const char *id_string(void);
177 virtual void reset(void);
178 virtual void set_PC(t_addr addr
);
180 virtual void mk_hw_elements(void);
181 virtual void make_cpu_hw(void);
182 virtual void make_memories(void);
183 virtual void setup_ccr(void);
185 virtual i8_t
*tick_tab(t_mem code
) { return p0ticks
; }
186 virtual double def_xtal(void) { return 1000000; }
187 virtual int clock_per_cycle(void) { return 1; }
188 virtual struct dis_entry
*dis_tbl(void);
189 virtual struct dis_entry
*get_dis_entry(t_addr addr
);
190 virtual char *disassc(t_addr addr
, chars
*comment
=NULL
);
191 virtual t_addr
read_addr(class cl_memory
*m
, t_addr start_addr
);
192 virtual void analyze_start(void);
193 virtual void analyze(t_addr addr
);
194 virtual int inst_length(t_addr addr
);
195 virtual int longest_inst(void) { return 4; }
197 virtual void print_regs(class cl_console_base
*con
);
199 virtual int exec_inst(void);
200 virtual int priority_of(uchar nuof_it
) { return nuof_it
; }
201 virtual int accept_it(class it_level
*il
);
202 virtual bool it_enabled(void) { return true; }
203 virtual void push_regs(bool inst_part
);
204 virtual void pull_regs(bool inst_part
);
206 virtual class cl_memory_cell
&idx(void);
207 virtual class cl_memory_cell
&ext(void);
208 virtual class cl_memory_cell
&dir(void);
209 u8_t
i8(void) { return fetch(); }
210 u16_t
i16(void) { u8_t h
, l
; h
= fetch(); l
= fetch(); return h
*256+l
; }
211 u8_t
iop(void) { vc
.rd
++; return idx().R(); }
212 u8_t
eop(void) { vc
.rd
++; return ext().R(); }
213 u8_t
dop(void) { vc
.rd
++; return dir().R(); }
221 virtual class cl_memory_cell
&idst(void) { vc
.rd
++; vc
.wr
++; return idx(); }
222 virtual class cl_memory_cell
&edst(void) { vc
.rd
++; vc
.wr
++; return ext(); }
223 virtual class cl_memory_cell
&ddst(void) { vc
.rd
++; vc
.wr
++; return dir(); }
225 virtual int sub(class cl_memory_cell
&dest
, u8_t op
, bool c
);
226 virtual int sub(class cl_memory_cell
&dest
, u8_t op
);
227 virtual int sbc(class cl_memory_cell
&dest
, u8_t op
);
228 virtual int cmp(u8_t op1
, u8_t op2
);
229 virtual int add(class cl_memory_cell
&dest
, u8_t op
, bool c
);
230 virtual int add(class cl_memory_cell
&dest
, u8_t op
);
231 virtual int adc(class cl_memory_cell
&dest
, u8_t op
);
232 virtual int neg(class cl_memory_cell
&dest
);
233 virtual int com(class cl_memory_cell
&dest
);
234 virtual int lsr(class cl_memory_cell
&dest
);
235 virtual int ror(class cl_memory_cell
&dest
);
236 virtual int asr(class cl_memory_cell
&dest
);
237 virtual int asl(class cl_memory_cell
&dest
);
238 virtual int rol(class cl_memory_cell
&dest
);
239 virtual int dec(class cl_memory_cell
&dest
);
240 virtual int inc(class cl_memory_cell
&dest
);
241 virtual int tst(u8_t op
);
242 virtual int clr(class cl_memory_cell
&dest
);
243 virtual int And(class cl_memory_cell
&dest
, u8_t op
);
244 virtual int bit(u8_t op1
, u8_t op2
);
245 virtual int eor(class cl_memory_cell
&dest
, u8_t op
);
246 virtual int Or (class cl_memory_cell
&dest
, u8_t op
);
247 virtual int lda(class cl_memory_cell
&dest
, u8_t op
);
248 virtual int sta(class cl_memory_cell
&dest
, u8_t op
);
249 virtual int cpx(u16_t op
);
250 virtual int ldsx(class cl_cell16
&dest
, u16_t op
);
251 virtual int stsx(t_addr a
, u16_t op
);
252 virtual int call(t_addr a
);
253 virtual int branch(t_addr a
, bool cond
);
255 virtual int NOP(t_mem code
);
256 virtual int TAP(t_mem code
);
257 virtual int TPA(t_mem code
);
258 virtual int INX(t_mem code
);
259 virtual int DEX(t_mem code
);
260 virtual int CLV(t_mem code
);
261 virtual int SEV(t_mem code
);
262 virtual int CLC(t_mem code
);
263 virtual int SEc(t_mem code
);
264 virtual int CLI(t_mem code
);
265 virtual int SEI(t_mem code
);
267 virtual int SBA(t_mem code
) { return sub(cA
, rB
, false); }
268 virtual int CBA(t_mem code
) { return cmp(rA
, rB
); }
269 virtual int TAB(t_mem code
);
270 virtual int TBA(t_mem code
);
271 virtual int DAA(t_mem code
);
272 virtual int ABA(t_mem code
) { return add(cA
, rB
, false); }
274 virtual int BRA(t_mem code
) { return branch(raddr(), ifA
); }
275 virtual int BRN(t_mem code
) { return branch(raddr(), ifN
); }
276 virtual int BHI(t_mem code
) { return branch(raddr(), ifHI
); }
277 virtual int BLS(t_mem code
) { return branch(raddr(), ifLS
); }
278 virtual int BCC(t_mem code
) { return branch(raddr(), ifCC
); }
279 virtual int BCS(t_mem code
) { return branch(raddr(), ifCS
); }
280 virtual int BNE(t_mem code
) { return branch(raddr(), ifNE
); }
281 virtual int BEQ(t_mem code
) { return branch(raddr(), ifEQ
); }
282 virtual int BVC(t_mem code
) { return branch(raddr(), ifVC
); }
283 virtual int BVS(t_mem code
) { return branch(raddr(), ifVS
); }
284 virtual int BPL(t_mem code
) { return branch(raddr(), ifPL
); }
285 virtual int BMI(t_mem code
) { return branch(raddr(), ifMI
); }
286 virtual int BGE(t_mem code
) { return branch(raddr(), ifGE
); }
287 virtual int BLT(t_mem code
) { return branch(raddr(), ifLT
); }
288 virtual int BGT(t_mem code
) { return branch(raddr(), ifGT
); }
289 virtual int BLE(t_mem code
) { return branch(raddr(), ifLE
); }
291 virtual int TSX(t_mem code
);
292 virtual int INS(t_mem code
);
293 virtual int PULA(t_mem code
);
294 virtual int PULB(t_mem code
);
295 virtual int DES(t_mem code
);
296 virtual int TXS(t_mem code
);
297 virtual int PSHA(t_mem code
);
298 virtual int PSHB(t_mem code
);
299 virtual int RTS(t_mem code
);
301 virtual int RTI(t_mem code
);
302 virtual int WAI(t_mem code
);
303 virtual int SWI(t_mem code
);
305 virtual int NEGA(t_mem code
) { return neg(cA
); }
306 virtual int COMA(t_mem code
) { return com(cA
); }
307 virtual int LSRA(t_mem code
) { return lsr(cA
); }
308 virtual int RORA(t_mem code
) { return ror(cA
); }
309 virtual int ASRA(t_mem code
) { return asr(cA
); }
310 virtual int ASLA(t_mem code
) { return asl(cA
); }
311 virtual int ROLA(t_mem code
) { return rol(cA
); }
312 virtual int DECA(t_mem code
) { return dec(cA
); }
313 virtual int INCA(t_mem code
) { return inc(cA
); }
314 virtual int TSTA(t_mem code
) { return tst(rA
); }
315 virtual int CLRA(t_mem code
) { return clr(cA
); }
317 virtual int NEGB(t_mem code
) { return neg(cB
); }
318 virtual int COMB(t_mem code
) { return com(cB
); }
319 virtual int LSRB(t_mem code
) { return lsr(cB
); }
320 virtual int RORB(t_mem code
) { return ror(cB
); }
321 virtual int ASRB(t_mem code
) { return asr(cB
); }
322 virtual int ASLB(t_mem code
) { return asl(cB
); }
323 virtual int ROLB(t_mem code
) { return rol(cB
); }
324 virtual int DECB(t_mem code
) { return dec(cB
); }
325 virtual int INCB(t_mem code
) { return inc(cB
); }
326 virtual int TSTB(t_mem code
) { return tst(rB
); }
327 virtual int CLRB(t_mem code
) { return clr(cB
); }
329 virtual int NEGi(t_mem code
) { return neg(idst()); }
330 virtual int COMi(t_mem code
) { return com(idst()); }
331 virtual int LSRi(t_mem code
) { return lsr(idst()); }
332 virtual int RORi(t_mem code
) { return ror(idst()); }
333 virtual int ASRi(t_mem code
) { return asr(idst()); }
334 virtual int ASLi(t_mem code
) { return asl(idst()); }
335 virtual int ROLi(t_mem code
) { return rol(idst()); }
336 virtual int DECi(t_mem code
) { return dec(idst()); }
337 virtual int INCi(t_mem code
) { return inc(idst()); }
338 virtual int TSTi(t_mem code
) { vc
.rd
++; return tst(idx().R()); }
339 virtual int JMPi(t_mem code
);
340 virtual int CLRi(t_mem code
) { vc
.wr
++; return clr(idx()); }
342 virtual int NEGe(t_mem code
) { return neg(edst()); }
343 virtual int COMe(t_mem code
) { return com(edst()); }
344 virtual int LSRe(t_mem code
) { return lsr(edst()); }
345 virtual int RORe(t_mem code
) { return ror(edst()); }
346 virtual int ASRe(t_mem code
) { return asr(edst()); }
347 virtual int ASLe(t_mem code
) { return asl(edst()); }
348 virtual int ROLe(t_mem code
) { return rol(edst()); }
349 virtual int DECe(t_mem code
) { return dec(edst()); }
350 virtual int INCe(t_mem code
) { return inc(edst()); }
351 virtual int TSTe(t_mem code
) { vc
.rd
++; return tst(ext().R()); }
352 virtual int JMPe(t_mem code
);
353 virtual int CLRe(t_mem code
) { vc
.wr
++; return clr(ext()); }
355 virtual int SUBA8(t_mem code
) { return sub(cA
, i8(), false); }
356 virtual int CMPA8(t_mem code
) { return cmp(rA
, i8()); }
357 virtual int SBCA8(t_mem code
) { return sub(cA
, i8(), true); }
358 virtual int ANDA8(t_mem code
) { return And(cA
, i8()); }
359 virtual int BITA8(t_mem code
) { return bit(rA
, i8()); }
360 virtual int LDAA8(t_mem code
) { return lda(cA
, i8()); }
361 virtual int EORA8(t_mem code
) { return eor(cA
, i8()); }
362 virtual int ADCA8(t_mem code
) { return add(cA
, i8(), true); }
363 virtual int ORAA8(t_mem code
) { return Or (cA
, i8()); }
364 virtual int ADDA8(t_mem code
) { return add(cA
, i8(), false); }
365 virtual int CPX16(t_mem code
) { return cpx(i16()); }
366 virtual int BSR (t_mem code
) { return call(raddr()); }
367 virtual int LDS16(t_mem code
) { return ldsx(cSP
, i16()); }
369 virtual int SUBAd(t_mem code
) { return sub(cA
, dop(), false); }
370 virtual int CMPAd(t_mem code
) { return cmp(rA
, dop()); }
371 virtual int SBCAd(t_mem code
) { return sub(cA
, dop(), true); }
372 virtual int ANDAd(t_mem code
) { return And(cA
, dop()); }
373 virtual int BITAd(t_mem code
) { return bit(rA
, dop()); }
374 virtual int LDAAd(t_mem code
) { return lda(cA
, dop()); }
375 virtual int STAAd(t_mem code
) { return lda(ddst(), rA
); }
376 virtual int EORAd(t_mem code
) { return eor(cA
, dop()); }
377 virtual int ADCAd(t_mem code
) { return add(cA
, dop(), true); }
378 virtual int ORAAd(t_mem code
) { return Or (cA
, dop()); }
379 virtual int ADDAd(t_mem code
) { return add(cA
, dop(), false); }
380 virtual int CPXd (t_mem code
) { return cpx(dop16()); }
381 virtual int LDSd (t_mem code
) { return ldsx(cSP
, dop16()); }
382 virtual int STSd (t_mem code
) { return stsx(daddr(), rSP
); }
384 virtual int SUBAi(t_mem code
) { return sub(cA
, iop(), false); }
385 virtual int CMPAi(t_mem code
) { return cmp(rA
, iop()); }
386 virtual int SBCAi(t_mem code
) { return sub(cA
, iop(), true); }
387 virtual int ANDAi(t_mem code
) { return And(cA
, iop()); }
388 virtual int BITAi(t_mem code
) { return bit(rA
, iop()); }
389 virtual int LDAAi(t_mem code
) { return lda(cA
, iop()); }
390 virtual int STAAi(t_mem code
) { return lda(idst(), rA
); }
391 virtual int EORAi(t_mem code
) { return eor(cA
, iop()); }
392 virtual int ADCAi(t_mem code
) { return add(cA
, iop(), true); }
393 virtual int ORAAi(t_mem code
) { return Or (cA
, iop()); }
394 virtual int ADDAi(t_mem code
) { return add(cA
, iop(), false); }
395 virtual int CPXi (t_mem code
) { return cpx(iop16()); }
396 virtual int JSRi (t_mem code
) { return call(iaddr()); }
397 virtual int LDSi (t_mem code
) { return ldsx(cSP
, iop16()); }
398 virtual int STSi (t_mem code
) { return stsx(iaddr(), rSP
); }
400 virtual int SUBAe(t_mem code
) { return sub(cA
, eop(), false); }
401 virtual int CMPAe(t_mem code
) { return cmp(rA
, eop()); }
402 virtual int SBCAe(t_mem code
) { return sub(cA
, eop(), true); }
403 virtual int ANDAe(t_mem code
) { return And(cA
, eop()); }
404 virtual int BITAe(t_mem code
) { return bit(rA
, eop()); }
405 virtual int LDAAe(t_mem code
) { return lda(cA
, eop()); }
406 virtual int STAAe(t_mem code
) { return lda(edst(), rA
); }
407 virtual int EORAe(t_mem code
) { return eor(cA
, eop()); }
408 virtual int ADCAe(t_mem code
) { return add(cA
, eop(), true); }
409 virtual int ORAAe(t_mem code
) { return Or (cA
, eop()); }
410 virtual int ADDAe(t_mem code
) { return add(cA
, eop(), false); }
411 virtual int CPXe (t_mem code
) { return cpx(eop16()); }
412 virtual int JSRe (t_mem code
) { return call(eaddr()); }
413 virtual int LDSe (t_mem code
) { return ldsx(cSP
, eop16()); }
414 virtual int STSe (t_mem code
) { return stsx(eaddr(), rSP
); }
416 virtual int SUBB8(t_mem code
) { return sub(cB
, i8(), false); }
417 virtual int CMPB8(t_mem code
) { return cmp(rB
, i8()); }
418 virtual int SBCB8(t_mem code
) { return sub(cB
, i8(), true); }
419 virtual int ANDB8(t_mem code
) { return And(cB
, i8()); }
420 virtual int BITB8(t_mem code
) { return bit(rB
, i8()); }
421 virtual int LDAB8(t_mem code
) { return lda(cB
, i8()); }
422 virtual int EORB8(t_mem code
) { return eor(cB
, i8()); }
423 virtual int ADCB8(t_mem code
) { return add(cB
, i8(), true); }
424 virtual int ORAB8(t_mem code
) { return Or (cB
, i8()); }
425 virtual int ADDB8(t_mem code
) { return add(cB
, i8(), false); }
426 virtual int LDX16(t_mem code
) { return ldsx(cX
, i16()); }
428 virtual int SUBBd(t_mem code
) { return sub(cB
, dop(), false); }
429 virtual int CMPBd(t_mem code
) { return cmp(rB
, dop()); }
430 virtual int SBCBd(t_mem code
) { return sub(cB
, dop(), true); }
431 virtual int ANDBd(t_mem code
) { return And(cB
, dop()); }
432 virtual int BITBd(t_mem code
) { return bit(rB
, dop()); }
433 virtual int LDABd(t_mem code
) { return lda(cB
, dop()); }
434 virtual int STABd(t_mem code
) { return lda(ddst(), rB
); }
435 virtual int EORBd(t_mem code
) { return eor(cB
, dop()); }
436 virtual int ADCBd(t_mem code
) { return add(cB
, dop(), true); }
437 virtual int ORABd(t_mem code
) { return Or (cB
, dop()); }
438 virtual int ADDBd(t_mem code
) { return add(cB
, dop(), false); }
439 virtual int LDXd (t_mem code
) { return ldsx(cX
, dop16()); }
440 virtual int STXd (t_mem code
) { return stsx(daddr(), rX
); }
442 virtual int SUBBi(t_mem code
) { return sub(cB
, iop(), false); }
443 virtual int CMPBi(t_mem code
) { return cmp(rB
, iop()); }
444 virtual int SBCBi(t_mem code
) { return sub(cB
, iop(), true); }
445 virtual int ANDBi(t_mem code
) { return And(cB
, iop()); }
446 virtual int BITBi(t_mem code
) { return bit(rB
, iop()); }
447 virtual int LDABi(t_mem code
) { return lda(cB
, iop()); }
448 virtual int STABi(t_mem code
) { return lda(idst(), rB
); }
449 virtual int EORBi(t_mem code
) { return eor(cB
, iop()); }
450 virtual int ADCBi(t_mem code
) { return add(cB
, iop(), true); }
451 virtual int ORABi(t_mem code
) { return Or (cB
, iop()); }
452 virtual int ADDBi(t_mem code
) { return add(cB
, iop(), false); }
453 virtual int LDXi (t_mem code
) { return ldsx(cX
, iop16()); }
454 virtual int STXi (t_mem code
) { return stsx(iaddr(), rX
); }
456 virtual int SUBBe(t_mem code
) { return sub(cB
, eop(), false); }
457 virtual int CMPBe(t_mem code
) { return cmp(rB
, eop()); }
458 virtual int SBCBe(t_mem code
) { return sub(cB
, eop(), true); }
459 virtual int ANDBe(t_mem code
) { return And(cB
, eop()); }
460 virtual int BITBe(t_mem code
) { return bit(rB
, eop()); }
461 virtual int LDABe(t_mem code
) { return lda(cB
, eop()); }
462 virtual int STABe(t_mem code
) { return lda(edst(), rB
); }
463 virtual int EORBe(t_mem code
) { return eor(cB
, eop()); }
464 virtual int ADCBe(t_mem code
) { return add(cB
, eop(), true); }
465 virtual int ORABe(t_mem code
) { return Or (cB
, eop()); }
466 virtual int ADDBe(t_mem code
) { return add(cB
, eop(), false); }
467 virtual int LDXe (t_mem code
) { return ldsx(cX
, eop16()); }
468 virtual int STXe (t_mem code
) { return stsx(eaddr(), rX
); }
472 /* Unused bits of CC forced to be 1 */
474 class cl_cc_operator
: public cl_memory_operator
477 cl_cc_operator(class cl_memory_cell
*acell
): cl_memory_operator(acell
) {}
478 virtual t_mem
write(t_mem val
) { return val
|= 0xc0; }
484 /* End of motorola.src/m6800cl.h */