From 4d07bd7640b16801fbcb96bfe3e75ee9197e953c Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Wed, 3 Jan 2018 20:51:20 -0600 Subject: [PATCH] Add objstore code, tests --- include/objstore.h | 23 +++++++++++++++ sources.cmake | 1 + src/objstore.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++ tests/objstore.c | 9 ++++++ 4 files changed, 106 insertions(+) create mode 100644 include/objstore.h create mode 100644 src/objstore.c create mode 100644 tests/objstore.c diff --git a/include/objstore.h b/include/objstore.h new file mode 100644 index 0000000..535f2ae --- /dev/null +++ b/include/objstore.h @@ -0,0 +1,23 @@ +#ifndef _OBJSTORE_H_ +#define _OBJSTORE_H_ + +#include "apple2.mem.h" +#include "vm_bits.h" + +typedef struct { + char header[4]; + vm_8bit apple2_disk2_rom[APPLE2_DISK2_ROM_SIZE]; + vm_8bit apple2_sys_rom[0x4000]; + vm_8bit apple2_sysfont[APPLE2_SYSFONT_SIZE]; +} objstore; + +extern int objstore_init(); + +#define OBJSTORE_DECL(x) \ + const vm_8bit *objstore_##x() + +OBJSTORE_DECL(apple2_disk2_rom); +OBJSTORE_DECL(apple2_sys_rom); +OBJSTORE_DECL(apple2_sysfont); + +#endif diff --git a/sources.cmake b/sources.cmake index 823b9b9..9c604e0 100644 --- a/sources.cmake +++ b/sources.cmake @@ -13,6 +13,7 @@ set(erc_sources mos6502.exec.c mos6502.loadstor.c mos6502.stat.c + objstore.c option.c vm_bitfont.c vm_screen.c diff --git a/src/objstore.c b/src/objstore.c new file mode 100644 index 0000000..9807807 --- /dev/null +++ b/src/objstore.c @@ -0,0 +1,73 @@ +/* + * obj.c + */ + +#include + +#include "objstore.h" +#include "objstore_data.h" + +/* + * This is data that we expect to find in the store_data variable that + * is defined in objstore_data.h. + */ +#define HEADER_DATA "hope" + +/* + * Our object store. Just a simple struct that we pull data out from. + */ +static objstore store; + +/* + * This function will set up the store variable so that it contains + * useful information rather than garbage data from the stack. It does + * so with a simply memcpy from store_data directly into store, such + * that the store data itself can be aligned for access. + */ +int +objstore_init() +{ + int cmp; + + // We want to input some bad header data and compare with what + // eventually should get put into there by memcmp. + store.header[0] = 'h'; + store.header[1] = 'e'; + store.header[2] = 'y'; + store.header[3] = '\0'; + + memcpy(&store, store_data, sizeof(store)); + + // Test if the header field data is exactly equivalent to that + // defined in HEADER_DATA. Note we use memcmp(), because the header + // field is an array of just 4 bytes; strcmp() and strncmp() have an + // expectation that both pointers will be directed at NUL-terminated + // strings, which is _not_ the case for store.header (but, + // incidentally, _is_ the case for HEADER_DATA!). + cmp = memcmp(store.header, HEADER_DATA, + sizeof(store.header) / sizeof(char)); + + // If the two areas of memory were not a complete match... + if (cmp != 0) { + log_critical("Object store initialization failed with bad data"); + return ERR_BADFILE; + } + + return OK; +} + +/* + * Something to simplify the very simple getter functions we use for + * getting data from the object store. + */ +#define OBJSTORE_DEFN(x) \ + const vm_8bit *objstore_##x() { return store.x; } + +/* + * Note we have an extra semicolon on the end... this is mostly to avoid + * screwing up my indent, to be honest. But C will ignore the + * semicolons, so all is well. + */ +OBJSTORE_DEFN(apple2_sys_rom); +OBJSTORE_DEFN(apple2_disk2_rom); +OBJSTORE_DEFN(apple2_sysfont); diff --git a/tests/objstore.c b/tests/objstore.c new file mode 100644 index 0000000..4faaea6 --- /dev/null +++ b/tests/objstore.c @@ -0,0 +1,9 @@ +#include + +#include "log.h" +#include "objstore.h" + +Test(objstore, init) +{ + cr_assert_eq(objstore_init(), OK); +}