From b8be4b768af4380e3bf49d9d5b27def1de73926e Mon Sep 17 00:00:00 2001 From: Paul Molloy Date: Sun, 20 Nov 2016 23:57:09 -0600 Subject: [PATCH] new file: shared/usb_commands.h -shared .h files used in both firmware and host app modified: firmware/Makefile modified: host/Makefile -added shared dependency to copy shared files to source prior to building modified: host/source/usb_operations.h modified: firmware/source/main.c -removing usb commands (now in shared) modified: host/include/dbg.h -adding SUCCESS defintion modified: host/source/inlprog.c -calling write file if write arg sent new file: host/source/write_operations.c new file: host/source/write_operations.h -creation of write operations files -opens file and reads header -some skeleton comments -lots of work left to do here new file: roms/nrom_v_test.nes -adding NROM test rom file --- firmware/Makefile | 6 ++- firmware/source/main.c | 4 +- host/Makefile | 9 +++- host/include/dbg.h | 2 + host/source/inlprog.c | 47 +++++++----------- host/source/usb_operations.h | 6 +-- host/source/write_operations.c | 85 +++++++++++++++++++++++++++++++++ host/source/write_operations.h | 8 ++++ roms/nrom_v_test.nes | Bin 0 -> 40976 bytes shared/usb_commands.h | 7 +++ 10 files changed, 134 insertions(+), 40 deletions(-) create mode 100644 host/source/write_operations.c create mode 100644 host/source/write_operations.h create mode 100644 roms/nrom_v_test.nes create mode 100644 shared/usb_commands.h 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 0000000000000000000000000000000000000000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