Skip to content

Commit

Permalink
Refactor out EntryPage and Cpu from Table
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Jan 30, 2025
1 parent 1d6b050 commit 285492e
Show file tree
Hide file tree
Showing 53 changed files with 906 additions and 986 deletions.
5 changes: 2 additions & 3 deletions src/asm_mc6809.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,9 @@ void AsmMc6809::encodeIndexed(AsmInsn &insn, const Operand &op) const {
}
spec.size = size;
}
const auto postSpec = TABLE.searchPostSpec(cpuType(), spec);
if (postSpec < 0)
if (TABLE.searchPostSpec(cpuType(), spec))
insn.setErrorIf(op, UNKNOWN_OPERAND);
uint8_t post = postSpec;
uint8_t post = spec.post;
const auto size = spec.size;
if (size == 5)
post |= disp & 0x1F;
Expand Down
36 changes: 15 additions & 21 deletions src/table_cdp1802.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@

#include "table_cdp1802.h"
#include "entry_cdp1802.h"
#include "entry_table.h"
#include "text_cdp1802.h"

using namespace libasm::text::cdp1802;

namespace libasm {
namespace cdp1802 {

#define E2(_opc, _name, _op1, _op2) \
{ _opc, Entry::Flags::create(_op1, _op2), _name }
#define E2(_opc, _name, _op1, _op2) {_opc, Entry::Flags::create(_op1, _op2), _name}
#define E1(_opc, _name, _op1) E2(_opc, _name, _op1, M_NONE)
#define E0(_opc, _name) E1(_opc, _name, M_NONE)

// clang-format off
static constexpr Entry TABLE_CDP1802[] PROGMEM = {
constexpr Entry TABLE_CDP1802[] PROGMEM = {
E0(0x00, TEXT_IDL),
E1(0x00, TEXT_LDN, M_REG1),
E1(0x10, TEXT_INC, M_REGN),
Expand Down Expand Up @@ -124,7 +122,7 @@ static constexpr Entry TABLE_CDP1802[] PROGMEM = {
E1(0xFF, TEXT_SMI, M_IMM8),
};

static constexpr uint8_t INDEX_CDP1802[] PROGMEM = {
constexpr uint8_t INDEX_CDP1802[] PROGMEM = {
34, // TEXT_ADC
43, // TEXT_ADCI
79, // TEXT_ADD
Expand Down Expand Up @@ -218,7 +216,7 @@ static constexpr uint8_t INDEX_CDP1802[] PROGMEM = {
86, // TEXT_XRI
};

static constexpr Entry TABLE_CDP1804[] PROGMEM = {
constexpr Entry TABLE_CDP1804[] PROGMEM = {
E0(0x00, TEXT_STPC),
E0(0x01, TEXT_DTC),
E0(0x02, TEXT_SPM2),
Expand All @@ -243,7 +241,7 @@ static constexpr Entry TABLE_CDP1804[] PROGMEM = {
E2(0xC0, TEXT_RLDI, M_REGN, M_ADDR),
};

static constexpr uint8_t INDEX_CDP1804[] PROGMEM = {
constexpr uint8_t INDEX_CDP1804[] PROGMEM = {
14, // TEXT_BCI
15, // TEXT_BXI
13, // TEXT_CID
Expand All @@ -268,7 +266,7 @@ static constexpr uint8_t INDEX_CDP1804[] PROGMEM = {
10, // TEXT_XIE
};

static constexpr Entry TABLE_CDP1804A[] PROGMEM = {
constexpr Entry TABLE_CDP1804A[] PROGMEM = {
E2(0x20, TEXT_DBNZ, M_REGN, M_ADDR),
E0(0x74, TEXT_DADC),
E0(0x76, TEXT_DSAV),
Expand All @@ -281,7 +279,7 @@ static constexpr Entry TABLE_CDP1804A[] PROGMEM = {
E1(0xFF, TEXT_DSMI, M_IMM8),
};

static constexpr uint8_t INDEX_CDP1804A[] PROGMEM = {
constexpr uint8_t INDEX_CDP1804A[] PROGMEM = {
4, // TEXT_DACI
1, // TEXT_DADC
6, // TEXT_DADD
Expand All @@ -295,36 +293,32 @@ static constexpr uint8_t INDEX_CDP1804A[] PROGMEM = {
};
// clang-format on

using EntryPage = entry::PrefixTableBase<Entry>;

static constexpr EntryPage CDP1802_PAGES[] PROGMEM = {
constexpr EntryPage CDP1802_PAGES[] PROGMEM = {
{0x00, ARRAY_RANGE(TABLE_CDP1802), ARRAY_RANGE(INDEX_CDP1802)},
};

static constexpr EntryPage CDP1804_PAGES[] PROGMEM = {
constexpr EntryPage CDP1804_PAGES[] PROGMEM = {
{0x00, ARRAY_RANGE(TABLE_CDP1802), ARRAY_RANGE(INDEX_CDP1802)},
{0x68, ARRAY_RANGE(TABLE_CDP1804), ARRAY_RANGE(INDEX_CDP1804)},
};

static constexpr EntryPage CDP1804A_PAGES[] PROGMEM = {
constexpr EntryPage CDP1804A_PAGES[] PROGMEM = {
{0x00, ARRAY_RANGE(TABLE_CDP1802), ARRAY_RANGE(INDEX_CDP1802)},
{0x68, ARRAY_RANGE(TABLE_CDP1804), ARRAY_RANGE(INDEX_CDP1804)},
{0x68, ARRAY_RANGE(TABLE_CDP1804A), ARRAY_RANGE(INDEX_CDP1804A)},
};

using Cpu = entry::CpuBase<CpuType, EntryPage>;

static constexpr Cpu CPU_TABLE[] PROGMEM = {
constexpr Cpu CPU_TABLE[] PROGMEM = {
{CDP1802, TEXT_CPU_1802, ARRAY_RANGE(CDP1802_PAGES)},
{CDP1804, TEXT_CPU_1804, ARRAY_RANGE(CDP1804_PAGES)},
{CDP1804A, TEXT_CPU_1804A, ARRAY_RANGE(CDP1804A_PAGES)},
};

static const Cpu *cpu(CpuType cpuType) {
const Cpu *cpu(CpuType cpuType) {
return Cpu::search(cpuType, ARRAY_RANGE(CPU_TABLE));
}

static bool acceptMode(AddrMode opr, AddrMode table) {
bool acceptMode(AddrMode opr, AddrMode table) {
if (opr == table)
return true;
if (opr == M_REGN)
Expand All @@ -335,7 +329,7 @@ static bool acceptMode(AddrMode opr, AddrMode table) {
return false;
}

static bool acceptModes(AsmInsn &insn, const Entry *entry) {
bool acceptModes(AsmInsn &insn, const Entry *entry) {
const auto table = entry->readFlags();
return acceptMode(insn.op1.mode, table.mode1()) && acceptMode(insn.op2.mode, table.mode2());
}
Expand All @@ -345,7 +339,7 @@ Error TableCdp1802::searchName(CpuType cpuType, AsmInsn &insn) const {
return insn.getError();
}

static bool matchOpCode(DisInsn &insn, const Entry *entry, const EntryPage *) {
bool matchOpCode(DisInsn &insn, const Entry *entry, const EntryPage *) {
auto opc = insn.opCode();
auto flags = entry->readFlags();
auto mode = flags.mode1();
Expand Down
5 changes: 4 additions & 1 deletion src/table_cdp1802.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
#define __LIBASM_TABLE_CDP1802_H__

#include "config_cdp1802.h"
#include "entry_table.h"
#include "insn_cdp1802.h"
#include "str_buffer.h"

namespace libasm {
namespace cdp1802 {

using EntryPage = entry::PrefixTableBase<Entry>;
using Cpu = entry::CpuBase<CpuType, EntryPage>;

struct TableCdp1802 final : InsnTable<CpuType> {
const /*PROGMEM*/ char *listCpu_P() const override;
const /*PROGMEM*/ char *cpuName_P(CpuType cpuType) const override;
Expand Down
24 changes: 9 additions & 15 deletions src/table_f3850.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@

#include "table_f3850.h"
#include "entry_f3850.h"
#include "entry_table.h"
#include "text_f3850.h"

using namespace libasm::text::f3850;

namespace libasm {
namespace f3850 {

#define E2(_opc, _name, _mode1, _mode2) \
{ _opc, Entry::Flags::create(_mode1, _mode2), _name }
#define E2(_opc, _name, _mode1, _mode2) {_opc, Entry::Flags::create(_mode1, _mode2), _name}
#define E1(_opc, _name, _mode1) E2(_opc, _name, _mode1, M_NONE)
#define E0(_opc, _name) E1(_opc, _name, M_NONE)

// clang-format off
static constexpr Entry TABLE_3850[] PROGMEM = {
constexpr Entry TABLE_3850[] PROGMEM = {
E2(0x00, TEXT_LR, M_A, M_KU),
E2(0x01, TEXT_LR, M_A, M_KL),
E2(0x02, TEXT_LR, M_A, M_QU),
Expand Down Expand Up @@ -109,7 +107,7 @@ static constexpr Entry TABLE_3850[] PROGMEM = {
E1(0xF0, TEXT_NS, M_REG),
};

static constexpr uint8_t INDEX_3850[] PROGMEM = {
constexpr uint8_t INDEX_3850[] PROGMEM = {
61, // TEXT_ADC
36, // TEXT_AI
55, // TEXT_AM
Expand Down Expand Up @@ -190,19 +188,15 @@ static constexpr uint8_t INDEX_3850[] PROGMEM = {

// clang-format on

using EntryPage = entry::TableBase<Entry>;

static constexpr EntryPage F3850_PAGES[] PROGMEM = {
constexpr EntryPage F3850_PAGES[] PROGMEM = {
{ARRAY_RANGE(TABLE_3850), ARRAY_RANGE(INDEX_3850)},
};

using Cpu = entry::CpuBase<CpuType, EntryPage>;

static constexpr Cpu CPU_TABLE[] PROGMEM = {
constexpr Cpu CPU_TABLE[] PROGMEM = {
{F3850, TEXT_CPU_3850, ARRAY_RANGE(F3850_PAGES)},
};

static const Cpu *cpu(CpuType) {
const Cpu *cpu(CpuType) {
return &CPU_TABLE[0];
}

Expand All @@ -211,7 +205,7 @@ bool TableF3850::hasOperand(CpuType cpuType, AsmInsn &insn) const {
return insn.isOK() && insn.mode1() != M_NONE;
}

static bool acceptMode(AddrMode opr, AddrMode table) {
bool acceptMode(AddrMode opr, AddrMode table) {
if (opr == table)
return true;
if (opr == M_J)
Expand All @@ -223,7 +217,7 @@ static bool acceptMode(AddrMode opr, AddrMode table) {
return false;
}

static bool acceptModes(AsmInsn &insn, const Entry *entry) {
bool acceptModes(AsmInsn &insn, const Entry *entry) {
const auto table = entry->readFlags();
return acceptMode(insn.op1.mode, table.mode1()) && acceptMode(insn.op2.mode, table.mode2());
}
Expand All @@ -233,7 +227,7 @@ Error TableF3850::searchName(CpuType cpuType, AsmInsn &insn) const {
return insn.getError();
}

static bool matchOpCode(DisInsn &insn, const Entry *entry, const EntryPage *) {
bool matchOpCode(DisInsn &insn, const Entry *entry, const EntryPage *) {
auto opCode = insn.opCode();
const auto flags = entry->readFlags();
const auto mode1 = flags.mode1();
Expand Down
4 changes: 4 additions & 0 deletions src/table_f3850.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
#define __LIBASM_TABLE_F3850_H__

#include "config_f3850.h"
#include "entry_table.h"
#include "insn_f3850.h"

namespace libasm {
namespace f3850 {

using EntryPage = entry::TableBase<Entry>;
using Cpu = entry::CpuBase<CpuType, EntryPage>;

struct TableF3850 final : InsnTable<CpuType> {
const /*PROGMEM*/ char *listCpu_P() const override;
const /*PROGMEM*/ char *cpuName_P(CpuType cpuType) const override;
Expand Down
48 changes: 21 additions & 27 deletions src/table_i8048.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@

#include "table_i8048.h"
#include "entry_i8048.h"
#include "entry_table.h"
#include "text_i8048.h"

using namespace libasm::text::i8048;

namespace libasm {
namespace i8048 {

#define E2(_opc, _name, _dst, _src) \
{ _opc, Entry::Flags::create(_dst, _src), _name }
#define E2(_opc, _name, _dst, _src) {_opc, Entry::Flags::create(_dst, _src), _name}
#define E1(_opc, _name, _dst) E2(_opc, _name, _dst, M_NONE)
#define E0(_opc, _name) E1(_opc, _name, M_NONE)

// clang-format off
static constexpr Entry TABLE_I8039[] PROGMEM = {
constexpr Entry TABLE_I8039[] PROGMEM = {
E2(0x68, TEXT_ADD, M_A, M_R),
E2(0x60, TEXT_ADD, M_A, M_IR),
E2(0x03, TEXT_ADD, M_A, M_IMM8),
Expand Down Expand Up @@ -121,7 +119,7 @@ static constexpr Entry TABLE_I8039[] PROGMEM = {
E0(0x00, TEXT_NOP),
};

static constexpr uint8_t INDEX_I8039[] PROGMEM = {
constexpr uint8_t INDEX_I8039[] PROGMEM = {
0, // TEXT_ADD
1, // TEXT_ADD
2, // TEXT_ADD
Expand Down Expand Up @@ -212,29 +210,29 @@ static constexpr uint8_t INDEX_I8039[] PROGMEM = {
14, // TEXT_XRL
};

static constexpr Entry TABLE_I8048[] PROGMEM = {
constexpr Entry TABLE_I8048[] PROGMEM = {
E2(0x08, TEXT_INS, M_A, M_BUS),
E2(0x02, TEXT_OUTL, M_BUS, M_A),
E2(0x98, TEXT_ANL, M_BUS, M_BIT8),
E2(0x88, TEXT_ORL, M_BUS, M_BIT8),
};

static constexpr uint8_t INDEX_I8048[] PROGMEM = {
constexpr uint8_t INDEX_I8048[] PROGMEM = {
2, // TEXT_ANL
0, // TEXT_INS
3, // TEXT_ORL
1, // TEXT_OUTL
};

static constexpr Entry TABLE_I80C39[] PROGMEM = {
constexpr Entry TABLE_I80C39[] PROGMEM = {
E0(0x01, TEXT_HALT),
};

static constexpr uint8_t INDEX_I80C39[] PROGMEM = {
constexpr uint8_t INDEX_I80C39[] PROGMEM = {
0, // TEXT_HALT
};

static constexpr Entry TABLE_MSM80C39[] PROGMEM = {
constexpr Entry TABLE_MSM80C39[] PROGMEM = {
E2(0x63, TEXT_MOV, M_A, M_P1),
E2(0x73, TEXT_MOV, M_A, M_P2),
E1(0xC0, TEXT_DEC, M_IR),
Expand All @@ -247,7 +245,7 @@ static constexpr Entry TABLE_MSM80C39[] PROGMEM = {
E0(0xE2, TEXT_FRES),
};

static constexpr uint8_t INDEX_MSM80C39[] PROGMEM = {
constexpr uint8_t INDEX_MSM80C39[] PROGMEM = {
2, // TEXT_DEC
3, // TEXT_DJNZ
7, // TEXT_FLT
Expand All @@ -261,31 +259,27 @@ static constexpr uint8_t INDEX_MSM80C39[] PROGMEM = {
};
// clang-format on

using EntryPage = entry::TableBase<Entry>;

static constexpr EntryPage INST_PAGES[] PROGMEM = {
constexpr EntryPage INST_PAGES[] PROGMEM = {
{ARRAY_RANGE(TABLE_I8048), ARRAY_RANGE(INDEX_I8048)}, // 0
{ARRAY_RANGE(TABLE_I8039), ARRAY_RANGE(INDEX_I8039)}, // 1
{ARRAY_RANGE(TABLE_I80C39), ARRAY_RANGE(INDEX_I80C39)}, // 2
{ARRAY_RANGE(TABLE_MSM80C39), ARRAY_RANGE(INDEX_MSM80C39)}, // 3
};

using Cpu = entry::CpuBase<CpuType, EntryPage>;

static constexpr Cpu CPU_TABLE[] PROGMEM = {
{I8039, TEXT_CPU_8039, &INST_PAGES[1], &INST_PAGES[2]},
{I80C39, TEXT_CPU_80C39, &INST_PAGES[1], &INST_PAGES[3]},
{MSM80C39, TEXT_CPU_MSM80C39, &INST_PAGES[1], &INST_PAGES[4]},
{I8048, TEXT_CPU_8048, &INST_PAGES[0], &INST_PAGES[2]},
{I80C48, TEXT_CPU_80C48, &INST_PAGES[0], &INST_PAGES[3]},
{MSM80C48, TEXT_CPU_MSM80C48, &INST_PAGES[0], &INST_PAGES[4]},
constexpr Cpu CPU_TABLE[] PROGMEM = {
{I8039, TEXT_CPU_8039, &INST_PAGES[1], &INST_PAGES[2]},
{I80C39, TEXT_CPU_80C39, &INST_PAGES[1], &INST_PAGES[3]},
{MSM80C39, TEXT_CPU_MSM80C39, &INST_PAGES[1], &INST_PAGES[4]},
{I8048, TEXT_CPU_8048, &INST_PAGES[0], &INST_PAGES[2]},
{I80C48, TEXT_CPU_80C48, &INST_PAGES[0], &INST_PAGES[3]},
{MSM80C48, TEXT_CPU_MSM80C48, &INST_PAGES[0], &INST_PAGES[4]},
};

static const Cpu *cpu(CpuType cpuType) {
const Cpu *cpu(CpuType cpuType) {
return Cpu::search(cpuType, ARRAY_RANGE(CPU_TABLE));
}

static bool acceptMode(AddrMode opr, AddrMode table) {
bool acceptMode(AddrMode opr, AddrMode table) {
if (opr == table)
return true;
if (opr == M_IMM8)
Expand All @@ -297,7 +291,7 @@ static bool acceptMode(AddrMode opr, AddrMode table) {
return false;
}

static bool acceptModes(AsmInsn &insn, const Entry *entry) {
bool acceptModes(AsmInsn &insn, const Entry *entry) {
const auto table = entry->readFlags();
return acceptMode(insn.dstOp.mode, table.dst()) && acceptMode(insn.srcOp.mode, table.src());
}
Expand All @@ -307,7 +301,7 @@ Error TableI8048::searchName(CpuType cpuType, AsmInsn &insn) const {
return insn.getError();
}

static bool matchOpCode(DisInsn &insn, const Entry *entry, const EntryPage *) {
bool matchOpCode(DisInsn &insn, const Entry *entry, const EntryPage *) {
auto opc = insn.opCode();
const auto flags = entry->readFlags();
const auto dst = flags.dst();
Expand Down
Loading

0 comments on commit 285492e

Please sign in to comment.