8 #include "reader_master.h"
11 static struct anago_flash_order{
12 int program_count, command_change;
14 long c2aaa, c5555, unit;
15 struct memory *memory;
16 void (*config)(long c2aaa, long c5555, long unit);
17 void (*write)(long address, long length, const uint8_t *data);
18 void (*read)(long address, long length, u8 *data);
19 void (*erase)(long address, bool dowait);
20 long (*program)(long address, long length, u8 *data, bool dowait);
21 }order_cpu, order_ppu;
22 static inline long long_get(lua_State *t, int index)
24 lua_Number d = lua_tonumber(t, index);
27 static void command_set(lua_State *l, struct anago_flash_order *t)
29 long command = long_get(l, 1);
30 long address = long_get(l, 2);
31 long mask = long_get(l, 3);
32 long d = command & (mask - 1);
35 case 0x02aa: case 0x2aaa:
38 case 0x0555: case 0x5555:
42 puts("unknown command address");
45 t->command_change += 1;
47 static int cpu_command(lua_State *l)
49 command_set(l, &order_cpu);
52 static int ppu_command(lua_State *l)
54 command_set(l, &order_ppu);
57 static int write(lua_State *l, struct anago_flash_order *t)
59 long address = long_get(l, 1);
60 long data = long_get(l, 2);
61 uint8_t d8 = (uint8_t) data;
62 t->write(address, 1, &d8);
65 static int cpu_write(lua_State *l)
67 return write(l, &order_cpu);
69 static int program_regist(lua_State *l, const char *name, struct anago_flash_order *t)
71 t->address = long_get(l, 1);
72 t->length = long_get(l, 2);
73 if(t->command_change != 0){
74 t->config(t->c2aaa, t->c5555, t->unit);
75 t->command_change = 0;
78 printf("programming %s area 0x%06x...\n", name, t->memory->offset);
80 return lua_yield(l, 0);
82 static void program_execute(struct anago_flash_order *t)
84 if(t->program_count == 0){
85 t->erase(t->c2aaa, false);
86 t->program_count += 1;
91 t->program_count += 1;
92 // printf("writing %06x\n", t->memory->offset);
94 const long w = t->program(t->address, t->length, t->memory->data + t->memory->offset, false);
97 t->memory->offset += w;
99 static int cpu_program(lua_State *l)
101 return program_regist(l, "program ROM", &order_cpu);
103 static int ppu_program(lua_State *l)
105 return program_regist(l, "charcter ROM", &order_ppu);
107 static int mmc1_write(lua_State *l)
109 long address = long_get(l, 1);
110 uint8_t data = (uint8_t) long_get(l, 2);
113 order_cpu.write(address, 1, &data);
120 void script_execute(struct config *c)
122 order_cpu.command_change = 0;
123 order_cpu.program_count = 0;
124 order_cpu.unit = c->flash_cpu->pagesize;
125 order_cpu.memory = &c->rom.cpu_rom;
126 order_cpu.config = c->reader->cpu_flash_config;
127 order_cpu.write = c->reader->cpu_write_6502;
128 order_cpu.read = c->reader->cpu_read;
129 order_cpu.erase = c->reader->cpu_flash_erase;
130 order_cpu.program = c->reader->cpu_flash_program;
132 order_ppu.command_change = 0;
133 order_ppu.program_count = 0;
134 order_ppu.unit = c->flash_ppu->pagesize;
135 order_ppu.memory = &c->rom.ppu_rom;
136 order_ppu.config = c->reader->ppu_flash_config;
137 order_ppu.write = c->reader->ppu_write; //warning ¤Ï̵»ë
138 order_ppu.read = c->reader->ppu_read;
139 order_ppu.erase = c->reader->ppu_flash_erase;
140 order_ppu.program = c->reader->ppu_flash_program;
142 lua_State *const l = lua_open();
144 lua_register(l, "cpu_write", cpu_write);
145 lua_register(l, "cpu_program", cpu_program);
146 lua_register(l, "cpu_command", cpu_command);
147 lua_register(l, "ppu_program", ppu_program);
148 lua_register(l, "ppu_command", ppu_command);
149 lua_register(l, "mmc1_write", mmc1_write);
150 if(luaL_loadfile(l, c->script) == LUA_ERRFILE){
154 if(lua_pcall(l, 0, 0, 0) != 0){
155 puts(lua_tostring(l, -1));
158 lua_getfield(l, LUA_GLOBALSINDEX, "initalize");
159 lua_getglobal(l, "initalize");
161 lua_State *const co_cpu = lua_newthread(l);
162 lua_State *const co_ppu = lua_newthread(l);
163 lua_getglobal(co_cpu, "program_cpu");
164 lua_getglobal(co_ppu, "program_ppu");
165 int state_cpu = LUA_YIELD, state_ppu = LUA_YIELD;
166 if(order_cpu.memory->size == 0 || c->flash_cpu->id_device == FLASH_ID_DEVICE_DUMMY){
169 if(order_ppu.memory->size == 0 || c->flash_ppu->id_device == FLASH_ID_DEVICE_DUMMY){
173 state_cpu = lua_resume(co_cpu, 0);
176 state_ppu = lua_resume(co_ppu, 0);
181 c->reader->flash_status(s);
182 if(state_cpu != 0 && s[0] == 0){
183 if(order_cpu.length == 0){
184 state_cpu = lua_resume(co_cpu, 0);
186 program_execute(&order_cpu);
189 if(state_ppu != 0 && s[1] == 0){
190 if(order_ppu.length == 0){
191 state_ppu = lua_resume(co_ppu, 0);
193 program_execute(&order_ppu);
196 }while(state_cpu != 0 || state_ppu != 0);