version 0.5.2
[fw/sdcc] / sim / ucsim / z80.src / inst_cb.cc
1 /*
2  * Simulator of microcontrollers (inst.cc)
3  *
4  * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
5  * 
6  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
7  *
8  */
9
10 /* This file is part of microcontroller simulator: ucsim.
11
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.
16
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.
21
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
25 02111-1307, USA. */
26 /*@1@*/
27
28 #include "ddconfig.h"
29
30 // local
31 #include "z80cl.h"
32 #include "regsz80.h"
33 #include "z80mac.h"
34
35 int
36 cl_z80::inst_cb_rlc(t_mem code)
37 {
38   switch(code) {
39     case 0x00: // RLC B
40       rlc_byte(regs.bc.h);
41     break;
42     case 0x01: // RLC C
43       rlc_byte(regs.bc.l);
44     break;
45     case 0x02: // RLC D
46       rlc_byte(regs.de.h);
47     break;
48     case 0x03: // RLC E
49       rlc_byte(regs.de.l);
50     break;
51     case 0x04: // RLC H
52       rlc_byte(regs.hl.h);
53     break;
54     case 0x05: // RLC L
55       rlc_byte(regs.hl.l);
56     break;
57     case 0x06: // RLC (HL)
58       { unsigned char tmp;
59         tmp = get1(regs.HL);
60         rlc_byte(tmp);
61         store1(regs.HL, tmp);
62       }
63     break;
64     case 0x07: // RLC A
65       rlc_byte(regs.A);
66     break;
67   }
68   return(resGO);
69 }
70
71 int
72 cl_z80::inst_cb_rrc(t_mem code)
73 {
74   switch(code) {
75     case 0x08: // RRC B
76       rrc_byte(regs.bc.h);
77     break;
78     case 0x09: // RRC C
79       rrc_byte(regs.bc.l);
80     break;
81     case 0x0A: // RRC D
82       rrc_byte(regs.de.h);
83     break;
84     case 0x0B: // RRC E
85       rrc_byte(regs.de.l);
86     break;
87     case 0x0C: // RRC H
88       rrc_byte(regs.hl.h);
89     break;
90     case 0x0D: // RRC L
91       rrc_byte(regs.hl.l);
92     break;
93     case 0x0E: // RRC (HL)
94       { unsigned char tmp;
95         tmp = get1(regs.HL);
96         rrc_byte(tmp);
97         store1(regs.HL, tmp);
98       }
99     break;
100     case 0x0F: // RRC A
101       rrc_byte(regs.A);
102     break;
103   }
104   return(resGO);
105 }
106
107 int
108 cl_z80::inst_cb_rl(t_mem code)
109 {
110   switch(code) {
111     case 0x10: // RL B
112       rl_byte(regs.bc.h);
113     break;
114     case 0x11: // RL C
115       rl_byte(regs.bc.l);
116     break;
117     case 0x12: // RL D
118       rl_byte(regs.de.h);
119     break;
120     case 0x13: // RL E
121       rl_byte(regs.de.l);
122     break;
123     case 0x14: // RL H
124       rl_byte(regs.hl.h);
125     break;
126     case 0x15: // RL L
127       rl_byte(regs.hl.l);
128     break;
129     case 0x16: // RL (HL)
130       { unsigned char tmp;
131         tmp = get1(regs.HL);
132         rl_byte(tmp);
133         store1(regs.HL, tmp);
134       }
135     break;
136     case 0x17: // RL A
137       rl_byte(regs.A);
138     break;
139   }
140   return(resGO);
141 }
142
143 int
144 cl_z80::inst_cb_rr(t_mem code)
145 {
146   switch(code) {
147     case 0x18: // RR B
148       rr_byte(regs.bc.h);
149     break;
150     case 0x19: // RR C
151       rr_byte(regs.bc.l);
152     break;
153     case 0x1A: // RR D
154       rr_byte(regs.de.h);
155     break;
156     case 0x1B: // RR E
157       rr_byte(regs.de.l);
158     break;
159     case 0x1C: // RR H
160       rr_byte(regs.hl.h);
161     break;
162     case 0x1D: // RR L
163       rr_byte(regs.hl.l);
164     break;
165     case 0x1E: // RR (HL)
166       { unsigned char tmp;
167         tmp = get1(regs.HL);
168         rr_byte(tmp);
169         store1(regs.HL, tmp);
170       }
171     break;
172     case 0x1F: // RR A
173       rr_byte(regs.A);
174     break;
175   }
176   return(resGO);
177 }
178
179 int
180 cl_z80::inst_cb_sla(t_mem code)
181 {
182   switch(code) {
183     case 0x20: // SLA B
184       sla_byte(regs.bc.h);
185     break;
186     case 0x21: // SLA C
187       sla_byte(regs.bc.l);
188     break;
189     case 0x22: // SLA D
190       sla_byte(regs.de.h);
191     break;
192     case 0x23: // SLA E
193       sla_byte(regs.de.l);
194     break;
195     case 0x24: // SLA H
196       sla_byte(regs.hl.h);
197     break;
198     case 0x25: // SLA L
199       sla_byte(regs.hl.l);
200     break;
201     case 0x26: // SLA (HL)
202       { unsigned char tmp;
203         tmp = get1(regs.HL);
204         sla_byte(tmp);
205         store1(regs.HL, tmp);
206       }
207     break;
208     case 0x27: // SLA A
209       sla_byte(regs.A);
210     break;
211   }
212   return(resGO);
213 }
214
215 int
216 cl_z80::inst_cb_sra(t_mem code)
217 {
218   switch(code) {
219     case 0x28: // SRA B
220       sra_byte(regs.bc.h);
221     break;
222     case 0x29: // SRA C
223       sra_byte(regs.bc.l);
224     break;
225     case 0x2A: // SRA D
226       sra_byte(regs.de.h);
227     break;
228     case 0x2B: // SRA E
229       sra_byte(regs.de.l);
230     break;
231     case 0x2C: // SRA H
232       sra_byte(regs.hl.h);
233     break;
234     case 0x2D: // SRA L
235       sra_byte(regs.hl.l);
236     break;
237     case 0x2E: // SRA (HL)
238       { unsigned char tmp;
239         tmp = get1(regs.HL);
240         sra_byte(tmp);
241         store1(regs.HL, tmp);
242       }
243     break;
244     case 0x2F: // SRA A
245       sra_byte(regs.A);
246     break;
247   }
248   return(resGO);
249 }
250
251 int
252 cl_z80::inst_cb_slia(t_mem code)
253 {
254   switch(code) {
255     case 0x30: // SLIA B        (Shift Left Inverted Arithmetic)
256       slia_byte(regs.bc.h);
257     break;
258     case 0x31: // SLIA C        like SLA, but shifts in a 1 bit
259       slia_byte(regs.bc.l);
260     break;
261     case 0x32: // SLIA D
262       slia_byte(regs.de.h);
263     break;
264     case 0x33: // SLIA E
265       slia_byte(regs.de.l);
266     break;
267     case 0x34: // SLIA H
268       slia_byte(regs.hl.h);
269     break;
270     case 0x35: // SLIA L
271       slia_byte(regs.hl.l);
272     break;
273     case 0x36: // SLIA (HL)
274       { unsigned char tmp;
275         tmp = get1(regs.HL);
276         slia_byte(tmp);
277         store1(regs.HL, tmp);
278       }
279     break;
280     case 0x37: // SLIA A
281       slia_byte(regs.A);
282     break;
283   }
284   return(resGO);
285 }
286
287 int
288 cl_z80::inst_cb_srl(t_mem code)
289 {
290   switch(code) {
291     case 0x38: // SRL B
292       srl_byte(regs.bc.h);
293     break;
294     case 0x39: // SRL C
295       srl_byte(regs.bc.l);
296     break;
297     case 0x3A: // SRL D
298       srl_byte(regs.de.h);
299     break;
300     case 0x3B: // SRL E
301       srl_byte(regs.de.l);
302     break;
303     case 0x3C: // SRL H
304       srl_byte(regs.hl.h);
305     break;
306     case 0x3D: // SRL L
307       srl_byte(regs.hl.l);
308     break;
309     case 0x3E: // SRL (HL)
310       { unsigned char tmp;
311         tmp = get1(regs.HL);
312         srl_byte(tmp);
313         store1(regs.HL, tmp);
314       }
315     break;
316     case 0x3F: // SRL A
317       srl_byte(regs.A);
318     break;
319   }
320   return(resGO);
321 }
322
323
324 int
325 cl_z80::inst_cb_bit(t_mem code)
326 {
327 #define bit_bitnum ((code >> 3) & 7)
328
329   switch(code & 7) {
330     case 0x0: // BIT x,B
331       bit_byte(regs.bc.h, bit_bitnum); break;
332     case 0x1: // BIT x,C
333       bit_byte(regs.bc.l, bit_bitnum); break;
334     case 0x2: // BIT x,D
335       bit_byte(regs.de.h, bit_bitnum); break;
336     case 0x3: // BIT x,E
337       bit_byte(regs.de.l, bit_bitnum); break;
338     case 0x4: // BIT x,H
339       bit_byte(regs.hl.h, bit_bitnum); break;
340     case 0x5: // BIT x,L
341       bit_byte(regs.hl.l, bit_bitnum); break;
342     case 0x6: // BIT x,(HL)
343       { unsigned char tmp;
344         tmp = get1(regs.HL);
345         bit_byte(tmp, bit_bitnum);
346         store1(regs.HL, tmp);
347       }
348     break;
349     case 0x7: // BIT x,A
350       bit_byte(regs.A, bit_bitnum); break;
351     break;
352   }
353   return(resGO);
354 }
355
356 int
357 cl_z80::inst_cb_res(t_mem code)
358 {
359 #define bit_bitnum ((code >> 3) & 7)
360
361   switch(code & 0x7) {
362     case 0x0: // RES x,B
363       regs.bc.h &= ~(1 << bit_bitnum); break;
364     case 0x1: // RES x,C
365       regs.bc.l &= ~(1 << bit_bitnum); break;
366     case 0x2: // RES x,D
367       regs.de.h &= ~(1 << bit_bitnum); break;
368     case 0x3: // RES x,E
369       regs.de.l &= ~(1 << bit_bitnum); break;
370     case 0x4: // RES x,H
371       regs.hl.h &= ~(1 << bit_bitnum); break;
372     case 0x5: // RES x,L
373       regs.hl.l &= ~(1 << bit_bitnum); break;
374     case 0x6: // RES x,(HL)
375       { unsigned char tmp;
376         tmp = get1(regs.HL);
377         tmp &= ~(1 << bit_bitnum);
378         store1(regs.HL, tmp);
379       }
380     break;
381     case 0x7: // RES x,A
382       regs.A &= ~(bit_bitnum); break;
383   }
384   return(resGO);
385 }
386
387 int
388 cl_z80::inst_cb_set(t_mem code)
389 {
390 #define bit_bitnum ((code >> 3) & 7)
391
392   switch(code) {
393     case 0x0: // SET x,B
394       regs.bc.h |= (1 << bit_bitnum); break;
395     case 0x1: // SET x,C
396       regs.bc.l |= (1 << bit_bitnum); break;
397     case 0x2: // SET x,D
398       regs.de.h |= (1 << bit_bitnum); break;
399     case 0x3: // SET x,E
400       regs.de.l |= (1 << bit_bitnum); break;
401     case 0x4: // SET x,H
402       regs.hl.h |= (1 << bit_bitnum); break;
403     case 0x5: // SET x,L
404       regs.hl.h |= (1 << bit_bitnum); break;
405     case 0x6: // SET x,(HL)
406       { unsigned char tmp;
407         tmp = get1(regs.HL);
408         tmp |= (1 << bit_bitnum);
409         store1(regs.HL, tmp);
410       }
411     break;
412     case 0x7: // SET x,A
413       regs.de.h |= (1 << bit_bitnum); break;
414   }
415   return(resGO);
416 }
417
418 /******** start CB codes *****************/
419 int
420 cl_z80::inst_cb(void)
421 {
422   t_mem code;
423
424   if (fetch(&code))
425     return(resBREAKPOINT);
426   tick(1);
427   switch (code)
428     {
429     case 0x00: // RLC B
430     case 0x01: // RLC C
431     case 0x02: // RLC D
432     case 0x03: // RLC E
433     case 0x04: // RLC H
434     case 0x05: // RLC L
435     case 0x06: // RLC (HL)
436     case 0x07: // RLC A
437       return (inst_cb_rlc(code));
438     case 0x08: // RRC B
439     case 0x09: // RRC C
440     case 0x0A: // RRC D
441     case 0x0B: // RRC E
442     case 0x0C: // RRC H
443     case 0x0D: // RRC L
444     case 0x0E: // RRC (HL)
445     case 0x0F: // RRC A
446       return (inst_cb_rrc(code));
447     case 0x10: // RL B
448     case 0x11: // RL C
449     case 0x12: // RL D
450     case 0x13: // RL E
451     case 0x14: // RL H
452     case 0x15: // RL L
453     case 0x16: // RL (HL)
454     case 0x17: // RL A
455       return (inst_cb_rl(code));
456     case 0x18: // RR B
457     case 0x19: // RR C
458     case 0x1A: // RR D
459     case 0x1B: // RR E
460     case 0x1C: // RR H
461     case 0x1D: // RR L
462     case 0x1E: // RR (HL)
463     case 0x1F: // RR A
464       return (inst_cb_rr(code));
465     case 0x20: // SLA B
466     case 0x21: // SLA C
467     case 0x22: // SLA D
468     case 0x23: // SLA E
469     case 0x24: // SLA H
470     case 0x25: // SLA L
471     case 0x26: // SLA (HL)
472     case 0x27: // SLA A
473       return (inst_cb_sla(code));
474     case 0x28: // SRA B
475     case 0x29: // SRA C
476     case 0x2A: // SRA D
477     case 0x2B: // SRA E
478     case 0x2C: // SRA H
479     case 0x2D: // SRA L
480     case 0x2E: // SRA (HL)
481     case 0x2F: // SRA A
482       return (inst_cb_sra(code));
483     case 0x30: // SLIA B        (Shift Left Inverted Arithmetic)
484     case 0x31: // SLIA C        like SLA, but shifts in a 1 bit
485     case 0x32: // SLIA D
486     case 0x33: // SLIA E
487     case 0x34: // SLIA H
488     case 0x35: // SLIA L
489     case 0x36: // SLIA (HL)
490     case 0x37: // SLIA A
491       return (inst_cb_slia(code));
492     case 0x38: // SRL B
493     case 0x39: // SRL C
494     case 0x3A: // SRL D
495     case 0x3B: // SRL E
496     case 0x3C: // SRL H
497     case 0x3D: // SRL L
498     case 0x3E: // SRL (HL)
499     case 0x3F: // SRL A
500       return (inst_cb_srl(code));
501     case 0x40: // BIT 0,B
502     case 0x41: // BIT 0,C
503     case 0x42: // BIT 0,D
504     case 0x43: // BIT 0,E
505     case 0x44: // BIT 0,H
506     case 0x45: // BIT 0,L
507     case 0x46: // BIT 0,(HL)
508     case 0x47: // BIT 0,A
509     case 0x48: // BIT 1,B
510     case 0x49: // BIT 1,C
511     case 0x4A: // BIT 1,D
512     case 0x4B: // BIT 1,E
513     case 0x4C: // BIT 1,H
514     case 0x4D: // BIT 1,L
515     case 0x4E: // BIT 1,(HL)
516     case 0x4F: // BIT 1,A
517     case 0x50: // BIT 2,B
518     case 0x51: // BIT 2,C
519     case 0x52: // BIT 2,D
520     case 0x53: // BIT 2,E
521     case 0x54: // BIT 2,H
522     case 0x55: // BIT 2,L
523     case 0x56: // BIT 2,(HL)
524     case 0x57: // BIT 2,A
525     case 0x58: // BIT 3,B
526     case 0x59: // BIT 3,C
527     case 0x5A: // BIT 3,D
528     case 0x5B: // BIT 3,E
529     case 0x5C: // BIT 3,H
530     case 0x5D: // BIT 3,L
531     case 0x5E: // BIT 3,(HL)
532     case 0x5F: // BIT 3,A
533     case 0x60: // BIT 4,B
534     case 0x61: // BIT 4,C
535     case 0x62: // BIT 4,D
536     case 0x63: // BIT 4,E
537     case 0x64: // BIT 4,H
538     case 0x65: // BIT 4,L
539     case 0x66: // BIT 4,(HL)
540     case 0x67: // BIT 4,A
541     case 0x68: // BIT 5,B
542     case 0x69: // BIT 5,C
543     case 0x6A: // BIT 5,D
544     case 0x6B: // BIT 5,E
545     case 0x6C: // BIT 5,H
546     case 0x6D: // BIT 5,L
547     case 0x6E: // BIT 5,(HL)
548     case 0x6F: // BIT 5,A
549     case 0x70: // BIT 6,B
550     case 0x71: // BIT 6,C
551     case 0x72: // BIT 6,D
552     case 0x73: // BIT 6,E
553     case 0x74: // BIT 6,H
554     case 0x75: // BIT 6,L
555     case 0x76: // BIT 6,(HL)
556     case 0x77: // BIT 6,A
557     case 0x78: // BIT 7,B
558     case 0x79: // BIT 7,C
559     case 0x7A: // BIT 7,D
560     case 0x7B: // BIT 7,E
561     case 0x7C: // BIT 7,H
562     case 0x7D: // BIT 7,L
563     case 0x7E: // BIT 7,(HL)
564     case 0x7F: // BIT 7,A
565       return (inst_cb_bit(code));
566     case 0x80: // RES 0,B
567     case 0x81: // RES 0,C
568     case 0x82: // RES 0,D
569     case 0x83: // RES 0,E
570     case 0x84: // RES 0,H
571     case 0x85: // RES 0,L
572     case 0x86: // RES 0,(HL)
573     case 0x87: // RES 0,A
574     case 0x88: // RES 1,B
575     case 0x89: // RES 1,C
576     case 0x8A: // RES 1,D
577     case 0x8B: // RES 1,E
578     case 0x8C: // RES 1,H
579     case 0x8D: // RES 1,L
580     case 0x8E: // RES 1,(HL)
581     case 0x8F: // RES 1,A
582     case 0x90: // RES 2,B
583     case 0x91: // RES 2,C
584     case 0x92: // RES 2,D
585     case 0x93: // RES 2,E
586     case 0x94: // RES 2,H
587     case 0x95: // RES 2,L
588     case 0x96: // RES 2,(HL)
589     case 0x97: // RES 2,A
590     case 0x98: // RES 3,B
591     case 0x99: // RES 3,C
592     case 0x9A: // RES 3,D
593     case 0x9B: // RES 3,E
594     case 0x9C: // RES 3,H
595     case 0x9D: // RES 3,L
596     case 0x9E: // RES 3,(HL)
597     case 0x9F: // RES 3,A
598     case 0xA0: // RES 4,B
599     case 0xA1: // RES 4,C
600     case 0xA2: // RES 4,D
601     case 0xA3: // RES 4,E
602     case 0xA4: // RES 4,H
603     case 0xA5: // RES 4,L
604     case 0xA6: // RES 4,(HL)
605     case 0xA7: // RES 4,A
606     case 0xA8: // RES 5,B
607     case 0xA9: // RES 5,C
608     case 0xAA: // RES 5,D
609     case 0xAB: // RES 5,E
610     case 0xAC: // RES 5,H
611     case 0xAD: // RES 5,L
612     case 0xAE: // RES 5,(HL)
613     case 0xAF: // RES 5,A
614     case 0xB0: // RES 6,B
615     case 0xB1: // RES 6,C
616     case 0xB2: // RES 6,D
617     case 0xB3: // RES 6,E
618     case 0xB4: // RES 6,H
619     case 0xB5: // RES 6,L
620     case 0xB6: // RES 6,(HL)
621     case 0xB7: // RES 6,A
622     case 0xB8: // RES 7,B
623     case 0xB9: // RES 7,C
624     case 0xBA: // RES 7,D
625     case 0xBB: // RES 7,E
626     case 0xBC: // RES 7,H
627     case 0xBD: // RES 7,L
628     case 0xBE: // RES 7,(HL)
629     case 0xBF: // RES 7,A
630       return (inst_cb_res(code));
631     case 0xC0: // SET 0,B
632     case 0xC1: // SET 0,C
633     case 0xC2: // SET 0,D
634     case 0xC3: // SET 0,E
635     case 0xC4: // SET 0,H
636     case 0xC5: // SET 0,L
637     case 0xC6: // SET 0,(HL)
638     case 0xC7: // SET 0,A
639     case 0xC8: // SET 1,B
640     case 0xC9: // SET 1,C
641     case 0xCA: // SET 1,D
642     case 0xCB: // SET 1,E
643     case 0xCC: // SET 1,H
644     case 0xCD: // SET 1,L
645     case 0xCE: // SET 1,(HL)
646     case 0xCF: // SET 1,A
647     case 0xD0: // SET 2,B
648     case 0xD1: // SET 2,C
649     case 0xD2: // SET 2,D
650     case 0xD3: // SET 2,E
651     case 0xD4: // SET 2,H
652     case 0xD5: // SET 2,L
653     case 0xD6: // SET 2,(HL)
654     case 0xD7: // SET 2,A
655     case 0xD8: // SET 3,B
656     case 0xD9: // SET 3,C
657     case 0xDA: // SET 3,D
658     case 0xDB: // SET 3,E
659     case 0xDC: // SET 3,H
660     case 0xDD: // SET 3,L
661     case 0xDE: // SET 3,(HL)
662     case 0xDF: // SET 3,A
663     case 0xE0: // SET 4,B
664     case 0xE1: // SET 4,C
665     case 0xE2: // SET 4,D
666     case 0xE3: // SET 4,E
667     case 0xE4: // SET 4,H
668     case 0xE5: // SET 4,L
669     case 0xE6: // SET 4,(HL)
670     case 0xE7: // SET 4,A
671     case 0xE8: // SET 5,B
672     case 0xE9: // SET 5,C
673     case 0xEA: // SET 5,D
674     case 0xEB: // SET 5,E
675     case 0xEC: // SET 5,H
676     case 0xED: // SET 5,L
677     case 0xEE: // SET 5,(HL)
678     case 0xEF: // SET 5,A
679     case 0xF0: // SET 6,B
680     case 0xF1: // SET 6,C
681     case 0xF2: // SET 6,D
682     case 0xF3: // SET 6,E
683     case 0xF4: // SET 6,H
684     case 0xF5: // SET 6,L
685     case 0xF6: // SET 6,(HL)
686     case 0xF7: // SET 6,A
687     case 0xF8: // SET 7,B
688     case 0xF9: // SET 7,C
689     case 0xFA: // SET 7,D
690     case 0xFB: // SET 7,E
691     case 0xFC: // SET 7,H
692     case 0xFD: // SET 7,L
693     case 0xFE: // SET 7,(HL)
694     case 0xFF: // SET 7,A
695       return (inst_cb_set(code));
696     }
697   /*if (PC)
698     PC--;
699   else
700   PC= get_mem_size(MEM_ROM_ID)-1;*/
701   PC= rom->inc_address(PC, -1);
702   return(resINV_INST);
703 }
704
705 /* End of z80.src/inst_cb.cc */