OSDN Git Service

client 0.6.2 release
[unagi/old-svn-converted.git] / client / tag / 0.6.2 / anago / script.lua.c
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include "lua.h"
4 #include "lualib.h"
5 #include "lauxlib.h"
6 #include "type.h"
7 #include "header.h"
8 #include "reader_master.h"
9 #include "script.h"
10
11 static struct anago_flash_order{
12         int program_count, command_change;
13         long address, length;
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)
23 {
24         lua_Number d = lua_tonumber(t, index);
25         return (long) d;
26 }
27 static void command_set(lua_State *l, struct anago_flash_order *t)
28 {
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);
33         d |= address;
34         switch(command){
35         case 0x02aa: case 0x2aaa:
36                 t->c2aaa = d;
37                 break;
38         case 0x0555: case 0x5555:
39                 t->c5555 = d;
40                 break;
41         default:
42                 puts("unknown command address");
43                 return;
44         }
45         t->command_change += 1;
46 }
47 static int cpu_command(lua_State *l)
48 {
49         command_set(l, &order_cpu);
50         return 0;
51 }
52 static int ppu_command(lua_State *l)
53 {
54         command_set(l, &order_ppu);
55         return 0;
56 }
57 static int write(lua_State *l, struct anago_flash_order *t)
58 {
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);
63         return 0;
64 }
65 static int cpu_write(lua_State *l)
66 {
67         return write(l, &order_cpu);
68 }
69 static int program_regist(lua_State *l, const char *name, struct anago_flash_order *t)
70 {
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;
76         }
77         
78         printf("programming %s area 0x%06x...\n", name, t->memory->offset);
79         fflush(stdout);
80         return lua_yield(l, 0);
81 }
82 static void program_execute(struct anago_flash_order *t)
83 {
84         if(t->program_count == 0){
85                 t->erase(t->c2aaa, false);
86                 t->program_count += 1;
87                 printf("erase...\n");
88                 fflush(stdout);
89                 return;
90         }
91         t->program_count += 1;
92 //      printf("writing %06x\n", t->memory->offset);
93 //      fflush(stdout);
94         const long w = t->program(t->address, t->length, t->memory->data + t->memory->offset, false);
95         t->address += w;
96         t->length -= w;
97         t->memory->offset += w;
98 }
99 static int cpu_program(lua_State *l)
100 {
101         return program_regist(l, "program ROM", &order_cpu);
102 }
103 static int ppu_program(lua_State *l)
104 {
105         return program_regist(l, "charcter ROM", &order_ppu);
106 }
107 static int mmc1_write(lua_State *l)
108 {
109         long address = long_get(l, 1);
110         uint8_t data = (uint8_t) long_get(l, 2);
111         int i = 5;
112         while(i != 0){
113                 order_cpu.write(address, 1, &data);
114                 data >>= 1;
115                 i--;
116         }
117         return 0;
118 }
119
120 void script_execute(struct config *c)
121 {
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;
131         
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;
141         
142         lua_State *const l = lua_open();
143         luaL_openlibs(l);
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){
151                 lua_close(l);
152                 return;
153         }
154         if(lua_pcall(l, 0, 0, 0) != 0){
155                 puts(lua_tostring(l, -1));
156                 return;
157         }
158         lua_getfield(l, LUA_GLOBALSINDEX, "initalize");
159         lua_getglobal(l, "initalize");
160         lua_call(l, 0, 0);
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){
167                 state_cpu = 0;
168         }
169         if(order_ppu.memory->size == 0 || c->flash_ppu->id_device == FLASH_ID_DEVICE_DUMMY){
170                 state_ppu = 0;
171         }
172         if(state_cpu != 0){
173                 state_cpu = lua_resume(co_cpu, 0);
174         }
175         if(state_ppu != 0){
176                 state_ppu = lua_resume(co_ppu, 0);
177         }
178         do{
179                 uint8_t s[2];
180                 Sleep(2);
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);
185                         }else{
186                                 program_execute(&order_cpu);
187                         }
188                 }
189                 if(state_ppu != 0 && s[1] == 0){
190                         if(order_ppu.length == 0){
191                                 state_ppu = lua_resume(co_ppu, 0);
192                         }else{
193                                 program_execute(&order_ppu);
194                         }
195                 }
196         }while(state_cpu != 0 || state_ppu != 0);
197         lua_close(l);
198 }