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