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