]> git.gag.com Git - fw/sdcc/blob - sim/ucsim/s51.src/uc390.cc
38531068757b67f78bcf2ff6d7c62d95d327e1da
[fw/sdcc] / sim / ucsim / s51.src / uc390.cc
1 /*
2  * Simulator of microcontrollers (uc390.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  * uc390.cc - module created by Karl Bongers 2001, karl@turbobit.com
9  */
10
11 /* This file is part of microcontroller simulator: ucsim.
12
13 UCSIM is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 UCSIM is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with UCSIM; see the file COPYING.  If not, write to the Free
25 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 02111-1307, USA. */
27 /*@1@*/
28
29 #include "ddconfig.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include "i_string.h"
34
35 #include "glob.h"
36 #include "uc390cl.h"
37 #include "regs51.h"
38
39 /*
40  * Names of instructions
41  */
42  
43 struct dis_entry disass_390f[]= {
44   { 0x00, 0xff, ' ', 1, "NOP"},
45    { 0x01, 0xff, 'A', 3, "AJMP %A"},
46    { 0x02, 0xff, 'L', 4, "LJMP %l"},
47   { 0x03, 0xff, ' ', 1, "RR A"},
48   { 0x04, 0xff, ' ', 1, "INC A"},
49   { 0x05, 0xff, ' ', 2, "INC %a"},
50   { 0x06, 0xff, ' ', 1, "INC @R0"},
51   { 0x07, 0xff, ' ', 1, "INC @R1"},
52   { 0x08, 0xff, ' ', 1, "INC R0"},
53   { 0x09, 0xff, ' ', 1, "INC R1"},
54   { 0x0a, 0xff, ' ', 1, "INC R2"},
55   { 0x0b, 0xff, ' ', 1, "INC R3"},
56   { 0x0c, 0xff, ' ', 1, "INC R4"},
57   { 0x0d, 0xff, ' ', 1, "INC R5"},
58   { 0x0e, 0xff, ' ', 1, "INC R6"},
59   { 0x0f, 0xff, ' ', 1, "INC R7"},
60   { 0x10, 0xff, 'R', 3, "JBC %b,%R"},
61    { 0x11, 0xff, 'a', 3, "ACALL %A"},
62    { 0x12, 0xff, 'l', 4, "LCALL %l"},
63   { 0x13, 0xff, ' ', 1, "RRC A"},
64   { 0x14, 0xff, ' ', 1, "DEC A"},
65   { 0x15, 0xff, ' ', 2, "DEC %a"},
66   { 0x16, 0xff, ' ', 1, "DEC @R0"},
67   { 0x17, 0xff, ' ', 1, "DEC @R1"},
68   { 0x18, 0xff, ' ', 1, "DEC R0"},
69   { 0x19, 0xff, ' ', 1, "DEC R1"},
70   { 0x1a, 0xff, ' ', 1, "DEC R2"},
71   { 0x1b, 0xff, ' ', 1, "DEC R3"},
72   { 0x1c, 0xff, ' ', 1, "DEC R4"},
73   { 0x1d, 0xff, ' ', 1, "DEC R5"},
74   { 0x1e, 0xff, ' ', 1, "DEC R6"},
75   { 0x1f, 0xff, ' ', 1, "DEC R7"},
76   { 0x20, 0xff, 'R', 3, "JB %b,%R"},
77    { 0x21, 0xff, 'A', 3, "AJMP %A"},
78   { 0x22, 0xff, '_', 1, "RET"},
79   { 0x23, 0xff, ' ', 1, "RL A"},
80   { 0x24, 0xff, ' ', 2, "ADD A,#%d"},
81   { 0x25, 0xff, ' ', 2, "ADD A,%a"},
82   { 0x26, 0xff, ' ', 1, "ADD A,@R0"},
83   { 0x27, 0xff, ' ', 1, "ADD A,@R1"},
84   { 0x28, 0xff, ' ', 1, "ADD A,R0"},
85   { 0x29, 0xff, ' ', 1, "ADD A,R1"},
86   { 0x2a, 0xff, ' ', 1, "ADD A,R2"},
87   { 0x2b, 0xff, ' ', 1, "ADD A,R3"},
88   { 0x2c, 0xff, ' ', 1, "ADD A,R4"},
89   { 0x2d, 0xff, ' ', 1, "ADD A,R5"},
90   { 0x2e, 0xff, ' ', 1, "ADD A,R6"},
91   { 0x2f, 0xff, ' ', 1, "ADD A,R7"},
92   { 0x30, 0xff, 'R', 3, "JNB %b,%R"},
93    { 0x31, 0xff, 'a', 3, "ACALL %A"},
94   { 0x32, 0xff, '_', 1, "RETI"},
95   { 0x33, 0xff, ' ', 1, "RLC A"},
96   { 0x34, 0xff, ' ', 2, "ADDC A,#%d"},
97   { 0x35, 0xff, ' ', 2, "ADDC A,%a"},
98   { 0x36, 0xff, ' ', 1, "ADDC A,@R0"},
99   { 0x37, 0xff, ' ', 1, "ADDC A,@R1"},
100   { 0x38, 0xff, ' ', 1, "ADDC A,R0"},
101   { 0x39, 0xff, ' ', 1, "ADDC A,R1"},
102   { 0x3a, 0xff, ' ', 1, "ADDC A,R2"},
103   { 0x3b, 0xff, ' ', 1, "ADDC A,R3"},
104   { 0x3c, 0xff, ' ', 1, "ADDC A,R4"},
105   { 0x3d, 0xff, ' ', 1, "ADDC A,R5"},
106   { 0x3e, 0xff, ' ', 1, "ADDC A,R6"},
107   { 0x3f, 0xff, ' ', 1, "ADDC A,R7"},
108   { 0x40, 0xff, 'r', 2, "JC %r"},
109    { 0x41, 0xff, 'A', 3, "AJMP %A"},
110   { 0x42, 0xff, ' ', 2, "ORL %a,A"},
111   { 0x43, 0xff, ' ', 3, "ORL %a,#%D"},
112   { 0x44, 0xff, ' ', 2, "ORL A,#%d"},
113   { 0x45, 0xff, ' ', 2, "ORL A,%a"},
114   { 0x46, 0xff, ' ', 1, "ORL A,@R0"},
115   { 0x47, 0xff, ' ', 1, "ORL A,@R1"},
116   { 0x48, 0xff, ' ', 1, "ORL A,R0"},
117   { 0x49, 0xff, ' ', 1, "ORL A,R1"},
118   { 0x4a, 0xff, ' ', 1, "ORL A,R2"},
119   { 0x4b, 0xff, ' ', 1, "ORL A,R3"},
120   { 0x4c, 0xff, ' ', 1, "ORL A,R4"},
121   { 0x4d, 0xff, ' ', 1, "ORL A,R5"},
122   { 0x4e, 0xff, ' ', 1, "ORL A,R6"},
123   { 0x4f, 0xff, ' ', 1, "ORL A,R7"},
124   { 0x50, 0xff, 'r', 2, "JNC %r"},
125    { 0x51, 0xff, 'a', 3, "ACALL %A"},
126   { 0x52, 0xff, ' ', 2, "ANL %a,A"},
127   { 0x53, 0xff, ' ', 3, "ANL %a,#%D"},
128   { 0x54, 0xff, ' ', 2, "ANL A,#%d"},
129   { 0x55, 0xff, ' ', 2, "ANL A,%a"},
130   { 0x56, 0xff, ' ', 1, "ANL A,@R0"},
131   { 0x57, 0xff, ' ', 1, "ANL A,@R1"},
132   { 0x58, 0xff, ' ', 1, "ANL A,R0"},
133   { 0x59, 0xff, ' ', 1, "ANL A,R1"},
134   { 0x5a, 0xff, ' ', 1, "ANL A,R2"},
135   { 0x5b, 0xff, ' ', 1, "ANL A,R3"},
136   { 0x5c, 0xff, ' ', 1, "ANL A,R4"},
137   { 0x5d, 0xff, ' ', 1, "ANL A,R5"},
138   { 0x5e, 0xff, ' ', 1, "ANL A,R6"},
139   { 0x5f, 0xff, ' ', 1, "ANL A,R7"},
140   { 0x60, 0xff, 'r', 2, "JZ %r"},
141    { 0x61, 0xff, 'A', 3, "AJMP %A"},
142   { 0x62, 0xff, ' ', 2, "XRL %a,A"},
143   { 0x63, 0xff, ' ', 3, "XRL %a,#%D"},
144   { 0x64, 0xff, ' ', 2, "XRL A,#%d"},
145   { 0x65, 0xff, ' ', 2, "XRL A,%a"},
146   { 0x66, 0xff, ' ', 1, "XRL A,@R0"},
147   { 0x67, 0xff, ' ', 1, "XRL A,@R1"},
148   { 0x68, 0xff, ' ', 1, "XRL A,R0"},
149   { 0x69, 0xff, ' ', 1, "XRL A,R1"},
150   { 0x6a, 0xff, ' ', 1, "XRL A,R2"},
151   { 0x6b, 0xff, ' ', 1, "XRL A,R3"},
152   { 0x6c, 0xff, ' ', 1, "XRL A,R4"},
153   { 0x6d, 0xff, ' ', 1, "XRL A,R5"},
154   { 0x6e, 0xff, ' ', 1, "XRL A,R6"},
155   { 0x6f, 0xff, ' ', 1, "XRL A,R7"},
156   { 0x70, 0xff, 'r', 2, "JNZ %r"},
157    { 0x71, 0xff, 'a', 3, "ACALL %A"},
158   { 0x72, 0xff, ' ', 2, "ORL C,%b"},
159   { 0x73, 0xff, '_', 1, "JMP @A+DPTR"},
160   { 0x74, 0xff, ' ', 2, "MOV A,#%d"},
161   { 0x75, 0xff, ' ', 3, "MOV %a,#%D"},
162   { 0x76, 0xff, ' ', 2, "MOV @R0,#%d"},
163   { 0x77, 0xff, ' ', 2, "MOV @R1,#%d"},
164   { 0x78, 0xff, ' ', 2, "MOV R0,#%d"},
165   { 0x79, 0xff, ' ', 2, "MOV R1,#%d"},
166   { 0x7a, 0xff, ' ', 2, "MOV R2,#%d"},
167   { 0x7b, 0xff, ' ', 2, "MOV R3,#%d"},
168   { 0x7c, 0xff, ' ', 2, "MOV R4,#%d"},
169   { 0x7d, 0xff, ' ', 2, "MOV R5,#%d"},
170   { 0x7e, 0xff, ' ', 2, "MOV R6,#%d"},
171   { 0x7f, 0xff, ' ', 2, "MOV R7,#%d"},
172   { 0x80, 0xff, 's', 2, "SJMP %r"},
173    { 0x81, 0xff, 'A', 3, "AJMP %A"},
174   { 0x82, 0xff, ' ', 2, "ANL C,%b"},
175   { 0x83, 0xff, ' ', 1, "MOVC A,@A+PC"},
176   { 0x84, 0xff, ' ', 1, "DIV AB"},
177   { 0x85, 0xff, ' ', 3, "MOV %8,%a"},
178   { 0x86, 0xff, ' ', 2, "MOV %a,@R0"},
179   { 0x87, 0xff, ' ', 2, "MOV %a,@R1"},
180   { 0x88, 0xff, ' ', 2, "MOV %a,R0"},
181   { 0x89, 0xff, ' ', 2, "MOV %a,R1"},
182   { 0x8a, 0xff, ' ', 2, "MOV %a,R2"},
183   { 0x8b, 0xff, ' ', 2, "MOV %a,R3"},
184   { 0x8c, 0xff, ' ', 2, "MOV %a,R4"},
185   { 0x8d, 0xff, ' ', 2, "MOV %a,R5"},
186   { 0x8e, 0xff, ' ', 2, "MOV %a,R6"},
187   { 0x8f, 0xff, ' ', 2, "MOV %a,R7"},
188    { 0x90, 0xff, ' ', 4, "MOV DPTR,#%6"},
189    { 0x91, 0xff, 'a', 3, "ACALL %A"},
190   { 0x92, 0xff, ' ', 2, "MOV %b,C"},
191   { 0x93, 0xff, ' ', 1, "MOVC A,@A+DPTR"},
192   { 0x94, 0xff, ' ', 2, "SUBB A,#%d"},
193   { 0x95, 0xff, ' ', 2, "SUBB A,%a"},
194   { 0x96, 0xff, ' ', 1, "SUBB A,@R0"},
195   { 0x97, 0xff, ' ', 1, "SUBB A,@R1"},
196   { 0x98, 0xff, ' ', 1, "SUBB A,R0"},
197   { 0x99, 0xff, ' ', 1, "SUBB A,R1"},
198   { 0x9a, 0xff, ' ', 1, "SUBB A,R2"},
199   { 0x9b, 0xff, ' ', 1, "SUBB A,R3"},
200   { 0x9c, 0xff, ' ', 1, "SUBB A,R4"},
201   { 0x9d, 0xff, ' ', 1, "SUBB A,R5"},
202   { 0x9e, 0xff, ' ', 1, "SUBB A,R6"},
203   { 0x9f, 0xff, ' ', 1, "SUBB A,R7"},
204   { 0xa0, 0xff, ' ', 2, "ORL C,/%b"},
205    { 0xa1, 0xff, 'A', 3, "AJMP %A"},
206   { 0xa2, 0xff, ' ', 2, "MOV C,%b"},
207   { 0xa3, 0xff, ' ', 1, "INC DPTR"},
208   { 0xa4, 0xff, ' ', 1, "MUL AB"},
209   { 0xa5, 0xff, '_', 1, "-"},
210   { 0xa6, 0xff, ' ', 2, "MOV @R0,%a"},
211   { 0xa7, 0xff, ' ', 2, "MOV @R1,%a"},
212   { 0xa8, 0xff, ' ', 2, "MOV R0,%a"},
213   { 0xa9, 0xff, ' ', 2, "MOV R1,%a"},
214   { 0xaa, 0xff, ' ', 2, "MOV R2,%a"},
215   { 0xab, 0xff, ' ', 2, "MOV R3,%a"},
216   { 0xac, 0xff, ' ', 2, "MOV R4,%a"},
217   { 0xad, 0xff, ' ', 2, "MOV R5,%a"},
218   { 0xae, 0xff, ' ', 2, "MOV R6,%a"},
219   { 0xaf, 0xff, ' ', 2, "MOV R7,%a"},
220   { 0xb0, 0xff, ' ', 2, "ANL C,/%b"},
221    { 0xb1, 0xff, 'a', 3, "ACALL %A"},
222   { 0xb2, 0xff, ' ', 2, "CPL %b"},
223   { 0xb3, 0xff, ' ', 1, "CPL C"},
224   { 0xb4, 0xff, 'R', 3, "CJNE A,#%d,%R"},
225   { 0xb5, 0xff, 'R', 3, "CJNE A,%a,%R"},
226   { 0xb6, 0xff, 'R', 3, "CJNE @R0,#%d,%R"},
227   { 0xb7, 0xff, 'R', 3, "CJNE @R1,#%d,%R"},
228   { 0xb8, 0xff, 'R', 3, "CJNE R0,#%d,%R"},
229   { 0xb9, 0xff, 'R', 3, "CJNE R1,#%d,%R"},
230   { 0xba, 0xff, 'R', 3, "CJNE R2,#%d,%R"},
231   { 0xbb, 0xff, 'R', 3, "CJNE R3,#%d,%R"},
232   { 0xbc, 0xff, 'R', 3, "CJNE R4,#%d,%R"},
233   { 0xbd, 0xff, 'R', 3, "CJNE R5,#%d,%R"},
234   { 0xbe, 0xff, 'R', 3, "CJNE R6,#%d,%R"},
235   { 0xbf, 0xff, 'R', 3, "CJNE R7,#%d,%R"},
236   { 0xc0, 0xff, ' ', 2, "PUSH %a"},
237    { 0xc1, 0xff, 'A', 3, "AJMP %A"},
238   { 0xc2, 0xff, ' ', 2, "CLR %b"},
239   { 0xc3, 0xff, ' ', 1, "CLR C"},
240   { 0xc4, 0xff, ' ', 1, "SWAP A"},
241   { 0xc5, 0xff, ' ', 2, "XCH A,%a"},
242   { 0xc6, 0xff, ' ', 1, "XCH A,@R0"},
243   { 0xc7, 0xff, ' ', 1, "XCH A,@R1"},
244   { 0xc8, 0xff, ' ', 1, "XCH A,R0"},
245   { 0xc9, 0xff, ' ', 1, "XCH A,R1"},
246   { 0xca, 0xff, ' ', 1, "XCH A,R2"},
247   { 0xcb, 0xff, ' ', 1, "XCH A,R3"},
248   { 0xcc, 0xff, ' ', 1, "XCH A,R4"},
249   { 0xcd, 0xff, ' ', 1, "XCH A,R5"},
250   { 0xce, 0xff, ' ', 1, "XCH A,R6"},
251   { 0xcf, 0xff, ' ', 1, "XCH A,R7"},
252   { 0xd0, 0xff, ' ', 2, "POP %a"},
253    { 0xd1, 0xff, 'a', 3, "ACALL %A"},
254   { 0xd2, 0xff, ' ', 2, "SETB %b"},
255   { 0xd3, 0xff, ' ', 1, "SETB C"},
256   { 0xd4, 0xff, ' ', 1, "DA A"},
257   { 0xd5, 0xff, 'R', 3, "DJNZ %a,%R"},
258   { 0xd6, 0xff, ' ', 1, "XCHD A,@R0"},
259   { 0xd7, 0xff, ' ', 1, "XCHD A,@R1"},
260   { 0xd8, 0xff, 'r', 2, "DJNZ R0,%r"},
261   { 0xd9, 0xff, 'r', 2, "DJNZ R1,%r"},
262   { 0xda, 0xff, 'r', 2, "DJNZ R2,%r"},
263   { 0xdb, 0xff, 'r', 2, "DJNZ R3,%r"},
264   { 0xdc, 0xff, 'r', 2, "DJNZ R4,%r"},
265   { 0xdd, 0xff, 'r', 2, "DJNZ R5,%r"},
266   { 0xde, 0xff, 'r', 2, "DJNZ R6,%r"},
267   { 0xdf, 0xff, 'r', 2, "DJNZ R7,%r"},
268   { 0xe0, 0xff, ' ', 1, "MOVX A,@DPTR"},
269    { 0xe1, 0xff, 'A', 3, "AJMP %A"},
270   { 0xe2, 0xff, ' ', 1, "MOVX A,@R0"},
271   { 0xe3, 0xff, ' ', 1, "MOVX A,@R1"},
272   { 0xe4, 0xff, ' ', 1, "CLR A"},
273   { 0xe5, 0xff, ' ', 2, "MOV A,%a"},
274   { 0xe6, 0xff, ' ', 1, "MOV A,@R0"},
275   { 0xe7, 0xff, ' ', 1, "MOV A,@R1"},
276   { 0xe8, 0xff, ' ', 1, "MOV A,R0"},
277   { 0xe9, 0xff, ' ', 1, "MOV A,R1"},
278   { 0xea, 0xff, ' ', 1, "MOV A,R2"},
279   { 0xeb, 0xff, ' ', 1, "MOV A,R3"},
280   { 0xec, 0xff, ' ', 1, "MOV A,R4"},
281   { 0xed, 0xff, ' ', 1, "MOV A,R5"},
282   { 0xee, 0xff, ' ', 1, "MOV A,R6"},
283   { 0xef, 0xff, ' ', 1, "MOV A,R7"},
284   { 0xf0, 0xff, ' ', 1, "MOVX @DPTR,A"},
285    { 0xf1, 0xff, 'a', 3, "ACALL %A"},
286   { 0xf2, 0xff, ' ', 1, "MOVX @R0,A"},
287   { 0xf3, 0xff, ' ', 1, "MOVX @R1,A"},
288   { 0xf4, 0xff, ' ', 1, "CPL A"},
289   { 0xf5, 0xff, ' ', 2, "MOV %a,A"},
290   { 0xf6, 0xff, ' ', 1, "MOV @R0,A"},
291   { 0xf7, 0xff, ' ', 1, "MOV @R1,A"},
292   { 0xf8, 0xff, ' ', 1, "MOV R0,A"},
293   { 0xf9, 0xff, ' ', 1, "MOV R1,A"},
294   { 0xfa, 0xff, ' ', 1, "MOV R2,A"},
295   { 0xfb, 0xff, ' ', 1, "MOV R3,A"},
296   { 0xfc, 0xff, ' ', 1, "MOV R4,A"},
297   { 0xfd, 0xff, ' ', 1, "MOV R5,A"},
298   { 0xfe, 0xff, ' ', 1, "MOV R6,A"},
299   { 0xff, 0xff, ' ', 1, "MOV R7,A"},
300   { 0, 0, 0, 0, NULL }
301 };
302
303 /*
304  * Making an 390 CPU object
305  */
306
307 t_uc390::t_uc390(int Itype, int Itech, class cl_sim *asim):
308   t_uc52(Itype, Itech, asim)
309 {
310   if (Itype == CPU_DS390F) {
311     printf("FLAT24 MODE SET, warning: experimental code\n");
312     flat24_flag = 1;
313   }
314 }
315
316
317 /*
318  * 0x05 2 12 INC addr
319  *____________________________________________________________________________
320  *
321  */
322 int
323 t_uc390::inst_inc_addr(uchar code)
324 {
325   uchar *addr;
326
327   addr= get_direct(fetch(), &event_at.wi, &event_at.ws);
328
329   /* mask off the 2Hex bit adjacent to the 1H bit which selects
330      which DPTR we use.  This is a feature of 80C390.
331      You can do INC DPS and it only effects bit 1. */
332   if (code  == DPS)
333     (*addr) ^= 1;  /* just toggle */
334   else {
335     (*addr)++;
336   }
337
338   proc_write(addr);
339   return(resGO);
340 }
341
342 /*
343  * 0xa3 1 24 INC DPTR
344  *____________________________________________________________________________
345  *
346  */
347
348 int
349 t_uc390::inst_inc_dptr(uchar code)
350 {
351   uint dptr;
352
353   unsigned char pl,ph,dps;
354
355   dps = sfr->get(DPS);
356   if (dps & 1) {
357     pl = DPL1;
358     ph = DPH1;
359   } else {
360     pl = DPL;
361     ph = DPH;
362   }
363
364   if (dps & 1) { /* alternate DPTR */
365     if (dps & 0x80)  /* decr set */
366       dptr= sfr->get(ph)*256 + sfr->get(pl) - 1;
367     else
368       dptr= sfr->get(ph)*256 + sfr->get(pl) + 1;
369   } else {
370     if (dps & 0x40)  /* decr set */
371       dptr= sfr->get(ph)*256 + sfr->get(pl) - 1;
372     else
373       dptr= sfr->get(ph)*256 + sfr->get(pl) + 1;
374   }
375
376   sfr->set(event_at.ws= ph, (dptr >> 8) & 0xff);
377   sfr->set(pl, dptr & 0xff);
378
379   if (dps & 0x20) {                  /* auto-switch dptr */
380     sfr->set(DPS, (dps ^ 1));  /* toggle dual-dptr switch */
381   }
382   tick(1);
383   return(resGO);
384 }
385
386 /*
387  * 0x73 1 24 JMP @A+DPTR
388  *____________________________________________________________________________
389  *
390  */
391
392 int
393 t_uc390::inst_jmp_$a_dptr(uchar code)
394 {
395     unsigned char pl,ph,dps;
396
397     dps = sfr->get(DPS);
398     if (dps & 1) {
399       pl = DPL1;
400       ph = DPH1;
401     } else {
402       pl = DPL;
403       ph = DPH;
404     }
405
406     PC= (sfr->get(ph)*256 + sfr->get(pl) +
407          read_mem(MEM_SFR, ACC)) &
408       (EROM_SIZE - 1);
409
410   tick(1);
411   return(resGO);
412 }
413
414 /*
415  * 0x90 3 24 MOV DPTR,#data
416  *____________________________________________________________________________
417  *
418  */
419
420 int
421 t_uc390::inst_mov_dptr_$data(uchar code)
422 {
423     unsigned char pl,ph,dps;
424
425     dps = sfr->get(DPS);
426     if (dps & 1) {
427       pl = DPL1;
428       ph = DPH1;
429     } else {
430       pl = DPL;
431       ph = DPH;
432     }
433
434     //if ((sfr->get(ACON) & 0x3) == 2)
435     if (flat24_flag) {
436       fetch();  /* throw away msb of address for now */
437     }
438
439     sfr->set(event_at.ws= ph, fetch());
440     sfr->set(pl, fetch());
441
442     if (dps & 0x20) {                  /* auto-switch dptr */
443       sfr->set(DPS, (dps ^ 1));  /* toggle dual-dptr switch */
444     }
445
446   tick(1);
447   return(resGO);
448 }
449
450
451 /*
452  * 0x93 1 24 MOVC A,@A+DPTR
453  *____________________________________________________________________________
454  *
455  */
456
457 int
458 t_uc390::inst_movc_a_$a_dptr(uchar code)
459 {
460     unsigned char pl,ph,dps;
461
462     dps = sfr->get(DPS);
463     if (dps & 1) {
464       pl = DPL1;
465       ph = DPH1;
466     } else {
467       pl = DPL;
468       ph = DPH;
469     }
470
471     sfr->set(ACC, get_mem(MEM_ROM, event_at.rc=
472                                 (sfr->get(ph)*256+sfr->get(pl) +
473                                  sfr->get(ACC)) & (EROM_SIZE-1)));
474
475     if (dps & 0x20) {                  /* auto-switch dptr */
476       sfr->set(DPS, (dps ^ 1));  /* toggle dual-dptr switch */
477     }
478
479   tick(1);
480   return(resGO);
481 }
482
483 /*
484  * 0xe0 1 24 MOVX A,@DPTR
485  *____________________________________________________________________________
486  *
487  */
488
489 int
490 t_uc390::inst_movx_a_$dptr(uchar code)
491 {
492     unsigned char pl,ph,dps;
493
494     dps = sfr->get(DPS);
495     if (dps & 1) {
496       pl = DPL1;
497       ph = DPH1;
498     } else {
499       pl = DPL;
500       ph = DPH;
501     }
502
503     sfr->set(event_at.ws= ACC,
504            get_mem(MEM_XRAM, event_at.rx=sfr->get(ph)*256+sfr->get(pl)));
505
506     if (dps & 0x20) {                  /* auto-switch dptr */
507       sfr->set(DPS, (dps ^ 1));  /* toggle dual-dptr switch */
508     }
509   tick(1);
510   return(resGO);
511 }
512
513 /*
514  * 0xf0 1 24 MOVX @DPTR,A
515  *____________________________________________________________________________
516  *
517  */
518
519 int
520 t_uc390::inst_movx_$dptr_a(uchar code)
521 {
522     unsigned char pl,ph,dps;
523
524     dps = sfr->get(DPS);
525     if (dps & 1) {
526       pl = DPL1;
527       ph = DPH1;
528     } else {
529       pl = DPL;
530       ph = DPH;
531     }
532
533     set_mem(MEM_XRAM, event_at.wx= sfr->get(ph)*256+sfr->get(pl),
534           sfr->get(event_at.rs= ACC));
535
536     if (dps & 0x20) {                  /* auto-switch dptr */
537       sfr->set(DPS, (dps ^ 1));  /* toggle dual-dptr switch */
538     }
539   tick(1);
540   return(resGO);
541 }
542
543 /*
544  * 0x[02468ace]1 2 24 AJMP addr
545  *____________________________________________________________________________
546  *
547  */
548
549 int
550 t_uc390::inst_ajmp_addr(uchar code)
551 {
552   uchar h, l;
553
554   if (flat24_flag) {
555     /* throw away high address byte for now... */
556     h= (code >> 5) & 0x07;
557     h= fetch();
558     l= fetch();
559     PC= (PC & 0xf800) | (h*256 + l);
560   } else
561   {
562     h= (code >> 5) & 0x07;
563     l= fetch();
564     PC= (PC & 0xf800) | (h*256 + l);
565   }
566   tick(1);
567
568   return(resGO);
569 }
570
571 /*
572  * 0x02 3 24 LJMP addr
573  *____________________________________________________________________________
574  *
575  */
576
577 int
578 t_uc390::inst_ljmp(uchar code)
579 {
580   if (flat24_flag) {
581     fetch(); /* throw away msb of address for now */
582
583     PC= fetch()*256 + fetch();
584   } else {
585     PC= fetch()*256 + fetch();
586   }
587   tick(1);
588   return(resGO);
589 }
590
591 /*
592  * 0x[13579bdf]1 2 24 ACALL addr
593  *____________________________________________________________________________
594  *
595  */
596
597 int
598 t_uc390::inst_acall_addr(uchar code)
599 {
600   uchar h, l, *sp, *aof_SP;
601   int res;
602
603   if (flat24_flag) {
604     /* throw away msb of address for now */
605     h= (code >> 5) & 0x07;
606
607     h= fetch();
608     l= fetch();
609
610     aof_SP= &((sfr->umem8)[SP]);
611     //MEM(MEM_SFR)[SP]++;
612     (*aof_SP)++;
613     proc_write_sp(*aof_SP);
614     sp= get_indirect(*aof_SP/*sfr->get(SP)*/, &res);
615     if (res != resGO)
616       res= resSTACK_OV;
617     (*sp)= PC & 0xff; // push low byte
618
619     //MEM(MEM_SFR)[SP]++;
620     (*aof_SP)++;
621     proc_write_sp(*aof_SP);
622     sp= get_indirect(*aof_SP/*sfr->get(SP)*/, &res);
623     if (res != resGO)
624       res= resSTACK_OV;
625     (*sp)= (PC >> 8) & 0xff; // push high byte
626     PC= (PC & 0xf800) | (h*256 + l);
627
628     // need to push msb, skip for now...
629
630   } else {
631     /* stock mcs51 mode */
632     h= (code >> 5) & 0x07;
633     l= fetch();
634     aof_SP= &((sfr->umem8)[SP]);
635     //MEM(MEM_SFR)[SP]++;
636     (*aof_SP)++;
637     proc_write_sp(*aof_SP);
638     sp= get_indirect(*aof_SP/*sfr->get(SP)*/, &res);
639     if (res != resGO)
640       res= resSTACK_OV;
641     (*sp)= PC & 0xff; // push low byte
642
643     //MEM(MEM_SFR)[SP]++;
644     (*aof_SP)++;
645     proc_write_sp(*aof_SP);
646     sp= get_indirect(*aof_SP/*sfr->get(SP)*/, &res);
647     if (res != resGO)
648       res= resSTACK_OV;
649     (*sp)= (PC >> 8) & 0xff; // push high byte
650     PC= (PC & 0xf800) | (h*256 + l);
651   }
652
653   tick(1);
654   return(res);
655 }
656
657
658 /*
659  * 0x12 3 24 LCALL addr
660  *____________________________________________________________________________
661  *
662  */
663
664 int
665 t_uc390::inst_lcall(uchar code, uint addr)
666 {
667   uchar h= 0, l= 0, *sp, *aof_SP;
668   int res;
669
670   if (!addr)
671     {
672       /* this is a normal lcall */
673       if (flat24_flag) {
674         fetch();  /* drop for now */
675         h= fetch();
676         l= fetch();
677       } else {
678         h= fetch();
679         l= fetch();
680       }
681     }
682     /* else, this is interrupt processing */
683
684   aof_SP= &((sfr->umem8)[SP]);
685   //MEM(MEM_SFR)[SP]++;
686   (*aof_SP)++;
687   proc_write_sp(*aof_SP);
688   sp= get_indirect(*aof_SP/*sfr->get(SP)*/, &res);
689   if (res != resGO)
690     res= resSTACK_OV;
691   (*sp)= PC & 0xff; // push low byte
692   if (!addr)
693     tick(1);
694
695   //MEM(MEM_SFR)[SP]++;
696   (*aof_SP)++;
697   proc_write_sp(*aof_SP);
698   sp= get_indirect(*aof_SP/*sfr->get(SP)*/, &res);
699   if (res != resGO)
700     res= resSTACK_OV;
701   (*sp)= (PC >> 8) & 0xff; // push high byte
702   if (addr)
703     PC= addr;
704   else
705     PC= h*256 + l;
706
707
708   return(res);
709 }
710
711 /*
712  * 0x22 1 24 RET
713  *____________________________________________________________________________
714  *
715  */
716
717 int
718 t_uc390::inst_ret(uchar code)
719 {
720   uchar h, l, *sp, *aof_SP;
721   int res;
722
723   aof_SP= &((sfr->umem8)[SP]);
724   sp= get_indirect(*aof_SP/*sfr->get(SP)*/, &res);
725   if (res != resGO)
726     res= resSTACK_OV;
727   h= *sp;
728   //MEM(MEM_SFR)[SP]--;
729   (*aof_SP)--;
730   proc_write_sp(*aof_SP);
731   tick(1);
732
733   sp= get_indirect(*aof_SP/*sfr->get(SP)*/, &res);
734   if (res != resGO)
735     res= resSTACK_OV;
736   l= *sp;
737   //MEM(MEM_SFR)[SP]--;
738   (*aof_SP)--;
739   proc_write_sp(*aof_SP);
740   PC= h*256 + l;
741
742   if (flat24_flag) {
743     tick(1);
744   }
745
746   return(res);
747 }
748
749 /*
750  * 0x32 1 24 RETI
751  *____________________________________________________________________________
752  *
753  */
754
755 int
756 t_uc390::inst_reti(uchar code)
757 {
758   uchar h, l, *sp, *aof_SP;
759   int res;
760
761   aof_SP= &((sfr->umem8)[SP]);
762   sp= get_indirect(*aof_SP/*sfr->get(SP)*/, &res);
763   if (res != resGO)
764     res= resSTACK_OV;
765   h= *sp;
766   //MEM(MEM_SFR)[SP]--;
767   (*aof_SP)--;
768   proc_write_sp(*aof_SP);
769   tick(1);
770
771   sp= get_indirect(*aof_SP/*sfr->get(SP)*/, &res);
772   if (res != resGO)
773     res= resSTACK_OV;
774   l= *sp;
775   //MEM(MEM_SFR)[SP]--;
776   (*aof_SP)--;
777   proc_write_sp(*aof_SP);
778   PC= h*256 + l;
779
780   if (flat24_flag) {
781     tick(1);
782   }
783
784   was_reti= DD_TRUE;
785   class it_level *il= (class it_level *)(it_levels->top());
786   if (il &&
787       il->level >= 0)
788     {
789       il= (class it_level *)(it_levels->pop());
790       delete il;
791     }
792
793   return(res);
794 }
795
796 /*
797  * Disassembling an instruction
798  */
799
800 struct dis_entry *
801 t_uc390::dis_tbl(void)
802 {
803   if (!flat24_flag) {
804     return(disass_51);
805     //t_uc51::dis_tbl();
806   }
807
808   return(disass_390f);
809 }
810
811 char *
812 t_uc390::disass(t_addr addr, char *sep)
813 {
814   char work[256], temp[20], c[2];
815   char *buf, *p, *b, *t;
816   t_mem code;
817
818   if (!flat24_flag) {
819     t_uc51::disass(addr, sep);
820   }
821
822   code= get_mem(MEM_ROM, addr);
823   p= work;
824   b= dis_tbl()[code].mnemonic;
825   while (*b)
826     {
827       if (*b == '%')
828         {
829           b++;
830           switch (*(b++))
831             {
832             case 'A': // absolute address
833        // stock:
834               //sprintf(temp, "%04lx",
835                      // (addr&0xf800)|
836                      // (((code>>5)&0x07)*256 +
837                      //  get_mem(MEM_ROM, addr+1)));
838
839               sprintf(temp, "%06lx",
840                       (addr&0xf80000)|
841                       (((code>>5)&0x07)*(256*256) +
842                        (get_mem(MEM_ROM, addr+1) * 256) +
843                        get_mem(MEM_ROM, addr+2)));
844               break;
845             case 'l': // long address
846               sprintf(temp, "%06lx",
847                       get_mem(MEM_ROM, addr+1)*(256*256) + get_mem(MEM_ROM, addr+1)*256
848         + get_mem(MEM_ROM, addr+2));
849                       //get_mem(MEM_ROM, addr+1)*256 + get_mem(MEM_ROM, addr+2));
850               break;
851             case 'a': // addr8 (direct address) at 2nd byte
852               if (!get_name(get_mem(MEM_ROM, addr+1), sfr_tbl(), temp))
853                 sprintf(temp, "%02lx", get_mem(MEM_ROM, addr+1));
854               break;
855             case '8': // addr8 (direct address) at 3rd byte
856               if (!get_name(get_mem(MEM_ROM, addr+2), sfr_tbl(), temp))
857                 sprintf(temp, "%02lx", get_mem(MEM_ROM, addr+1));
858               sprintf(temp, "%02lx", get_mem(MEM_ROM, addr+2));
859               break;
860             case 'b': // bitaddr at 2nd byte
861               if (get_name(get_mem(MEM_ROM, addr+1), bit_tbl(), temp))
862                 break;
863               if (get_name(get_bitidx(get_mem(MEM_ROM, addr+1)),
864                            sfr_tbl(), temp))
865                 {
866                   strcat(temp, ".");
867                   sprintf(c, "%1ld", get_mem(MEM_ROM, addr+1)&0x07);
868                   strcat(temp, c);
869                   break;
870                 }
871               sprintf(temp, "%02x.%ld",
872                       get_bitidx(get_mem(MEM_ROM, addr+1)),
873                       get_mem(MEM_ROM, addr+1)&0x07);
874               break;
875             case 'r': // rel8 address at 2nd byte
876               sprintf(temp, "%04lx",
877                       addr+2+(signed char)(get_mem(MEM_ROM, addr+1)));
878               break;
879             case 'R': // rel8 address at 3rd byte
880               sprintf(temp, "%04lx",
881                       addr+3+(signed char)(get_mem(MEM_ROM, addr+2)));
882               break;
883             case 'd': // data8 at 2nd byte
884               sprintf(temp, "%02lx", get_mem(MEM_ROM, addr+1));
885               break;
886             case 'D': // data8 at 3rd byte
887               sprintf(temp, "%02lx", get_mem(MEM_ROM, addr+2));
888               break;
889             case '6': // data24 at 2nd(HH)-3rd(H)-4th(L) byte
890               sprintf(temp, "%06lx",
891                       get_mem(MEM_ROM, addr+1)*256*256 + get_mem(MEM_ROM, addr+2)*256 +
892                 get_mem(MEM_ROM, addr+3));
893             default:
894               strcpy(temp, "?");
895               break;
896             }
897           t= temp;
898           while (*t)
899             *(p++)= *(t++);
900         }
901       else
902         *(p++)= *(b++);
903     }
904   *p= '\0';
905
906   p= strchr(work, ' ');
907   if (!p)
908     {
909       buf= strdup(work);
910       return(buf);
911     }
912   if (sep == NULL)
913     buf= (char *)malloc(6+strlen(p)+1);
914   else
915     buf= (char *)malloc((p-work)+strlen(sep)+strlen(p)+1);
916   for (p= work, b= buf; *p != ' '; p++, b++)
917     *b= *p;
918   p++;
919   *b= '\0';
920   if (sep == NULL)
921     {
922       while (strlen(buf) < 6)
923         strcat(buf, " ");
924     }
925   else
926     strcat(buf, sep);
927   strcat(buf, p);
928   return(buf);
929 }
930
931 /* End of s51.src/uc390.cc */