Browse Source

Fix code formatting

master
Forest Belton 2 years ago
parent
commit
14b140cfe1
16 changed files with 208 additions and 235 deletions
  1. +2
    -3
      .vscode/settings.json
  2. +1
    -2
      include/boot.h
  3. +8
    -9
      include/cpu.h
  4. +72
    -74
      include/insn.h
  5. +5
    -4
      include/log.h
  6. +1
    -2
      include/psx.h
  7. +3
    -4
      include/util.h
  8. +14
    -19
      src/boot.c
  9. +7
    -12
      src/cpu.c
  10. +23
    -27
      src/insn.c
  11. +5
    -5
      src/log.c
  12. +10
    -12
      src/main.c
  13. +30
    -35
      src/util.c
  14. +9
    -9
      test/runner.c
  15. +2
    -2
      test/test.h
  16. +16
    -16
      test/test_util.c

+ 2
- 3
.vscode/settings.json View File

@ -1,5 +1,4 @@
{
"files.associations": {
"test.h": "c"
}
"C_Cpp.default.intelliSenseMode": "clang-x64",
"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 80}"
}

+ 1
- 2
include/boot.h View File

@ -3,8 +3,7 @@
#include "util.h"
typedef struct __attribute__((packed))
{
typedef struct __attribute__((packed)) {
uint32_t pc;
uint32_t gp;
uint32_t addr_dest;

+ 8
- 9
include/cpu.h View File

@ -3,21 +3,20 @@
#include <stdint.h>
#define REG_GP 28 // Global pointer
#define REG_SP 29 // Stack pointer
#define REG_FP 30 // Frame pointer
#define REG_GP 28 // Global pointer
#define REG_SP 29 // Stack pointer
#define REG_FP 30 // Frame pointer
#define MAIN_RAM_SIZE 0x200000
#define SCRATCHPAD_BASE 0x1F800000
#define SCRATCHPAD_END 0x1F8003FF
#define SCRATCHPAD_SIZE (SCRATCHPAD_END - SCRATCHPAD_BASE + 1)
typedef struct
{
uint32_t regs[32];
uint32_t pc;
uint32_t main_ram[MAIN_RAM_SIZE];
uint32_t sratchpad_ram[SCRATCHPAD_SIZE];
typedef struct {
uint32_t regs[32];
uint32_t pc;
uint32_t main_ram[MAIN_RAM_SIZE];
uint32_t sratchpad_ram[SCRATCHPAD_SIZE];
} cpu_t;
void cpu_write32(cpu_t *cpu, uint32_t addr, uint32_t x);

+ 72
- 74
include/insn.h View File

@ -1,84 +1,82 @@
#ifndef PSXC_INSN_H_
#define PSXC_INSN_H_
#include "cpu.h"
#include <stdint.h>
typedef enum
{
SPECIAL = 0x00,
BCONDZ = 0x01,
J = 0x02,
JAL = 0x03,
BEQ = 0x04,
BNE = 0x05,
BLEZ = 0x06,
BGTZ = 0x07,
ADDI = 0x08,
ADDIU = 0x09,
SLTI = 0x0a,
SLTIU = 0x0b,
ANDI = 0x0c,
ORI = 0x0d,
XORI = 0x0e,
LUI = 0x0f,
COP0 = 0x10,
COP1 = 0x11,
COP2 = 0x12,
COP3 = 0x13,
LB = 0x20,
LH = 0x21,
LWL = 0x22,
LW = 0x23,
LBU = 0x24,
LHU = 0x25,
LWR = 0x26,
SB = 0x28,
SH = 0x29,
SWL = 0x2a,
SW = 0x2b,
SWR = 0x2e,
LWC0 = 0x30,
LWC1 = 0x31,
LWC2 = 0x32,
LWC3 = 0x33,
SWC0 = 0x38,
SWC1 = 0x39,
SWC2 = 0x3a,
SWC3 = 0x3b,
#include "cpu.h"
typedef enum {
SPECIAL = 0x00,
BCONDZ = 0x01,
J = 0x02,
JAL = 0x03,
BEQ = 0x04,
BNE = 0x05,
BLEZ = 0x06,
BGTZ = 0x07,
ADDI = 0x08,
ADDIU = 0x09,
SLTI = 0x0a,
SLTIU = 0x0b,
ANDI = 0x0c,
ORI = 0x0d,
XORI = 0x0e,
LUI = 0x0f,
COP0 = 0x10,
COP1 = 0x11,
COP2 = 0x12,
COP3 = 0x13,
LB = 0x20,
LH = 0x21,
LWL = 0x22,
LW = 0x23,
LBU = 0x24,
LHU = 0x25,
LWR = 0x26,
SB = 0x28,
SH = 0x29,
SWL = 0x2a,
SW = 0x2b,
SWR = 0x2e,
LWC0 = 0x30,
LWC1 = 0x31,
LWC2 = 0x32,
LWC3 = 0x33,
SWC0 = 0x38,
SWC1 = 0x39,
SWC2 = 0x3a,
SWC3 = 0x3b,
} op_primary_t;
typedef enum
{
SLL = 0x00,
SRL = 0x02,
SRA = 0x03,
SLLV = 0x04,
SRLV = 0x06,
SRAV = 0x07,
JR = 0x08,
JALR = 0x09,
SYSCALL = 0x0c,
BREAK = 0x0d,
MFHI = 0x10,
MTHI = 0x11,
MFLO = 0x12,
MTLO = 0x13,
MULT = 0x18,
MULTU = 0x19,
DIV = 0x1a,
DIVU = 0x1b,
ADD = 0x20,
ADDU = 0x21,
SUB = 0x22,
SUBU = 0x23,
AND = 0x24,
OR = 0x25,
XOR = 0x26,
NOR = 0x27,
SLT = 0x2a,
SLTU = 0x2b,
typedef enum {
SLL = 0x00,
SRL = 0x02,
SRA = 0x03,
SLLV = 0x04,
SRLV = 0x06,
SRAV = 0x07,
JR = 0x08,
JALR = 0x09,
SYSCALL = 0x0c,
BREAK = 0x0d,
MFHI = 0x10,
MTHI = 0x11,
MFLO = 0x12,
MTLO = 0x13,
MULT = 0x18,
MULTU = 0x19,
DIV = 0x1a,
DIVU = 0x1b,
ADD = 0x20,
ADDU = 0x21,
SUB = 0x22,
SUBU = 0x23,
AND = 0x24,
OR = 0x25,
XOR = 0x26,
NOR = 0x27,
SLT = 0x2a,
SLTU = 0x2b,
} op_secondary_t;
typedef void (*cpu_insn_handler)(cpu_t *, uint32_t);

+ 5
- 4
include/log.h View File

@ -3,18 +3,19 @@
#include <stdlib.h>
#define log(level, fmt, ...) _log(__FILE__, __func__, __LINE__, (level), fmt, ##__VA_ARGS__)
#define log(level, fmt, ...) \
_log(__FILE__, __func__, __LINE__, (level), fmt, ##__VA_ARGS__)
#define debug(fmt, ...) log("DEBUG", fmt, ##__VA_ARGS__)
#define fatal(fmt, ...) \
do \
{ \
do { \
log("FATAL", fmt, ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
#define info(fmt, ...) log("INFO", fmt, ##__VA_ARGS__)
extern void _log(const char *filename, const char *funcname, int line_num, const char *lvl, const char *fmt, ...);
extern void _log(const char *filename, const char *funcname, int line_num,
const char *lvl, const char *fmt, ...);
extern const char *REG_NAMES[32];

+ 1
- 2
include/psx.h View File

@ -4,8 +4,7 @@
#include "boot.h"
#include "cpu.h"
typedef struct
{
typedef struct {
psx_header_t header;
cpu_t *cpu;
} psx_t;

+ 3
- 4
include/util.h View File

@ -4,10 +4,9 @@
#include <stddef.h>
#include <stdint.h>
typedef struct
{
size_t size;
uint8_t data[];
typedef struct {
size_t size;
uint8_t data[];
} byte_arr_t;
extern byte_arr_t *read_file(const char *filepath);

+ 14
- 19
src/boot.c View File

@ -1,19 +1,19 @@
#include "boot.h"
#include "cpu.h"
#include "insn.h"
#include "log.h"
#include "psx.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpu.h"
#include "insn.h"
#include "log.h"
#include "psx.h"
static const uint8_t MAGIC[0x10] = "PS-X EXE";
static const uint8_t RESERVED[0x10] = {0};
psx_header_t parse_psx_header(byte_arr_t *prgm)
{
psx_header_t parse_psx_header(byte_arr_t *prgm) {
psx_header_t header;
// TODO: Convert asserts into recoverable error
@ -37,29 +37,25 @@ psx_header_t parse_psx_header(byte_arr_t *prgm)
return header;
}
uint32_t translate_addr(uint32_t addr)
{
if (addr >= 0xA0000000)
{
uint32_t translate_addr(uint32_t addr) {
if (addr >= 0xA0000000) {
addr = addr - 0xA0000000;
}
else if (addr >= 0x80000000)
{
} else if (addr >= 0x80000000) {
addr = addr - 0x80000000;
}
return addr;
}
void boot_psx_prgm(byte_arr_t *prgm)
{
void boot_psx_prgm(byte_arr_t *prgm) {
debug("Parsing program header");
psx_header_t header = parse_psx_header(prgm);
debug("Program header");
debug("size=%u bytes", header.size_file);
debug("pc=%08x, gp=%08x, sp=%08x, fp=%08x", header.pc, header.gp, header.sp, header.fp);
debug("pc=%08x, gp=%08x, sp=%08x, fp=%08x", header.pc, header.gp, header.sp,
header.fp);
debug("data=%08x (%u bytes)", header.addr_data, header.size_data);
debug("bss=%08x (%u bytes)", header.addr_bss, header.size_bss);
debug("dest=%08x", header.addr_dest);
@ -77,8 +73,7 @@ void boot_psx_prgm(byte_arr_t *prgm)
psx_t psx = {header, cpu};
while (1)
{
while (1) {
debug("Executing instruction at %08x", psx.cpu->pc);
const uint32_t insn = cpu_read32(cpu, psx.cpu->pc);

+ 7
- 12
src/cpu.c View File

@ -1,31 +1,26 @@
#include "cpu.h"
#include "log.h"
#include <stdio.h>
static uint32_t translate_addr(uint32_t addr)
{
if (addr >= 0xA0000000)
{
#include "log.h"
static uint32_t translate_addr(uint32_t addr) {
if (addr >= 0xA0000000) {
addr = addr - 0xA0000000;
}
else if (addr >= 0x80000000)
{
} else if (addr >= 0x80000000) {
addr = addr - 0x80000000;
}
return addr;
}
void cpu_write32(cpu_t *cpu, uint32_t addr, uint32_t x)
{
void cpu_write32(cpu_t *cpu, uint32_t addr, uint32_t x) {
debug("Write [%08x] = %08x", addr, x);
addr = translate_addr(addr);
cpu->main_ram[addr] = x;
}
uint32_t cpu_read32(cpu_t *cpu, uint32_t addr)
{
uint32_t cpu_read32(cpu_t *cpu, uint32_t addr) {
debug("Read [%08x]", addr);
addr = translate_addr(addr);
return cpu->main_ram[addr];

+ 23
- 27
src/insn.c View File

@ -1,10 +1,11 @@
#include "insn.h"
#include "log.h"
#include "cpu.h"
#include <stddef.h>
#include <stdio.h>
#include "cpu.h"
#include "log.h"
#define TABLE_SIZE 0x34
void insn_lw(cpu_t *cpu, uint32_t insn);
@ -26,37 +27,32 @@ static cpu_insn_handler secondary_insn_handler[TABLE_SIZE] = {NULL};
#define OP2(insn) BITS(insn, 0, 6)
#define IMM(insn) BITS(insn, 0, 16)
void insn_execute(cpu_t *cpu, uint32_t insn)
{
const op_primary_t op = OP(insn);
void insn_execute(cpu_t *cpu, uint32_t insn) {
const op_primary_t op = OP(insn);
if (op == SPECIAL)
{
const op_secondary_t op2 = OP2(insn);
if (op == SPECIAL) {
const op_secondary_t op2 = OP2(insn);
if (op2 > TABLE_SIZE || secondary_insn_handler[op2] == NULL)
{
fatal("Unsupported instruction: insn=%08x, op=%02x, op2=%02x", insn,
op, op2);
}
if (op2 > TABLE_SIZE || secondary_insn_handler[op2] == NULL) {
fatal("Unsupported instruction: insn=%08x, op=%02x, op2=%02x", insn,
op, op2);
}
secondary_insn_handler[op2](cpu, insn);
}
secondary_insn_handler[op2](cpu, insn);
}
if (op > TABLE_SIZE || primary_insn_handler[op] == NULL)
{
fatal("Unsupported instruction: insn=%08x, op=%02x", insn, op);
}
if (op > TABLE_SIZE || primary_insn_handler[op] == NULL) {
fatal("Unsupported instruction: insn=%08x, op=%02x", insn, op);
}
primary_insn_handler[op](cpu, insn);
primary_insn_handler[op](cpu, insn);
}
void insn_lw(cpu_t *cpu, uint32_t insn)
{
const uint8_t rs = RS(insn);
const uint8_t rt = RT(insn);
const uint16_t imm = IMM(insn);
void insn_lw(cpu_t *cpu, uint32_t insn) {
const uint8_t rs = RS(insn);
const uint8_t rt = RT(insn);
const uint16_t imm = IMM(insn);
debug("LW %s, [%s + %x]", REG_NAMES[rt], REG_NAMES[rs], imm);
cpu->regs[rt] = cpu_read32(cpu, cpu->regs[rs] + imm);
debug("LW %s, [%s + %x]", REG_NAMES[rt], REG_NAMES[rs], imm);
cpu->regs[rt] = cpu_read32(cpu, cpu->regs[rs] + imm);
}

+ 5
- 5
src/log.c View File

@ -5,12 +5,12 @@
#include <time.h>
const char *REG_NAMES[32] = {
"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12",
"R13", "R14", "R15", "R16", "R17", "R18", "R19", "R20", "R21", "R22", "R23",
"R24", "R25", "R26", "R27", "GP", "SP", "FP", "RA"};
"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10",
"R11", "R12", "R13", "R14", "R15", "R16", "R17", "R18", "R19", "R20", "R21",
"R22", "R23", "R24", "R25", "R26", "R27", "GP", "SP", "FP", "RA"};
void _log(const char *filename, const char *funcname, int line_num, const char *level, const char *fmt, ...)
{
void _log(const char *filename, const char *funcname, int line_num,
const char *level, const char *fmt, ...) {
time_t now;
char fmt_time[sizeof "2011-10-08T07:07:09Z"];

+ 10
- 12
src/main.c View File

@ -1,20 +1,18 @@
#include <stdio.h>
#include "boot.h"
#include "insn.h"
#include "log.h"
#include "util.h"
#include <stdio.h>
int main()
{
info("Loading program");
byte_arr_t *prgm = read_file("boot.rom");
if (prgm == NULL)
{
fatal("Couldn't load program");
}
int main() {
info("Loading program");
byte_arr_t *prgm = read_file("boot.rom");
if (prgm == NULL) {
fatal("Couldn't load program");
}
boot_psx_prgm(prgm);
boot_psx_prgm(prgm);
return 0;
return 0;
}

+ 30
- 35
src/util.c View File

@ -4,41 +4,36 @@
#include <stdio.h>
#include <stdlib.h>
byte_arr_t *read_file(const char *filepath)
{
FILE *fp = fopen(filepath, "rb");
byte_arr_t *arr = NULL;
if (fp == NULL)
{
// TODO: Log correctly
fprintf(stderr, "%s: ", filepath);
perror("could not open");
goto end;
}
fseek(fp, 0, SEEK_END);
const long size = ftell(fp);
if (size < 0)
{
goto end;
}
arr = malloc(sizeof *arr + size);
if (arr == NULL)
{
goto end;
}
arr->size = size;
rewind(fp);
fread(arr->data, 1, size, fp);
byte_arr_t *read_file(const char *filepath) {
FILE *fp = fopen(filepath, "rb");
byte_arr_t *arr = NULL;
if (fp == NULL) {
// TODO: Log correctly
fprintf(stderr, "%s: ", filepath);
perror("could not open");
goto end;
}
fseek(fp, 0, SEEK_END);
const long size = ftell(fp);
if (size < 0) {
goto end;
}
arr = malloc(sizeof *arr + size);
if (arr == NULL) {
goto end;
}
arr->size = size;
rewind(fp);
fread(arr->data, 1, size, fp);
end:
if (fp != NULL)
{
fclose(fp);
}
return arr;
if (fp != NULL) {
fclose(fp);
}
return arr;
}

+ 9
- 9
test/runner.c View File

@ -1,13 +1,13 @@
#include "test.h"
#include <stdio.h>
#include "test.h"
int main(int argc, char *argv[]) {
printf("%s:\n", argv[0]);
for (size_t i = 0; i < NUM_TEST_CASES; ++i) {
printf(" * %s: ", TEST_CASES[i].name);
TEST_CASES[i].test();
printf("OK!\n");
}
return 0;
printf("%s:\n", argv[0]);
for (size_t i = 0; i < NUM_TEST_CASES; ++i) {
printf(" * %s: ", TEST_CASES[i].name);
TEST_CASES[i].test();
printf("OK!\n");
}
return 0;
}

+ 2
- 2
test/test.h View File

@ -4,8 +4,8 @@
#include <stddef.h>
typedef struct {
const char *name;
void (*test)(void);
const char *name;
void (*test)(void);
} test_case_t;
extern const test_case_t TEST_CASES[];

+ 16
- 16
test/test_util.c View File

@ -1,32 +1,32 @@
#include "test.h"
#include "util.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "test.h"
#include "util.h"
#define ARR_SIZE 50
void test_read_file(void) {
const char *filepath = tmpnam(NULL);
const char *filepath = tmpnam(NULL);
uint8_t arr[ARR_SIZE] = {0};
for (size_t i = 0; i < sizeof arr; ++i) {
arr[i] = rand() % 256;
}
uint8_t arr[ARR_SIZE] = {0};
for (size_t i = 0; i < sizeof arr; ++i) {
arr[i] = rand() % 256;
}
FILE *fp = fopen(filepath, "wb");
assert(fp != NULL);
FILE *fp = fopen(filepath, "wb");
assert(fp != NULL);
fwrite(&arr[0], 1, ARR_SIZE, fp);
fclose(fp);
fwrite(&arr[0], 1, ARR_SIZE, fp);
fclose(fp);
byte_arr_t *barr = read_file(filepath);
assert(barr != NULL);
assert(barr->size == ARR_SIZE);
byte_arr_t *barr = read_file(filepath);
assert(barr != NULL);
assert(barr->size == ARR_SIZE);
assert(memcmp(&arr[0], barr->data, ARR_SIZE) == 0);
assert(memcmp(&arr[0], barr->data, ARR_SIZE) == 0);
}
const test_case_t TEST_CASES[] = {

Loading…
Cancel
Save