diff --git a/firmware/Makefile b/firmware/Makefile index 9d6fb4d..fec210c 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -55,7 +55,7 @@ clean: # file targets: -main.elf: $(OBJECTS) +main.elf: shared $(OBJECTS) $(COMPILE) -o main.elf $(OBJECTS) main.hex: main.elf @@ -70,3 +70,7 @@ disasm: main.elf cpp: $(COMPILE) -E main.c + +#copy shared .h files which are used in host and firmware +shared: + cp -r ../shared/* source/ diff --git a/firmware/source/main.c b/firmware/source/main.c index 862acaf..1e994ee 100644 --- a/firmware/source/main.c +++ b/firmware/source/main.c @@ -5,9 +5,7 @@ #include "usbdrv.h" #include "macro.h" - -#define REQ_LED_ON 1 -#define REQ_LED_OFF 2 +#include "usb_commands.h" //USB_PUBLIC usbMsgLen_t usbFunctionSetup(uchar data[8]); diff --git a/host/Makefile b/host/Makefile index 1c8c5fd..dc06cc1 100644 --- a/host/Makefile +++ b/host/Makefile @@ -11,12 +11,12 @@ OBJECTS=$(patsubst %.c,%.o,$(SOURCES)) TARGET=inlretro #default to windows build -all: +all: shared $(CC) $(INCLUDE) $(SOURCES) -o $(TARGET) $(WINLIB) $(CFLAGS) $(LIBUSB) #unix build doesn't need winlib directory as libusb should be installed on OS. #sudo apt-get install libusb-1.0-0-dev -unix: +unix: shared $(CC) $(INCLUDE) $(SOURCES) -o $(TARGET) $(CFLAGS) $(LIBUSB) # "make debug" will build program with debug print messages @@ -29,6 +29,11 @@ debug: all unixdebug: CFLAGS += -DDEBUG -g unixdebug: unix + +#copy shared .h files which are used in host and firmware +shared: + cp -r ../shared/* source/ + #clean on unix and windows(.exe) clean: rm -f $(TARGET) $(TARGET).exe $(OBJECTS) diff --git a/host/include/dbg.h b/host/include/dbg.h index a7663e7..3ed2de3 100644 --- a/host/include/dbg.h +++ b/host/include/dbg.h @@ -5,6 +5,8 @@ #include #include +#define SUCCESS 0 + #ifdef DEBUG #define debug(M, ...) fprintf(stderr, "DEBUG %s:%d: " M "\n",\ __FILE__, __LINE__, ##__VA_ARGS__) diff --git a/host/source/inlprog.c b/host/source/inlprog.c index 39e3c82..6a5cfae 100644 --- a/host/source/inlprog.c +++ b/host/source/inlprog.c @@ -13,12 +13,14 @@ #include "dbg.h" #include "usb_operations.h" +#include "usb_commands.h" +#include "write_operations.h" // vendor requests also defined in firmware // TODO put in combined .h file for both host and fw -#define REQ_LED_ON 1 -#define REQ_LED_OFF 2 +//#define REQ_LED_ON 1 +//#define REQ_LED_OFF 2 int main(int argc, char *argv[]) @@ -45,33 +47,15 @@ int main(int argc, char *argv[]) while( (rv = getopt( argc, argv, "ofemw:d:s:i:b:")) != -1) { switch(rv) { - case 'o': - o_flag = 1; - break; - case 'f': - f_flag = 1; - break; - case 'e': - e_flag = 1; - break; - case 'm': - m_flag = 1; - break; - case 'w': - w_value = optarg; - break; - case 'd': - d_value = optarg; - break; - case 's': - s_value = optarg; - break; - case 'i': - i_value = optarg; - break; - case 'b': - b_value = optarg; - break; + case 'o': o_flag = 1; break; + case 'f': f_flag = 1; break; + case 'e': e_flag = 1; break; + case 'm': m_flag = 1; break; + case 'w': w_value = optarg; break; + case 'd': d_value = optarg; break; + case 's': s_value = optarg; break; + case 'i': i_value = optarg; break; + case 'b': b_value = optarg; break; case '?': if ( optopt == 'w' ) { log_err("Option -%c requires an argument.", optopt); @@ -126,6 +110,11 @@ int main(int argc, char *argv[]) REQ_LED_OFF, (unsigned char *)buffer254, sizeof(buffer254) ); printf("total bytes xfrd: %d \n", xfr_cnt); } + if (w_value) { //OFF send REQ_LED_OFF + check( write_file( rprog_handle, w_value, i_value, b_value) == SUCCESS, + "Failed to write file: %s", w_value); + } + close_usb( context, rprog_handle); diff --git a/host/source/usb_operations.h b/host/source/usb_operations.h index d0d3b87..c7b4bb0 100644 --- a/host/source/usb_operations.h +++ b/host/source/usb_operations.h @@ -2,6 +2,7 @@ #define _usb_operations_h #include "usb_operations.h" +#include "usb_commands.h" //control transfer request types //uint8_t libusb_control_setup::bmRequestType @@ -23,11 +24,6 @@ //LIBUSB_ENDPOINT_IN In: device-to-host. //LIBUSB_ENDPOINT_OUT Out: host-to-device. -// vendor requests also defined in firmware -// TODO put in combined .h file for both host and fw -#define REQ_LED_ON 1 -#define REQ_LED_OFF 2 - //USB timeout #define SEC_5 5000 diff --git a/host/source/write_operations.c b/host/source/write_operations.c new file mode 100644 index 0000000..2dbb21f --- /dev/null +++ b/host/source/write_operations.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include + +//uncomment to DEBUG this file alone +#define DEBUG +//"make debug" to get DEBUG msgs on entire program +#include "dbg.h" + +#include "write_operations.h" +#include "usb_operations.h" +#include "usb_commands.h" + +#define SIZE_NES_HEADER 16 +#define SIZE_PRG_BANK 16384 +#define SIZE_CHR_BANK 8192 + +int write_file( libusb_device_handle *usbhandle, char *filename, char *ines_mapper, char *board_config ) +{ + int rv = 0; + int index = 0; + FILE *fileptr = NULL; + uint8_t data[128]; + + //first open file + fileptr = fopen( filename, "rb"); + //returns file ptr on success, NULL on fail + check( fileptr, "Unable to open file: %s in read binary mode", filename); + + //then determine file type + uint8_t header[SIZE_NES_HEADER]; + //size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); + rv = fread( header, sizeof(header[0]), (sizeof(header)/sizeof(header[0])), fileptr); + check( rv = sizeof(header), "Unable to read NES header"); + + for ( index = 0; index < SIZE_NES_HEADER; index++ ) { + debug("header byte #%d = %x", index, header[index]); + } + + //0-3: Constant $4E $45 $53 $1A ("NES" followed by MS-DOS end-of-file) + //4: Size of PRG ROM in 16 KB units + //5: Size of CHR ROM in 8 KB units (Value 0 means the board uses CHR RAM) + //6: Flags 6 + //7: Flags 7 + //8: Size of PRG RAM in 8 KB units (Value 0 infers 8 KB for compatibility; see PRG RAM circuit) + //9: Flags 9 + //10: Flags 10 (unofficial) + //11-15: Zero filled + uint8_t num_prg_banks = header[4]; + uint8_t num_chr_banks = header[5]; + + int prg_size = num_prg_banks * SIZE_PRG_BANK; + int chr_size = num_chr_banks * SIZE_CHR_BANK; + + //next check board inserted + + //determine if file is compatible with board + + //load script file + + //load retro-prog commands + + //check if erased, erase as needed, and verify + + //write proper data from file to board proper location on board + + //close file + //int fclose(FILE *fp); + check( fclose(fileptr) == SUCCESS, "Unable to close file"); + fileptr = NULL; + + return 0; + +error: + + if (fileptr) { + fclose(fileptr); + } + + return -1; + +} diff --git a/host/source/write_operations.h b/host/source/write_operations.h new file mode 100644 index 0000000..201fbeb --- /dev/null +++ b/host/source/write_operations.h @@ -0,0 +1,8 @@ +#ifndef _write_operations_h +#define _write_operations_h + +#include "write_operations.h" + +int write_file( libusb_device_handle *usbhandle, char *filename, char *ines_mapper, char *board_config ); + +#endif diff --git a/roms/nrom_v_test.nes b/roms/nrom_v_test.nes new file mode 100644 index 0000000..818a432 Binary files /dev/null and b/roms/nrom_v_test.nes differ diff --git a/shared/usb_commands.h b/shared/usb_commands.h new file mode 100644 index 0000000..c96b359 --- /dev/null +++ b/shared/usb_commands.h @@ -0,0 +1,7 @@ +#ifndef _usb_commands_h +#define _usb_commands_h + +#define REQ_LED_ON 1 +#define REQ_LED_OFF 2 + +#endif