2 // This file is part of the Cyclone 68000 Emulator
\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
7 // SVN repository can be found at http://code.google.com/p/cyclone68000/
\r
11 static FILE *AsmFile=NULL;
\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
23 void ot(char *format, ...)
\r
25 va_list valist=NULL;
\r
26 va_start(valist,format);
\r
27 if (AsmFile) vfprintf(AsmFile,format,valist);
\r
33 if (ms) ot(" LTORG\n");
\r
34 else ot(" .ltorg\n");
\r
37 static void PrintException()
\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
44 ot(" ldr r0,[r7,#0x50] ;@ Get Vector\n");
\r
45 ot(";@ Read IRQ Vector:\n");
\r
47 ot(" add r0,r0,r10 ;@ r0 = Memory Base + New PC\n");
\r
49 ot(" ldr pc,[r7,#0x64] ;@ Call checkpc()\n");
\r
53 // todo - make Interrupt code use this function as well
\r
57 void CheckInterrupt()
\r
59 ot(";@ CheckInterrupt:\n");
\r
60 ot(" ldrb r0,[r7,#0x47] ;@ Get IRQ level\n");
\r
62 ot(" blne DoInterrupt\n");
\r
66 static void PrintFramework()
\r
68 ot(";@ --------------------------- Framework --------------------------\n");
\r
69 if (ms) ot("CycloneRun\n");
\r
70 else ot("CycloneRun:\n");
\r
72 ot(" stmdb sp!,{r4-r11,lr}\n");
\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
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
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
104 ot(";@ DoInterrupt - r0=IRQ number\n");
\r
105 ot("DoInterrupt%s\n", ms?"":":");
\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
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
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
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
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
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
141 ot("Exception%s\n", ms?"":":");
\r
143 ot(" mov r11,lr ;@ Preserve ARM return address\n");
\r
145 ot(" mov pc,r11 ;@ Return\n");
\r
149 // ---------------------------------------------------------------------------
\r
150 // Call Read(r0), Write(r0,r1) or Fetch(r0)
\r
152 int MemHandler(int type,int size)
\r
155 func=0x68+type*0xc+(size<<2); // Find correct offset
\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
160 ot(" mov lr,pc\n");
\r
161 ot(" ldr pc,[r7,#0x%x] ;@ Call ",func);
\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
168 if (type==1) ot("%d(r0,r1)",8<<size);
\r
169 else ot("%d(r0)", 8<<size);
\r
172 if (Debug&2) ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");
\r
176 static void PrintOpcodes()
\r
180 printf("Creating Opcodes: [");
\r
182 ot(";@ ---------------------------- Opcodes ---------------------------\n");
\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
188 ot(" b CycloneEnd\n\n");
\r
190 for (op=0;op<0x10000;op++)
\r
192 if ((op&0xfff)==0) { printf("%x",op>>12); fflush(stdout); } // Update progress
\r
202 static void PrintJumpTable()
\r
204 int i=0,op=0,len=0;
\r
206 ot(";@ -------------------------- Jump Table --------------------------\n");
\r
207 ot("JumpTab%s\n", ms?"":":");
\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
214 if ((i&7)==0) ot(ms?" dcd ":" .long ");
\r
215 if (op<0) ot("Op____"); else ot("Op%.4x",op);
\r
217 if ((i&7)==7) ot(" ;@ %.4x\n",i-7);
\r
218 else if (i+1<len) ot(",");
\r
224 static int CycloneMake()
\r
226 char *name="Cyclone.s";
\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
232 printf("Making %s...\n",name);
\r
234 ot("\n;@ Cyclone 68000 Emulator v%x.%.3x - Assembler Output\n\n",CycloneVer>>12,CycloneVer&0xfff);
\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
240 CyJump=(int *)malloc(0x40000); if (CyJump==NULL) return 1;
\r
241 memset(CyJump,0xff,0x40000); // Init to -1
\r
245 ot(" area |.text|, code\n");
\r
246 ot(" export CycloneRun\n");
\r
247 ot(" export CycloneVer\n");
\r
249 ot("CycloneVer dcd 0x%.4x\n",CycloneVer);
\r
253 ot(" .global CycloneRun\n");
\r
254 ot(" .global CycloneVer\n");
\r
255 ot("CycloneVer: .long 0x%.4x\n",CycloneVer);
\r
263 if (ms) ot(" END\n");
\r
265 fclose(AsmFile); AsmFile=NULL;
\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
279 printf("\n Cyclone 68000 Emulator v%x.%.3x - Core Creator\n\n",CycloneVer>>12,CycloneVer&0xfff);
\r
281 // Make GAS and ARMASM versions
\r
282 for (ms=0;ms<2;ms++) CycloneMake();
\r