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