Skip to content

Ghidra is a software reverse engineering (SRE) framework

Add support for 65C02 opcodes.

Alessandro Gattib91b43100def
diff --git a/Ghidra/Processors/6502/data/languages/6502.ldefs b/Ghidra/Processors/6502/data/languages/6502.ldefs
index dd4ea563f5..cbe053b9cb 100644
--- a/Ghidra/Processors/6502/data/languages/6502.ldefs
+++ b/Ghidra/Processors/6502/data/languages/6502.ldefs
@@ -16,7 +16,21 @@
<external_name tool="IDA-PRO" name="m6502"/>
<external_name tool="IDA-PRO" name="m65c02"/>
</language>
-
+
+ <language processor="65C02"
+ endian="little"
+ size="16"
+ variant="default"
+ version="1.0"
+ slafile="65c02.sla"
+ processorspec="6502.pspec"
+ manualindexfile="../manuals/65c02.idx"
+ id="65C02:LE:16:default">
+ <description>65C02 Microcontroller Family</description>
+ <compiler name="default" spec="6502.cspec" id="default"/>
+ <external_name tool="IDA-PRO" name="m65c02"/>
+ </language>
+
<!-- The following entry has a very misleading language id but
can not be removed since it may have already been referenced.
This entry has been marked deprecated to avoid its use in the future.
diff --git a/Ghidra/Processors/6502/data/languages/65c02.slaspec b/Ghidra/Processors/6502/data/languages/65c02.slaspec
new file mode 100644
index 0000000000..a640234651
--- /dev/null
+++ b/Ghidra/Processors/6502/data/languages/65c02.slaspec
@@ -0,0 +1,184 @@
+@include "6502.slaspec"
+
+define token testopbyte (8)
+ top = (0, 7)
+ taaa = (5, 7)
+ td = (4, 4)
+ tbb = (2, 3)
+ tcc = (0, 1)
+;
+
+################################################################
+
+# Zero Page Indirect
+ZIOP: (imm8) is bbb=4; imm8 { addr:2 = imm8; tmp:2 = *:2 addr; export *:1 tmp; }
+
+OPTB: imm8 is tbb=1; imm8 { export *:1 imm8; }
+OPTB: imm16 is tbb=3; imm16 { export *:1 imm16; }
+
+# Absolute Indexed Indirect
+ADDRIX: (imm16,X) is X; imm16 { addr:2 = imm16 + zext(X); tmp:2 = *:2 addr; export tmp; }
+
+# Instructions
+
+:ADC ZIOP is (cc=2 & aaa=3) ... & ZIOP
+{
+ local op1 = ZIOP;
+ local tmpC = C;
+
+ C = carry(A, op1);
+ A = A + op1 + tmpC;
+ resultFlags(A);
+ V = C;
+}
+
+:AND ZIOP is (cc=2 & aaa=1) ... & ZIOP
+{
+ A = A & ZIOP;
+ resultFlags(A);
+}
+
+:BIT "#"imm8 is op=0x89; imm8
+{
+ local value:1 = imm8;
+ N = (value & 0x80) == 0x80;
+ V = (value & 0x40) == 0x40;
+ value = A & value;
+ Z = (value == 0);
+}
+
+:BIT OP2 is (op=0x34 | op=0x3C) ... & OP2
+{
+ N = (OP2 & 0x80) == 0x80;
+ V = (OP2 & 0x40) == 0x40;
+ local value = A & OP2;
+ Z = (value == 0);
+}
+
+:BRA REL is op=0x80; REL
+{
+ goto REL;
+}
+
+:CMP ZIOP is (cc=2 & aaa=6) ... & ZIOP
+{
+ local op1 = ZIOP;
+ local tmp = A - op1;
+ resultFlags(tmp);
+ C = (A >= op1);
+}
+
+:DEC A is op=0x3A & A
+{
+ local tmp = A - 1;
+ A = tmp;
+ resultFlags(tmp);
+}
+
+:EOR ZIOP is (cc=2 & aaa=2) ... & ZIOP
+{
+ local op1 = ZIOP;
+ A = A ^ op1;
+ resultFlags(A);
+}
+
+:INC A is op=0x1A & A
+{
+ A = A + 1;
+ resultFlags(A);
+}
+
+:JMP ADDRIX is (op=0x7C); ADDRIX
+{
+ goto [ADDRIX];
+}
+
+:LDA ZIOP is (cc=2 & aaa=5) ... & ZIOP
+{
+ A = ZIOP;
+ resultFlags(A);
+}
+
+:ORA ZIOP is (cc=2 & aaa=0) ... & ZIOP
+{
+ A = A | ZIOP;
+ resultFlags(A);
+}
+
+:PHX is op=0xDA
+{
+ *:1 (SP) = X;
+ SP = SP - 1;
+}
+
+:PLX is op=0xFA
+{
+ SP = SP + 1;
+ X = *:1 (SP);
+ resultFlags(X);
+}
+
+:PHY is op=0x5A
+{
+ *:1 (SP) = Y;
+ SP = SP - 1;
+}
+
+:PLY is op=0x7A
+{
+ SP = SP + 1;
+ Y = *:1 (SP);
+ resultFlags(Y);
+}
+
+:SBC ZIOP is (cc=2 & aaa=7) ... & ZIOP
+{
+ local op1 = ZIOP;
+ local result = A - op1 - C;
+
+ subtraction_flags1(A, op1, result);
+ A = result;
+}
+
+:STA ZIOP is (cc=2 & aaa=4) ... & ZIOP
+{
+ ZIOP = A;
+}
+
+:STZ imm8 is op=0x64 ; imm8
+{
+ local tmp:2 = imm8;
+ *:1 tmp = 0;
+}
+
+:STZ imm8,X is op=0x74 & X ; imm8
+{
+ local tmp:2 = zext(imm8 + X);
+ *:1 tmp = 0;
+}
+
+:STZ imm16 is op=0x9C ; imm16
+{
+ local tmp:2 = imm16;
+ *:1 tmp = 0;
+}
+
+:STZ imm16,X is op=0x9E & X ; imm16
+{
+ local tmp:2 = imm16 + zext(X);
+ *:1 tmp = 0;
+}
+
+:TRB OPTB is (tcc=0 & taaa=0 & td=1) ... & OPTB
+{
+ local op1 = OPTB;
+ local result = A & ~op1;
+ Z = result == 0;
+}
+
+:TSB OPTB is (tcc=0 & taaa=0 & td=0) ... & OPTB
+{
+ local op1 = OPTB;
+ local result = A | op1;
+ Z = result == 0;
+}
diff --git a/Ghidra/Processors/6502/data/manuals/65c02.idx b/Ghidra/Processors/6502/data/manuals/65c02.idx
new file mode 100644
index 0000000000..88feff6332
--- /dev/null
+++ b/Ghidra/Processors/6502/data/manuals/65c02.idx
@@ -0,0 +1,65 @@
+@wdc_65816_programming_manual.pdf [Programming the 65816 - Including the 6502, 65C02 and 65802, 2007]
+ADC, 327
+AND, 328
+ASL, 329
+BCC, 330
+BCS, 331
+BEQ, 332
+BIT, 333
+BMI, 334
+BNE, 335
+BPL, 336
+BRA, 337
+BRK, 338
+BVC, 341
+BVS, 342
+CLC, 343
+CLD, 344
+CLI, 345
+CLV, 346
+CMP, 347
+CPX, 350
+CPY, 351
+DEC, 352
+DEX, 353
+DEY, 354
+EOR, 355
+INC, 357
+INX, 358
+INY, 359
+JMP, 360
+JSR, 362
+LDA, 363
+LDX, 364
+LDY, 365
+LSR, 366
+NOP, 369
+ORA, 370
+PHA, 375
+PHP, 379
+PHX, 380
+PHY, 381
+PLA, 382
+PLP, 385
+PLX, 386
+PLY, 387
+ROL, 389
+ROR, 390
+RTI, 391
+RTS, 393
+SBC, 395
+SEC, 397
+SED, 398
+SEI, 399
+STA, 401
+STX, 403
+STY, 404
+STZ, 405
+TAX, 406
+TAY, 407
+TRB, 411
+TSB, 412
+TSX, 414
+TXA, 415
+TXS, 416
+TYA, 418