fixed a bug where the Opcode jump table was written to out of bounds
[cyclone68000.git] / Cyclone / OpAny.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 unsigned char OpData[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};\r
14\r
15static unsigned short CPU_CALL OpRead16(unsigned int a)\r
16{\r
17 return (unsigned short)( (OpData[a&15]<<8) | OpData[(a+1)&15] );\r
18}\r
19\r
20// For opcode 'op' use handler 'use'\r
21void OpUse(int op,int use)\r
22{\r
23 char text[64]="";\r
90b92e36 24 \r
25 if (op>=0)\r
26 CyJump[op]=use;\r
6003a768 27\r
28 if (op!=use) return;\r
29\r
30 // Disassemble opcode\r
31 DisaPc=0;\r
32 DisaText=text;\r
33 DisaWord=OpRead16;\r
34\r
35 DisaGet();\r
36 ot(";@ ---------- [%.4x] %s uses Op%.4x ----------\n",op,text,use);\r
37}\r
38\r
39void OpFirst()\r
40{\r
41 ot(" ldrh r8,[r4],#2 ;@ Fetch first opcode\n");\r
42 ot(" ldr pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");\r
43}\r
44\r
45void OpStart(int op)\r
46{\r
47 Cycles=0;\r
48 OpUse(op,op); // This opcode obviously uses this handler\r
49 ot("Op%.4x%s\n", op, ms?"":":");\r
50}\r
51\r
52void OpEnd()\r
53{\r
54 ot(" ldrh r8,[r4],#2 ;@ Fetch next opcode\n");\r
55 ot(" subs r5,r5,#%d ;@ Subtract cycles\n",Cycles);\r
56 ot(" ldrge pc,[r6,r8,asl #2] ;@ Jump to opcode handler\n");\r
57 ot(" b CycloneEnd\n");\r
58 ltorg();\r
59 ot("\n");\r
60}\r
61\r
62int OpBase(int op)\r
63{\r
64 int ea=op&0x3f; // Get Effective Address\r
65 if (ea<0x10) return op&~0xf; // Use 1 handler for d0-d7 and a0-a7\r
66 if (ea>=0x18 && ea<0x28 && (ea&7)==7) return op; // Specific handler for (a7)+ and -(a7)\r
67 if (ea<0x38) return op&~7; // Use 1 handler for (a0)-(a7), etc...\r
68 return op;\r
69}\r
70\r
71// Get flags, trashes r2\r
72int OpGetFlags(int subtract,int xbit)\r
73{\r
74 ot(" mrs r9,cpsr ;@ r9=flags\n");\r
75\r
76 if (subtract) ot(" eor r9,r9,#0x20000000 ;@ Invert carry\n");\r
77\r
78 if (Accu&1) if (xbit)\r
79 {\r
80 ot(" mov r2,r9,lsr #28\n");\r
81 ot(" strb r2,[r7,#0x45] ;@ Save X bit\n");\r
82 }\r
83 return 0;\r
84}\r
85\r
86// -----------------------------------------------------------------\r
87\r
88void OpAny(int op)\r
89{\r
90 memset(OpData,0x33,sizeof(OpData));\r
91 OpData[0]=(unsigned char)(op>>8);\r
92 OpData[1]=(unsigned char)op;\r
93\r
94 if ((op&0xf100)==0x0000) OpArith(op);\r
95 if ((op&0xc000)==0x0000) OpMove(op);\r
96 if ((op&0xf5bf)==0x003c) OpArithSr(op); // Ori/Andi/Eori $nnnn,sr\r
97 if ((op&0xf100)==0x0100) OpBtstReg(op);\r
98 if ((op&0xff00)==0x0800) OpBtstImm(op);\r
99 if ((op&0xf900)==0x4000) OpNeg(op);\r
100 if ((op&0xf1c0)==0x41c0) OpLea(op);\r
101 if ((op&0xf9c0)==0x40c0) OpMoveSr(op);\r
102 if ((op&0xfff8)==0x4840) OpSwap(op);\r
103 if ((op&0xffc0)==0x4840) OpPea(op);\r
104 if ((op&0xffb8)==0x4880) OpExt(op);\r
105 if ((op&0xfb80)==0x4880) OpMovem(op);\r
106 if ((op&0xff00)==0x4a00) OpTst(op);\r
107 if ((op&0xfff0)==0x4e40) OpTrap(op);\r
108 if ((op&0xfff8)==0x4e50) OpLink(op);\r
109 if ((op&0xfff8)==0x4e58) OpUnlk(op);\r
110 if ((op&0xfff0)==0x4e60) OpMoveUsp(op);\r
111 if ((op&0xfff8)==0x4e70) Op4E70(op); // Reset/Rts etc\r
112 if ((op&0xff80)==0x4e80) OpJsr(op);\r
113 if ((op&0xf000)==0x5000) OpAddq(op);\r
114 if ((op&0xf0c0)==0x50c0) OpSet(op);\r
115 if ((op&0xf0f8)==0x50c8) OpDbra(op);\r
116 if ((op&0xf000)==0x6000) OpBranch(op);\r
117 if ((op&0xf100)==0x7000) OpMoveq(op);\r
118 if ((op&0xa000)==0x8000) OpArithReg(op); // Or/Sub/And/Add\r
119 if ((op&0xb1f0)==0x8100) OpAbcd(op);\r
120 if ((op&0xb0c0)==0x80c0) OpMul(op);\r
121 if ((op&0x90c0)==0x90c0) OpAritha(op);\r
122 if ((op&0xb138)==0x9100) OpAddx(op);\r
123 if ((op&0xf000)==0xb000) OpCmpEor(op);\r
124 if ((op&0xf130)==0xc100) OpExg(op);\r
125 if ((op&0xf000)==0xe000) OpAsr(op); // Asr/l/Ror/l etc\r
126 if ((op&0xf8c0)==0xe0c0) OpAsrEa(op);\r
127\r
128}\r