diff --git a/src/disk.c b/src/disk.c index 42d4cb50..956672eb 100644 --- a/src/disk.c +++ b/src/disk.c @@ -21,6 +21,11 @@ #define NIB_SIZE 232960 #define DSK_SIZE 143360 +#ifdef TESTING +static FILE *test_read_fp = NULL; +static FILE *test_write_fp = NULL; +#endif + extern uint8_t slot6_rom[256]; extern bool slot6_rom_loaded; @@ -115,6 +120,30 @@ void c_init_6() #endif } +#ifdef TESTING +void c_begin_test_6(const char *read_file, const char *write_file) { + if (read_file) { + test_read_fp = fopen(read_file, "w"); + } + if (write_file) { + test_write_fp = fopen(write_file, "w"); + } +} + +void c_end_test_6() { + if (test_read_fp) { + fflush(test_read_fp); + fclose(test_read_fp); + test_read_fp = NULL; + } + if (test_write_fp) { + fflush(test_write_fp); + fclose(test_write_fp); + test_write_fp = NULL; + } +} +#endif + /* ------------------------------------------------------------------------- c_eject_6() - ejects/gzips image file ------------------------------------------------------------------------- */ @@ -257,6 +286,12 @@ unsigned char c_read_nibblized_6_6() fseek(disk6.disk[disk6.drive].fp, -2 * PHASE_BYTES, SEEK_CUR); } +#ifdef TESTING + if (test_read_fp) { + fputc(ch, test_read_fp); + } +#endif + return ch; } @@ -427,6 +462,12 @@ unsigned char c_read_normal_6() disk6.disk[disk6.drive].run_byte = 0; } +#ifdef TESTING + if (test_read_fp) { + fputc(value, test_read_fp); + } +#endif + disk6.disk_byte = value; return value; } @@ -448,6 +489,13 @@ void c_write_nibblized_6_6() } fputc(disk6.disk_byte, disk6.disk[disk6.drive].fp); + +#ifdef TESTING + if (test_write_fp) { + fputc(disk6.disk_byte, test_write_fp); + } +#endif + /* track revolves... */ if (ftell(disk6.disk[disk6.drive].fp) == (PHASE_BYTES * (disk6.disk[disk6.drive].phase + 2))) { @@ -554,6 +602,11 @@ void c_write_normal_6() /* Write sector */ fwrite(disk6.disk_data, 1, 256, disk6.disk[disk6.drive].fp); +#ifdef TESTING + if (test_write_fp) { + fwrite(disk6.disk_data, 1, 256, test_write_fp); + } +#endif fflush( disk6.disk[disk6.drive].fp ); /* Increment sector number (and wrap if necessary) */ disk6.disk[disk6.drive].sector++; diff --git a/src/test/testvm.c b/src/test/testvm.c index 4117c845..c67db741 100644 --- a/src/test/testvm.c +++ b/src/test/testvm.c @@ -50,7 +50,7 @@ static void sha1_to_str(const uint8_t * const md, char *buf) { // ---------------------------------------------------------------------------- // VM TESTS ... -TEST test_boot_disk() { +TEST setup_boot_disk(void) { char *disk = NULL; #ifdef __APPLE__ CFBundleRef mainBundle = CFBundleGetMainBundle(); @@ -74,7 +74,55 @@ TEST test_boot_disk() { disk[len-3] = '\0'; ASSERT(!c_new_diskette_6(0, disk, 0)); } - free(disk); + FREE(disk); + + PASS(); +} + +TEST test_boot_disk_bytes() { + setup_boot_disk(); + + char *homedir = getenv("HOME"); + char *disk = NULL; + asprintf(&disk, "%s/a2_read_disk_test.raw", homedir); + if (disk) { + unlink(disk); + c_begin_test_6(disk, NULL); + } + + BOOT_TO_DOS(); + + c_end_test_6(); + c_eject_6(0); + + do { + uint8_t md[SHA_DIGEST_LENGTH]; + char mdstr[(SHA_DIGEST_LENGTH*2)+1]; + + FILE *fp = fopen(disk, "r"); + fseek(fp, 0L, SEEK_END); + long nbytes = ftell(fp); + fseek(fp, 0L, SEEK_SET); + char *buf = malloc(nbytes); + if (fread(buf, 1, nbytes, fp) != nbytes) { + ASSERT(false); + } + fclose(fp); fp = NULL; + SHA1(buf, nbytes, md); + FREE(buf); + + sha1_to_str(md, mdstr); + ASSERT(strcmp(mdstr, "D21CC686571ADE868A909B5A7044A973DE70DFFB") == 0); + } while(0); + + unlink(disk); + FREE(disk); + + PASS(); +} + +TEST test_boot_disk() { + setup_boot_disk(); BOOT_TO_DOS(); @@ -3244,6 +3292,8 @@ GREATEST_SUITE(test_suite_vm) { // TESTS -------------------------- + RUN_TESTp(test_boot_disk_bytes); + RUN_TESTp(test_boot_disk); RUN_TESTp(test_read_keyboard);