4 static FILE *AsmFile=NULL;
\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
16 void ot(char *format, ...)
\r
18 va_list valist=NULL;
\r
19 va_start(valist,format);
\r
20 if (AsmFile) vfprintf(AsmFile,format,valist);
\r
26 if (ms) ot(" LTORG\n");
\r
27 else ot(" .ltorg\n");
\r
30 static void PrintException()
\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
37 ot(" ldr r0,[r7,#0x50] ;@ Get Vector\n");
\r
38 ot(";@ Read IRQ Vector:\n");
\r
40 ot(" add r0,r0,r10 ;@ r0 = Memory Base + New PC\n");
\r
42 ot(" ldr pc,[r7,#0x64] ;@ Call checkpc()\n");
\r
46 // todo - make Interrupt code use this function as well
\r
50 void CheckInterrupt()
\r
52 ot(";@ CheckInterrupt:\n");
\r
53 ot(" ldrb r0,[r7,#0x47] ;@ Get IRQ level\n");
\r
55 ot(" blne DoInterrupt\n");
\r
59 static void PrintFramework()
\r
61 ot(";@ --------------------------- Framework --------------------------\n");
\r
62 if (ms) ot("CycloneRun\n");
\r
63 else ot("CycloneRun:\n");
\r
65 ot(" stmdb sp!,{r4-r11,lr}\n");
\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
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
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
97 ot(";@ DoInterrupt - r0=IRQ number\n");
\r
98 ot("DoInterrupt%s\n", ms?"":":");
\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
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
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
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
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
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
134 ot("Exception%s\n", ms?"":":");
\r
136 ot(" mov r11,lr ;@ Preserve ARM return address\n");
\r
138 ot(" mov pc,r11 ;@ Return\n");
\r
142 // ---------------------------------------------------------------------------
\r
143 // Call Read(r0), Write(r0,r1) or Fetch(r0)
\r
145 int MemHandler(int type,int size)
\r
148 func=0x68+type*0xc+(size<<2); // Find correct offset
\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
153 ot(" mov lr,pc\n");
\r
154 ot(" ldr pc,[r7,#0x%x] ;@ Call ",func);
\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
161 if (type==1) ot("%d(r0,r1)",8<<size);
\r
162 else ot("%d(r0)", 8<<size);
\r
165 if (Debug&2) ot(" ldr r5,[r7,#0x5c] ;@ Load Cycles\n");
\r
169 static void PrintOpcodes()
\r
173 printf("Creating Opcodes: [");
\r
175 ot(";@ ---------------------------- Opcodes ---------------------------\n");
\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
181 ot(" b CycloneEnd\n\n");
\r
183 for (op=0;op<0x10000;op++)
\r
185 if ((op&0xfff)==0) { printf("%x",op>>12); fflush(stdout); } // Update progress
\r
195 static void PrintJumpTable()
\r
197 int i=0,op=0,len=0;
\r
199 ot(";@ -------------------------- Jump Table --------------------------\n");
\r
200 ot("JumpTab%s\n", ms?"":":");
\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
207 if ((i&7)==0) ot(ms?" dcd ":" .long ");
\r
208 if (op<0) ot("Op____"); else ot("Op%.4x",op);
\r
210 if ((i&7)==7) ot(" ;@ %.4x\n",i-7);
\r
211 else if (i+1<len) ot(",");
\r
217 static int CycloneMake()
\r
219 char *name="Cyclone.s";
\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
225 printf("Making %s...\n",name);
\r
227 ot("\n;@ Cyclone 68000 Emulator v%x.%.3x - Assembler Output\n\n",CycloneVer>>12,CycloneVer&0xfff);
\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
233 CyJump=(int *)malloc(0x40000); if (CyJump==NULL) return 1;
\r
234 memset(CyJump,0xff,0x40000); // Init to -1
\r
238 ot(" area |.text|, code\n");
\r
239 ot(" export CycloneRun\n");
\r
240 ot(" export CycloneVer\n");
\r
242 ot("CycloneVer dcd 0x%.4x\n",CycloneVer);
\r
246 ot(" .global CycloneRun\n");
\r
247 ot(" .global CycloneVer\n");
\r
248 ot("CycloneVer: .long 0x%.4x\n",CycloneVer);
\r
256 if (ms) ot(" END\n");
\r
258 fclose(AsmFile); AsmFile=NULL;
\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
272 printf("\n Cyclone 68000 Emulator v%x.%.3x - Core Creator\n\n",CycloneVer>>12,CycloneVer&0xfff);
\r
274 // Make GAS and ARMASM versions
\r
275 for (ms=0;ms<2;ms++) CycloneMake();
\r