first check-in of Cyclone 0.069 under GPLv2 and MAME License. Pico Disa.c/h included too
[cyclone68000.git] / Cyclone / Main.cpp
1 \r
2 #include "app.h"\r
3 \r
4 static FILE *AsmFile=NULL;\r
5 \r
6 static int CycloneVer=0x0069; // Version number of library\r
7 int *CyJump=NULL; // Jump table\r
8 int ms=0; // If non-zero, output in Microsoft ARMASM format\r
9 char *Narm[4]={ "b", "h","",""}; // Normal ARM Extensions for operand sizes 0,1,2\r
10 char *Sarm[4]={"sb","sh","",""}; // Sign-extend ARM Extensions for operand sizes 0,1,2\r
11 int Cycles=0; // Current cycles for opcode\r
12 int Amatch=1; // If one, try to match A68K timing\r
13 int Accu=-1; // Accuracy\r
14 int Debug=0; // Debug info\r
15 \r
16 void ot(char *format, ...)\r
17 {\r
18   va_list valist=NULL;\r
19   va_start(valist,format);\r
20   if (AsmFile) vfprintf(AsmFile,format,valist);\r
21   va_end(valist);\r
22 }\r
23 \r
24 void ltorg()\r
25 {\r
26   if (ms) ot("  LTORG\n");\r
27   else    ot("  .ltorg\n");\r
28 }\r
29 \r
30 static void PrintException()\r
31 {\r
32   ot("  ;@ Cause an Exception - Vector in [r7,#0x50]\n");\r
33   ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
34   ot("  sub r1,r4,r10 ;@ r1 = Old PC\n");\r
35   OpPush32();\r
36   OpPushSr(1);\r
37   ot("  ldr r0,[r7,#0x50] ;@ Get Vector\n");\r
38   ot(";@ Read IRQ Vector:\n");\r
39   MemHandler(0,2);\r
40   ot("  add r0,r0,r10 ;@ r0 = Memory Base + New PC\n");\r
41   ot("  mov lr,pc\n");\r
42   ot("  ldr pc,[r7,#0x64] ;@ Call checkpc()\n");\r
43   ot("  mov r4,r0\n");\r
44   ot("\n");\r
45 \r
46   // todo - make Interrupt code use this function as well\r
47 }\r
48 \r
49 // Trashes r0\r
50 void CheckInterrupt()\r
51 {\r
52   ot(";@ CheckInterrupt:\n");\r
53   ot("  ldrb r0,[r7,#0x47] ;@ Get IRQ level\n");\r
54   ot("  tst r0,r0\n");\r
55   ot("  blne DoInterrupt\n");\r
56   ot("\n");\r
57 }\r
58 \r
59 static void PrintFramework()\r
60 {\r
61   ot(";@ --------------------------- Framework --------------------------\n");\r
62   if (ms) ot("CycloneRun\n");\r
63   else    ot("CycloneRun:\n");\r
64 \r
65   ot("  stmdb sp!,{r4-r11,lr}\n");\r
66 \r
67   ot("  mov r7,r0          ;@ r7 = Pointer to Cpu Context\n");\r
68   ot("                     ;@ r0-3 = Temporary registers\n");\r
69   ot("  ldrb r9,[r7,#0x46] ;@ r9 = Flags (NZCV)\n");\r
70   ot("  ldr r6,=JumpTab    ;@ r6 = Opcode Jump table\n");\r
71   ot("  ldr r5,[r7,#0x5c]  ;@ r5 = Cycles\n");\r
72   ot("  ldr r4,[r7,#0x40]  ;@ r4 = Current PC + Memory Base\n");\r
73   ot("                     ;@ r8 = Current Opcode\n");\r
74   ot("  mov r9,r9,lsl #28  ;@ r9 = Flags 0xf0000000, cpsr format\n");\r
75   ot("                     ;@ r10 = Source value / Memory Base\n");\r
76   ot("\n");\r
77   CheckInterrupt();\r
78   ot(";@ Check if interrupt used up all the cycles:\n");\r
79   ot("  subs r5,r5,#0\n");\r
80   ot("  blt CycloneEndNoBack\n");\r
81 \r
82   OpFirst();\r
83   ltorg();\r
84   ot("\n");\r
85 \r
86   ot(";@ We come back here after execution\n");\r
87   ot("CycloneEnd%s\n", ms?"":":");\r
88   ot("  sub r4,r4,#2\n");\r
89   ot("CycloneEndNoBack%s\n", ms?"":":");\r
90   ot("  mov r9,r9,lsr #28\n");\r
91   ot("  str r4,[r7,#0x40]  ;@ Save Current PC + Memory Base\n");\r
92   ot("  str r5,[r7,#0x5c]  ;@ Save Cycles\n");\r
93   ot("  strb r9,[r7,#0x46] ;@ Save Flags (NZCV)\n");\r
94   ot("  ldmia sp!,{r4-r11,pc}\n");\r
95   ot("\n");\r
96 \r
97   ot(";@ DoInterrupt - r0=IRQ number\n");\r
98   ot("DoInterrupt%s\n", ms?"":":");\r
99   ot("\n");\r
100   ot("  ldrb r1,[r7,#0x44] ;@ Get SR high: T_S__III\n");\r
101   ot("  and r1,r1,#7 ;@ Get interrupt mask\n");\r
102   ot("  cmp r0,#6 ;@ irq>6 ?\n");\r
103   ot("  cmple r0,r1 ;@ irq<=6: Is irq<=mask ?\n");\r
104   ot("  movle pc,lr ;@ irq<=6 and mask, not allowed\n");\r
105   ot("\n");\r
106   ot("  ldr r10,[r7,#0x60] ;@ Get Memory base\n");\r
107   ot("  mov r11,lr ;@ Preserve ARM return address\n");\r
108   ot("  sub r1,r4,r10 ;@ r1 = Old PC\n");\r
109   OpPush32();\r
110   OpPushSr(1);\r
111   ot(";@ Get IRQ Vector address:\n");\r
112   ot("  ldrb r1,[r7,#0x47] ;@ IRQ\n");\r
113   ot("  mov r0,r1,asl #2\n");\r
114   ot("  add r0,r0,#0x60\n");\r
115   ot(";@ Read IRQ Vector:\n");\r
116   MemHandler(0,2);\r
117   ot("  add r0,r0,r10 ;@ r0 = Memory Base + New PC\n");\r
118   ot("  mov lr,pc\n");\r
119   ot("  ldr pc,[r7,#0x64] ;@ Call checkpc()\n");\r
120   ot("  mov r4,r0\n");\r
121   ot("\n");\r
122   ot(";@ todo - swap OSP and A7 if not in Supervisor mode\n");\r
123   ot("  ldrb r0,[r7,#0x47] ;@ IRQ\n");\r
124   ot("  orr r0,r0,#0x20 ;@ Supervisor mode + IRQ number\n");\r
125   ot("  strb r0,[r7,#0x44] ;@ Put SR high\n");\r
126   ot("\n");\r
127   ot(";@ Clear irq:\n");\r
128   ot("  mov r0,#0\n");\r
129   ot("  strb r0,[r7,#0x47]\n");\r
130   ot("  subs r5,r5,#%d ;@ Subtract cycles\n",46);\r
131   ot("  mov pc,r11 ;@ Return\n");\r
132   ot("\n");\r
133 \r
134   ot("Exception%s\n", ms?"":":");\r
135   ot("\n");\r
136   ot("  mov r11,lr ;@ Preserve ARM return address\n");\r
137   PrintException();\r
138   ot("  mov pc,r11 ;@ Return\n");\r
139   ot("\n");\r
140 }\r
141 \r
142 // ---------------------------------------------------------------------------\r
143 // Call Read(r0), Write(r0,r1) or Fetch(r0)\r
144 // Trashes r0-r3\r
145 int MemHandler(int type,int size)\r
146 {\r
147   int func=0;\r
148   func=0x68+type*0xc+(size<<2); // Find correct offset\r
149 \r
150   if (Debug&4) ot("  str r4,[r7,#0x40] ;@ Save PC\n");\r
151   if (Debug&3) ot("  str r5,[r7,#0x5c] ;@ Save Cycles\n");\r
152 \r
153   ot("  mov lr,pc\n");\r
154   ot("  ldr pc,[r7,#0x%x] ;@ Call ",func);\r
155 \r
156   // Document what we are calling:\r
157   if (type==0) ot("read");\r
158   if (type==1) ot("write");\r
159   if (type==2) ot("fetch");\r
160 \r
161   if (type==1) ot("%d(r0,r1)",8<<size);\r
162   else         ot("%d(r0)",   8<<size);\r
163   ot(" handler\n");\r
164 \r
165   if (Debug&2) ot("  ldr r5,[r7,#0x5c] ;@ Load Cycles\n");\r
166   return 0;\r
167 }\r
168 \r
169 static void PrintOpcodes()\r
170 {\r
171   int op=0;\r
172  \r
173   printf("Creating Opcodes: [");\r
174 \r
175   ot(";@ ---------------------------- Opcodes ---------------------------\n");\r
176 \r
177   // Emit null opcode:\r
178   ot("Op____%s ;@ Called if an opcode is not recognised\n", ms?"":":");\r
179   OpStart(-1); Cycles=4; OpEnd(); //test\r
180 \r
181   ot("  b CycloneEnd\n\n");\r
182 \r
183   for (op=0;op<0x10000;op++)\r
184   {\r
185     if ((op&0xfff)==0) { printf("%x",op>>12); fflush(stdout); } // Update progress\r
186 \r
187     OpAny(op);\r
188   }\r
189 \r
190   ot("\n");\r
191 \r
192   printf("]\n");\r
193 }\r
194 \r
195 static void PrintJumpTable()\r
196 {\r
197   int i=0,op=0,len=0;\r
198 \r
199   ot(";@ -------------------------- Jump Table --------------------------\n");\r
200   ot("JumpTab%s\n", ms?"":":");\r
201 \r
202   len=0xfffe; // Hmmm, armasm 2.50.8684 messes up with a 0x10000 long jump table\r
203   for (i=0;i<len;i++)\r
204   {\r
205     op=CyJump[i];\r
206 \r
207     if ((i&7)==0) ot(ms?"  dcd ":"  .long ");\r
208     if (op<0) ot("Op____"); else ot("Op%.4x",op);\r
209     \r
210     if ((i&7)==7) ot(" ;@ %.4x\n",i-7);\r
211     else if (i+1<len) ot(",");\r
212   }\r
213 \r
214   ot("\n");\r
215 }\r
216 \r
217 static int CycloneMake()\r
218 {\r
219   char *name="Cyclone.s";\r
220   \r
221   // Open the assembly file\r
222   if (ms) name="Cyclone.asm";\r
223   AsmFile=fopen(name,"wt"); if (AsmFile==NULL) return 1;\r
224   \r
225   printf("Making %s...\n",name);\r
226 \r
227   ot("\n;@ Cyclone 68000 Emulator v%x.%.3x - Assembler Output\n\n",CycloneVer>>12,CycloneVer&0xfff);\r
228 \r
229   ot(";@ This code is licensed under the GNU General Public License version 2.0 and the MAME License.\n");\r
230   ot(";@ You can choose the license that has the most advantages for you.\n\n");\r
231   ot(";@ SVN repository can be found at http://code.google.com/p/cyclone68000/\n\n");\r
232 \r
233   CyJump=(int *)malloc(0x40000); if (CyJump==NULL) return 1;\r
234   memset(CyJump,0xff,0x40000); // Init to -1\r
235 \r
236   if (ms)\r
237   {\r
238     ot("  area |.text|, code\n");\r
239     ot("  export CycloneRun\n");\r
240     ot("  export CycloneVer\n");\r
241     ot("\n");\r
242     ot("CycloneVer dcd 0x%.4x\n",CycloneVer);\r
243   }\r
244   else\r
245   {\r
246     ot("  .global CycloneRun\n");\r
247     ot("  .global CycloneVer\n");\r
248     ot("CycloneVer: .long 0x%.4x\n",CycloneVer);\r
249   }\r
250   ot("\n");\r
251 \r
252   PrintFramework();\r
253   PrintOpcodes();\r
254   PrintJumpTable();\r
255 \r
256   if (ms) ot("  END\n");\r
257 \r
258   fclose(AsmFile); AsmFile=NULL;\r
259 \r
260   printf("Assembling...\n");\r
261   // Assemble the file\r
262   if (ms) system("armasm Cyclone.asm");\r
263   else    system("as -o Cyclone.o Cyclone.s");\r
264   printf("Done!\n\n");\r
265 \r
266   free(CyJump);\r
267   return 0;\r
268 }\r
269 \r
270 int main()\r
271 {\r
272   printf("\n  Cyclone 68000 Emulator v%x.%.3x - Core Creator\n\n",CycloneVer>>12,CycloneVer&0xfff);\r
273 \r
274   // Make GAS and ARMASM versions\r
275   for (ms=0;ms<2;ms++) CycloneMake();\r
276   return 0;\r
277 }\r