4 #include <kazzo_request.h>
5 #include <kazzo_task.h>
6 #include "memory_manage.h"
7 #include "reader_master.h"
8 #include "usb_device.h"
9 #include "reader_kazzo.h"
11 static usb_dev_handle *device_open(void)
13 usb_dev_handle *handle = NULL;
14 const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID};
15 const unsigned char rawPid[2] = {USB_CFG_DEVICE_ID};
16 char vendor[] = {USB_CFG_VENDOR_NAME, 0};
17 char product[] = {USB_CFG_DEVICE_NAME, 0};
20 /* compute VID/PID from usbconfig.h so that there is a central source of information */
21 vid = (rawVid[1] << 8) | rawVid[0];
22 pid = (rawPid[1] << 8) | rawPid[0];
24 if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) == 0){
27 fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid);
30 static usb_dev_handle *handle = NULL;
31 static int kazzo_open_close(enum reader_control oc)
35 handle = device_open();
36 return handle == NULL ? NG : OK;
47 //-------- read sequence --------
48 static void device_read(usb_dev_handle *handle, enum request r, long address, long length, uint8_t *data)
50 int cnt = usb_control_msg(
52 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
54 0, data, length, TIMEOUT
62 static void read_main(const enum request r, long address, long length, uint8_t *data)
64 const int packet = READ_PACKET_SIZE;
65 while(length >= packet){
66 device_read(handle, r, address, packet, data);
72 device_read(handle, r, address, length, data);
75 static void kazzo_cpu_read(long address, long length, uint8_t *data)
77 read_main(REQUEST_CPU_READ, address, length, data);
78 // read_main(REQUEST_CPU_READ_6502, address, length, data);
80 static void kazzo_ppu_read(long address, long length, uint8_t *data)
82 read_main(REQUEST_PPU_READ, address, length, data);
84 //-------- write sequence --------
85 static void device_write(usb_dev_handle *handle, enum request w, long address, long length, const uint8_t *data)
87 //Removing const attribute is not good method....
88 uint8_t *d = Malloc(length);
89 memcpy(d, data, length);
90 int cnt = usb_control_msg(
92 USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
104 static void kazzo_init(void)
106 device_write(handle, REQUEST_PHI2_INIT, 0, 0, NULL);
109 static void kazzo_cpu_write_6502(long address, long length, const uint8_t *data)
111 device_write(handle, REQUEST_CPU_WRITE_6502, address, length, data);
113 /*static void kazzo_cpu_write_flash(long address, long data)
115 uint8_t d = (uint8_t) (data & 0xff);
116 device_write(handle, REQUEST_CPU_WRITE_FLASH, address, 1, &d);
118 static void kazzo_ppu_write(long address, long length, const uint8_t *data)
120 device_write(handle, REQUEST_PPU_WRITE, address, length, data);
123 static inline void pack_short_le(long l, uint8_t *t)
126 t[1] = (l >> 8) & 0xff;
128 static void flash_config(enum request r, long c000x, long c2aaa, long c5555, long unit)
130 const int size = 2 * 4;
132 assert(unit >= 1 && unit < 0x400);
133 pack_short_le(c000x, t);
134 pack_short_le(c2aaa, t + 2);
135 pack_short_le(c5555, t + 4);
136 pack_short_le(unit, t + 6);
137 device_write(handle, r, 0, size, t);
139 static void kazzo_cpu_flash_config(long c000x, long c2aaa, long c5555, long unit)
141 flash_config(REQUEST_CPU_FLASH_CONFIG_SET, c000x, c2aaa, c5555, unit);
143 static void kazzo_ppu_flash_config(long c000x, long c2aaa, long c5555, long unit)
145 flash_config(REQUEST_PPU_FLASH_CONFIG_SET, c000x, c2aaa, c5555, unit);
148 static inline void flash_execute(enum request p, enum request s, long address, const uint8_t *data, int size, bool dowait)
151 device_write(handle, p, address, size, data);
155 device_read(handle, s, 0, 1, &status);
156 }while(status != KAZZO_TASK_FLASH_IDLE);
159 static void kazzo_cpu_flash_erase(long address, bool dowait)
161 flash_execute(REQUEST_CPU_FLASH_ERASE, REQUEST_CPU_FLASH_STATUS, address, NULL, 0, dowait);
163 static void kazzo_ppu_flash_erase(long address, bool dowait)
165 flash_execute(REQUEST_PPU_FLASH_ERASE, REQUEST_PPU_FLASH_STATUS, address, NULL, 0, dowait);
168 static long flash_program(enum request p, enum request s, long address, long length, const uint8_t *data, bool dowait)
171 flash_execute(p, s, address, data, FLASH_PACKET_SIZE, dowait);
172 return FLASH_PACKET_SIZE;
175 uint8_t *d = Malloc(FLASH_PACKET_SIZE);
176 while(length >= FLASH_PACKET_SIZE){
177 flash_execute(p, s, address, data, FLASH_PACKET_SIZE, dowait);
178 device_read(handle, REQUEST_PPU_FLASH_BUFFER_GET, 0, FLASH_PACKET_SIZE, d);
179 if(memcmp(d, data, FLASH_PACKET_SIZE) != 0){
180 puts("packet send error");
182 address += FLASH_PACKET_SIZE;
183 data += FLASH_PACKET_SIZE;
184 count += FLASH_PACKET_SIZE;
185 length -= FLASH_PACKET_SIZE;
190 static long kazzo_cpu_flash_program(long address, long length, const uint8_t *data, bool dowait)
192 enum request p = REQUEST_CPU_FLASH_PROGRAM;
193 enum request s = REQUEST_CPU_FLASH_STATUS;
194 return flash_program(p, s, address, length, data, dowait);
196 static long kazzo_ppu_flash_program(long address, long length, const uint8_t *data, bool dowait)
198 return flash_program(REQUEST_PPU_FLASH_PROGRAM, REQUEST_PPU_FLASH_STATUS, address, length, data, dowait);
201 static void kazzo_flash_status(uint8_t s[2])
203 read_main(REQUEST_BOTH_FLASH_STATUS, 0, 2, s);
205 static void kazzo_cpu_flash_device_get(uint8_t s[2])
207 read_main(REQUEST_CPU_FLASH_DEVICE, 0, 2, s);
209 static void kazzo_ppu_flash_device_get(uint8_t s[2])
211 read_main(REQUEST_PPU_FLASH_DEVICE, 0, 2, s);
213 static uint8_t kazzo_vram_connection(void)
216 read_main(REQUEST_VRAM_CONNECTION, 0, 1, &s);
219 const struct reader_driver DRIVER_KAZZO = {
221 .open_or_close = kazzo_open_close,
223 .cpu_read = kazzo_cpu_read, .ppu_read = kazzo_ppu_read,
224 .cpu_write_6502 = kazzo_cpu_write_6502,
225 .flash_support = true,
226 .ppu_write = kazzo_ppu_write,
227 .cpu_flash_config = kazzo_cpu_flash_config,
228 .cpu_flash_erase = kazzo_cpu_flash_erase,
229 .cpu_flash_program = kazzo_cpu_flash_program,
230 .cpu_flash_device_get = kazzo_cpu_flash_device_get,
231 .ppu_flash_config = kazzo_ppu_flash_config,
232 .ppu_flash_erase = kazzo_ppu_flash_erase,
233 .ppu_flash_program = kazzo_ppu_flash_program,
234 .ppu_flash_device_get = kazzo_ppu_flash_device_get,
235 .flash_status = kazzo_flash_status,
236 .vram_connection = kazzo_vram_connection