Skip to content

Commit

Permalink
[emulator] reformat to latest style
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Hall <[email protected]>
  • Loading branch information
hxw committed Jun 30, 2020
1 parent a1f9a5b commit 75e0686
Show file tree
Hide file tree
Showing 22 changed files with 455 additions and 330 deletions.
140 changes: 74 additions & 66 deletions emulator/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ typedef struct {
} file_mode_t;

const file_mode_t modes[] = {
{L"hex5", io5_mode_hex5}, {L"h5", io5_mode_hex5},
{L"hex8", io5_mode_hex8}, {L"h8", io5_mode_hex8},
{L"binary", io5_mode_binary}, {L"bin", io5_mode_binary},
{L"elliott", io5_mode_elliott}, {L"utf-8", io5_mode_elliott},
{L"utf8", io5_mode_elliott},
{L"hex5", io5_mode_hex5},
{L"h5", io5_mode_hex5},
{L"hex8", io5_mode_hex8},
{L"h8", io5_mode_hex8},
{L"binary", io5_mode_binary},
{L"bin", io5_mode_binary},
{L"elliott", io5_mode_elliott},
{L"utf-8", io5_mode_elliott},
{L"utf8", io5_mode_elliott},
};

static io5_mode_t string_to_mode(const wchar_t *w) {
Expand Down Expand Up @@ -58,8 +62,8 @@ static void command_wait(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {
}
}

static void command_screen(commands_t *cmd, const wchar_t *name,
wchar_t **ptr) {
static void
command_screen(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {
const wchar_t *w = parser_get_token(ptr);
if (NULL != w) {
long s = wcstol(w, NULL, 10);
Expand Down Expand Up @@ -101,8 +105,8 @@ static void command_list(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {
}
}

static void command_memory_write(commands_t *cmd, const wchar_t *name,
wchar_t **ptr) {
static void
command_memory_write(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {

long address = -1;

Expand Down Expand Up @@ -154,17 +158,17 @@ static void command_run(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {

char packet[256];
memset(packet, 0, sizeof(packet));
int n = snprintf(packet, sizeof(packet), "run %ld%s", address,
half ? ".5" : ".0");
int n =
snprintf(packet, sizeof(packet), "run %ld%s", address, half ? ".5" : ".0");
elliott803_send(cmd->proc, packet, n);
}

static void command_stop(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {
elliott803_send(cmd->proc, "stop", 5);
}

static void command_registers(commands_t *cmd, const wchar_t *name,
wchar_t **ptr) {
static void
command_registers(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {
elliott803_send(cmd->proc, "status", 7);
}

Expand Down Expand Up @@ -196,9 +200,9 @@ static void command_hello(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {
}

const char *hello_init[] = {
"mw %ld 26 4 : 02 4", //
"mw %ld 55 2 : 22 4", //
"mw %ld 04 4 : 21 4", // set [4] = -5
"mw %ld 26 4 : 02 4", //
"mw %ld 55 2 : 22 4", //
"mw %ld 04 4 : 21 4", // set [4] = -5
};

const char *hello_punch_two = "mw %ld 74 %4d : 74 %4d";
Expand All @@ -207,21 +211,21 @@ static void command_hello(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {
char c1;
char c2;
} hello_chars[] = {
{27, 31}, // FS LS
{29, 30}, // CR LF
{8, 5}, // hello, world.
{12, 12}, //
{15, 27}, //
{10, 28}, //
{31, 23}, //
{15, 18}, //
{12, 4}, //
{27, 14}, //
{27, 31}, // FS LS
{29, 30}, // CR LF
{8, 5}, // hello, world.
{12, 12}, //
{15, 27}, //
{10, 28}, //
{31, 23}, //
{15, 18}, //
{12, 4}, //
{27, 14}, //
};

const char *hello_loop[] = {
"mw %ld 22 4 : 30 4", //
"mw %ld 42 %1$4d : 40%2$4d", //
"mw %ld 22 4 : 30 4", //
"mw %ld 42 %1$4d : 40%2$4d", //
};

char packet[256];
Expand All @@ -236,25 +240,29 @@ static void command_hello(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {

for (size_t i = 0; i < SizeOfArray(hello_chars); ++i) {
memset(packet, 0, sizeof(packet));
int n = snprintf(packet, sizeof(packet), hello_punch_two, address,
hello_chars[i].c1 + punch, hello_chars[i].c2 + punch);
int n = snprintf(packet,
sizeof(packet),
hello_punch_two,
address,
hello_chars[i].c1 + punch,
hello_chars[i].c2 + punch);
elliott803_send(cmd->proc, packet, n);
++address;
}

for (size_t i = 0; i < SizeOfArray(hello_loop); ++i) {
memset(packet, 0, sizeof(packet));
int n =
snprintf(packet, sizeof(packet), hello_loop[i], address, loop_address);
snprintf(packet, sizeof(packet), hello_loop[i], address, loop_address);
elliott803_send(cmd->proc, packet, n);
++address;
}
}

// reader unit [mode] file
// if mode is absent then assume "hex5"
static void command_reader(commands_t *cmd, const wchar_t *name,
wchar_t **ptr) {
static void
command_reader(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {
long unit = 0;

const wchar_t *w = parser_get_token(ptr);
Expand Down Expand Up @@ -373,8 +381,8 @@ static void command_punch(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {
}
}

static void command_word_generator(commands_t *cmd, const wchar_t *name,
wchar_t **ptr) {
static void
command_word_generator(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {

if (L'\0' == **ptr) {
elliott803_send(cmd->proc, "wg", 3);
Expand Down Expand Up @@ -411,25 +419,25 @@ static void command_word_generator(commands_t *cmd, const wchar_t *name,

static void command_help(commands_t *cmd, const wchar_t *name, wchar_t **ptr) {
const wchar_t *m =
// clang-format: off
L"help (?) this message\n"
L"exit (x) exit emulation\n"
L"wait wait for stop or wg polling\n"
L"list [ADDR [COUNT]] (l) display memory words\n"
L"mw ADDR CODE|±DEC write memory word\n"
L"reset reset all regs and stop execution\n"
L"reset run reset all regs and restart from zero\n"
L"run [ADDR] run from address or continue after a stop\n"
L"stop stop execution\n"
L"regs (r) display registers and status\n"
L"hello [ADDR [1|2|3]] load hello world [4096 1]\n"
L"reader 1|2 [MODE] FILE attach an existing file to a reader "
L"(hex5)\n"
L"punch 1|2 [MODE] FILE create file and attach to a punch (hex5)\n"
L"screen 1|2|3|4 select current screen as Fn\n"
L"wg [msb|o2l|lsb|CODE|±N] set or display word generator\n"
// clang-format: on
;
// clang-format: off
L"help (?) this message\n"
L"exit (x) exit emulation\n"
L"wait wait for stop or wg polling\n"
L"list [ADDR [COUNT]] (l) display memory words\n"
L"mw ADDR CODE|±DEC write memory word\n"
L"reset reset all regs and stop execution\n"
L"reset run reset all regs and restart from zero\n"
L"run [ADDR] run from address or continue after a stop\n"
L"stop stop execution\n"
L"regs (r) display registers and status\n"
L"hello [ADDR [1|2|3]] load hello world [4096 1]\n"
L"reader 1|2 [MODE] FILE attach an existing file to a reader "
L"(hex5)\n"
L"punch 1|2 [MODE] FILE create file and attach to a punch (hex5)\n"
L"screen 1|2|3|4 select current screen as Fn\n"
L"wg [msb|o2l|lsb|CODE|±N] set or display word generator\n"
// clang-format: on
;

cmd->error = wcsdup(m);
}
Expand All @@ -442,18 +450,18 @@ typedef struct {
} command_t;

const command_t commands[] = {
{L"exit", command_exit}, {L"x", command_exit},
{L"quit", command_exit}, {L"q", command_exit},
{L"wait", command_wait}, {L"screen", command_screen},

{L"list", command_list}, {L"l", command_list},
{L"mw", command_memory_write}, {L"reset", command_reset},
{L"run", command_run}, {L"stop", command_stop},
{L"regs", command_registers}, {L"r", command_registers},
{L"hello", command_hello}, {L"reader", command_reader},
{L"punch", command_punch}, {L"wg", command_word_generator},

{L"help", command_help}, {L"?", command_help},
{L"exit", command_exit}, {L"x", command_exit},
{L"quit", command_exit}, {L"q", command_exit},
{L"wait", command_wait}, {L"screen", command_screen},

{L"list", command_list}, {L"l", command_list},
{L"mw", command_memory_write}, {L"reset", command_reset},
{L"run", command_run}, {L"stop", command_stop},
{L"regs", command_registers}, {L"r", command_registers},
{L"hello", command_hello}, {L"reader", command_reader},
{L"punch", command_punch}, {L"wg", command_word_generator},

{L"help", command_help}, {L"?", command_help},
};

void commands_run(commands_t *cmd, wchar_t *buffer, size_t buffer_size) {
Expand Down
3 changes: 2 additions & 1 deletion emulator/cpu/alu.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ void alu_multiply(int64_t *acc, int64_t *ar, int64_t md1, int64_t mr1) {
// 128-bit / 64-bit unsigned divide
// derived from:
// https://codereview.stackexchange.com/questions/67962/mostly-portable-128-by-64-bit-division
static uint64_t internal_divide(uint64_t dividend_high, uint64_t dividend_low,
static uint64_t internal_divide(uint64_t dividend_high,
uint64_t dividend_low,
uint64_t divisor) {

uint64_t quotient = dividend_low << 1;
Expand Down
4 changes: 3 additions & 1 deletion emulator/cpu/alu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

int64_t alu_add(bool *overflow, int op, int64_t acc, int64_t a, int64_t n);
void alu_multiply(int64_t *acc, int64_t *ar, int64_t md1, int64_t mr1);
int64_t alu_divide(bool *overflow, int64_t dividend_high, int64_t dividend_low,
int64_t alu_divide(bool *overflow,
int64_t dividend_high,
int64_t dividend_low,
int64_t divisor);

#endif
37 changes: 26 additions & 11 deletions emulator/cpu/alu_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,53 @@
#include "alu.h"
#include "constants.h"

static void test_add_op(const char *title, int op, int64_t acc, int64_t a,
int64_t n, int64_t result, bool overflow) {
static void test_add_op(const char *title,
int op,
int64_t acc,
int64_t a,
int64_t n,
int64_t result,
bool overflow) {

bool actual_overflow = false;
int64_t actual = alu_add(&actual_overflow, op, acc, a, n);

if (actual != result || actual_overflow != overflow) {
printf("%-16s: actual: %013lo (%s)\n"
"%-16s expected: %013lo (%s)\n",
title, (actual >> word_shift) & lsb_thirty_nine_bits,
actual_overflow ? "v" : "_", "",
(result >> word_shift) & lsb_thirty_nine_bits, overflow ? "V" : "_");
title,
(actual >> word_shift) & lsb_thirty_nine_bits,
actual_overflow ? "v" : "_",
"",
(result >> word_shift) & lsb_thirty_nine_bits,
overflow ? "V" : "_");
exit(1);
}
}

static void test_mpy(const char *title, int64_t a, int64_t b, int64_t acc,
int64_t ar) {
static void
test_mpy(const char *title, int64_t a, int64_t b, int64_t acc, int64_t ar) {
int64_t actual_acc;
int64_t actual_ar;
alu_multiply(&actual_acc, &actual_ar, a, b);

if (actual_acc != acc || actual_ar != ar) {
printf("%-16s: actual: %013lo (%013lo)\n"
"%-16s expected: %013lo (%013lo)\n",
title, (actual_acc >> word_shift) & lsb_thirty_nine_bits,
(actual_ar >> word_shift) & lsb_thirty_nine_bits, "",
title,
(actual_acc >> word_shift) & lsb_thirty_nine_bits,
(actual_ar >> word_shift) & lsb_thirty_nine_bits,
"",
(acc >> word_shift) & lsb_thirty_nine_bits,
(ar >> word_shift) & lsb_thirty_nine_bits);
exit(1);
}
}

static void test_div(const char *title, int64_t dh, int64_t dl, int64_t divisor,
static void test_div(const char *title,
int64_t dh,
int64_t dl,
int64_t divisor,
int64_t expected_q) {

bool overflow = false;
Expand All @@ -52,7 +65,9 @@ static void test_div(const char *title, int64_t dh, int64_t dl, int64_t divisor,

printf("%-16s: actual: %013lo\n"
"%-16s expected: %013lo\n",
title, (actual_q >> word_shift) & lsb_thirty_nine_bits, "",
title,
(actual_q >> word_shift) & lsb_thirty_nine_bits,
"",
(expected_q >> word_shift) & lsb_thirty_nine_bits);
exit(1);
}
Expand Down
13 changes: 11 additions & 2 deletions emulator/cpu/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,17 @@ char *to_machine_code(const char *prefix, int64_t word) {
char *p = NULL;

// this allocates memory for the returned string
asprintf(&p, "%s%02o %4d %s %02o %4d X%010lx 8%013lo %+13ld", prefix, op1,
addr1, b, op2, addr2, value, value, value_signed);
asprintf(&p,
"%s%02o %4d %s %02o %4d X%010lx 8%013lo %+13ld",
prefix,
op1,
addr1,
b,
op2,
addr2,
value,
value,
value_signed);

return p;
}
8 changes: 4 additions & 4 deletions emulator/cpu/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
#include "processor.h"

static const uint64_t T1[4] = {
ELLIOTT(026, 4, 0, 006, 0), // 0
ELLIOTT(022, 4, 1, 016, 3), // 1
ELLIOTT(055, 5, 0, 071, 0), // 2
ELLIOTT(043, 1, 0, 040, 2), // 3
ELLIOTT(026, 4, 0, 006, 0), // 0
ELLIOTT(022, 4, 1, 016, 3), // 1
ELLIOTT(055, 5, 0, 071, 0), // 2
ELLIOTT(043, 1, 0, 040, 2), // 3
};

// read data from core; zero for boot loader
Expand Down
Loading

0 comments on commit 75e0686

Please sign in to comment.