623d3915ae784dca32376fbade99c66e98edbd52
[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;          // 128 kByte; 4 MByte possible
394       case MEM_XRAM:
395         /* tinibios copies 128 bytes of the Interrupt Vector Table
396            to 0x100000. This is exactly the minimum of memory to run ucSim
397            without any error. */
398         return 1*1024*1024 + 128; // 1 MByte + 128 bytes; 4 Mbytes possible
399       case MEM_IRAM:
400         return 256;
401       case MEM_SFR:
402         return 256;
403       case MEM_IXRAM:
404         return 4*1024;          // internal XRAM
405       case MEM_TYPES:
406       default:
407         return 0;
408     }
409   return 0;
410 }
411
412 t_mem
413 t_uc390::read_mem(enum mem_class type, t_addr addr)
414 {
415
416   if (type == MEM_XRAM &&
417       addr >= 0x400000 &&
418       (sfr->get (ACON) & 0x02)) /* AM1 set: 24-bit flat? */
419     {
420       addr -= 0x400000;
421       type = MEM_IXRAM;
422     }
423   return t_uc51::read_mem (type, addr); /* 24 bit */
424 }
425
426 t_mem
427 t_uc390::get_mem (enum mem_class type, t_addr addr)
428 {
429   if (type == MEM_XRAM &&
430       addr >= 0x400000 &&
431       (sfr->get (ACON) & 0x02)) /* AM1 set: 24-bit flat? */
432     {
433       addr -= 0x400000;
434       type = MEM_IXRAM;
435     }
436   return t_uc51::get_mem (type, addr);
437 }
438
439 void
440 t_uc390::write_mem (enum mem_class type, t_addr addr, t_mem val)
441 {
442   if (type == MEM_XRAM &&
443       addr >= 0x400000 &&
444       (sfr->get (ACON) & 0x02)) /* AM1 set: 24-bit flat? */
445     {
446       addr -= 0x400000;
447       type = MEM_IXRAM;
448     }
449   t_uc51::write_mem (type, addr, val);
450 }
451
452 void
453 t_uc390::set_mem (enum mem_class type, t_addr addr, t_mem val)
454 {
455   if (type == MEM_XRAM &&
456       addr >= 0x400000 &&
457       (sfr->get (ACON) & 0x02)) /* AM1 set: 24-bit flat? */
458     {
459       addr -= 0x400000;
460       type = MEM_IXRAM;
461     }
462   t_uc51::set_mem (type, addr, val);
463 }
464
465 /*
466  *____________________________________________________________________________
467  */
468
469 void
470 t_uc390::push_byte (t_mem uc)
471 {
472   t_addr sp;
473
474   sp = sfr->wadd (SP, 1);
475   if (sfr->get (ACON) & 0x04) /* SA: 10 bit stack */
476     {
477       if (sp == 0) /* overflow SP */
478         sfr->wadd (ESP, 1);
479       sp += (sfr->read (ESP) & 0x3) * 256;
480       write_mem (MEM_IXRAM, sp, uc); // fixme
481     }
482   else
483     {
484       class cl_cell *stck;
485
486       stck = iram->get_cell (sp);
487       stck->write (uc);
488     }
489 }
490
491 t_mem
492 t_uc390::pop_byte (void)
493 {
494   t_mem temp;
495   t_addr sp;
496
497   if (sfr->get (ACON) & 0x04) /* SA: 10 bit stack */
498     {
499       sp = sfr->read (SP);
500       sp += (sfr->read (ESP) & 0x3) * 256;
501       temp = read_mem (MEM_IXRAM, sp); // fixme
502       sp = sfr->wadd (SP, -1);
503       if (sp == 0xff) /* underflow SP */
504         sfr->wadd (ESP, -1);
505       return temp;
506     }
507   else
508     {
509       class cl_cell *stck;
510
511       stck = iram->get_cell (sfr->get (SP));
512       temp = stck->read();
513       sp = sfr->wadd (SP, -1);
514       return temp;
515     }
516 }
517
518 /*
519  * 0xa3 1 24 INC DPTR
520  *____________________________________________________________________________
521  *
522  */
523
524 int
525 t_uc390::inst_inc_dptr (uchar code)
526 {
527   ulong dptr;
528
529   uchar pl, ph, px, dps;
530
531   dps = sfr->get (DPS);
532   if (dps & 0x01)
533     {
534       pl = DPL1;
535       ph = DPH1;
536       px = DPX1;
537     }
538   else
539     {
540       pl = DPL;
541       ph = DPH;
542       px = DPX;
543     }
544
545   dptr = sfr->read (ph) * 256 + sfr->read (pl);
546   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
547     dptr += sfr->read (px) *256*256;
548   if (dps & 0x80) /* decr set */
549     dptr--;
550   else
551     dptr++;
552
553   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
554     sfr->write (px, (dptr >> 16) & 0xff);
555   sfr->write (ph, (dptr >> 8) & 0xff);
556   sfr->write (pl, dptr & 0xff);
557
558   if (dps & 0x20)                      /* auto-switch dptr */
559     sfr->write (DPS, dps ^ 1);  /* toggle dual-dptr switch */
560   tick (1);
561   return resGO;
562 }
563
564 /*
565  * 0x73 1 24 JMP @A+DPTR
566  *____________________________________________________________________________
567  *
568  */
569
570 int
571 t_uc390::inst_jmp_Sa_dptr (uchar code)
572 {
573   uchar pl, ph, px, dps;
574
575   dps = sfr->get (DPS);
576   if (dps & 0x01)
577     {
578       pl = DPL1;
579       ph = DPH1;
580       px = DPX1;
581     }
582   else
583     {
584       pl = DPL;
585       ph = DPH;
586       px = DPX;
587     }
588
589   PC = (sfr->read (ph) * 256 + sfr->read (pl) + acc->read()) &
590        (EROM_SIZE - 1);
591   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
592     PC += sfr->read (px) * 256*256;
593
594   tick (1);
595   return resGO;
596 }
597
598 /*
599  * 0x90 3 24 MOV DPTR,#data
600  *____________________________________________________________________________
601  *
602  */
603
604 int
605 t_uc390::inst_mov_dptr_Sdata (uchar code)
606 {
607   uchar pl, ph, px, dps;
608
609   dps = sfr->get (DPS);
610   if (dps & 0x01)
611     {
612       pl = DPL1;
613       ph = DPH1;
614       px = DPX1;
615     }
616   else
617     {
618       pl = DPL;
619       ph = DPH;
620       px = DPX;
621     }
622
623   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
624     sfr->write (px, fetch ());
625   sfr->write (ph, fetch ());
626   sfr->write (pl, fetch ());
627
628   if (dps & 0x20)                      /* auto-switch dptr */
629     sfr->write (DPS, dps ^ 1);    /* toggle dual-dptr switch */
630
631   tick (1);
632   return resGO;
633 }
634
635
636 /*
637  * 0x93 1 24 MOVC A,@A+DPTR
638  *____________________________________________________________________________
639  *
640  */
641
642 int
643 t_uc390::inst_movc_a_Sa_dptr (uchar code)
644 {
645   uchar pl, ph, px, dps;
646
647   dps = sfr->get (DPS);
648   if (dps & 0x01)
649     {
650       pl = DPL1;
651       ph = DPH1;
652       px = DPX1;
653     }
654   else
655     {
656       pl = DPL;
657       ph = DPH;
658       px = DPX;
659     }
660
661   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
662     acc->write (read_mem (MEM_ROM,
663                 (sfr->read (px) * 256*256 + sfr->read (ph) * 256 + sfr->read (pl) +
664                 acc->read())));
665   else
666     acc->write (read_mem (MEM_ROM,
667                 (sfr->read (ph) * 256 + sfr->read (pl) +
668                 acc->read())));
669
670   if (dps & 0x20)                      /* auto-switch dptr */
671     sfr->write (DPS, dps ^ 1);  /* toggle dual-dptr switch */
672
673   tick (1);
674   return resGO;
675 }
676
677 /*
678  * 0xc0 2 24 PUSH addr
679  *____________________________________________________________________________
680  *
681  */
682
683 int
684 t_uc390::inst_push (uchar code)
685 {
686   class cl_cell *cell;
687
688   cell = get_direct(fetch());
689   push_byte (cell->read());
690   tick (1);
691   return resGO;
692 }
693
694
695 /*
696  * 0xd0 2 24 POP addr
697  *____________________________________________________________________________
698  *
699  */
700
701 int
702 t_uc390::inst_pop (uchar code)
703 {
704   class cl_cell *cell;
705
706   cell = get_direct (fetch());
707   cell->write (pop_byte());
708   tick (1);
709   return resGO;
710 }
711
712
713 /*
714  * 0xe0 1 24 MOVX A,@DPTR
715  *____________________________________________________________________________
716  *
717  */
718
719 int
720 t_uc390::inst_movx_a_Sdptr (uchar code)
721 {
722   uchar pl, ph, px, dps;
723
724   dps = sfr->get (DPS);
725   if (dps & 0x01)
726     {
727       pl = DPL1;
728       ph = DPH1;
729       px = DPX1;
730     }
731   else
732     {
733       pl = DPL;
734       ph = DPH;
735       px = DPX;
736     }
737
738   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
739     acc->write (read_mem (MEM_XRAM,
740                 sfr->read (px) * 256*256 + sfr->read (ph) * 256 + sfr->read (pl)));
741   else
742     acc->write (read_mem (MEM_XRAM,
743                 sfr->read (ph) * 256 + sfr->read (pl)));
744
745   if (dps & 0x20)                      /* auto-switch dptr */
746     sfr->write (DPS, dps ^ 1);   /* toggle dual-dptr switch */
747
748   tick (1);
749   return resGO;
750 }
751
752 /*
753  * 0xf0 1 24 MOVX @DPTR,A
754  *____________________________________________________________________________
755  *
756  */
757
758 int
759 t_uc390::inst_movx_Sdptr_a (uchar code)
760 {
761   uchar pl, ph, px, dps;
762
763   dps = sfr->get (DPS);
764   if (dps & 0x01)
765     {
766       pl = DPL1;
767       ph = DPH1;
768       px = DPX1;
769     }
770   else
771     {
772       pl = DPL;
773       ph = DPH;
774       px = DPX;
775     }
776
777   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
778     write_mem (MEM_XRAM,
779                sfr->read (px) * 256*256 + sfr->read (ph) * 256 + sfr->read (pl),
780                acc->read());
781   else
782     write_mem (MEM_XRAM,
783                sfr->read (ph) * 256 + sfr->read (pl),
784                acc->read());
785
786   if (dps & 0x20)                      /* auto-switch dptr */
787     sfr->write (DPS, dps ^ 1);   /* toggle dual-dptr switch */
788
789   tick (1);
790   return resGO;
791 }
792
793 /*
794  * 0x[02468ace]1 2 24 AJMP addr
795  *____________________________________________________________________________
796  *
797  */
798
799 int
800 t_uc390::inst_ajmp_addr (uchar code)
801 {
802   uchar x, h, l;
803
804   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
805     {
806       x = (code >> 5) & 0x07;
807       h = fetch ();
808       l = fetch ();
809       PC = (PC & 0xf800) | (x * 256*256 + h * 256 + l);
810     }
811   else
812     {
813       h = (code >> 5) & 0x07;
814       l = fetch ();
815       PC = (PC & 0xf800) | (h * 256 + l);
816     }
817   tick (1);
818   return resGO;
819 }
820
821 /*
822  * 0x02 3 24 LJMP addr
823  *____________________________________________________________________________
824  *
825  */
826
827 int
828 t_uc390::inst_ljmp (uchar code)
829 {
830   uchar x, h, l;
831
832   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
833     {
834       x = fetch ();
835       h = fetch ();
836       l = fetch ();
837       PC = x * 256*256 + h * 256 + l;
838     }
839   else
840     {
841       h = fetch ();
842       l = fetch ();
843       PC = h * 256 + l;
844     }
845   tick (1);
846   return resGO;
847 }
848
849 /*
850  * 0x[13579bdf]1 2 24 ACALL addr
851  *____________________________________________________________________________
852  *
853  */
854
855 int
856 t_uc390::inst_acall_addr (uchar code)
857 {
858   uchar x, h, l;
859
860   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
861     {
862       x = (code >> 5) & 0x07;
863       h = fetch ();
864       l = fetch ();
865
866       push_byte ( PC        & 0xff); /* push low byte  */
867       push_byte ((PC >>  8) & 0xff); /* push high byte */
868       push_byte ((PC >> 16) & 0xff); /* push x byte    */
869
870       PC = (PC & 0xf800) | (x * 256*256 + h * 256 + l);
871     }
872   else
873     {
874       /* stock mcs51 mode */
875       class cl_cell *stck;
876       t_mem sp;
877
878       h = (code >> 5) & 0x07;
879       l = fetch();
880       sp = sfr->wadd (SP, 1);
881       stck = iram->get_cell (sp);
882       stck->write (PC & 0xff); // push low byte
883
884       sp = sfr->wadd (SP, 1);
885       stck = iram->get_cell (sp);
886       stck->write ((PC >> 8) & 0xff); // push high byte
887       PC = (PC & 0xf800) | (h*256 + l);
888     }
889   tick (1);
890   return resGO;
891 }
892
893
894 /*
895  * 0x12 3 24 LCALL addr
896  *____________________________________________________________________________
897  *
898  */
899
900 int
901 t_uc390::inst_lcall (uchar code, uint addr)
902 {
903   uchar x = 0, h = 0, l = 0;
904
905   if (!addr)
906     { /* this is a normal lcall */
907      if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
908         x = fetch ();
909       h = fetch ();
910       l = fetch ();
911     }
912     /* else, this is interrupt processing */
913
914   push_byte ( PC       & 0xff); /* push low byte  */
915   push_byte ((PC >> 8) & 0xff); /* push high byte */
916
917   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
918     {
919       push_byte ((PC >> 16) & 0xff); /* push x byte */
920       if (addr)
921         PC = addr & 0xfffful; /* if interrupt: x-Byte is 0 */
922       else
923         PC = x * 256*256 + h * 256 + l;
924     }
925   else
926     {
927       if (addr)
928         PC = addr;
929       else
930         PC = h * 256 + l;
931     }
932   return resGO;
933 }
934
935 /*
936  * 0x22 1 24 RET
937  *____________________________________________________________________________
938  *
939  */
940
941 int
942 t_uc390::inst_ret (uchar code)
943 {
944   uchar x = 0, h, l;
945
946   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
947     x = pop_byte ();
948   h = pop_byte ();
949   l = pop_byte ();
950
951   tick (1);
952
953   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
954     {
955       tick (1);
956       PC = x * 256*256 + h * 256 + l;
957     }
958   else
959     PC = h * 256 + l;
960
961   return resGO;
962 }
963
964 /*
965  * 0x32 1 24 RETI
966  *____________________________________________________________________________
967  *
968  */
969
970 int
971 t_uc390::inst_reti (uchar code)
972 {
973   uchar x = 0, h, l;
974
975   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
976     x = pop_byte ();
977   h = pop_byte ();
978   l = pop_byte ();
979   tick (1);
980
981   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
982     {
983       tick (1);
984       PC = x * 256*256 + h * 256 + l;
985     }
986   else
987     PC = h * 256 + l;
988
989   interrupt->was_reti = DD_TRUE;
990   class it_level *il = (class it_level *) (it_levels->top ());
991   if (il &&
992       il->level >= 0)
993     {
994       il = (class it_level *) (it_levels->pop ());
995       delete il;
996     }
997
998   return resGO;
999 }
1000
1001
1002 /*
1003  * Disassembling an instruction
1004  */
1005
1006 struct dis_entry *
1007 t_uc390::dis_tbl (void)
1008 {
1009   if (sfr->get (ACON) & 0x02) /* AM1 set: 24-bit flat? */
1010     return disass_390f;
1011   else
1012     return disass_51;
1013     //t_uc51::dis_tbl ();
1014
1015 }
1016
1017 char *
1018 t_uc390::disass (t_addr addr, char *sep)
1019 {
1020   char work[256], temp[20], c[2];
1021   char *buf, *p, *b, *t;
1022   t_mem code;
1023
1024   if (! (sfr->get (ACON) & 0x02)) /* AM1 set: 24-bit flat? */
1025     return t_uc51::disass (addr, sep);
1026   code = get_mem (MEM_ROM, addr);
1027
1028   p = work;
1029   b = dis_tbl()[code].mnemonic;
1030   while (*b)
1031     {
1032       if (*b == '%')
1033         {
1034           b++;
1035           switch (*(b++))
1036             {
1037               case 'A': // absolute address
1038                 // stock:
1039                 // sprintf (temp, "%04lx",
1040                 //          (addr & 0xf800)|
1041                 //          (((code >> 5) & 0x07) * 256 +
1042                 //          get_mem (MEM_ROM, addr + 1)));
1043
1044                 sprintf (temp, "%06lx",
1045                          (addr & 0xf80000L) |
1046                          (((code >> 5) & 0x07) * (256 * 256) +
1047                          (get_mem (MEM_ROM, addr + 1) * 256) +
1048                           get_mem (MEM_ROM, addr + 2)));
1049               break;
1050             case 'l': // long address
1051               sprintf (temp, "%06lx",
1052                        get_mem (MEM_ROM, addr + 1) * (256*256L) +
1053                        get_mem (MEM_ROM, addr + 2) * 256 +
1054                        get_mem (MEM_ROM, addr + 3));
1055                        // get_mem (MEM_ROM, addr + 1) * 256 + get_mem (MEM_ROM, addr + 2));
1056               break;
1057             case 'a': // addr8 (direct address) at 2nd byte
1058                if (!get_name (get_mem (MEM_ROM, addr + 1), sfr_tbl (), temp))
1059                  sprintf (temp, "%02"_M_"x", get_mem (MEM_ROM, addr + 1));
1060               break;
1061             case '8': // addr8 (direct address) at 3rd byte
1062               if (!get_name (get_mem (MEM_ROM, addr + 2), sfr_tbl (), temp))
1063                 sprintf (temp, "%02"_M_"x", get_mem (MEM_ROM, addr + 2));
1064               break;
1065             case 'b': // bitaddr at 2nd byte
1066               {
1067                 t_addr ba = get_mem (MEM_ROM, addr+1);
1068                 if (get_name (ba, bit_tbl(), temp))
1069                   break;
1070                 if (get_name ((ba<128) ? ((ba/8)+32) : (ba&0xf8), sfr_tbl(), temp))
1071                   {
1072                     strcat (temp, ".");
1073                     sprintf (c, "%1"_M_"d", ba & 0x07);
1074                     strcat (temp, c);
1075                     break;
1076                   }
1077                 sprintf (temp, "%02x.%"_M_"d", (ba<128) ? ((ba/8)+32) : (ba&0xf8),
1078                          ba & 0x07);
1079                 break;
1080               }
1081             case 'r': // rel8 address at 2nd byte
1082               sprintf (temp, "%04"_A_"x",
1083                        t_addr (addr + 2 + (signed char) (get_mem (MEM_ROM, addr + 1))));
1084               break;
1085             case 'R': // rel8 address at 3rd byte
1086               sprintf (temp, "%04"_A_"x",
1087                        t_addr (addr + 3 + (signed char) (get_mem (MEM_ROM, addr + 2))));
1088               break;
1089             case 'd': // data8 at 2nd byte
1090               sprintf (temp, "%02"_M_"x", get_mem (MEM_ROM, addr + 1));
1091               break;
1092             case 'D': // data8 at 3rd byte
1093               sprintf (temp, "%02"_M_"x", get_mem (MEM_ROM, addr + 2));
1094               break;
1095             default:
1096               strcpy (temp, "?");
1097               break;
1098             }
1099           t = temp;
1100           while (*t)
1101             *p++ = *t++;
1102         }
1103       else
1104         *p++ = *b++;
1105     }
1106   *p = '\0';
1107
1108   p = strchr (work, ' ');
1109   if (!p)
1110     {
1111       buf = strdup (work);
1112       return buf;
1113     }
1114   if (sep == NULL)
1115     buf = (char *) malloc (6 + strlen (p) + 1);
1116   else
1117     buf = (char *) malloc ((p - work) + strlen (sep) + strlen (p) + 1);
1118   for (p = work, b = buf; *p != ' '; p++, b++)
1119     *b = *p;
1120   p++;
1121   *b = '\0';
1122   if (sep == NULL)
1123     while (strlen (buf) < 6)
1124       strcat (buf, " ");
1125   else
1126     strcat (buf, sep);
1127   strcat (buf, p);
1128   return buf;
1129 }
1130
1131 void
1132 t_uc390::print_regs (class cl_console *con)
1133 {
1134   t_addr start;
1135   t_mem data;
1136
1137   if (! (sfr->get (ACON) & 0x02)) /* AM1 set: 24-bit flat? */
1138     {
1139       t_uc51::print_regs (con);
1140       return;
1141     }
1142   start = sfr->get (PSW) & 0x18;
1143   //dump_memory(iram, &start, start+7, 8, /*sim->cmd_out()*/con, sim);
1144   iram->dump (start, start + 7, 8, con);
1145   start = sfr->get (PSW) & 0x18;
1146   data = iram->get (iram->get (start));
1147   con->dd_printf("%06x %02x %c",
1148                   iram->get (start), data, isprint (data) ? data : '.');
1149   con->dd_printf("  ACC= 0x%02x %3d %c  B= 0x%02x",
1150                  sfr->get (ACC), sfr->get (ACC),
1151                  isprint (sfr->get (ACC)) ?
1152                  (sfr->get (ACC)) : '.', sfr->get (B));
1153   eram2xram ();
1154   data = get_mem (MEM_XRAM,
1155                   sfr->get (DPX) * 256*256 + sfr->get (DPH) * 256 + sfr->get (DPL));
1156   con->dd_printf ("   DPTR= 0x%02x%02x%02x @DPTR= 0x%02x %3d %c\n",
1157                   sfr->get (DPX), sfr->get (DPH), sfr->get (DPL),
1158                   data, data, isprint (data) ? data : '.');
1159   data = iram->get (iram->get (start + 1));
1160   con->dd_printf ("%06x %02x %c", iram->get (start + 1), data,
1161                   isprint (data) ? data : '.');
1162   data= sfr->get (PSW);
1163   con->dd_printf ("  PSW= 0x%02x CY=%c AC=%c OV=%c P=%c    ",
1164                   data,
1165                   (data & bmCY) ? '1' : '0', (data & bmAC) ? '1' : '0',
1166                   (data & bmOV) ? '1' : '0', (data & bmP ) ? '1' : '0'
1167                   );
1168   /* show stack pointer */
1169   if (sfr->get (ACON) & 0x04)
1170     /* SA: 10 bit stack */
1171     con->dd_printf ("SP10 0x%03x %3d\n",
1172                     (sfr->get (ESP) & 3) * 256 + sfr->get (SP),
1173                     get_mem (MEM_IXRAM, (sfr->get (ESP) & 3) * 256 + sfr->get (SP))
1174                     );
1175   else
1176     con->dd_printf ("SP 0x%02x %3d\n",
1177                     sfr->get (SP),
1178                     iram->get (sfr->get (SP))
1179                     );
1180   print_disass (PC, con);
1181 }
1182 /* End of s51.src/uc390.cc */