5 #include <kazzo_request.h>
6 #include <kazzo_task.h>
7 #include "memory_manage.h"
8 #include "reader_master.h"
9 #include "usb_device.h"
10 #include "reader_kazzo.h"
13 static inline usb_dev_handle *device_open(void)
15 usb_dev_handle *handle = NULL;
16 const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID};
17 const unsigned char rawPid[2] = {USB_CFG_DEVICE_ID};
18 char vendor[] = {USB_CFG_VENDOR_NAME, 0};
19 char product[] = {USB_CFG_DEVICE_NAME, 0};
22 /* compute VID/PID from usbconfig.h so that there is a central source of information */
23 vid = (rawVid[1] << 8) | rawVid[0];
24 pid = (rawPid[1] << 8) | rawPid[0];
26 if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) == 0){
32 static void kazzo_open(struct reader_handle *h)
34 h->handle = device_open();
37 static void kazzo_close(struct reader_handle *h)
44 //-------- read sequence --------
45 static void device_read(usb_dev_handle *handle, enum request r, enum index index, long address, long length, uint8_t *data)
47 int cnt = usb_control_msg(
49 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
51 index, (char *) data, length, TIMEOUT
59 static void read_main(const struct reader_handle *d, const struct gauge *g, const enum request r, enum index index, long address, long length, uint8_t *data)
61 const int packet = READ_PACKET_SIZE;
62 while(length >= packet){
64 (usb_dev_handle *) d->handle,
65 r, index, address, packet, data
70 g->value_add(g->bar, g->label, packet);
74 (usb_dev_handle *) d->handle,
75 r, index, address, length, data
77 g->value_add(g->bar, g->label, packet);
80 static void kazzo_cpu_read(const struct reader_handle *d, const struct gauge *g, long address, long length, uint8_t *data)
82 read_main(d, g, REQUEST_CPU_READ, INDEX_IMPLIED, address, length, data);
83 // read_main(REQUEST_CPU_READ_6502, address, length, data);
85 static void kazzo_ppu_read(const struct reader_handle *d, const struct gauge *g, long address, long length, uint8_t *data)
87 read_main(d, g, REQUEST_PPU_READ, INDEX_IMPLIED, address, length, data);
89 //-------- write sequence --------
91 When host send data that contains 0xff many times, v-usb losts some
92 bits. To prevent losting bits, mask data xor 0xa5;
94 static void device_write(usb_dev_handle *handle, enum request w, enum index index, long address, long length, const uint8_t *data)
96 uint8_t *d = Malloc(length);
98 memcpy(d, data, length);
99 for(i = 0; i < length; i++){
102 int cnt = usb_control_msg(
104 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
106 index, (char *) d, length, TIMEOUT
110 puts(usb_strerror());
116 static void kazzo_init(const struct reader_handle *d)
118 device_write((usb_dev_handle *) d->handle, REQUEST_PHI2_INIT, INDEX_IMPLIED, 0, 0, NULL);
121 static void write_memory(const struct reader_handle *d, enum request r, long address, long length, const uint8_t *data)
124 long l = length >= FLASH_PACKET_SIZE ? FLASH_PACKET_SIZE : length;
125 device_write((usb_dev_handle *) d->handle, r, INDEX_IMPLIED, address, l, data);
131 static void kazzo_cpu_write_6502(const struct reader_handle *d, long address, long length, const uint8_t *data)
133 write_memory(d, REQUEST_CPU_WRITE_6502, address, length, data);
136 static void kazzo_ppu_write(const struct reader_handle *d, long address, long length, const uint8_t *data)
138 write_memory(d, REQUEST_PPU_WRITE, address, length, data);
141 static inline void pack_short_le(long l, uint8_t *t)
144 t[1] = (l >> 8) & 0xff;
146 static void flash_config(const struct reader_handle *d, enum request r, enum index index, long c000x, long c2aaa, long c5555, long unit, bool retry)
148 const int size = 2 * 4 + 1;
151 assert(unit >= 1 && unit < 0x400);
152 pack_short_le(c000x, t);
153 t += sizeof(uint16_t);
154 pack_short_le(c2aaa, t);
155 t += sizeof(uint16_t);
156 pack_short_le(c5555, t);
157 t += sizeof(uint16_t);
158 pack_short_le(unit, t);
159 t += sizeof(uint16_t);
160 *t = retry == true ? 1 : 0;
161 device_write((usb_dev_handle *) d->handle, r, index, 0, size, buf);
163 static void kazzo_cpu_flash_config(const struct reader_handle *d, long c000x, long c2aaa, long c5555, long unit, bool retry)
165 flash_config(d, REQUEST_FLASH_CONFIG_SET, INDEX_CPU, c000x, c2aaa, c5555, unit, retry);
167 static void kazzo_ppu_flash_config(const struct reader_handle *d, long c000x, long c2aaa, long c5555, long unit, bool retry)
169 flash_config(d, REQUEST_FLASH_CONFIG_SET, INDEX_PPU, c000x, c2aaa, c5555, unit, retry);
172 static inline void flash_execute(const struct reader_handle *d, enum request p, enum request s, enum index index, long address, const uint8_t *data, int size, bool dowait, bool skip)
177 uint8_t *filldata = Malloc(size);
178 memset(filldata, 0xff, size);
179 filled = memcmp(filldata, data, size);
180 if(0){ //nesasm fill 0 to unused area. When this routine is enabled, programming will speed up and compare mode will not work.
181 memset(filldata, 0, size);
182 filled &= memcmp(filldata, data, size);
187 device_write((usb_dev_handle *) d->handle, p, index, address, size, data);
192 device_read((usb_dev_handle *) d->handle, s, index, 0, 1, &status);
193 }while(status != KAZZO_TASK_FLASH_IDLE);
196 static void kazzo_cpu_flash_erase(const struct reader_handle *d, long address, bool dowait)
198 flash_execute(d, REQUEST_FLASH_ERASE, REQUEST_FLASH_STATUS, INDEX_CPU, address, NULL, 0, dowait, false);
200 static void kazzo_ppu_flash_erase(const struct reader_handle *d, long address, bool dowait)
202 flash_execute(d, REQUEST_FLASH_ERASE, REQUEST_FLASH_STATUS, INDEX_PPU, address, NULL, 0, dowait, false);
205 static void dump(const uint8_t *w, const uint8_t *r, long length)
208 if(memcmp(r, w, 0x10) != 0){
211 for(i = 0; i < 0x10; i+=4){
212 printf("%02x %02x %02x %02x-", w[i], w[i+1], w[i+2], w[i+3]);
216 for(i = 0; i < 0x10; i+=4){
217 printf("%02x %02x %02x %02x-", r[i], r[i+1], r[i+2], r[i+3]);
226 static long flash_program(const struct reader_handle *h, const struct gauge *g, enum index index, long address, long length, const uint8_t *data, bool dowait, bool skip)
228 enum request p = REQUEST_FLASH_PROGRAM;
229 enum request s = REQUEST_FLASH_STATUS;
231 flash_execute(h, p, s, index, address, data, FLASH_PACKET_SIZE, dowait, skip);
232 g->value_add(g->bar, g->label, FLASH_PACKET_SIZE);
233 return FLASH_PACKET_SIZE;
236 uint8_t *d = Malloc(FLASH_PACKET_SIZE);
237 while(length >= FLASH_PACKET_SIZE){
238 flash_execute(h, p, s, index, address, data, FLASH_PACKET_SIZE, dowait, skip);
240 //device_read(handle, REQUEST_FLASH_BUFFER_GET, index, 0, FLASH_PACKET_SIZE, d);
241 if(memcmp(d, data, FLASH_PACKET_SIZE) != 0){
242 puts("packet send error");
243 dump(data, d, FLASH_PACKET_SIZE);
246 g->value_add(g->bar, g->label, FLASH_PACKET_SIZE);
247 address += FLASH_PACKET_SIZE;
248 data += FLASH_PACKET_SIZE;
249 count += FLASH_PACKET_SIZE;
250 length -= FLASH_PACKET_SIZE;
255 static long kazzo_cpu_flash_program(const struct reader_handle *d, const struct gauge *g, long address, long length, const uint8_t *data, bool dowait, bool skip)
257 return flash_program(d, g, INDEX_CPU, address, length, data, dowait, skip);
259 static long kazzo_ppu_flash_program(const struct reader_handle *d, const struct gauge *g, long address, long length, const uint8_t *data, bool dowait, bool skip)
261 return flash_program(d, g, INDEX_PPU, address, length, data, dowait, skip);
264 static void kazzo_flash_status(const struct reader_handle *d, uint8_t s[2])
266 read_main(d, &GAUGE_DUMMY, REQUEST_FLASH_STATUS, INDEX_BOTH, 0, 2, s);
268 static void kazzo_cpu_flash_device_get(const struct reader_handle *d, uint8_t s[2])
270 read_main(d, &GAUGE_DUMMY, REQUEST_FLASH_DEVICE, INDEX_CPU, 0, 2, s);
272 static void kazzo_ppu_flash_device_get(const struct reader_handle *d, uint8_t s[2])
274 read_main(d, &GAUGE_DUMMY, REQUEST_FLASH_DEVICE, INDEX_PPU, 0, 2, s);
276 static uint8_t kazzo_vram_connection(const struct reader_handle *d)
279 read_main(d, &GAUGE_DUMMY, REQUEST_VRAM_CONNECTION, INDEX_IMPLIED, 0, 1, &s);
282 const struct reader_driver DRIVER_KAZZO = {
284 .memory_read = kazzo_cpu_read,
285 .memory_write = kazzo_cpu_write_6502,
286 .flash_config = kazzo_cpu_flash_config,
287 .flash_erase = kazzo_cpu_flash_erase,
288 .flash_program = kazzo_cpu_flash_program,
289 .flash_device_get = kazzo_cpu_flash_device_get
291 .memory_read = kazzo_ppu_read,
292 .memory_write = kazzo_ppu_write,
293 .flash_config = kazzo_ppu_flash_config,
294 .flash_erase = kazzo_ppu_flash_erase,
295 .flash_program = kazzo_ppu_flash_program,
296 .flash_device_get = kazzo_ppu_flash_device_get
299 .open = kazzo_open, .close = kazzo_close,
301 .flash_status = kazzo_flash_status,
302 .vram_connection = kazzo_vram_connection