e8e029feb5e3a0f4e7b88f0bbfbf9eb3a1429d82
[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
30 // Bernhard's ToDo list:
31
32 // - add sfr-descriptions to s51.src/glob.cc
33 // - implement math accelerator
34 // - consider ACON bits
35 // - proc_write_sp (*aof_SP); insert this at the appropriate places
36 // - buy some memory to run s51 with 2*4 Meg ROM/XRAM
37
38 #include "ddconfig.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <ctype.h>
43 #include "i_string.h"
44
45 #include "glob.h"
46 #include "uc390cl.h"
47 #include "regs51.h"
48
49 /*
50  * Names of instructions
51  */
52
53 struct dis_entry disass_390f[] = {
54   { 0x00, 0xff, ' ', 1, "NOP"},
55    { 0x01, 0xff, 'A', 3, "AJMP %A"},
56    { 0x02, 0xff, 'L', 4, "LJMP %l"},
57   { 0x03, 0xff, ' ', 1, "RR A"},
58   { 0x04, 0xff, ' ', 1, "INC A"},
59   { 0x05, 0xff, ' ', 2, "INC %a"},
60   { 0x06, 0xff, ' ', 1, "INC @R0"},
61   { 0x07, 0xff, ' ', 1, "INC @R1"},
62   { 0x08, 0xff, ' ', 1, "INC R0"},
63   { 0x09, 0xff, ' ', 1, "INC R1"},
64   { 0x0a, 0xff, ' ', 1, "INC R2"},
65   { 0x0b, 0xff, ' ', 1, "INC R3"},
66   { 0x0c, 0xff, ' ', 1, "INC R4"},
67   { 0x0d, 0xff, ' ', 1, "INC R5"},
68   { 0x0e, 0xff, ' ', 1, "INC R6"},
69   { 0x0f, 0xff, ' ', 1, "INC R7"},
70   { 0x10, 0xff, 'R', 3, "JBC %b,%R"},
71    { 0x11, 0xff, 'a', 3, "ACALL %A"},
72    { 0x12, 0xff, 'l', 4, "LCALL %l"},
73   { 0x13, 0xff, ' ', 1, "RRC A"},
74   { 0x14, 0xff, ' ', 1, "DEC A"},
75   { 0x15, 0xff, ' ', 2, "DEC %a"},
76   { 0x16, 0xff, ' ', 1, "DEC @R0"},
77   { 0x17, 0xff, ' ', 1, "DEC @R1"},
78   { 0x18, 0xff, ' ', 1, "DEC R0"},
79   { 0x19, 0xff, ' ', 1, "DEC R1"},
80   { 0x1a, 0xff, ' ', 1, "DEC R2"},
81   { 0x1b, 0xff, ' ', 1, "DEC R3"},
82   { 0x1c, 0xff, ' ', 1, "DEC R4"},
83   { 0x1d, 0xff, ' ', 1, "DEC R5"},
84   { 0x1e, 0xff, ' ', 1, "DEC R6"},
85   { 0x1f, 0xff, ' ', 1, "DEC R7"},
86   { 0x20, 0xff, 'R', 3, "JB %b,%R"},
87    { 0x21, 0xff, 'A', 3, "AJMP %A"},
88    { 0x22, 0xff, '_', 1, "RET"},
89   { 0x23, 0xff, ' ', 1, "RL A"},
90   { 0x24, 0xff, ' ', 2, "ADD A,#%d"},
91   { 0x25, 0xff, ' ', 2, "ADD A,%a"},
92   { 0x26, 0xff, ' ', 1, "ADD A,@R0"},
93   { 0x27, 0xff, ' ', 1, "ADD A,@R1"},
94   { 0x28, 0xff, ' ', 1, "ADD A,R0"},
95   { 0x29, 0xff, ' ', 1, "ADD A,R1"},
96   { 0x2a, 0xff, ' ', 1, "ADD A,R2"},
97   { 0x2b, 0xff, ' ', 1, "ADD A,R3"},
98   { 0x2c, 0xff, ' ', 1, "ADD A,R4"},
99   { 0x2d, 0xff, ' ', 1, "ADD A,R5"},
100   { 0x2e, 0xff, ' ', 1, "ADD A,R6"},
101   { 0x2f, 0xff, ' ', 1, "ADD A,R7"},
102   { 0x30, 0xff, 'R', 3, "JNB %b,%R"},
103    { 0x31, 0xff, 'a', 3, "ACALL %A"},
104    { 0x32, 0xff, '_', 1, "RETI"},
105   { 0x33, 0xff, ' ', 1, "RLC A"},
106   { 0x34, 0xff, ' ', 2, "ADDC A,#%d"},
107   { 0x35, 0xff, ' ', 2, "ADDC A,%a"},
108   { 0x36, 0xff, ' ', 1, "ADDC A,@R0"},
109   { 0x37, 0xff, ' ', 1, "ADDC A,@R1"},
110   { 0x38, 0xff, ' ', 1, "ADDC A,R0"},
111   { 0x39, 0xff, ' ', 1, "ADDC A,R1"},
112   { 0x3a, 0xff, ' ', 1, "ADDC A,R2"},
113   { 0x3b, 0xff, ' ', 1, "ADDC A,R3"},
114   { 0x3c, 0xff, ' ', 1, "ADDC A,R4"},
115   { 0x3d, 0xff, ' ', 1, "ADDC A,R5"},
116   { 0x3e, 0xff, ' ', 1, "ADDC A,R6"},
117   { 0x3f, 0xff, ' ', 1, "ADDC A,R7"},
118   { 0x40, 0xff, 'r', 2, "JC %r"},
119    { 0x41, 0xff, 'A', 3, "AJMP %A"},
120   { 0x42, 0xff, ' ', 2, "ORL %a,A"},
121   { 0x43, 0xff, ' ', 3, "ORL %a,#%D"},
122   { 0x44, 0xff, ' ', 2, "ORL A,#%d"},
123   { 0x45, 0xff, ' ', 2, "ORL A,%a"},
124   { 0x46, 0xff, ' ', 1, "ORL A,@R0"},
125   { 0x47, 0xff, ' ', 1, "ORL A,@R1"},
126   { 0x48, 0xff, ' ', 1, "ORL A,R0"},
127   { 0x49, 0xff, ' ', 1, "ORL A,R1"},
128   { 0x4a, 0xff, ' ', 1, "ORL A,R2"},
129   { 0x4b, 0xff, ' ', 1, "ORL A,R3"},
130   { 0x4c, 0xff, ' ', 1, "ORL A,R4"},
131   { 0x4d, 0xff, ' ', 1, "ORL A,R5"},
132   { 0x4e, 0xff, ' ', 1, "ORL A,R6"},
133   { 0x4f, 0xff, ' ', 1, "ORL A,R7"},
134   { 0x50, 0xff, 'r', 2, "JNC %r"},
135    { 0x51, 0xff, 'a', 3, "ACALL %A"},
136   { 0x52, 0xff, ' ', 2, "ANL %a,A"},
137   { 0x53, 0xff, ' ', 3, "ANL %a,#%D"},
138   { 0x54, 0xff, ' ', 2, "ANL A,#%d"},
139   { 0x55, 0xff, ' ', 2, "ANL A,%a"},
140   { 0x56, 0xff, ' ', 1, "ANL A,@R0"},
141   { 0x57, 0xff, ' ', 1, "ANL A,@R1"},
142   { 0x58, 0xff, ' ', 1, "ANL A,R0"},
143   { 0x59, 0xff, ' ', 1, "ANL A,R1"},
144   { 0x5a, 0xff, ' ', 1, "ANL A,R2"},
145   { 0x5b, 0xff, ' ', 1, "ANL A,R3"},
146   { 0x5c, 0xff, ' ', 1, "ANL A,R4"},
147   { 0x5d, 0xff, ' ', 1, "ANL A,R5"},
148   { 0x5e, 0xff, ' ', 1, "ANL A,R6"},
149   { 0x5f, 0xff, ' ', 1, "ANL A,R7"},
150   { 0x60, 0xff, 'r', 2, "JZ %r"},
151    { 0x61, 0xff, 'A', 3, "AJMP %A"},
152   { 0x62, 0xff, ' ', 2, "XRL %a,A"},
153   { 0x63, 0xff, ' ', 3, "XRL %a,#%D"},
154   { 0x64, 0xff, ' ', 2, "XRL A,#%d"},
155   { 0x65, 0xff, ' ', 2, "XRL A,%a"},
156   { 0x66, 0xff, ' ', 1, "XRL A,@R0"},
157   { 0x67, 0xff, ' ', 1, "XRL A,@R1"},
158   { 0x68, 0xff, ' ', 1, "XRL A,R0"},
159   { 0x69, 0xff, ' ', 1, "XRL A,R1"},
160   { 0x6a, 0xff, ' ', 1, "XRL A,R2"},
161   { 0x6b, 0xff, ' ', 1, "XRL A,R3"},
162   { 0x6c, 0xff, ' ', 1, "XRL A,R4"},
163   { 0x6d, 0xff, ' ', 1, "XRL A,R5"},
164   { 0x6e, 0xff, ' ', 1, "XRL A,R6"},
165   { 0x6f, 0xff, ' ', 1, "XRL A,R7"},
166   { 0x70, 0xff, 'r', 2, "JNZ %r"},
167    { 0x71, 0xff, 'a', 3, "ACALL %A"},
168   { 0x72, 0xff, ' ', 2, "ORL C,%b"},
169   { 0x73, 0xff, '_', 1, "JMP @A+DPTR"},
170   { 0x74, 0xff, ' ', 2, "MOV A,#%d"},
171   { 0x75, 0xff, ' ', 3, "MOV %a,#%D"},
172   { 0x76, 0xff, ' ', 2, "MOV @R0,#%d"},
173   { 0x77, 0xff, ' ', 2, "MOV @R1,#%d"},
174   { 0x78, 0xff, ' ', 2, "MOV R0,#%d"},
175   { 0x79, 0xff, ' ', 2, "MOV R1,#%d"},
176   { 0x7a, 0xff, ' ', 2, "MOV R2,#%d"},
177   { 0x7b, 0xff, ' ', 2, "MOV R3,#%d"},
178   { 0x7c, 0xff, ' ', 2, "MOV R4,#%d"},
179   { 0x7d, 0xff, ' ', 2, "MOV R5,#%d"},
180   { 0x7e, 0xff, ' ', 2, "MOV R6,#%d"},
181   { 0x7f, 0xff, ' ', 2, "MOV R7,#%d"},
182   { 0x80, 0xff, 's', 2, "SJMP %r"},
183    { 0x81, 0xff, 'A', 3, "AJMP %A"},
184   { 0x82, 0xff, ' ', 2, "ANL C,%b"},
185   { 0x83, 0xff, ' ', 1, "MOVC A,@A+PC"},
186   { 0x84, 0xff, ' ', 1, "DIV AB"},
187   { 0x85, 0xff, ' ', 3, "MOV %8,%a"},
188   { 0x86, 0xff, ' ', 2, "MOV %a,@R0"},
189   { 0x87, 0xff, ' ', 2, "MOV %a,@R1"},
190   { 0x88, 0xff, ' ', 2, "MOV %a,R0"},
191   { 0x89, 0xff, ' ', 2, "MOV %a,R1"},
192   { 0x8a, 0xff, ' ', 2, "MOV %a,R2"},
193   { 0x8b, 0xff, ' ', 2, "MOV %a,R3"},
194   { 0x8c, 0xff, ' ', 2, "MOV %a,R4"},
195   { 0x8d, 0xff, ' ', 2, "MOV %a,R5"},
196   { 0x8e, 0xff, ' ', 2, "MOV %a,R6"},
197   { 0x8f, 0xff, ' ', 2, "MOV %a,R7"},
198    { 0x90, 0xff, ' ', 4, "MOV DPTR,#%l"},
199    { 0x91, 0xff, 'a', 3, "ACALL %A"},
200   { 0x92, 0xff, ' ', 2, "MOV %b,C"},
201   { 0x93, 0xff, ' ', 1, "MOVC A,@A+DPTR"},
202   { 0x94, 0xff, ' ', 2, "SUBB A,#%d"},
203   { 0x95, 0xff, ' ', 2, "SUBB A,%a"},
204   { 0x96, 0xff, ' ', 1, "SUBB A,@R0"},
205   { 0x97, 0xff, ' ', 1, "SUBB A,@R1"},
206   { 0x98, 0xff, ' ', 1, "SUBB A,R0"},
207   { 0x99, 0xff, ' ', 1, "SUBB A,R1"},
208   { 0x9a, 0xff, ' ', 1, "SUBB A,R2"},
209   { 0x9b, 0xff, ' ', 1, "SUBB A,R3"},
210   { 0x9c, 0xff, ' ', 1, "SUBB A,R4"},
211   { 0x9d, 0xff, ' ', 1, "SUBB A,R5"},
212   { 0x9e, 0xff, ' ', 1, "SUBB A,R6"},
213   { 0x9f, 0xff, ' ', 1, "SUBB A,R7"},
214   { 0xa0, 0xff, ' ', 2, "ORL C,/%b"},
215    { 0xa1, 0xff, 'A', 3, "AJMP %A"},
216   { 0xa2, 0xff, ' ', 2, "MOV C,%b"},
217   { 0xa3, 0xff, ' ', 1, "INC DPTR"},
218   { 0xa4, 0xff, ' ', 1, "MUL AB"},
219   { 0xa5, 0xff, '_', 1, "-"},
220   { 0xa6, 0xff, ' ', 2, "MOV @R0,%a"},
221   { 0xa7, 0xff, ' ', 2, "MOV @R1,%a"},
222   { 0xa8, 0xff, ' ', 2, "MOV R0,%a"},
223   { 0xa9, 0xff, ' ', 2, "MOV R1,%a"},
224   { 0xaa, 0xff, ' ', 2, "MOV R2,%a"},
225   { 0xab, 0xff, ' ', 2, "MOV R3,%a"},
226   { 0xac, 0xff, ' ', 2, "MOV R4,%a"},
227   { 0xad, 0xff, ' ', 2, "MOV R5,%a"},
228   { 0xae, 0xff, ' ', 2, "MOV R6,%a"},
229   { 0xaf, 0xff, ' ', 2, "MOV R7,%a"},
230   { 0xb0, 0xff, ' ', 2, "ANL C,/%b"},
231    { 0xb1, 0xff, 'a', 3, "ACALL %A"},
232   { 0xb2, 0xff, ' ', 2, "CPL %b"},
233   { 0xb3, 0xff, ' ', 1, "CPL C"},
234   { 0xb4, 0xff, 'R', 3, "CJNE A,#%d,%R"},
235   { 0xb5, 0xff, 'R', 3, "CJNE A,%a,%R"},
236   { 0xb6, 0xff, 'R', 3, "CJNE @R0,#%d,%R"},
237   { 0xb7, 0xff, 'R', 3, "CJNE @R1,#%d,%R"},
238   { 0xb8, 0xff, 'R', 3, "CJNE R0,#%d,%R"},
239   { 0xb9, 0xff, 'R', 3, "CJNE R1,#%d,%R"},
240   { 0xba, 0xff, 'R', 3, "CJNE R2,#%d,%R"},
241   { 0xbb, 0xff, 'R', 3, "CJNE R3,#%d,%R"},
242   { 0xbc, 0xff, 'R', 3, "CJNE R4,#%d,%R"},
243   { 0xbd, 0xff, 'R', 3, "CJNE R5,#%d,%R"},
244   { 0xbe, 0xff, 'R', 3, "CJNE R6,#%d,%R"},
245   { 0xbf, 0xff, 'R', 3, "CJNE R7,#%d,%R"},
246    { 0xc0, 0xff, ' ', 2, "PUSH %a"},
247    { 0xc1, 0xff, 'A', 3, "AJMP %A"},
248   { 0xc2, 0xff, ' ', 2, "CLR %b"},
249   { 0xc3, 0xff, ' ', 1, "CLR C"},
250   { 0xc4, 0xff, ' ', 1, "SWAP A"},
251   { 0xc5, 0xff, ' ', 2, "XCH A,%a"},
252   { 0xc6, 0xff, ' ', 1, "XCH A,@R0"},
253   { 0xc7, 0xff, ' ', 1, "XCH A,@R1"},
254   { 0xc8, 0xff, ' ', 1, "XCH A,R0"},
255   { 0xc9, 0xff, ' ', 1, "XCH A,R1"},
256   { 0xca, 0xff, ' ', 1, "XCH A,R2"},
257   { 0xcb, 0xff, ' ', 1, "XCH A,R3"},
258   { 0xcc, 0xff, ' ', 1, "XCH A,R4"},
259   { 0xcd, 0xff, ' ', 1, "XCH A,R5"},
260   { 0xce, 0xff, ' ', 1, "XCH A,R6"},
261   { 0xcf, 0xff, ' ', 1, "XCH A,R7"},
262    { 0xd0, 0xff, ' ', 2, "POP %a"},
263    { 0xd1, 0xff, 'a', 3, "ACALL %A"},
264   { 0xd2, 0xff, ' ', 2, "SETB %b"},
265   { 0xd3, 0xff, ' ', 1, "SETB C"},
266   { 0xd4, 0xff, ' ', 1, "DA A"},
267   { 0xd5, 0xff, 'R', 3, "DJNZ %a,%R"},
268   { 0xd6, 0xff, ' ', 1, "XCHD A,@R0"},
269   { 0xd7, 0xff, ' ', 1, "XCHD A,@R1"},
270   { 0xd8, 0xff, 'r', 2, "DJNZ R0,%r"},
271   { 0xd9, 0xff, 'r', 2, "DJNZ R1,%r"},
272   { 0xda, 0xff, 'r', 2, "DJNZ R2,%r"},
273   { 0xdb, 0xff, 'r', 2, "DJNZ R3,%r"},
274   { 0xdc, 0xff, 'r', 2, "DJNZ R4,%r"},
275   { 0xdd, 0xff, 'r', 2, "DJNZ R5,%r"},
276   { 0xde, 0xff, 'r', 2, "DJNZ R6,%r"},
277   { 0xdf, 0xff, 'r', 2, "DJNZ R7,%r"},
278   { 0xe0, 0xff, ' ', 1, "MOVX A,@DPTR"},
279    { 0xe1, 0xff, 'A', 3, "AJMP %A"},
280   { 0xe2, 0xff, ' ', 1, "MOVX A,@R0"},
281   { 0xe3, 0xff, ' ', 1, "MOVX A,@R1"},
282   { 0xe4, 0xff, ' ', 1, "CLR A"},
283   { 0xe5, 0xff, ' ', 2, "MOV A,%a"},
284   { 0xe6, 0xff, ' ', 1, "MOV A,@R0"},
285   { 0xe7, 0xff, ' ', 1, "MOV A,@R1"},
286   { 0xe8, 0xff, ' ', 1, "MOV A,R0"},
287   { 0xe9, 0xff, ' ', 1, "MOV A,R1"},
288   { 0xea, 0xff, ' ', 1, "MOV A,R2"},
289   { 0xeb, 0xff, ' ', 1, "MOV A,R3"},
290   { 0xec, 0xff, ' ', 1, "MOV A,R4"},
291   { 0xed, 0xff, ' ', 1, "MOV A,R5"},
292   { 0xee, 0xff, ' ', 1, "MOV A,R6"},
293   { 0xef, 0xff, ' ', 1, "MOV A,R7"},
294   { 0xf0, 0xff, ' ', 1, "MOVX @DPTR,A"},
295    { 0xf1, 0xff, 'a', 3, "ACALL %A"},
296   { 0xf2, 0xff, ' ', 1, "MOVX @R0,A"},
297   { 0xf3, 0xff, ' ', 1, "MOVX @R1,A"},
298   { 0xf4, 0xff, ' ', 1, "CPL A"},
299   { 0xf5, 0xff, ' ', 2, "MOV %a,A"},
300   { 0xf6, 0xff, ' ', 1, "MOV @R0,A"},
301   { 0xf7, 0xff, ' ', 1, "MOV @R1,A"},
302   { 0xf8, 0xff, ' ', 1, "MOV R0,A"},
303   { 0xf9, 0xff, ' ', 1, "MOV R1,A"},
304   { 0xfa, 0xff, ' ', 1, "MOV R2,A"},
305   { 0xfb, 0xff, ' ', 1, "MOV R3,A"},
306   { 0xfc, 0xff, ' ', 1, "MOV R4,A"},
307   { 0xfd, 0xff, ' ', 1, "MOV R5,A"},
308   { 0xfe, 0xff, ' ', 1, "MOV R6,A"},
309   { 0xff, 0xff, ' ', 1, "MOV R7,A"},
310   { 0, 0, 0, 0, NULL }
311 };
312
313 /*
314  * Making an 390 CPU object
315  */
316
317 t_uc390::t_uc390 (int Itype, int Itech, class cl_sim *asim):
318   t_uc52 (Itype, Itech, asim)
319 {
320   if (Itype == CPU_DS390F)
321     {
322       printf ("FLAT24 MODE SET, warning: experimental code\n");
323       flat24_flag = 1;
324     }
325 }
326
327   // strcpy (mem(MEM_ROM) ->addr_format, "0x%06x");
328   // strcpy (mem(MEM_XRAM)->addr_format, "0x%06x");
329
330 t_addr
331 t_uc390::get_mem_size (enum mem_class type)
332 {
333   //if ((sfr->get (ACON) & 0x3) == 2)
334   if (!flat24_flag)
335     return t_uc52::get_mem_size (type);
336   switch (type)
337     {
338       case MEM_ROM:
339         return 128*1024;        // 4*1024*1024; 4 Meg possible
340       case MEM_XRAM:
341         return 128*1024;        // 4*1024*1024; 4 Meg possible
342       case MEM_IRAM:
343         return 256;
344       case MEM_SFR:
345         return 256;
346       case MEM_IXRAM:
347         return 4*1024;          // internal XRAM
348       case MEM_TYPES:
349       default:
350         return 0;
351     }
352   return 0;
353 }
354
355 ulong
356 t_uc390::read_mem(enum mem_class type, t_mem addr)
357 {
358   //if ((sfr->get (ACON) & 0x3) == 2)
359
360   if (type == MEM_XRAM &&
361       flat24_flag &&
362       addr >= 0x400000)
363     {
364       addr -= 0x400000;
365       type = MEM_IXRAM;
366     }
367   return t_uc51::read_mem (type, addr);
368 }
369
370 ulong
371 t_uc390::get_mem (enum mem_class type, t_addr addr)
372 {
373   if (type == MEM_XRAM &&
374       flat24_flag &&
375       addr >= 0x400000)
376     {
377       addr -= 0x400000;
378       type = MEM_IXRAM;
379     }
380   return t_uc51::get_mem (type, addr);
381 }
382
383 void
384 t_uc390::write_mem (enum mem_class type, t_addr addr, t_mem val)
385 {
386   if (type == MEM_XRAM &&
387       flat24_flag &&
388       addr >= 0x400000)
389     {
390       addr -= 0x400000;
391       type = MEM_IXRAM;
392     }
393   t_uc51::write_mem (type, addr, val);
394 }
395
396 void
397 t_uc390::set_mem (enum mem_class type, t_addr addr, t_mem val)
398 {
399   if (type == MEM_XRAM &&
400       flat24_flag &&
401       addr >= 0x400000)
402     {
403       addr -= 0x400000;
404       type = MEM_IXRAM;
405     }
406   t_uc51::set_mem (type, addr, val);
407 }
408
409 /*
410  *____________________________________________________________________________
411  */
412
413 int
414 t_uc390::push_byte (uchar uc)
415 {
416   int res;
417
418   sfr->add (SP, 1);
419   if (sfr->get (ACON) & 0x04) /* SA: 10 bit stack */
420     {
421       uint sp10;
422
423       if (get_mem (MEM_SFR, SP) == 0x00) /* overflow SP */
424         sfr->add (ESP, 1);
425       sp10 = (get_mem (MEM_SFR, ESP) & 0x3) * 256 +
426              get_mem (MEM_SFR, SP);
427       write_mem (MEM_IXRAM, sp10, uc);
428       res = 0;
429     }
430   else
431     {
432       uchar *sp;
433
434       sp = get_indirect (sfr->get (SP), &res);
435       if (res != resGO)
436         res = resSTACK_OV;
437       *sp = uc;
438     }
439   return res;
440 }
441
442 uchar
443 t_uc390::pop_byte (int *Pres)
444 {
445   uchar uc;
446
447   if (sfr->get (ACON) & 0x04) /* SA: 10 bit stack */
448     {
449       uint sp10;
450
451       sp10 = (get_mem (MEM_SFR, ESP) & 0x3) * 256 +
452              get_mem (MEM_SFR, SP);
453       sfr->add (SP, -1);
454       if (get_mem (MEM_SFR, SP) == 0xff) /* underflow SP */
455         sfr->add (ESP, -1);
456       uc = get_mem (MEM_IXRAM, sp10);
457       *Pres = 0;
458     }
459   else
460     {
461       uchar *sp;
462
463       sp = get_indirect (get_mem (MEM_SFR, SP), Pres);
464       if (*Pres != resGO)
465         *Pres = resSTACK_OV;
466       sfr->add (SP, -1);
467       uc = *sp;
468     }
469   return uc;
470 }
471
472 /*
473  * 0x05 2 12 INC addr
474  *____________________________________________________________________________
475  *
476  */
477 int
478 t_uc390::inst_inc_addr (uchar code)
479 {
480   uchar *addr;
481
482   addr = get_direct (fetch (), &event_at.wi, &event_at.ws);
483
484   /* mask off the 2Hex bit adjacent to the 1H bit which selects
485      which DPTR we use.  This is a feature of 80C390.
486      You can do INC DPS and it only effects bit 1. */
487   if (code == DPS)
488     (*addr) ^= 1;  /* just toggle */
489   else
490     (*addr)++;
491
492   proc_write (addr);
493   return resGO;
494 }
495
496 /*
497  * 0xa3 1 24 INC DPTR
498  *____________________________________________________________________________
499  *
500  */
501
502 int
503 t_uc390::inst_inc_dptr (uchar code)
504 {
505   ulong dptr;
506
507   uchar pl, ph, px, dps;
508
509   dps = sfr->get (DPS);
510   if (dps & 1)
511     {
512       pl = DPL1;
513       ph = DPH1;
514       px = DPX1;
515     }
516   else
517     {
518       pl = DPL;
519       ph = DPH;
520       px = DPX;
521     }
522
523   dptr = sfr->get (ph) * 256 + sfr->get (pl);
524   //if ((sfr->get (ACON) & 0x3) == 2)
525   if (flat24_flag)
526     dptr += sfr->get (px) *256*256;
527   if (dps & 0x80) /* decr set */
528     dptr--;
529   else
530     dptr++;
531
532   //if ((sfr->get (ACON) & 0x3) == 2)
533   if (flat24_flag)
534     sfr->set (px, (dptr >> 16) & 0xff);
535   sfr->set (event_at.ws = ph, (dptr >> 8) & 0xff);
536   sfr->set (pl, dptr & 0xff);
537
538   if (dps & 0x20)                      /* auto-switch dptr */
539     sfr->set (DPS, (dps ^ 1));  /* toggle dual-dptr switch */
540   tick (1);
541   return resGO;
542 }
543
544 /*
545  * 0x73 1 24 JMP @A+DPTR
546  *____________________________________________________________________________
547  *
548  */
549
550 int
551 t_uc390::inst_jmp_$a_dptr (uchar code)
552 {
553   uchar pl, ph, px, dps;
554
555   dps = sfr->get (DPS);
556   if (dps & 1)
557     {
558       pl = DPL1;
559       ph = DPH1;
560       px = DPX1;
561     }
562   else
563     {
564       pl = DPL;
565       ph = DPH;
566       px = DPX;
567     }
568
569   PC = (sfr->get (ph) * 256 + sfr->get (pl) +
570       read_mem (MEM_SFR, ACC)) &
571       (EROM_SIZE - 1);
572   //if ((sfr->get (ACON) & 0x3) == 2)
573   if (flat24_flag)
574     PC += sfr->get (px) * 256*256;
575
576   tick (1);
577   return resGO;
578 }
579
580 /*
581  * 0x90 3 24 MOV DPTR,#data
582  *____________________________________________________________________________
583  *
584  */
585
586 int
587 t_uc390::inst_mov_dptr_$data (uchar code)
588 {
589   uchar pl, ph, px, dps;
590
591   dps = sfr->get (DPS);
592   if (dps & 1)
593     {
594       pl = DPL1;
595       ph = DPH1;
596       px = DPX1;
597     }
598   else
599     {
600       pl = DPL;
601       ph = DPH;
602       px = DPX;
603     }
604
605   //if ((sfr->get (ACON) & 0x3) == 2)
606   if (flat24_flag)
607     sfr->set (px, fetch ());
608   sfr->set (event_at.ws = ph, fetch ());
609   sfr->set (pl, fetch ());
610
611   if (dps & 0x20)                      /* auto-switch dptr */
612     sfr->set (DPS, (dps ^ 1));  /* toggle dual-dptr switch */
613
614   tick (1);
615   return resGO;
616 }
617
618
619 /*
620  * 0x93 1 24 MOVC A,@A+DPTR
621  *____________________________________________________________________________
622  *
623  */
624
625 int
626 t_uc390::inst_movc_a_$a_dptr (uchar code)
627 {
628   uchar pl, ph, px, dps;
629
630   dps = sfr->get (DPS);
631   if (dps & 1)
632     {
633       pl = DPL1;
634       ph = DPH1;
635       px = DPX1;
636     }
637   else
638     {
639       pl = DPL;
640       ph = DPH;
641       px = DPX;
642     }
643
644   //if ((sfr->get (ACON) & 0x3) == 2)
645   if (flat24_flag)
646     sfr->set (ACC, get_mem (MEM_ROM,
647               event_at.rc =
648               (sfr->get (px) * 256*256 + sfr->get (ph) * 256 + sfr->get (pl) +
649               sfr->get (ACC)) & (EROM_SIZE-1)));
650   else
651     sfr->set (ACC, get_mem (MEM_ROM, event_at.rc =
652               (sfr->get (ph) * 256 + sfr->get (pl) +
653               sfr->get (ACC)) & (EROM_SIZE-1)));
654
655   if (dps & 0x20)                      /* auto-switch dptr */
656     sfr->set (DPS, (dps ^ 1));  /* toggle dual-dptr switch */
657
658   tick (1);
659   return resGO;
660 }
661
662 /*
663  * 0xc0 2 24 PUSH addr
664  *____________________________________________________________________________
665  *
666  */
667
668 int
669 t_uc390::inst_push (uchar code)
670 {
671   uchar *addr;
672   int res;
673
674   addr = get_direct (fetch (), &event_at.wi, &event_at.ws);
675   res = push_byte (read (addr));
676   tick (1);
677   return res;
678 }
679
680
681 /*
682  * 0xd0 2 24 POP addr
683  *____________________________________________________________________________
684  *
685  */
686
687 int
688 t_uc390::inst_pop (uchar code)
689 {
690   uchar *addr;
691   int res;
692
693   addr = get_direct (fetch (), &event_at.wi, &event_at.ws);
694   *addr = pop_byte (&res);
695   proc_write (addr);
696   tick (1);
697   return res;
698 }
699
700
701 /*
702  * 0xe0 1 24 MOVX A,@DPTR
703  *____________________________________________________________________________
704  *
705  */
706
707 int
708 t_uc390::inst_movx_a_$dptr (uchar code)
709 {
710   uchar pl, ph, px, dps;
711
712   dps = sfr->get (DPS);
713   if (dps & 1)
714     {
715       pl = DPL1;
716       ph = DPH1;
717       px = DPX1;
718     }
719   else
720     {
721       pl = DPL;
722       ph = DPH;
723       px = DPX;
724     }
725
726   //if ((sfr->get (ACON) & 0x3) == 2)
727   if (flat24_flag)
728     sfr->set (event_at.ws = ACC,
729               get_mem (MEM_XRAM,
730               event_at.rx = sfr->get (px) * 256*256 + sfr->get (ph) * 256 + sfr->get (pl)));
731   else
732     sfr->set (event_at.ws = ACC,
733              get_mem (MEM_XRAM,
734              event_at.rx = sfr->get (ph) * 256 + sfr->get (pl)));
735
736   if (dps & 0x20)                      /* auto-switch dptr */
737     sfr->set (DPS, (dps ^ 1));  /* toggle dual-dptr switch */
738
739   tick (1);
740   return resGO;
741 }
742
743 /*
744  * 0xf0 1 24 MOVX @DPTR,A
745  *____________________________________________________________________________
746  *
747  */
748
749 int
750 t_uc390::inst_movx_$dptr_a (uchar code)
751 {
752   uchar pl, ph, px, dps;
753
754   dps = sfr->get (DPS);
755   if (dps & 1)
756     {
757       pl = DPL1;
758       ph = DPH1;
759       px = DPX1;
760     }
761   else
762     {
763       pl = DPL;
764       ph = DPH;
765       px = DPX;
766     }
767
768   //if ((sfr->get (ACON) & 0x3) == 2)
769   if (flat24_flag)
770     set_mem (MEM_XRAM,
771              event_at.wx = sfr->get (px) * 256*256 + sfr->get (ph) * 256 + sfr->get (pl),
772              sfr->get (event_at.rs = ACC));
773   else
774     set_mem (MEM_XRAM,
775              event_at.wx = sfr->get (ph) * 256 + sfr->get (pl),
776              sfr->get (event_at.rs = ACC));
777
778   if (dps & 0x20)                      /* auto-switch dptr */
779     sfr->set (DPS, (dps ^ 1));  /* toggle dual-dptr switch */
780
781   tick (1);
782   return resGO;
783 }
784
785 /*
786  * 0x[02468ace]1 2 24 AJMP addr
787  *____________________________________________________________________________
788  *
789  */
790
791 int
792 t_uc390::inst_ajmp_addr (uchar code)
793 {
794   uchar x, h, l;
795
796   //if ((sfr->get (ACON) & 0x3) == 2)
797   if (flat24_flag)
798     {
799       x = (code >> 5) & 0x07;
800       h = fetch ();
801       l = fetch ();
802       PC = (PC & 0xf800) | (x * 256*256 + h * 256 + l);
803     }
804   else
805     {
806       h = (code >> 5) & 0x07;
807       l = fetch ();
808       PC = (PC & 0xf800) | (h * 256 + l);
809     }
810   tick (1);
811   return resGO;
812 }
813
814 /*
815  * 0x02 3 24 LJMP addr
816  *____________________________________________________________________________
817  *
818  */
819
820 int
821 t_uc390::inst_ljmp (uchar code)
822 {
823   uchar x, h, l;
824
825   //if ((sfr->get (ACON) & 0x3) == 2)
826   if (flat24_flag)
827     {
828       x = fetch ();
829       h = fetch ();
830       l = fetch ();
831       PC = x * 256*256 + h * 256 + l;
832     }
833   else
834     {
835       h = fetch ();
836       l = fetch ();
837       PC = h * 256 + l;
838     }
839   tick (1);
840   return resGO;
841 }
842
843 /*
844  * 0x[13579bdf]1 2 24 ACALL addr
845  *____________________________________________________________________________
846  *
847  */
848
849 int
850 t_uc390::inst_acall_addr (uchar code)
851 {
852   uchar x, h, l, *sp, *aof_SP;
853   int res;
854
855   //if ((sfr->get (ACON) & 0x3) == 2)
856   if (flat24_flag)
857     {
858       x = (code >> 5) & 0x07;
859       h = fetch ();
860       l = fetch ();
861
862       res = push_byte ( PC        & 0xff); /* push low byte  */
863       res = push_byte ((PC >>  8) & 0xff); /* push high byte */
864       res = push_byte ((PC >> 16) & 0xff); /* push x byte    */
865
866       PC = (PC & 0xf800) | (x * 256*256 + h * 256 + l);
867     }
868   else
869     {
870       /* stock mcs51 mode */
871       h = (code >> 5) & 0x07;
872       l = fetch ();
873       aof_SP = &((sfr->umem8)[SP]);
874
875       //MEM(MEM_SFR)[SP]++;
876       (*aof_SP)++;
877       proc_write_sp (*aof_SP);
878       sp = get_indirect (*aof_SP/*sfr->get (SP)*/, &res);
879       if (res != resGO)
880         res = resSTACK_OV;
881       *sp = PC & 0xff; // push low byte
882
883       //MEM(MEM_SFR)[SP]++;
884       (*aof_SP)++;
885       proc_write_sp (*aof_SP);
886       sp = get_indirect (*aof_SP/*sfr->get (SP)*/, &res);
887       if (res != resGO)
888         res = resSTACK_OV;
889       *sp = (PC >> 8) & 0xff; // push high byte
890
891       PC = (PC & 0xf800) | (h * 256 + l);
892     }
893   tick (1);
894   return res;
895 }
896
897
898 /*
899  * 0x12 3 24 LCALL addr
900  *____________________________________________________________________________
901  *
902  */
903
904 int
905 t_uc390::inst_lcall (uchar code, uint addr)
906 {
907   uchar x = 0, h = 0, l = 0;
908   int res;
909
910   if (!addr)
911     { /* this is a normal lcall */
912       //if ((sfr->get (ACON) & 0x3) == 2)
913       if (flat24_flag)
914         x = fetch ();
915       h = fetch ();
916       l = fetch ();
917     }
918     /* else, this is interrupt processing */
919
920   res = push_byte ( PC       & 0xff); /* push low byte  */
921   res = push_byte ((PC >> 8) & 0xff); /* push high byte */
922
923   //if ((sfr->get (ACON) & 0x3) == 2)
924   if (flat24_flag)
925     {
926       res = push_byte ((PC >> 16) & 0xff); /* push x byte */
927       if (addr)
928         PC = addr & 0xfffful; /* if interrupt: x-Byte is 0 */
929       else
930         PC = x * 256*256 + h * 256 + l;
931     }
932   else
933     {
934       if (addr)
935         PC = addr;
936       else
937         PC = h * 256 + l;
938     }
939   return res;
940 }
941
942 /*
943  * 0x22 1 24 RET
944  *____________________________________________________________________________
945  *
946  */
947
948 int
949 t_uc390::inst_ret (uchar code)
950 {
951   uchar x = 0, h, l;
952   int res;
953
954   //if ((sfr->get (ACON) & 0x3) == 2)
955   if (flat24_flag)
956     x = pop_byte (&res);
957   h = pop_byte (&res);
958   l = pop_byte (&res);
959
960   tick (1);
961
962   //if ((sfr->get (ACON) & 0x3) == 2)
963   if (flat24_flag)
964     {
965       tick (1);
966       PC = x * 256*256 + h * 256 + l;
967     }
968   else
969     PC = h * 256 + l;
970
971   return res;
972 }
973
974 /*
975  * 0x32 1 24 RETI
976  *____________________________________________________________________________
977  *
978  */
979
980 int
981 t_uc390::inst_reti (uchar code)
982 {
983   uchar x = 0, h, l;
984   int res;
985
986   //if ((sfr->get (ACON) & 0x3) == 2)
987   if (flat24_flag)
988   if (flat24_flag)
989     x = pop_byte (&res);
990   h = pop_byte (&res);
991   l = pop_byte (&res);
992   tick (1);
993
994   //if ((sfr->get (ACON) & 0x3) == 2)
995   if (flat24_flag)
996     {
997       tick (1);
998       PC = x * 256*256 + h * 256 + l;
999     }
1000   else
1001     PC = h * 256 + l;
1002
1003   was_reti = DD_TRUE;
1004   class it_level *il = (class it_level *) (it_levels->top ());
1005   if (il &&
1006       il->level >= 0)
1007     {
1008       il = (class it_level *) (it_levels->pop ());
1009       delete il;
1010     }
1011
1012   return res;
1013 }
1014
1015 /*
1016  * Disassembling an instruction
1017  */
1018
1019 struct dis_entry *
1020 t_uc390::dis_tbl (void)
1021 {
1022   //if ((sfr->get (ACON) & 0x3) == 2)
1023   if (!flat24_flag)
1024     return disass_51;
1025     //t_uc51::dis_tbl ();
1026
1027   return disass_390f;
1028 }
1029
1030 char *
1031 t_uc390::disass (t_addr addr, char *sep)
1032 {
1033   char work[256], temp[20], c[2];
1034   char *buf, *p, *b, *t;
1035   t_mem code;
1036
1037   //if ((sfr->get (ACON) & 0x3) == 2)
1038   if (!flat24_flag)
1039     return t_uc51::disass (addr, sep);
1040   code = get_mem (MEM_ROM, addr);
1041
1042   p = work;
1043   b = dis_tbl ()[code].mnemonic;
1044   while (*b)
1045     {
1046       if (*b == '%')
1047         {
1048           b++;
1049           switch (*(b++))
1050             {
1051               case 'A': // absolute address
1052                 // stock:
1053                 // sprintf (temp, "%04lx",
1054                 //          (addr & 0xf800)|
1055                 //          (((code >> 5) & 0x07) * 256 +
1056                 //          get_mem (MEM_ROM, addr + 1)));
1057
1058                 sprintf (temp, "%06lx",
1059                          (addr & 0xf80000) |
1060                          (((code >> 5) & 0x07) * (256 * 256) +
1061                          (get_mem (MEM_ROM, addr + 1) * 256) +
1062                           get_mem (MEM_ROM, addr + 2)));
1063               break;
1064             case 'l': // long address
1065               sprintf (temp, "%06lx",
1066                        get_mem (MEM_ROM, addr + 1) * (256*256) +
1067                        get_mem (MEM_ROM, addr + 2) * 256 +
1068                        get_mem (MEM_ROM, addr + 3));
1069                        // get_mem (MEM_ROM, addr + 1) * 256 + get_mem (MEM_ROM, addr + 2));
1070               break;
1071             case 'a': // addr8 (direct address) at 2nd byte
1072                if (!get_name (get_mem (MEM_ROM, addr + 1), sfr_tbl (), temp))
1073                  sprintf (temp, "%02lx", get_mem (MEM_ROM, addr + 1));
1074               break;
1075             case '8': // addr8 (direct address) at 3rd byte
1076               if (!get_name (get_mem (MEM_ROM, addr + 2), sfr_tbl (), temp))
1077                 sprintf (temp, "%02lx", get_mem (MEM_ROM, addr + 1));
1078               sprintf (temp, "%02lx", get_mem (MEM_ROM, addr + 2));
1079               break;
1080             case 'b': // bitaddr at 2nd byte
1081               if (get_name (get_mem (MEM_ROM, addr + 1), bit_tbl (), temp))
1082                 break;
1083               if (get_name (get_bitidx (get_mem (MEM_ROM, addr + 1)),
1084                             sfr_tbl (), temp))
1085                 {
1086                   strcat (temp, ".");
1087                   sprintf (c, "%1ld", get_mem (MEM_ROM, addr + 1) & 0x07);
1088                   strcat (temp, c);
1089                   break;
1090                 }
1091               sprintf (temp, "%02x.%ld",
1092                        get_bitidx (get_mem (MEM_ROM, addr + 1)),
1093                        get_mem (MEM_ROM, addr + 1) & 0x07);
1094               break;
1095             case 'r': // rel8 address at 2nd byte
1096               sprintf (temp, "%04lx",
1097                        addr + 2 + (signed char) (get_mem (MEM_ROM, addr + 1)));
1098               break;
1099             case 'R': // rel8 address at 3rd byte
1100               sprintf (temp, "%04lx",
1101                        addr + 3 + (signed char) (get_mem (MEM_ROM, addr + 2)));
1102               break;
1103             case 'd': // data8 at 2nd byte
1104               sprintf (temp, "%02lx", get_mem (MEM_ROM, addr + 1));
1105               break;
1106             case 'D': // data8 at 3rd byte
1107               sprintf (temp, "%02lx", get_mem (MEM_ROM, addr + 2));
1108               break;
1109             default:
1110               strcpy (temp, "?");
1111               break;
1112             }
1113           t = temp;
1114           while (*t)
1115             *p++ = *t++;
1116         }
1117       else
1118         *p++ = *b++;
1119     }
1120   *p = '\0';
1121
1122   p = strchr (work, ' ');
1123   if (!p)
1124     {
1125       buf = strdup (work);
1126       return buf;
1127     }
1128   if (sep == NULL)
1129     buf = (char *) malloc (6 + strlen (p) + 1);
1130   else
1131     buf = (char *) malloc ((p - work) + strlen (sep) + strlen (p) + 1);
1132   for (p = work, b = buf; *p != ' '; p++, b++)
1133     *b = *p;
1134   p++;
1135   *b = '\0';
1136   if (sep == NULL)
1137     while (strlen (buf) < 6)
1138       strcat (buf, " ");
1139   else
1140     strcat (buf, sep);
1141   strcat (buf, p);
1142   return buf;
1143 }
1144
1145 void
1146 t_uc390::print_regs(class cl_console *con)
1147 {
1148   t_addr start;
1149   uchar data;
1150
1151   //if ((sfr->get (ACON) & 0x3) == 2)
1152   if (!flat24_flag)
1153     {
1154       t_uc51::print_regs (con);
1155       return;
1156     }
1157   start = sfr->get (PSW) & 0x18;
1158   //dump_memory(iram, &start, start+7, 8, /*sim->cmd_out()*/con, sim);
1159   iram->dump (start, start + 7, 8, con);
1160   start = sfr->get (PSW) & 0x18;
1161   data = iram->get (iram->get (start));
1162   con->printf ("%06x %02x %c",
1163                iram->get (start), data, isprint (data) ? data : '.');
1164   con->printf ("  ACC= 0x%02x %3d %c  B= 0x%02x",
1165                sfr->get (ACC), sfr->get (ACC),
1166                isprint (sfr->get (ACC)) ? (sfr->get (ACC)) : '.', sfr->get (B));
1167   eram2xram ();
1168   data = get_mem (MEM_XRAM,
1169                   sfr->get (DPX) * 256*256 + sfr->get (DPH) * 256 + sfr->get (DPL));
1170   con->printf ("   DPTR= 0x%02x%02x%02x @DPTR= 0x%02x %3d %c\n",
1171                sfr->get (DPX), sfr->get (DPH), sfr->get (DPL),
1172                data, data, isprint (data) ? data : '.');
1173   data = iram->get (iram->get (start + 1));
1174   con->printf ("%06x %02x %c", iram->get (start + 1), data,
1175                isprint (data) ? data : '.');
1176   data= sfr->get (PSW);
1177   con->printf ("  PSW= 0x%02x CY=%c AC=%c OV=%c P=%c    ",
1178                data,
1179                (data & bmCY) ? '1' : '0', (data & bmAC) ? '1' : '0',
1180                (data & bmOV) ? '1' : '0', (data & bmP ) ? '1' : '0'
1181                );
1182   /* show stack pointer */
1183   if (sfr->get (ACON) & 0x04)
1184     /* SA: 10 bit stack */
1185     con->printf ("SP10 0x%03x %3d\n",
1186                  (sfr->get (ESP) & 3) * 256 + sfr->get (SP),
1187                  get_mem (MEM_IXRAM, (sfr->get (ESP) & 3) * 256 + sfr->get (SP))
1188                 );
1189   else
1190     con->printf ("SP 0x%02x %3d\n",
1191                  sfr->get (SP),
1192                  iram->get (sfr->get (SP))
1193                 );
1194   print_disass (PC, con);
1195 }
1196 /* End of s51.src/uc390.cc */