}request_both_write, request_cpu_program, request_ppu_program;
//---- function start ----
-static void flash_config_set(const uint8_t *t, void (*set)(uint16_t, uint16_t, uint16_t, uint16_t))
-{
- uint16_t c000x, c2aaa, c5555, unit;
- c000x = t[0];
- c000x |= t[1] << 8;
- c2aaa = t[2];
- c2aaa |= t[3] << 8;
- c5555 = t[4];
- c5555 |= t[5] << 8;
- unit = t[6];
- unit |= t[7] << 8;
- (*set)(c000x, c2aaa, c5555, unit);
-}
/*static uint8_t cpu_buffer[FLASH_PACKET_SIZE];
static uint8_t ppu_buffer[FLASH_PACKET_SIZE];*/
uchar usbFunctionWrite(uchar *data, uchar len)
goto BOTH_NEXT;
BOTH_NEXT:{
request_both_write.offset += length;
- int ret = request_both_write.offset == request_both_write.length;
+ uchar ret = request_both_write.offset == request_both_write.length;
if(ret){
request_both_write.request = REQUEST_NOP;
}
switch(request_cpu_program.request){
case REQUEST_FLASH_PROGRAM:
case REQUEST_FLASH_CONFIG_SET:{
- static uint8_t *w = cpu_buffer; //this is static pointer! be careful.
- if(request_cpu_program.offset == 0){
- w = cpu_buffer;
- }
- memcpy(w, data, length);
- w += length;
+ memcpy(cpu_buffer + request_cpu_program.offset, data, length);
request_cpu_program.offset += length;
- int ret = request_cpu_program.offset == request_cpu_program.length;
+ uchar ret = request_cpu_program.offset == request_cpu_program.length;
if(ret){
if(request_cpu_program.request == REQUEST_FLASH_CONFIG_SET){
- flash_config_set(cpu_buffer, flash_cpu_config);
+ flash_cpu_config(cpu_buffer);
}else{
flash_cpu_program(request_cpu_program.address, request_cpu_program.length, cpu_buffer);
}
switch(request_ppu_program.request){
case REQUEST_FLASH_PROGRAM:
case REQUEST_FLASH_CONFIG_SET:{
- static uint8_t *w = ppu_buffer; //static pointer
- if(request_ppu_program.offset == 0){
- w = ppu_buffer;
- }
- memcpy(w, data, length);
- w += length;
+ memcpy(ppu_buffer + request_ppu_program.offset, data, length);
request_ppu_program.offset += length;
- int ret = request_ppu_program.offset == request_ppu_program.length;
+ uchar ret = request_ppu_program.offset == request_ppu_program.length;
if(ret){
if(request_ppu_program.request == REQUEST_FLASH_CONFIG_SET){
- flash_config_set(ppu_buffer, flash_ppu_config);
+ flash_ppu_config(ppu_buffer);
}else{
flash_ppu_program(request_ppu_program.address, request_ppu_program.length, ppu_buffer);
}
enum compare_status cpu_compare(uint16_t address, uint16_t length, const uint8_t *data)
{
+ while(length != 0){
+ BUS_CONTROL_OUT = BUS_CLOSE;
+ direction_write();
+ address_set(address);
+ BUS_CONTROL_OUT = bit_get_negative(CPU_ROMCS);
+ direction_read();
+ if(DATABUS_IN != *data){
+ BUS_CONTROL_OUT = BUS_CLOSE;
+ direction_write();
+ return NG;
+ }
+ data += 1;
+ address += 1;
+ length--;
+ }
+ BUS_CONTROL_OUT = BUS_CLOSE;
+ direction_write();
return OK;
}
enum compare_status ppu_compare(uint16_t address, uint16_t length, const uint8_t *data)
PROGRAM, TOGGLE_FIRST, TOGGLE_CHECK
} status, request;
uint16_t command_000x, command_2aaa, command_5555;
+ uint8_t retry_enable;
uint16_t address, length, program_unit;
const uint8_t *data;
uint8_t toggle, retry_count;
seqence_cpu.status = IDLE;
seqence_ppu.status = IDLE;
}
-static void config_set(uint16_t c000x, uint16_t c2aaa, uint16_t c5555, uint16_t unit, struct flash_seqence *t)
+static inline uint16_t unpack_short_le(const uint8_t *t)
{
- t->command_000x = c000x;
- t->command_2aaa = c2aaa;
- t->command_5555 = c5555;
- t->program_unit = unit;
- t->program_command[0].address = c5555;
+ uint16_t r = t[0];
+ r |= t[1] << 8;
+ return r;
+}
+static void config_set(const uint8_t *data, struct flash_seqence *t)
+{
+ t->command_000x = unpack_short_le(data);
+ data += 2;
+ t->command_2aaa = unpack_short_le(data);
+ data += 2;
+ t->command_5555 = unpack_short_le(data);
+ data += 2;
+ t->program_unit = unpack_short_le(data);
+ data += 2;
+ t->retry_enable = *data;
+
+ t->program_command[0].address = t->command_5555;
t->program_command[0].data = 0xaa;
- t->program_command[1].address = c2aaa;
+ t->program_command[1].address = t->command_2aaa;
t->program_command[1].data = 0x55;
- t->program_command[2].address = c5555;
+ t->program_command[2].address = t->command_5555;
t->program_command[2].data = 0xa0;
};
-void flash_cpu_config(uint16_t c000x, uint16_t c2aaa, uint16_t c5555, uint16_t unit)
+void flash_cpu_config(const uint8_t *data)
{
- config_set(c000x, c2aaa, c5555, unit, &seqence_cpu);
+ config_set(data, &seqence_cpu);
}
-void flash_ppu_config(uint16_t c000x, uint16_t c2aaa, uint16_t c5555, uint16_t unit)
+void flash_ppu_config(const uint8_t *data)
{
- config_set(c000x, c2aaa, c5555, unit, &seqence_ppu);
+ config_set(data, &seqence_ppu);
}
static void program_assign(enum status status, uint16_t address, uint16_t length, const uint8_t *data, struct flash_seqence *t)
uint8_t d;
t->reader(t->address, 1, &d);
if(t->toggle == (d & 0x40)){
- if((t->program_unit != 1) && (t->request == PROGRAM)){ //page program device retry
- if(t->retry_count >= 20){
- if(t->compare(t->address, t->program_unit, t->data) == NG){
- t->retry_count += 1;
- t->status = PROGRAM;
- return;
- }
+ if((t->retry_enable != 0) && (t->request == PROGRAM) && (t->retry_count < 20)){
+ if(t->compare(t->address, t->program_unit, t->data) == NG){
+ t->retry_count += 1;
+ t->status = PROGRAM;
+ return;
}
- t->retry_count = 0;
}
+ t->retry_count = 0;
t->address += t->program_unit;
t->data += t->program_unit;
t->length -= t->program_unit;
void flash_both_idle(void);
//cpu
uint8_t flash_cpu_status(void);
-void flash_cpu_config(uint16_t c000x, uint16_t c2aaa, uint16_t c5555, uint16_t unit);
+void flash_cpu_config(const uint8_t *data);
void flash_cpu_program(uint16_t address, uint16_t length, uint8_t *data);
void flash_cpu_erase(uint16_t address);
void flash_cpu_device_get(uint8_t d[2]);
uint8_t flash_ppu_status(void);
//ppu
-void flash_ppu_config(uint16_t c000x, uint16_t c2aaa, uint16_t c5555, uint16_t unit);
+void flash_ppu_config(const uint8_t *data);
void flash_ppu_program(uint16_t address, uint16_t length, uint8_t *data);
void flash_ppu_erase(uint16_t address);
void flash_ppu_device_get(uint8_t d[2]);