Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/indigodarkwolf/box16 into…
Browse files Browse the repository at this point in the history
… boxmon
  • Loading branch information
indigodarkwolf committed Dec 30, 2023
2 parents 6150801 + 4d2cc36 commit 55c0e6d
Show file tree
Hide file tree
Showing 28 changed files with 1,151 additions and 233 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Build status](https://github.com/indigodarkwolf/box16/actions/workflows/build.yml/badge.svg)](https://github.com/indigodarkwolf/box16/actions/workflows/build.yml)<br/>
[![Release](https://img.shields.io/github/v/release/indigodarkwolf/box16)](https://github.com/indigodarkwolf/box16/releases)

This is an emulator for the Commander X16 computer system. Unlike [the official emulator](https://github.com/commanderx16/x16-emulator), this has a few more dependencies, see the build instructions below. It compiles on Windows, Debian Linux, and Raspbian, and probably
This is an emulator for the Commander X16 computer system. Unlike [the official emulator](https://github.com/X16Community/x16-emulator), this has a few more dependencies, see the build instructions below. It compiles on Windows, Debian Linux, and Raspbian, and probably
other Linux-based platforms.

Don't expect official "releases" until the physical X16 is out. Until then, there will be "non-releases" of Box16.
Expand Down Expand Up @@ -49,7 +49,7 @@ loaded from the directory containing the emulator binary, or you can use the `-r

> __WARNING:__ Older versions of the ROM might not work in newer versions of the emulator, and vice versa.
You can build a ROM image yourself using the [build instructions](https://github.com/commanderx16/x16-rom#releases-and-building) in the [x16-rom](https://github.com/commanderx16/x16-rom) repo. The `rom.bin` included in the [_latest_ release](https://github.com/commanderx16/x16-emulator/releases) of the emulator may also work with the HEAD of this repo, but this is not guaranteed.
You can build a ROM image yourself using the [build instructions](https://github.com/X16Community/x16-rom#releases-and-building) in the [x16-rom](https://github.com/X16Community/x16-rom) repo. The `rom.bin` included in the [_latest_ release](https://github.com/X16Community/x16-emulator/releases) of the emulator may also work with the HEAD of this repo, but this is not guaranteed.

### Linux Build

Expand Down Expand Up @@ -355,7 +355,7 @@ Forum
Wiki
----

[https://github.com/commanderx16/x16-emulator/wiki](https://github.com/commanderx16/x16-emulator/wiki)
[https://github.com/commanderx16/x16-emulator/wiki](https://github.com/X16Community/x16-emulator/wiki)


License
Expand Down
1 change: 0 additions & 1 deletion src/boxmon/boxmon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ enum class parse_command_result {

void boxmon_system_init()
{
boxmon::boxmon_command::finalize_list();
}

void boxmon_system_shutdown()
Expand Down
51 changes: 16 additions & 35 deletions src/boxmon/command.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "command.h"

#include <algorithm>
#include <string.h>
#include <string>

#include "boxmon.h"
#include "parser.h"
Expand All @@ -20,7 +20,7 @@ namespace boxmon
m_run(fn)
{
auto &command_list = get_command_list();
command_list.push_back(this);
command_list.insert({ name, this });
}

std::strong_ordering boxmon_command::operator<=>(char const *name) const
Expand Down Expand Up @@ -48,61 +48,39 @@ namespace boxmon
return m_description;
}

void boxmon_command::finalize_list()
{
auto &command_list = get_command_list();
std::sort(begin(command_list), end(command_list), [](const boxmon_command *a, const boxmon_command *b) { return *a > *b; });
}

const boxmon_command *boxmon_command::find(char const *name)
{
const auto &command_list = get_command_list();

size_t search_min = 0;
size_t search_max = command_list.size()-1;

while (search_min != search_max) {
const auto search_i = (search_min + search_max) >> 1;
const auto cmp_i = *command_list[search_i] <=> name;
if (is_eq(cmp_i)) {
return command_list[search_i];
} else if (is_lt(cmp_i)) {
search_max = std::max(search_min, search_i - 1);
} else {
search_min = std::min(search_max, search_i + 1);
}
}

if (is_eq(*command_list[search_min] <=> name)) {
return command_list[search_min];
const auto icmd = command_list.find(name);
if (icmd != command_list.end()) {
return icmd->second;
}

return nullptr;
}

void boxmon_command::for_each(std::function<void(const boxmon_command *cmd)> fn)
{
const auto &command_list = get_command_list();
for (auto cmd : command_list) {
fn(cmd);
fn(cmd.second);
}
}

void boxmon_command::for_each_partial(char const *name, std::function<void(const boxmon_command *cmd)> fn)
{
const auto &command_list = get_command_list();
for (auto cmd : command_list) {
if (strstr(cmd->get_name(), name) != nullptr) {
fn(cmd);
} else if (strstr(cmd->get_description(), name) != nullptr) {
fn(cmd);
if (strstr(cmd.second->get_name(), name) != nullptr) {
fn(cmd.second);
} else if (strstr(cmd.second->get_description(), name) != nullptr) {
fn(cmd.second);
}
}
}

std::vector<const boxmon_command *> &boxmon_command::get_command_list()
std::map<const std::string, const boxmon_command *> &boxmon_command::get_command_list()
{
static std::vector<const boxmon_command *> command_list;
static std::map<const std::string, const boxmon_command *> command_list;
return command_list;
}

Expand Down Expand Up @@ -196,7 +174,7 @@ BOXMON_ALIAS(al, add_label);
BOXMON_COMMAND(backtrace, "backtrace")
{
char const *names[] = { "N", "V", "-", "B", "D", "I", "Z", "C" };
for (size_t i = 0; i < stack6502.count(); ++i) {
for (size_t i = 0; i < state6502.sp_depth; ++i) {
const auto &ss = stack6502[static_cast<int>(i)];
char const *op = [&]() -> char const * {
switch (ss.op_type) {
Expand All @@ -206,6 +184,9 @@ BOXMON_COMMAND(backtrace, "backtrace")
case _stack_op_type::irq:
return "IRQ";
break;
case _stack_op_type::smart:
return "---";
break;
case _stack_op_type::op:
return mnemonics[ss.opcode];
break;
Expand Down
4 changes: 2 additions & 2 deletions src/boxmon/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <compare>
#include <functional>
#include <map>

#include "parser.h"

Expand All @@ -19,7 +20,6 @@ namespace boxmon
char const *get_name() const;
char const *get_description() const;

static void finalize_list();
static const boxmon_command *find(char const *name);
static void for_each(std::function<void(const boxmon_command *cmd)> fn);
static void for_each_partial(char const *name, std::function<void(const boxmon_command *cmd)> fn);
Expand All @@ -30,7 +30,7 @@ namespace boxmon

std::function<bool(char const *, parser &)> m_run;

static std::vector<const boxmon_command *> &get_command_list();
static std::map<const std::string, const boxmon_command *> &get_command_list();
};

class boxmon_alias : public boxmon_command
Expand Down
7 changes: 7 additions & 0 deletions src/boxmon/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ namespace boxmon
return namelist.front();
}

bool symbol_expression::is_valid() const
{
auto namelist = symbols_find(m_symbol);
return !namelist.empty();
}


unary_expression::unary_expression(expression_type type, const expression_base *param)
: expression_base(type),
m_param(param)
Expand Down
2 changes: 2 additions & 0 deletions src/boxmon/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ namespace boxmon
virtual ~symbol_expression() override final;
virtual int evaluate() const override final;

bool is_valid() const;

private:
std::string m_symbol;
};
Expand Down
11 changes: 9 additions & 2 deletions src/boxmon/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,15 @@ namespace boxmon
expression_stack.push(new value_expression(num));
return expression_stack.top()->get_type();
} else if (std::string symbol; parse_word(symbol, look)) {
expression_stack.push(new symbol_expression(symbol));
return expression_stack.top()->get_type();
symbol_expression *new_symbol = new symbol_expression(symbol);
if (new_symbol->is_valid()) {
expression_stack.push(new_symbol);
return expression_stack.top()->get_type();
}
if ((flags & expression_parse_flags_suppress_errors) == 0) {
boxmon_error_printf("Expression parse failed (invalid symbol name) at: \"%s\"\n", look);
}
return expression_type::invalid;
} else {
return expression_type::invalid;
}
Expand Down
25 changes: 18 additions & 7 deletions src/cpu/fake6502.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@

// 6502 CPU registers
_state6502 state6502;
_state6502 debug_state6502;

// helper variables
uint32_t instructions = 0; // keep track of total instructions executed
Expand All @@ -95,7 +96,8 @@ uint8_t debug6502 = 0;
uint8_t penaltyop, penaltyaddr;
uint8_t waiting = 0;

ring_buffer<_smart_stack, 256> stack6502;
_smart_stack stack6502[256];
uint8_t stack6502_underflow = 0;
ring_buffer<_cpuhistory, 256> history6502;

// externally supplied functions
Expand Down Expand Up @@ -130,10 +132,12 @@ static void putvalue(uint16_t saveval)

void nmi6502()
{
auto &ss = stack6502.allocate();
auto &ss = stack6502[state6502.sp_depth++];
ss.source_pc = state6502.pc;
ss.source_bank = bank6502(state6502.pc);
ss.state = state6502;
ss.push_depth = 0;
ss.push_unwind_depth = 0;
state6502.sp_unwind_depth = state6502.sp_depth;

push16(state6502.pc);
push8(state6502.status & ~FLAG_BREAK);
Expand All @@ -146,16 +150,21 @@ void nmi6502()
ss.dest_pc = state6502.pc;
ss.dest_bank = bank6502(state6502.pc);
ss.op_type = _stack_op_type::nmi;
ss.pop_type = _stack_pop_type::unknown;
ss.opcode = 0;
ss.state = debug_state6502;
}

void irq6502()
{
if (!(state6502.status & FLAG_INTERRUPT)) {
auto &ss = stack6502.allocate();
auto &ss = stack6502[state6502.sp_depth++];
ss.source_pc = state6502.pc;
ss.source_bank = bank6502(state6502.pc);
ss.state = state6502;
ss.push_depth = 0;
ss.push_unwind_depth = 0;
state6502.sp_unwind_depth = state6502.sp_depth;

push16(state6502.pc);
push8(state6502.status & ~FLAG_BREAK);
setinterrupt();
Expand All @@ -166,7 +175,9 @@ void irq6502()
ss.dest_pc = state6502.pc;
ss.dest_bank = bank6502(state6502.pc);
ss.op_type = _stack_op_type::irq;
ss.pop_type = _stack_pop_type::unknown;
ss.opcode = 0;
ss.state = debug_state6502;
}
waiting = 0;
}
Expand All @@ -184,7 +195,7 @@ void exec6502(uint32_t tickcount)
clockgoal6502 += tickcount;

while (clockticks6502 < clockgoal6502) {
const _state6502 debug_state6502 = state6502;
debug_state6502 = state6502;
const uint64_t debug_clockticks6502 = clockticks6502;

opcode = read6502(state6502.pc++);
Expand Down Expand Up @@ -231,7 +242,7 @@ void step6502()
return;
}

const _state6502 debug_state6502 = state6502;
debug_state6502 = state6502;
const uint64_t debug_clockticks6502 = clockticks6502;

opcode = read6502(state6502.pc++);
Expand Down
48 changes: 40 additions & 8 deletions src/cpu/fake6502.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,55 @@

struct _state6502 {
uint16_t pc;
uint8_t sp_depth;
uint8_t sp_unwind_depth;
uint8_t sp, a, x, y, status;
};

enum class _stack_op_type : uint8_t {
op,
nmi,
irq,
op,
smart,
};

enum class _stack_pop_type : uint8_t {
unknown,
rts,
rti
};

enum class _push_op_type : uint8_t {
unknown,
a,
x,
y,
status,
smart,
};

struct _smart_stack_ex {
_push_op_type push_type;
_push_op_type pull_type;
uint8_t value;
uint16_t pc;
uint8_t bank;
};

struct _smart_stack {
uint16_t source_pc;
uint16_t dest_pc;
uint8_t source_bank;
uint8_t dest_bank;
_stack_op_type op_type;
uint8_t opcode;
_state6502 state;
uint16_t source_pc;
uint16_t dest_pc;
uint8_t source_bank;
uint8_t dest_bank;
_stack_op_type op_type;
_stack_pop_type pop_type;
uint16_t pop_pc;
uint8_t pop_bank;
uint8_t opcode;
uint8_t push_depth;
uint8_t push_unwind_depth;
_smart_stack_ex pushed_bytes[256];
_state6502 state;
};

struct _cpuhistory {
Expand Down
Loading

0 comments on commit 55c0e6d

Please sign in to comment.