Skip to content

Commit

Permalink
Allow coexistance of usb and usb_cx2
Browse files Browse the repository at this point in the history
usblink calls the correct functions depending on emulate_cx2 now.
NavNet handling still needs fixing.
  • Loading branch information
Vogtinator committed Mar 28, 2021
1 parent ffe7deb commit 857fdc8
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 45 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
##CX II:
* Standby needs fast timer + GPIO bits
* Check other aladdin PMU bits
* USB needs refactoring, like usblink_cx2_reset called from usblink_reset
* CX usblink doesn't work anymore
* Flash creation needs changes
* Load product from manuf
Expand Down
1 change: 1 addition & 0 deletions core/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ bool memory_initialize(uint32_t sdram_size)
write_word_map[0xB4 >> 2] = nope_write_word;
}
add_reset_proc(usb_reset);
add_reset_proc(usb_cx2_reset);
add_reset_proc(usblink_reset);

read_word_map[0xC0 >> 2] = lcd_read_word;
Expand Down
21 changes: 1 addition & 20 deletions core/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,36 @@ struct usb_qh { // Queue head
uint32_t bufptr[5];
} overlay;
uint32_t reserved;
struct usb_setup {
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
} setup;
struct usb_setup setup;
};

static void usb_int_check() {
int_set(INT_USB, (usb.usbsts & usb.usbintr) | ((usb.otgsc >> 24) & (usb.otgsc >> 16)));
}

void usb_cx2_reset();
void usb_cx2_bus_reset_on();
void usb_cx2_bus_reset_off();
void usb_cx2_receive_setup_packet(const void *packet);

void usb_reset() {
return usb_cx2_reset();
memset(&usb, 0, sizeof usb);
usb.usbcmd = 0x80000;
usb.portsc = 0xEC000004;
usb.otgsc = 0x0F20; // 1120 if nothing plugged in
usb_int_check();
usblink_reset();
gui_debug_printf("usb_reset\n");
}

void usb_bus_reset_on() {
return usb_cx2_bus_reset_on();
usb.portsc &= ~1;
usb.portsc |= 0x0C000100;
usb.deviceaddr = 0;
usb.usbsts |= 0x40;
usb.epsr = 0;
usb_int_check();
gui_debug_printf("usb reset on\n");
}

void usb_bus_reset_off() {
return usb_cx2_bus_reset_off();
usb.portsc &= ~0x0C000100;
usb.portsc |= 1;
usb.usbsts |= 4;
usb_int_check();
gui_debug_printf("usb reset off\n");
}

static void usb_prime(struct usb_qh *qh, uint32_t epbit) {
Expand Down Expand Up @@ -102,8 +85,6 @@ static void usb_complete(struct usb_qh *qh, uint32_t epbit, uint32_t size) {
}

void usb_receive_setup_packet(int endpoint, const void *packet) {
return usb_cx2_receive_setup_packet(packet);

struct usb_qh *qh = (struct usb_qh *)(intptr_t)phys_mem_ptr(usb.eplistaddr + (endpoint * 0x80), 0x30);
if (!qh)
error("USB: bad QH");
Expand Down
8 changes: 8 additions & 0 deletions core/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ typedef struct usb_state {
uint32_t epcomplete; // +1BC
} usb_state;

typedef struct usb_setup {
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
} __attribute__((packed)) usb_setup;

extern usb_state usb;
void usb_reset(void);
typedef struct emu_snapshot emu_snapshot;
Expand Down
12 changes: 0 additions & 12 deletions core/usb_cx2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
#include "usb_cx2.h"
#include "usblink_cx2.h"

// TODO: Move into a separate header for usb stuff and add packed attrib
struct usb_setup {
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
};

usb_cx2_state usb_cx2;

static void usb_cx2_int_check()
Expand Down Expand Up @@ -112,7 +103,6 @@ static void usb_cx2_packet_from_calc(uint8_t ep, uint8_t *packet, size_t size)
warn("Packet not handled");
}

extern "C" {
void usb_cx2_reset()
{
usb_cx2 = {};
Expand All @@ -131,7 +121,6 @@ void usb_cx2_reset()
usb_cx2.gisr[2] |= 1;

usb_cx2_int_check();
usblink_cx2_reset();
}

void usb_cx2_bus_reset_on()
Expand Down Expand Up @@ -173,7 +162,6 @@ void usb_cx2_receive_setup_packet(const usb_setup *packet)

usb_cx2_int_check();
}
}

void usb_cx2_fdma_update(int fdma)
{
Expand Down
6 changes: 6 additions & 0 deletions core/usb_cx2.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef USB_CX2_H
#define USB_CX2_H

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -64,14 +65,19 @@ typedef struct usb_cx2_state {
extern struct usb_cx2_state usb_cx2;

void usb_cx2_reset(void);

typedef struct emu_snapshot emu_snapshot;
bool usb_cx2_suspend(emu_snapshot *snapshot);
bool usb_cx2_resume(const emu_snapshot *snapshot);

uint8_t usb_cx2_read_byte(uint32_t addr);
uint16_t usb_cx2_read_half(uint32_t addr);
uint32_t usb_cx2_read_word(uint32_t addr);
void usb_cx2_write_word(uint32_t addr, uint32_t value);

void usb_cx2_bus_reset_on();
void usb_cx2_bus_reset_off();
void usb_cx2_receive_setup_packet(const struct usb_setup *packet);
bool usb_cx2_packet_to_calc(uint8_t ep, const uint8_t *packet, size_t size);

#ifdef __cplusplus
Expand Down
28 changes: 17 additions & 11 deletions core/usblink.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,6 @@ extern void usb_bus_reset_off(void);
extern void usb_receive_setup_packet(int endpoint, void *packet);
extern void usb_receive_packet(int endpoint, void *packet, uint32_t size);

struct usb_setup {
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
};

void usblink_reset() {
if (put_file_state) {
put_file_state = 0;
Expand All @@ -776,6 +768,7 @@ void usblink_reset() {
gui_usblink_changed(usblink_connected);
usblink_state = 0;
usblink_sending = false;
usblink_cx2_reset();
}

void usblink_connect() {
Expand All @@ -791,14 +784,27 @@ void usblink_connect() {
void usblink_timer() {
switch (usblink_state) {
case 1:
usb_bus_reset_on();
if(emulate_cx2)
usb_cx2_bus_reset_on();
else
usb_bus_reset_on();

usblink_state++;
break;
case 2: {
usb_bus_reset_off();
//printf("Sending SET_ADDRESS\n");
struct usb_setup packet = { 0, 5, 1, 0, 0 };
usb_receive_setup_packet(0, &packet);
if(emulate_cx2)
{
usb_cx2_bus_reset_off();
usb_cx2_receive_setup_packet(&packet);
}
else
{
usb_bus_reset_off();
usb_receive_setup_packet(0, &packet);
}

usblink_state++;
break;
}
Expand Down
1 change: 0 additions & 1 deletion core/usblink_cx2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,4 @@ void usblink_cx2_reset()
{
usblink_cx2_state.seqno = 0;
usblink_cx2_state.handshake_complete = false;
usblink_reset();
}

0 comments on commit 857fdc8

Please sign in to comment.