diff --git a/.gitignore b/.gitignore index 4fa4957..d52c73c 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ images/ # Screenshot dir screens/ + +# build tools that are often kept/tested locally as well as on the ci machines +yoursway-create-dmg/ diff --git a/assets/uncrustify.cfg b/assets/uncrustify.cfg new file mode 100644 index 0000000..10a5e49 --- /dev/null +++ b/assets/uncrustify.cfg @@ -0,0 +1,19 @@ +output_tab_size = 2 # new tab size +indent_columns = output_tab_size +indent_switch_case = output_tab_size +indent_with_tabs = 0 +pp_indent_if = 0 # number +pp_if_indent_code = false # false/true +pp_define_at_level = false # false/true +nl_assign_leave_one_liners = true # false/true +nl_class_leave_one_liners = true # false/true +nl_enum_leave_one_liners = true # false/true +nl_getset_leave_one_liners = true # false/true +nl_func_leave_one_liners = true # false/true +nl_if_leave_one_liners = true # false/true +nl_while_leave_one_liners = true # false/true +nl_func_type_name = remove +nl_fdef_brace = remove +sp_fparen_brace = add + + diff --git a/src/adb.c b/src/adb.c index 11d31d3..1aff502 100644 --- a/src/adb.c +++ b/src/adb.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /* adb_mode bit 3 and bit 2 (faster repeats for arrows and space/del) not done*/ @@ -33,53 +33,53 @@ extern byte *g_memory_ptr; extern word32 g_mem_size_total; enum { - ADB_IDLE = 0, - ADB_IN_CMD, - ADB_SENDING_DATA, + ADB_IDLE = 0, + ADB_IN_CMD, + ADB_SENDING_DATA, }; -#define ADB_C027_MOUSE_DATA 0x80 -#define ADB_C027_MOUSE_INT 0x40 -#define ADB_C027_DATA_VALID 0x20 -#define ADB_C027_DATA_INT 0x10 -#define ADB_C027_KBD_VALID 0x08 -#define ADB_C027_KBD_INT 0x04 -#define ADB_C027_MOUSE_COORD 0x02 -#define ADB_C027_CMD_FULL 0x01 +#define ADB_C027_MOUSE_DATA 0x80 +#define ADB_C027_MOUSE_INT 0x40 +#define ADB_C027_DATA_VALID 0x20 +#define ADB_C027_DATA_INT 0x10 +#define ADB_C027_KBD_VALID 0x08 +#define ADB_C027_KBD_INT 0x04 +#define ADB_C027_MOUSE_COORD 0x02 +#define ADB_C027_CMD_FULL 0x01 -#define ADB_C027_NEG_MASK ( ~ ( \ - ADB_C027_MOUSE_DATA | ADB_C027_DATA_VALID | \ - ADB_C027_KBD_VALID | ADB_C027_MOUSE_COORD | \ - ADB_C027_CMD_FULL)) +#define ADB_C027_NEG_MASK ( ~( \ + ADB_C027_MOUSE_DATA | ADB_C027_DATA_VALID | \ + ADB_C027_KBD_VALID | ADB_C027_MOUSE_COORD | \ + ADB_C027_CMD_FULL)) int halt_on_all_c027 = 0; -word32 g_adb_repeat_delay = 45; -word32 g_adb_repeat_rate = 3; -word32 g_adb_repeat_info = 0x23; -word32 g_adb_char_set = 0x0; -word32 g_adb_layout_lang = 0x0; +word32 g_adb_repeat_delay = 45; +word32 g_adb_repeat_rate = 3; +word32 g_adb_repeat_info = 0x23; +word32 g_adb_char_set = 0x0; +word32 g_adb_layout_lang = 0x0; -word32 g_adb_interrupt_byte = 0; -int g_adb_state = ADB_IDLE; +word32 g_adb_interrupt_byte = 0; +int g_adb_state = ADB_IDLE; -word32 g_adb_cmd = (word32)-1; -int g_adb_cmd_len = 0; -int g_adb_cmd_so_far = 0; -word32 g_adb_cmd_data[16]; +word32 g_adb_cmd = (word32)-1; +int g_adb_cmd_len = 0; +int g_adb_cmd_so_far = 0; +word32 g_adb_cmd_data[16]; -#define MAX_ADB_DATA_PEND 16 +#define MAX_ADB_DATA_PEND 16 -word32 g_adb_data[MAX_ADB_DATA_PEND]; -int g_adb_data_pending = 0; +word32 g_adb_data[MAX_ADB_DATA_PEND]; +int g_adb_data_pending = 0; -word32 g_c027_val = 0; -word32 g_c025_val = 0; +word32 g_c027_val = 0; +word32 g_c025_val = 0; -byte adb_memory[256]; +byte adb_memory[256]; -word32 g_adb_mode = 0; /* mode set via set_modes, clear_modes */ +word32 g_adb_mode = 0; /* mode set via set_modes, clear_modes */ int g_warp_pointer = 0; int g_hide_pointer = 0; @@ -92,13 +92,13 @@ int g_mouse_fifo_pos = 0; int g_mouse_raw_x = 0; int g_mouse_raw_y = 0; -#define ADB_MOUSE_FIFO 8 +#define ADB_MOUSE_FIFO 8 STRUCT(Mouse_fifo) { - double dcycs; - int x; - int y; - int buttons; + double dcycs; + int x; + int y; + int buttons; }; Mouse_fifo g_mouse_fifo[ADB_MOUSE_FIFO] = { { 0, 0, 0, 0 } }; @@ -106,1795 +106,1714 @@ Mouse_fifo g_mouse_fifo[ADB_MOUSE_FIFO] = { { 0, 0, 0, 0 } }; int g_mouse_warp_x = 0; int g_mouse_warp_y = 0; -int g_adb_mouse_valid_data = 0; -int g_adb_mouse_coord = 0; +int g_adb_mouse_valid_data = 0; +int g_adb_mouse_coord = 0; -#define MAX_KBD_BUF 8 +#define MAX_KBD_BUF 8 -int g_key_down = 0; -int g_hard_key_down = 0; -int g_a2code_down = 0; -int g_kbd_read_no_update = 0; -int g_kbd_chars_buffered = 0; -int g_kbd_buf[MAX_KBD_BUF]; -word32 g_adb_repeat_vbl = 0; +int g_key_down = 0; +int g_hard_key_down = 0; +int g_a2code_down = 0; +int g_kbd_read_no_update = 0; +int g_kbd_chars_buffered = 0; +int g_kbd_buf[MAX_KBD_BUF]; +word32 g_adb_repeat_vbl = 0; -int g_kbd_dev_addr = 2; /* ADB physical kbd addr */ -int g_mouse_dev_addr = 3; /* ADB physical mouse addr */ +int g_kbd_dev_addr = 2; /* ADB physical kbd addr */ +int g_mouse_dev_addr = 3; /* ADB physical mouse addr */ -int g_kbd_ctl_addr = 2; /* ADB microcontroller's kbd addr */ -int g_mouse_ctl_addr = 3; /* ADB ucontroller's mouse addr*/ - /* above are ucontroller's VIEW of where mouse/kbd */ - /* are...if they are moved, mouse/keyboard funcs */ - /* should stop (c025, c000, c024, etc). */ +int g_kbd_ctl_addr = 2; /* ADB microcontroller's kbd addr */ +int g_mouse_ctl_addr = 3; /* ADB ucontroller's mouse addr*/ +/* above are ucontroller's VIEW of where mouse/kbd */ +/* are...if they are moved, mouse/keyboard funcs */ +/* should stop (c025, c000, c024, etc). */ -word32 g_virtual_key_up[4]; /* bitmask of all possible 128 a2codes */ - /* indicates which keys are up=1 by bit */ +word32 g_virtual_key_up[4]; /* bitmask of all possible 128 a2codes */ + /* indicates which keys are up=1 by bit */ -int g_keypad_key_is_down[10] = { 0 };/* List from 0-9 of which keypad */ - /* keys are currently pressed */ +int g_keypad_key_is_down[10] = { 0 }; /* List from 0-9 of which keypad */ + /* keys are currently pressed */ -#define SHIFT_DOWN ( (g_c025_val & 0x01) ) -#define CTRL_DOWN ( (g_c025_val & 0x02) ) -#define CAPS_LOCK_DOWN ( (g_c025_val & 0x04) ) -#define OPTION_DOWN ( (g_c025_val & 0x40) ) -#define CMD_DOWN ( (g_c025_val & 0x80) ) +#define SHIFT_DOWN ( (g_c025_val & 0x01) ) +#define CTRL_DOWN ( (g_c025_val & 0x02) ) +#define CAPS_LOCK_DOWN ( (g_c025_val & 0x04) ) +#define OPTION_DOWN ( (g_c025_val & 0x40) ) +#define CMD_DOWN ( (g_c025_val & 0x80) ) -#define MAX_ADB_KBD_REG3 16 +#define MAX_ADB_KBD_REG3 16 int g_kbd_reg0_pos = 0; int g_kbd_reg0_data[MAX_ADB_KBD_REG3]; -int g_kbd_reg3_16bit = 0x602; /* also set in adb_reset()! */ +int g_kbd_reg3_16bit = 0x602; /* also set in adb_reset()! */ -int g_adb_init = 0; +int g_adb_init = 0; -void -adb_init() -{ - int keycode; - int i; +void adb_init() { + int keycode; + int i; - if(g_adb_init) { - halt_printf("g_adb_init = %d!\n", g_adb_init); - } - g_adb_init = 1; + if(g_adb_init) { + halt_printf("g_adb_init = %d!\n", g_adb_init); + } + g_adb_init = 1; - for(i = 0; i < 128; i++) { - keycode = a2_key_to_ascii[i][0]; - if(keycode != i) { - glogf("ADB keycode lost/skipped: i=%x: keycode=%x", i, keycode); - my_exit(1); - } - } + for(i = 0; i < 128; i++) { + keycode = a2_key_to_ascii[i][0]; + if(keycode != i) { + glogf("ADB keycode lost/skipped: i=%x: keycode=%x", i, keycode); + my_exit(1); + } + } - g_c025_val = 0; + g_c025_val = 0; - for(i = 0; i < 4; i++) { - g_virtual_key_up[i] = -1; - } + for(i = 0; i < 4; i++) { + g_virtual_key_up[i] = -1; + } - for(i = 0; i < 10; i++) { - g_keypad_key_is_down[i] = 0; - } + for(i = 0; i < 10; i++) { + g_keypad_key_is_down[i] = 0; + } - adb_reset(); + adb_reset(); } // OG Added adb_shut() -void adb_shut() -{ - g_adb_init = 0; +void adb_shut() { + g_adb_init = 0; } -void -adb_reset() -{ +void adb_reset() { - g_c027_val = 0; + g_c027_val = 0; - g_key_down = 0; + g_key_down = 0; - g_kbd_dev_addr = 2; - g_mouse_dev_addr = 3; + g_kbd_dev_addr = 2; + g_mouse_dev_addr = 3; - g_kbd_ctl_addr = 2; - g_mouse_ctl_addr = 3; + g_kbd_ctl_addr = 2; + g_mouse_ctl_addr = 3; - adb_clear_data_int(); - adb_clear_mouse_int(); - adb_clear_kbd_srq(); + adb_clear_data_int(); + adb_clear_mouse_int(); + adb_clear_kbd_srq(); - g_adb_data_pending = 0; - g_adb_interrupt_byte = 0; - g_adb_state = ADB_IDLE; - g_adb_mouse_coord = 0; - g_adb_mouse_valid_data = 0; + g_adb_data_pending = 0; + g_adb_interrupt_byte = 0; + g_adb_state = ADB_IDLE; + g_adb_mouse_coord = 0; + g_adb_mouse_valid_data = 0; - g_kbd_reg0_pos = 0; - g_kbd_reg3_16bit = 0x602; + g_kbd_reg0_pos = 0; + g_kbd_reg3_16bit = 0x602; } -#define LEN_ADB_LOG 16 +#define LEN_ADB_LOG 16 STRUCT(Adb_log) { - word32 addr; - int val; - int state; + word32 addr; + int val; + int state; }; Adb_log g_adb_log[LEN_ADB_LOG]; -int g_adb_log_pos = 0; +int g_adb_log_pos = 0; -void -adb_log(word32 addr, int val) -{ - int pos; +void adb_log(word32 addr, int val) { + int pos; - pos = g_adb_log_pos; - g_adb_log[pos].addr = addr; - g_adb_log[pos].val = val; - g_adb_log[pos].state = g_adb_state; - pos++; - if(pos >= LEN_ADB_LOG) { - pos = 0; - } - g_adb_log_pos = pos; + pos = g_adb_log_pos; + g_adb_log[pos].addr = addr; + g_adb_log[pos].val = val; + g_adb_log[pos].state = g_adb_state; + pos++; + if(pos >= LEN_ADB_LOG) { + pos = 0; + } + g_adb_log_pos = pos; } -void -show_adb_log(void) -{ - int pos; - int i; +void show_adb_log(void) { + int pos; + int i; - pos = g_adb_log_pos; - printf("ADB log pos: %d\n", pos); - for(i = 0; i < LEN_ADB_LOG; i++) { - pos--; - if(pos < 0) { - pos = LEN_ADB_LOG - 1; - } - printf("%d:%d: addr:%04x = %02x, st:%d\n", i, pos, - g_adb_log[pos].addr, g_adb_log[pos].val, - g_adb_log[pos].state); - } - printf("kbd: dev: %x, ctl: %x; mouse: dev: %x, ctl: %x\n", - g_kbd_dev_addr, g_kbd_ctl_addr, - g_mouse_dev_addr, g_mouse_ctl_addr); - printf("g_adb_state: %d, g_adb_interrupt_byte: %02x\n", - g_adb_state, g_adb_interrupt_byte); + pos = g_adb_log_pos; + printf("ADB log pos: %d\n", pos); + for(i = 0; i < LEN_ADB_LOG; i++) { + pos--; + if(pos < 0) { + pos = LEN_ADB_LOG - 1; + } + printf("%d:%d: addr:%04x = %02x, st:%d\n", i, pos, + g_adb_log[pos].addr, g_adb_log[pos].val, + g_adb_log[pos].state); + } + printf("kbd: dev: %x, ctl: %x; mouse: dev: %x, ctl: %x\n", + g_kbd_dev_addr, g_kbd_ctl_addr, + g_mouse_dev_addr, g_mouse_ctl_addr); + printf("g_adb_state: %d, g_adb_interrupt_byte: %02x\n", + g_adb_state, g_adb_interrupt_byte); } -void -adb_error(void) -{ - halt_printf("Adb Error\n"); +void adb_error(void) { + halt_printf("Adb Error\n"); - show_adb_log(); + show_adb_log(); } -void -adb_add_kbd_srq() -{ - if(g_kbd_reg3_16bit & 0x200) { - /* generate SRQ */ - g_adb_interrupt_byte |= 0x08; - add_irq(IRQ_PENDING_ADB_KBD_SRQ); - } else { - printf("Got keycode but no kbd SRQ!\n"); - } +void adb_add_kbd_srq() { + if(g_kbd_reg3_16bit & 0x200) { + /* generate SRQ */ + g_adb_interrupt_byte |= 0x08; + add_irq(IRQ_PENDING_ADB_KBD_SRQ); + } else { + printf("Got keycode but no kbd SRQ!\n"); + } } -void -adb_clear_kbd_srq() -{ - remove_irq(IRQ_PENDING_ADB_KBD_SRQ); +void adb_clear_kbd_srq() { + remove_irq(IRQ_PENDING_ADB_KBD_SRQ); - /* kbd SRQ's are the only ones to handle now, so just clean it out */ - g_adb_interrupt_byte &= (~(0x08)); + /* kbd SRQ's are the only ones to handle now, so just clean it out */ + g_adb_interrupt_byte &= (~(0x08)); } -void -adb_add_data_int() -{ - if(g_c027_val & ADB_C027_DATA_INT) { - add_irq(IRQ_PENDING_ADB_DATA); - } +void adb_add_data_int() { + if(g_c027_val & ADB_C027_DATA_INT) { + add_irq(IRQ_PENDING_ADB_DATA); + } } -void -adb_add_mouse_int() -{ - if(g_c027_val & ADB_C027_MOUSE_INT) { - add_irq(IRQ_PENDING_ADB_MOUSE); - } +void adb_add_mouse_int() { + if(g_c027_val & ADB_C027_MOUSE_INT) { + add_irq(IRQ_PENDING_ADB_MOUSE); + } } -void -adb_clear_data_int() -{ - remove_irq(IRQ_PENDING_ADB_DATA); +void adb_clear_data_int() { + remove_irq(IRQ_PENDING_ADB_DATA); } -void -adb_clear_mouse_int() -{ - remove_irq(IRQ_PENDING_ADB_MOUSE); +void adb_clear_mouse_int() { + remove_irq(IRQ_PENDING_ADB_MOUSE); } -void -adb_send_bytes(int num_bytes, word32 val0, word32 val1, word32 val2) -{ - word32 val; - int shift_amount; - int i; +void adb_send_bytes(int num_bytes, word32 val0, word32 val1, word32 val2) { + word32 val; + int shift_amount; + int i; - if((num_bytes >= 12) || (num_bytes >= MAX_ADB_DATA_PEND)) { - halt_printf("adb_send_bytes: %d is too many!\n", num_bytes); - } + if((num_bytes >= 12) || (num_bytes >= MAX_ADB_DATA_PEND)) { + halt_printf("adb_send_bytes: %d is too many!\n", num_bytes); + } - g_adb_state = ADB_SENDING_DATA; - g_adb_data_pending = num_bytes; - adb_add_data_int(); + g_adb_state = ADB_SENDING_DATA; + g_adb_data_pending = num_bytes; + adb_add_data_int(); - for(i = 0; i < num_bytes; i++) { - if(i < 4) { - val = val0; - } else if(i < 8) { - val = val1; - } else { - val = val2; - } + for(i = 0; i < num_bytes; i++) { + if(i < 4) { + val = val0; + } else if(i < 8) { + val = val1; + } else { + val = val2; + } - shift_amount = 8*(3 - i); - g_adb_data[i] = (val >> shift_amount) & 0xff; - adb_printf("adb_send_bytes[%d] = %02x\n", i, g_adb_data[i]); - } + shift_amount = 8*(3 - i); + g_adb_data[i] = (val >> shift_amount) & 0xff; + adb_printf("adb_send_bytes[%d] = %02x\n", i, g_adb_data[i]); + } } -void -adb_send_1byte(word32 val) -{ +void adb_send_1byte(word32 val) { - if(g_adb_data_pending != 0) { - halt_printf("g_adb_data_pending: %d\n", g_adb_data_pending); - } + if(g_adb_data_pending != 0) { + halt_printf("g_adb_data_pending: %d\n", g_adb_data_pending); + } - adb_send_bytes(1, val << 24, 0, 0); + adb_send_bytes(1, val << 24, 0, 0); } -void -adb_response_packet(int num_bytes, word32 val) -{ +void adb_response_packet(int num_bytes, word32 val) { - if(g_adb_data_pending != 0) { - halt_printf("adb_response_packet, but pending: %d\n", - g_adb_data_pending); - } + if(g_adb_data_pending != 0) { + halt_printf("adb_response_packet, but pending: %d\n", + g_adb_data_pending); + } - g_adb_state = ADB_IDLE; - g_adb_data_pending = num_bytes; - g_adb_data[0] = val & 0xff; - g_adb_data[1] = (val >> 8) & 0xff; - g_adb_data[2] = (val >> 16) & 0xff; - g_adb_data[3] = (val >> 24) & 0xff; - if(num_bytes) { - g_adb_interrupt_byte |= 0x80 + num_bytes - 1; - } else { - g_adb_interrupt_byte |= 0x80; - } + g_adb_state = ADB_IDLE; + g_adb_data_pending = num_bytes; + g_adb_data[0] = val & 0xff; + g_adb_data[1] = (val >> 8) & 0xff; + g_adb_data[2] = (val >> 16) & 0xff; + g_adb_data[3] = (val >> 24) & 0xff; + if(num_bytes) { + g_adb_interrupt_byte |= 0x80 + num_bytes - 1; + } else { + g_adb_interrupt_byte |= 0x80; + } - adb_printf("adb_response packet: %d: %08x\n", - num_bytes, val); + adb_printf("adb_response packet: %d: %08x\n", + num_bytes, val); - adb_add_data_int(); + adb_add_data_int(); } -void -adb_kbd_reg0_data(int a2code, int is_up) -{ - if(g_kbd_reg0_pos >= MAX_ADB_KBD_REG3) { - /* too many keys, toss */ - halt_printf("Had to toss key: %02x, %d\n", a2code, is_up); - return; - } +void adb_kbd_reg0_data(int a2code, int is_up) { + if(g_kbd_reg0_pos >= MAX_ADB_KBD_REG3) { + /* too many keys, toss */ + halt_printf("Had to toss key: %02x, %d\n", a2code, is_up); + return; + } - g_kbd_reg0_data[g_kbd_reg0_pos] = a2code + (is_up << 7); + g_kbd_reg0_data[g_kbd_reg0_pos] = a2code + (is_up << 7); - adb_printf("g_kbd_reg0_data[%d] = %02x\n", g_kbd_reg0_pos, - g_kbd_reg0_data[g_kbd_reg0_pos]); + adb_printf("g_kbd_reg0_data[%d] = %02x\n", g_kbd_reg0_pos, + g_kbd_reg0_data[g_kbd_reg0_pos]); - g_kbd_reg0_pos++; + g_kbd_reg0_pos++; - adb_add_kbd_srq(); + adb_add_kbd_srq(); } -void -adb_kbd_talk_reg0() -{ - word32 val0, val1; - word32 reg; - int num_bytes; - int num; - int i; +void adb_kbd_talk_reg0() { + word32 val0, val1; + word32 reg; + int num_bytes; + int num; + int i; - num = 0; - val0 = g_kbd_reg0_data[0]; - val1 = g_kbd_reg0_data[1]; + num = 0; + val0 = g_kbd_reg0_data[0]; + val1 = g_kbd_reg0_data[1]; - num_bytes = 0; - if(g_kbd_reg0_pos > 0) { - num_bytes = 2; - num = 1; - if((val0 & 0x7f) == 0x7f) { - /* reset */ - val1 = val0; - } else if(g_kbd_reg0_pos > 1) { - num = 2; - if((val1 & 0x7f) == 0x7f) { - /* If first byte some other key, don't */ - /* put RESET next! */ - num = 1; - val1 = 0xff; - } - } else { - val1 = 0xff; - } - } + num_bytes = 0; + if(g_kbd_reg0_pos > 0) { + num_bytes = 2; + num = 1; + if((val0 & 0x7f) == 0x7f) { + /* reset */ + val1 = val0; + } else if(g_kbd_reg0_pos > 1) { + num = 2; + if((val1 & 0x7f) == 0x7f) { + /* If first byte some other key, don't */ + /* put RESET next! */ + num = 1; + val1 = 0xff; + } + } else { + val1 = 0xff; + } + } - if(num) { - for(i = num; i < g_kbd_reg0_pos; i++) { - g_kbd_reg0_data[i-1] = g_kbd_reg0_data[i]; - } - g_kbd_reg0_pos -= num; - } + if(num) { + for(i = num; i < g_kbd_reg0_pos; i++) { + g_kbd_reg0_data[i-1] = g_kbd_reg0_data[i]; + } + g_kbd_reg0_pos -= num; + } - reg = (val0 << 8) + val1; + reg = (val0 << 8) + val1; - adb_printf("adb_kbd_talk0: %04x\n", reg); + adb_printf("adb_kbd_talk0: %04x\n", reg); - adb_response_packet(num_bytes, reg); - if(g_kbd_reg0_pos == 0) { - adb_clear_kbd_srq(); - } + adb_response_packet(num_bytes, reg); + if(g_kbd_reg0_pos == 0) { + adb_clear_kbd_srq(); + } } -void -adb_set_config(word32 val0, word32 val1, word32 val2) -{ - int new_mouse; - int new_kbd; - int tmp1; - - new_mouse = val0 >> 4; - new_kbd = val0 & 0xf; - if(new_mouse != g_mouse_ctl_addr) { - glogf("ADB config: mouse from %x to %x!", g_mouse_ctl_addr, new_mouse); - adb_error(); - g_mouse_ctl_addr = new_mouse; - } - if(new_kbd != g_kbd_ctl_addr) { - glogf("ADB config: kbd from %x to %x!", g_kbd_ctl_addr, new_kbd); - adb_error(); - g_kbd_ctl_addr = new_kbd; - } - - - tmp1 = val2 >> 4; - if(tmp1 == 4) { - g_adb_repeat_delay = 0; - } else if(tmp1 < 4) { - g_adb_repeat_delay = (tmp1 + 1) * 15; - } else { - halt_printf("Bad ADB repeat delay: %02x\n", tmp1); - } - - tmp1 = val2 & 0xf; - if(g_rom_version >= 3) { - tmp1 = 9 - tmp1; - } - - switch(tmp1) { - case 0: - g_adb_repeat_rate = 1; - break; - case 1: - g_adb_repeat_rate = 2; - break; - case 2: - g_adb_repeat_rate = 3; - break; - case 3: - g_adb_repeat_rate = 3; - break; - case 4: - g_adb_repeat_rate = 4; - break; - case 5: - g_adb_repeat_rate = 5; - break; - case 6: - g_adb_repeat_rate = 7; - break; - case 7: - g_adb_repeat_rate = 15; - break; - case 8: - /* I don't know what this should be, ROM 03 uses it */ - g_adb_repeat_rate = 30; - break; - case 9: - /* I don't know what this should be, ROM 03 uses it */ - g_adb_repeat_rate = 60; - break; - default: - halt_printf("Bad repeat rate: %02x\n", tmp1); - } - -} - -void -adb_set_new_mode(word32 val) -{ - if(val & 0x03) { - glogf("Disabling keyboard/mouse:%02x!", val); - } - - if(val & 0xa2) { - halt_printf("ADB set mode: %02x!\n", val); - adb_error(); - } - - g_adb_mode = val; -} - - -int -adb_read_c026() -{ - word32 ret; - int i; - - ret = 0; - switch(g_adb_state) { - case ADB_IDLE: - ret = g_adb_interrupt_byte; - g_adb_interrupt_byte = 0; - if(g_irq_pending & IRQ_PENDING_ADB_KBD_SRQ) { - g_adb_interrupt_byte |= 0x08; - } - if(g_adb_data_pending == 0) { - if(ret & 0x80) { - halt_printf("read_c026: ret:%02x, pend:%d\n", - ret, g_adb_data_pending); - } - adb_clear_data_int(); - } - if(g_adb_data_pending) { - if(g_adb_state != ADB_IN_CMD) { - g_adb_state = ADB_SENDING_DATA; - } - } - break; - case ADB_IN_CMD: - ret = 0; - break; - case ADB_SENDING_DATA: - ret = g_adb_data[0]; - for(i = 1; i < g_adb_data_pending; i++) { - g_adb_data[i-1] = g_adb_data[i]; - } - g_adb_data_pending--; - if(g_adb_data_pending <= 0) { - g_adb_data_pending = 0; - g_adb_state = ADB_IDLE; - adb_clear_data_int(); - } - break; - default: - halt_printf("Bad ADB state: %d!\n", g_adb_state); - adb_clear_data_int(); - break; - } - - adb_printf("Reading c026. Returning %02x, st: %02x, pend: %d\n", - ret, g_adb_state, g_adb_data_pending); - - adb_log(0xc026, ret); - return (ret & 0xff); -} - - -void -adb_write_c026(int val) -{ - word32 tmp; - int dev; - - adb_printf("Writing c026 with %02x\n", val); - adb_log(0x1c026, val); - - - switch(g_adb_state) { - case ADB_IDLE: - g_adb_cmd = val; - g_adb_cmd_so_far = 0; - g_adb_cmd_len = 0; - - dev = val & 0xf; - switch(val) { - case 0x01: /* Abort */ - adb_printf("Performing adb abort\n"); - /* adb_abort() */ - break; - case 0x03: /* Flush keyboard buffer */ - adb_printf("Flushing adb keyboard buffer\n"); - /* Do nothing */ - break; - case 0x04: /* Set modes */ - adb_printf("ADB set modes\n"); - g_adb_state = ADB_IN_CMD; - g_adb_cmd_len = 1; - break; - case 0x05: /* Clear modes */ - adb_printf("ADB clear modes\n"); - g_adb_state = ADB_IN_CMD; - g_adb_cmd_len = 1; - break; - case 0x06: /* Set config */ - adb_printf("ADB set config\n"); - g_adb_state = ADB_IN_CMD; - g_adb_cmd_len = 3; - break; - case 0x07: /* Sync */ - adb_printf("Performing sync cmd!\n"); - g_adb_state = ADB_IN_CMD; - if(g_rom_version == 1) { - g_adb_cmd_len = 4; - } else { - g_adb_cmd_len = 8; - } - break; - case 0x08: /* Write mem */ - adb_printf("Starting write_mem cmd\n"); - g_adb_state = ADB_IN_CMD; - g_adb_cmd_len = 2; - break; - case 0x09: /* Read mem */ - adb_printf("Performing read_mem cmd!\n"); - g_adb_state = ADB_IN_CMD; - g_adb_cmd_len = 2; - break; - case 0x0a: /* Read modes byte */ - printf("Performing read_modes cmd!\n"); - /* set_halt(1); */ - adb_send_1byte(g_adb_mode); - break; - case 0x0b: /* Read config bytes */ - printf("Performing read_configs cmd!\n"); - tmp = (g_mouse_ctl_addr << 20) + - (g_kbd_ctl_addr << 16) + - (g_adb_char_set << 12) + - (g_adb_layout_lang << 8) + - (g_adb_repeat_info << 0); - tmp = (0x82U << 24) + tmp; - adb_send_bytes(4, tmp, 0, 0); - break; - case 0x0d: /* Get Version */ - adb_printf("Performing get_version cmd!\n"); - val = 0; - if(g_rom_version == 1) { - /* ROM 01 = revision 5 */ - val = 5; - } else { - /* ROM 03 checks for rev >= 6 */ - val = 6; - } - adb_send_1byte(val); - break; - case 0x0e: /* Read avail char sets */ - adb_printf("Performing read avail char sets cmd!\n"); - adb_send_bytes(2, /* just 2 bytes */ - 0x08000000, /* number of ch sets=0x8 */ - 0, 0); - /* set_halt(1); */ - break; - case 0x0f: /* Read avail kbd layouts */ - adb_printf("Performing read avail kbd layouts cmd!\n"); - adb_send_bytes(0x2, /* number of kbd layouts=0xa */ - 0x0a000000, 0, 0); - /* set_halt(1); */ - break; - case 0x10: /* Reset */ - printf("ADB reset, cmd 0x10\n"); - do_reset(); - break; - case 0x11: /* Send ADB keycodes */ - adb_printf("Sending ADB keycodes\n"); - g_adb_state = ADB_IN_CMD; - g_adb_cmd_len = 1; - break; - case 0x12: /* ADB cmd 12: ROM 03 only! */ - if(g_rom_version >= 3) { - g_adb_state = ADB_IN_CMD; - g_adb_cmd_len = 2; - } else { - printf("ADB cmd 12, but not ROM 3!\n"); - adb_error(); - } - break; - case 0x13: /* ADB cmd 13: ROM 03 only! */ - if(g_rom_version >= 3) { - g_adb_state = ADB_IN_CMD; - g_adb_cmd_len = 2; - } else { - printf("ADB cmd 13, but not ROM 3!\n"); - adb_error(); - } - break; - case 0x73: /* Disable SRQ device 3: mouse */ - adb_printf("Disabling Mouse SRQ's (device 3)\n"); - /* HACK HACK...should deal with SRQs on mouse */ - break; - case 0xb0: case 0xb1: case 0xb2: case 0xb3: - case 0xb4: case 0xb5: case 0xb6: case 0xb7: - case 0xb8: case 0xb9: case 0xba: case 0xbb: - case 0xbc: case 0xbd: case 0xbe: case 0xbf: - /* Listen dev x reg 3 */ - adb_printf("Sending data to dev %x reg 3\n", dev); - g_adb_state = ADB_IN_CMD; - g_adb_cmd_len = 2; - break; - case 0xc0: case 0xc1: case 0xc2: case 0xc3: - case 0xc4: case 0xc5: case 0xc6: case 0xc7: - case 0xc8: case 0xc9: case 0xca: case 0xcb: - case 0xcc: case 0xcd: case 0xce: case 0xcf: - /* Talk dev x reg 0 */ - adb_printf("Performing talk dev %x reg 0\n", dev); - if(dev == g_kbd_dev_addr) { - adb_kbd_talk_reg0(); - } else { - printf("Unknown talk dev %x reg 0!\n", dev); - /* send no data, on SRQ, system polls devs */ - /* so we don't want to send anything */ - adb_error(); - } - break; - case 0xf0: case 0xf1: case 0xf2: case 0xf3: - case 0xf4: case 0xf5: case 0xf6: case 0xf7: - case 0xf8: case 0xf9: case 0xfa: case 0xfb: - case 0xfc: case 0xfd: case 0xfe: case 0xff: - /* Talk dev x reg 3 */ - adb_printf("Performing talk dev %x reg 3\n", dev); - if(dev == g_kbd_dev_addr) { - adb_response_packet(2, g_kbd_reg3_16bit); - } else { - printf("Performing talk dev %x reg 3!!\n", dev); - adb_error(); - } - break; - default: - /* The Gog's says ACS Demo 2 has a bug and writes to */ - /* c026 */ - // OG - if (val==0x84) - printf("ACS Demo2 (3: Colum& Music scroll) : discarding unknown controller command\n"); - else - halt_printf("ADB ucontroller cmd %02x unknown!\n", val); - - break; - } - break; - case ADB_IN_CMD: - adb_printf("Setting byte %d of cmd %02x to %02x\n", - g_adb_cmd_so_far, g_adb_cmd, val); - - g_adb_cmd_data[g_adb_cmd_so_far] = val; - g_adb_cmd_so_far++; - if(g_adb_cmd_so_far >= g_adb_cmd_len) { - adb_printf("Finished cmd %02x\n", g_adb_cmd); - do_adb_cmd(); - } - - break; - default: - printf("adb_state: %02x is unknown! Setting it to ADB_IDLE\n", - g_adb_state); - g_adb_state = ADB_IDLE; - adb_error(); - halt_on_all_c027 = 1; - break; - } - return; -} - -void -do_adb_cmd() -{ - int dev; - int new_kbd; - int addr; - int val; - - dev = g_adb_cmd & 0xf; - - g_adb_state = ADB_IDLE; - - switch(g_adb_cmd) { - case 0x04: /* Set modes */ - adb_printf("Performing ADB set mode: OR'ing in %02x\n", - g_adb_cmd_data[0]); - - val = g_adb_cmd_data[0] | g_adb_mode; - adb_set_new_mode(val); - - break; - case 0x05: /* clear modes */ - adb_printf("Performing ADB clear mode: AND'ing in ~%02x\n", - g_adb_cmd_data[0]); - - val = g_adb_cmd_data[0]; - val = g_adb_mode & (~val); - adb_set_new_mode(val); - break; - case 0x06: /* Set config */ - adb_printf("Set ADB config to %02x %02x %02x\n", - g_adb_cmd_data[0], g_adb_cmd_data[1],g_adb_cmd_data[2]); - - adb_set_config(g_adb_cmd_data[0], g_adb_cmd_data[1], - g_adb_cmd_data[2]); - - break; - case 0x07: /* SYNC */ - adb_printf("Performing ADB SYNC\n"); - adb_printf("data: %02x %02x %02x %02x\n", - g_adb_cmd_data[0], g_adb_cmd_data[1], g_adb_cmd_data[2], - g_adb_cmd_data[3]); - - adb_set_new_mode(g_adb_cmd_data[0]); - adb_set_config(g_adb_cmd_data[1], g_adb_cmd_data[2], - g_adb_cmd_data[3]); - - if(g_rom_version >= 3) { - adb_printf(" and cmd12:%02x %02x cmd13:%02x %02x\n", - g_adb_cmd_data[4], g_adb_cmd_data[5], - g_adb_cmd_data[6], g_adb_cmd_data[7]); - } - break; - case 0x08: /* Write mem */ - addr = g_adb_cmd_data[0]; - val = g_adb_cmd_data[1]; - write_adb_ram(addr, val); - break; - case 0x09: /* Read mem */ - addr = (g_adb_cmd_data[1] << 8) + g_adb_cmd_data[0]; - adb_printf("Performing mem read to addr %04x\n", addr); - adb_send_1byte(read_adb_ram(addr)); - break; - case 0x11: /* Send ADB keycodes */ - val = g_adb_cmd_data[0]; - adb_printf("Performing send ADB keycodes: %02x\n", val); - adb_virtual_key_update(val & 0x7f, val >> 7); - break; - case 0x12: /* ADB cmd12 */ - adb_printf("Performing ADB cmd 12\n"); - adb_printf("data: %02x %02x\n", g_adb_cmd_data[0], - g_adb_cmd_data[1]); - break; - case 0x13: /* ADB cmd13 */ - adb_printf("Performing ADB cmd 13\n"); - adb_printf("data: %02x %02x\n", g_adb_cmd_data[0], - g_adb_cmd_data[1]); - break; - case 0xb0: case 0xb1: case 0xb2: case 0xb3: - case 0xb4: case 0xb5: case 0xb6: case 0xb7: - case 0xb8: case 0xb9: case 0xba: case 0xbb: - case 0xbc: case 0xbd: case 0xbe: case 0xbf: - /* Listen dev x reg 3 */ - if(dev == g_kbd_dev_addr) { - if(g_adb_cmd_data[1] == 0xfe) { - /* change keyboard addr? */ - new_kbd = g_adb_cmd_data[0] & 0xf; - if(new_kbd != dev) { - printf("Moving kbd to dev %x!\n", - new_kbd); - adb_error(); - } - g_kbd_dev_addr = new_kbd; - } else if(g_adb_cmd_data[1] != 1) { - /* see what new device handler id is */ - printf("KBD listen to dev %x reg 3: 1:%02x\n", - dev, g_adb_cmd_data[1]); - adb_error(); - } - if(g_adb_cmd_data[0] != (word32)g_kbd_dev_addr) { - /* see if app is trying to change addr */ - printf("KBD listen to dev %x reg 3: 0:%02x!\n", - dev, g_adb_cmd_data[0]); - adb_error(); - } - g_kbd_reg3_16bit = ((g_adb_cmd_data[0] & 0xf) << 12) + - (g_kbd_reg3_16bit & 0x0fff); - } else if(dev == g_mouse_dev_addr) { - if(g_adb_cmd_data[0] != (word32)dev) { - /* see if app is trying to change mouse addr */ - printf("MOUS listen to dev %x reg3: 0:%02x!\n", - dev, g_adb_cmd_data[0]); - adb_error(); - } - if(g_adb_cmd_data[1] != 1 && g_adb_cmd_data[1] != 2) { - /* see what new device handler id is */ - printf("MOUS listen to dev %x reg 3: 1:%02x\n", - dev, g_adb_cmd_data[1]); - adb_error(); - } - } else { - printf("Listen cmd to dev %x reg3????\n", dev); - printf("data0: %02x, data1: %02x ????\n", - g_adb_cmd_data[0], g_adb_cmd_data[1]); - adb_error(); - } - break; - default: - printf("Doing adb_cmd %02x: UNKNOWN!\n", g_adb_cmd); - break; - } -} - - -int -adb_read_c027() -{ - word32 ret; - - if(halt_on_all_c027) { - halt_printf("halting on all c027 reads!\n"); - } - - if(g_c027_val & (~ADB_C027_NEG_MASK)) { - halt_printf("read_c027: g_c027_val: %02x\n", g_c027_val); - } - - ret = (g_c027_val & ADB_C027_NEG_MASK); - - if(g_adb_mouse_valid_data) { - ret |= ADB_C027_MOUSE_DATA; - } - - if(g_adb_interrupt_byte != 0) { - ret |= ADB_C027_DATA_VALID; - } else if(g_adb_data_pending > 0) { - if((g_adb_state != ADB_IN_CMD)) { - ret |= ADB_C027_DATA_VALID; - } - } - - if(g_adb_mouse_coord) { - ret |= ADB_C027_MOUSE_COORD; - } - -#if 0 - adb_printf("Read c027: %02x, int_byte: %02x, d_pend: %d\n", - ret, g_adb_interrupt_byte, g_adb_data_pending); -#endif - -#if 0 - adb_log(0xc027, ret); -#endif - return ret; -} - -void -adb_write_c027(int val) -{ - word32 old_val; - word32 new_int; - word32 old_int; - - adb_printf("Writing c027 with %02x\n", val); - adb_log(0x1c027, val); - - - old_val = g_c027_val; - - g_c027_val = (val & ADB_C027_NEG_MASK); - new_int = g_c027_val & ADB_C027_MOUSE_INT; - old_int = old_val & ADB_C027_MOUSE_INT; - if(!new_int && old_int) { - adb_clear_mouse_int(); - } - - new_int = g_c027_val & ADB_C027_DATA_INT; - old_int = old_val & ADB_C027_DATA_INT; - if(!new_int && old_int) { - /* ints were on, now off */ - adb_clear_data_int(); - } - - if(g_c027_val & ADB_C027_KBD_INT) { - halt_printf("Can't support kbd interrupts!\n"); - } - - return; -} - -int -read_adb_ram(word32 addr) -{ - int val; - - adb_printf("Reading adb ram addr: %02x\n", addr); - - if(addr >= 0x100) { - if(addr >= 0x1000 && addr < 0x2000) { - /* ROM self-test checksum */ - if(addr == 0x1400) { - val = 0x72; - } else if(addr == 0x1401) { - val = 0xf7; - } else { - val = 0; - } - } else { - printf("adb ram addr out of range: %04x!\n", addr); - val = 0; - } - } else { - val = adb_memory[addr]; - if((addr == 0xb) && (g_rom_version == 1)) { - // read special key state byte for Out of This World - val = (g_c025_val >> 1) & 0x43; - val |= (g_c025_val << 2) & 0x4; - val |= (g_c025_val >> 2) & 0x10; - } - if((addr == 0xc) && (g_rom_version >= 3)) { - // read special key state byte for Out of This World - val = g_c025_val & 0xc7; - printf("val is %02x\n", val); - } - } - - adb_printf("adb_ram returning %02x\n", val); - return val; -} - -void -write_adb_ram(word32 addr, int val) -{ - - adb_printf("Writing adb_ram addr: %02x: %02x\n", addr, val); - - if(addr >= 0x100) { - printf("write adb_ram addr: %02x: %02x!\n", addr, val); - adb_error(); - } else { - adb_memory[addr] = val; - } -} - -int -adb_get_keypad_xy(int get_y) -{ - int x, y; - int key; - int num_keys; - int i, j; - - key = 1; - num_keys = 0; - x = 0; - y = 0; - for(i = 0; i < 3; i++) { - for(j = 0; j < 3; j++) { - if(g_keypad_key_is_down[key]) { - num_keys++; - x = x + (j - 1)*32768; - y = y + (1 - i)*32768; - } - key++; - } - } - if(num_keys == 0) { - num_keys = 1; - } - - adb_printf("get_xy=%d, num_keys: %d, x:%d, y:%d\n", get_y, - num_keys, x, y); - - if(get_y) { - return y / num_keys; - } else { - return x / num_keys; - } -} - -int -update_mouse(int x, int y, int button_states, int buttons_valid) -{ - double dcycs; - int button1_changed; - int mouse_moved; - int unhide; - int pos; - int i; - - dcycs = g_cur_dcycs; - - g_mouse_raw_x = x; - g_mouse_raw_y = y; - - unhide = 0; - if(x < 0) { - x = 0; - unhide = 1; - } - if(x >= 640) { - x = 639; - unhide = 1; - } - if(y < 0) { - y = 0; - unhide = 1; - } - if(y >= 400) { - y = 399; - unhide = 1; - } - - - g_unhide_pointer = unhide && !g_warp_pointer; - - if(!g_warp_pointer) { - if(g_hide_pointer && g_unhide_pointer) { - /* cursor has left a2 window, show it */ - g_hide_pointer = 0; - x_hide_pointer(0); - } - if((g_num_lines_prev_superhires == 200) && - (g_num_lines_prev_superhires640 == 0) && - ((g_slow_memory_ptr[0x19d00] & 0x80) == 0)) { - // In 320-mode superhires, cut mouse range in half - x = x >> 1; - } - y = y >> 1; - } - - mouse_compress_fifo(dcycs); - -#if 0 - printf("Update Mouse called with buttons:%d x,y:%d,%d, fifo:%d,%d, " - " a2: %d,%d\n", buttons_valid, x, y, - g_mouse_fifo[0].x, g_mouse_fifo[0].y, - g_mouse_a2_x, g_mouse_a2_y); -#endif - - if((buttons_valid < 0) && g_warp_pointer) { - /* Warping the pointer causes it to jump here...this is not */ - /* real motion, just update info and get out */ - g_mouse_a2_x += (x - g_mouse_fifo[0].x); - g_mouse_a2_y += (y - g_mouse_fifo[0].y); - g_mouse_fifo[0].x = x; - g_mouse_fifo[0].y = y; - return 0; - } - -#if 0 - printf("...real move, warp: %d, %d, new x: %d, %d, a2:%d,%d\n", - g_mouse_warp_x, g_mouse_warp_y, g_mouse_fifo[0].x, - g_mouse_fifo[0].y, g_mouse_a2_x, g_mouse_a2_y); -#endif - - mouse_moved = (g_mouse_fifo[0].x != x) || (g_mouse_fifo[0].y != y); - - g_mouse_a2_x += g_mouse_warp_x; - g_mouse_a2_y += g_mouse_warp_y; - g_mouse_fifo[0].x = x; - g_mouse_fifo[0].y = y; - g_mouse_fifo[0].dcycs = dcycs; - g_mouse_warp_x = 0; - g_mouse_warp_y = 0; - - button1_changed = (buttons_valid & 1) && - ((button_states & 1) != (g_mouse_fifo[0].buttons & 1)); - - if((button_states & 4) && !(g_mouse_fifo[0].buttons & 4) && - (buttons_valid & 4)) { - /* right button pressed */ - adb_increment_speed(); - } - if((button_states & 2) && !(g_mouse_fifo[0].buttons & 2) && - (buttons_valid & 2)) { - /* middle button pressed */ - adb_increment_speed(); - //halt2_printf("Middle button pressed\n"); - } - - pos = g_mouse_fifo_pos; - if((pos < (ADB_MOUSE_FIFO - 2)) && button1_changed) { - /* copy delta to overflow, set overflow */ - /* overflow ensures the mouse button state is precise at */ - /* button up/down times. Using a mouse event list where */ - /* deltas accumulate until a button change would work, too */ - for(i = pos; i >= 0; i--) { - g_mouse_fifo[i + 1] = g_mouse_fifo[i]; /* copy struct*/ - } - g_mouse_fifo_pos = pos + 1; - } - - g_mouse_fifo[0].buttons = (button_states & buttons_valid) | - (g_mouse_fifo[0].buttons & ~buttons_valid); - - if(mouse_moved || button1_changed) { - if( (g_mouse_ctl_addr == g_mouse_dev_addr) && - ((g_adb_mode & 0x2) == 0)) { - g_adb_mouse_valid_data = 1; - adb_add_mouse_int(); - } - } - - return mouse_moved; -} - -int -mouse_read_c024(double dcycs) -{ - word32 ret; - word32 tool_start; - int em_active; - int target_x, target_y; - int delta_x, delta_y; - int a2_x, a2_y; - int mouse_button; - int clamped; - int pos; - - if(((g_adb_mode & 0x2) != 0) || (g_mouse_dev_addr != g_mouse_ctl_addr)){ - /* mouse is off, return 0, or mouse is not autopoll */ - g_adb_mouse_valid_data = 0; - adb_clear_mouse_int(); - return 0; - } - - mouse_compress_fifo(dcycs); - - pos = g_mouse_fifo_pos; - target_x = g_mouse_fifo[pos].x; - target_y = g_mouse_fifo[pos].y; - mouse_button = (g_mouse_fifo[pos].buttons & 1); - delta_x = target_x - g_mouse_a2_x; - delta_y = target_y - g_mouse_a2_y; - - clamped = 0; - if(delta_x > 0x3f) { - delta_x = 0x3f; - clamped = 1; - } else if(delta_x < -0x3f) { - delta_x = -0x3f; - clamped = 1; - } - if(delta_y > 0x3f) { - delta_y = 0x3f; - clamped = 1; - } else if(delta_y < -0x3f) { - delta_y = -0x3f; - clamped = 1; - } - - if(pos > 0) { - /* peek into next entry's button info if we are not clamped */ - /* and we're returning the y-coord */ - if(!clamped && g_adb_mouse_coord) { - mouse_button = g_mouse_fifo[pos - 1].buttons & 1; - } - } - - if(g_adb_mouse_coord) { - /* y coord */ - delta_x = 0; /* clear unneeded x delta */ - } else { - delta_y = 0; /* clear unneeded y delta */ - } - - - adb_printf(" pre a2_x:%02x,%02x,%02x,%02x\n", - g_slow_memory_ptr[0x100e9], g_slow_memory_ptr[0x100ea], - g_slow_memory_ptr[0x100eb], g_slow_memory_ptr[0x100ec]); - adb_printf(" pre a2_x:%02x,%02x,%02x,%02x\n", - g_slow_memory_ptr[0x10190], g_slow_memory_ptr[0x10192], - g_slow_memory_ptr[0x10191], g_slow_memory_ptr[0x10193]); - - /* Update event manager internal state */ - tool_start = (g_slow_memory_ptr[0x103ca] << 16) + - (g_slow_memory_ptr[0x103c9] << 8) + - g_slow_memory_ptr[0x103c8]; - - em_active = 0; - if((tool_start >= 0x20000) && (tool_start < (g_mem_size_total - 28)) ) { - /* seems to be valid ptr to addr of mem space for tools */ - /* see if event manager appears to be active */ - em_active = g_memory_ptr[tool_start + 6*4] + - (g_memory_ptr[tool_start + 6*4 + 1] << 8); - if(g_warp_pointer) { - em_active = 0; - } - } - - a2_x = g_mouse_a2_x; - a2_y = g_mouse_a2_y; - - if(em_active) { - if((!g_hide_pointer) && (g_num_lines_prev_superhires == 200) && - !g_unhide_pointer) { - /* if super-hires and forcing tracking, then hide */ - g_hide_pointer = 1; - x_hide_pointer(1); - } - if(g_adb_mouse_coord == 0) { - /* update x coord values */ - g_slow_memory_ptr[0x47c] = a2_x & 0xff; - g_slow_memory_ptr[0x57c] = a2_x >> 8; - g_memory_ptr[0x47c] = a2_x & 0xff; - g_memory_ptr[0x57c] = a2_x >> 8; - - g_slow_memory_ptr[0x10190] = a2_x & 0xff; - g_slow_memory_ptr[0x10192] = a2_x >> 8; - } else { - g_slow_memory_ptr[0x4fc] = a2_y & 0xff; - g_slow_memory_ptr[0x5fc] = a2_y >> 8; - g_memory_ptr[0x4fc] = a2_y & 0xff; - g_memory_ptr[0x5fc] = a2_y >> 8; - - g_slow_memory_ptr[0x10191] = a2_y & 0xff; - g_slow_memory_ptr[0x10193] = a2_y >> 8; - } - } else { - if(g_hide_pointer && !g_warp_pointer) { - g_hide_pointer = 0; - x_hide_pointer(0); - } - } - - ret = ((!mouse_button) << 7) + ((delta_x | delta_y) & 0x7f); - if(g_adb_mouse_coord) { - g_mouse_a2_button = mouse_button; /* y coord has button*/ - } else { - ret |= 0x80; /* mouse button not down on x coord rd */ - } - - a2_x += delta_x; - a2_y += delta_y; - g_mouse_a2_x = a2_x; - g_mouse_a2_y = a2_y; - if(g_mouse_fifo_pos) { - if((target_x == a2_x) && (target_y == a2_y) && - (g_mouse_a2_button == mouse_button)) { - g_mouse_fifo_pos--; - } - } - - - adb_printf("Read c024, mouse is_y:%d, %02x, vbl:%08x, dcyc:%f, em:%d\n", - g_adb_mouse_coord, ret, g_vbl_count, dcycs, em_active); - adb_printf("...mouse targ_x:%d,%d delta_x,y:%d,%d fifo:%d, a2:%d,%d\n", - target_x, target_y, delta_x, delta_y, g_mouse_fifo_pos, - a2_x, a2_y); - adb_printf(" post a2_x:%02x,%02x,%02x,%02x\n", - g_slow_memory_ptr[0x10190], g_slow_memory_ptr[0x10192], - g_slow_memory_ptr[0x10191], g_slow_memory_ptr[0x10193]); - - if((g_mouse_fifo_pos == 0) && (g_mouse_fifo[0].x == a2_x) && - (g_mouse_fifo[0].y == a2_y) && - ((g_mouse_fifo[0].buttons & 1) == g_mouse_a2_button)) { - g_adb_mouse_valid_data = 0; - adb_clear_mouse_int(); - } - - g_adb_mouse_coord = !g_adb_mouse_coord; - return ret; -} - -void -mouse_compress_fifo(double dcycs) -{ - int pos; - - /* The mouse fifo exists so that fast button changes don't get lost */ - /* if the emulator lags behind the mouse events */ - /* But the FIFO means really old mouse events are saved if */ - /* the emulated code isn't looking at the mouse registers */ - /* This routine compresses all mouse events > 0.5 seconds old */ - - for(pos = g_mouse_fifo_pos; pos >= 1; pos--) { - if(g_mouse_fifo[pos].dcycs < (dcycs - 500*1000.0)) { - /* Remove this entry */ - adb_printf("Old mouse FIFO pos %d removed\n", pos); - g_mouse_fifo_pos = pos - 1; - continue; - } - /* Else, stop searching the FIFO */ - break; - } -} - -void -adb_key_event(int a2code, int is_up) -{ - word32 special; - word32 vbl_count; - int key; - int hard_key; - int pos; - int tmp_ascii; - int ascii; - - if(is_up) { - adb_printf("adb_key_event, key:%02x, is up, g_key_down: %02x\n", - a2code, g_key_down); - } - - if(a2code < 0 || a2code > 0x7f) { - halt_printf("add_key_event: a2code: %04x!\n", a2code); - return; - } - - if(!is_up && a2code == 0x35) { - /* ESC pressed, see if ctrl & cmd key down */ - if(CTRL_DOWN && CMD_DOWN) { - /* Desk mgr int */ - glog("Desk mgr int!"); - - g_adb_interrupt_byte |= 0x20; - adb_add_data_int(); - } - } - - /* convert key to ascii, if possible */ - hard_key = 0; - if(a2_key_to_ascii[a2code][1] & 0xef00) { - /* special key */ - } else { - /* we have ascii */ - hard_key = 1; - } - - pos = 1; - ascii = a2_key_to_ascii[a2code][1]; - if(CAPS_LOCK_DOWN && (ascii >= 'a' && ascii <= 'z')) { - pos = 2; - if(SHIFT_DOWN && (g_adb_mode & 0x40)) { - /* xor shift mode--capslock and shift == lowercase */ - pos = 1; - } - } else if(SHIFT_DOWN) { - pos = 2; - } - - ascii = a2_key_to_ascii[a2code][pos]; - if(CTRL_DOWN) { - tmp_ascii = a2_key_to_ascii[a2code][3]; - if(tmp_ascii >= 0) { - ascii = tmp_ascii; - } - } - key = (ascii & 0x7f) + 0x80; - - special = (ascii >> 8) & 0xff; - if(ascii < 0) { - glogf("ascii1: %d, a2code: %02x, pos: %d", ascii,a2code,pos); - ascii = 0; - special = 0; - } - - - if(!is_up) { - if(hard_key) { - g_kbd_buf[g_kbd_chars_buffered] = key; - g_kbd_chars_buffered++; - if(g_kbd_chars_buffered >= MAX_KBD_BUF) { - g_kbd_chars_buffered = MAX_KBD_BUF - 1; - } - g_key_down = 1; - g_a2code_down = a2code; - - /* first key down, set up autorepeat */ - vbl_count = g_vbl_count; - if(g_config_control_panel) { - vbl_count = g_cfg_vbl_count; - } - g_adb_repeat_vbl = vbl_count + g_adb_repeat_delay; - if(g_adb_repeat_delay == 0) { - g_key_down = 0; - } - g_hard_key_down = 1; - } - - g_c025_val = g_c025_val | special; - adb_printf("new c025_or: %02x\n", g_c025_val); - } else { - if(hard_key && (a2code == g_a2code_down)) { - g_hard_key_down = 0; - /* Turn off repeat */ - g_key_down = 0; - } - - g_c025_val = g_c025_val & (~ special); - adb_printf("new c025_and: %02x\n", g_c025_val); - } - - if(g_key_down) { - g_c025_val = g_c025_val & (~0x20); - } else { - /* If no hard key down, set update mod latch */ - g_c025_val = g_c025_val | 0x20; - } - -} - -word32 -adb_read_c000() -{ - word32 vbl_count; - - if( ((g_kbd_buf[0] & 0x80) == 0) && (g_key_down == 0)) { - /* nothing happening, check clipboard */ - int c = clipboard_get_char(); - if(c) { - /* inject clipboard char into keyboard buffer */ - g_kbd_buf[0] = c; - } - /* just get out */ - return g_kbd_buf[0]; - } - if(g_kbd_buf[0] & 0x80) { - /* got one */ - if((g_kbd_read_no_update++ > 5) && (g_kbd_chars_buffered > 1)) { - /* read 5 times, keys pending, let's move it along */ - glogf("Read %02x %d times, tossing", g_kbd_buf[0], g_kbd_read_no_update); - adb_access_c010(); - } - } else { - vbl_count = g_vbl_count; - if(g_config_control_panel) { - vbl_count = g_cfg_vbl_count; - } - if(g_key_down && vbl_count >= g_adb_repeat_vbl) { - /* repeat the g_key_down */ - g_c025_val |= 0x8; - adb_key_event(g_a2code_down, 0); - g_adb_repeat_vbl = vbl_count + g_adb_repeat_rate; - } - } - - return g_kbd_buf[0]; -} - -word32 -adb_access_c010() -{ - int tmp; - int i; - - g_kbd_read_no_update = 0; - - tmp = g_kbd_buf[0] & 0x7f; - g_kbd_buf[0] = tmp; - - tmp = tmp | (g_hard_key_down << 7); - if(g_kbd_chars_buffered) { - for(i = 1; i < g_kbd_chars_buffered; i++) { - g_kbd_buf[i - 1] = g_kbd_buf[i]; - } - g_kbd_chars_buffered--; - } - - g_c025_val = g_c025_val & (~ (0x08)); - - return tmp; -} - -word32 -adb_read_c025() -{ - return g_c025_val; -} - -int -adb_is_cmd_key_down() -{ - return CMD_DOWN; -} - -int -adb_is_option_key_down() -{ - return OPTION_DOWN; -} - -void -adb_increment_speed() -{ - const char *str; - - g_limit_speed++; - if(g_limit_speed > 3) { - g_limit_speed = 0; - } - - str = ""; - switch(g_limit_speed) { - case 0: - str = "... as fast as possible!"; - break; - case 1: - str = "... 1.024MHz"; - break; - case 2: - str = "... 2.8MHz"; - break; - case 3: - str = "... 8.0MHz"; - break; - } - glogf("Setting g_limit_speed = %d %s", g_limit_speed, str); -} - -void -adb_physical_key_update(int a2code, int is_up) -{ - int autopoll; - int special; - int ascii_and_type; - int ascii; - - /* this routine called by xdriver to pass raw codes--handle */ - /* ucontroller and ADB bus protocol issues here */ - /* if autopoll on, pass it on through to c025,c000 regs */ - /* else only put it in kbd reg 3, and pull SRQ if needed */ - - adb_printf("adb_phys_key_update: %02x, %d\n", a2code, is_up); - - adb_printf("Handle a2code: %02x, is_up: %d\n", a2code, is_up); - - if(a2code < 0 || a2code > 0x7f) { - halt_printf("a2code: %04x!\n", a2code); - return; - } - - /* Remap 0x7b-0x7e to 0x3b-0x3e (arrow keys on new mac keyboards) */ - if(a2code >= 0x7b && a2code <= 0x7e) { - a2code = a2code - 0x40; - } - - /* Now check for special keys (function keys, etc) */ - ascii_and_type = a2_key_to_ascii[a2code][1]; - special = 0; - if((ascii_and_type & 0xf000) == 0x8000) { - /* special function key */ - special = ascii_and_type & 0xff; - switch(special) { - case 0x01: /* F1 - remap to cmd */ - a2code = 0x37; - special = 0; - break; - case 0x02: /* F2 - remap to option */ - a2code = 0x3a; - special = 0; - break; - case 0x0c: /* F12 - remap to reset */ - a2code = 0x7f; - special = 0; +void adb_set_config(word32 val0, word32 val1, word32 val2) { + int new_mouse; + int new_kbd; + int tmp1; + + new_mouse = val0 >> 4; + new_kbd = val0 & 0xf; + if(new_mouse != g_mouse_ctl_addr) { + glogf("ADB config: mouse from %x to %x!", g_mouse_ctl_addr, new_mouse); + adb_error(); + g_mouse_ctl_addr = new_mouse; + } + if(new_kbd != g_kbd_ctl_addr) { + glogf("ADB config: kbd from %x to %x!", g_kbd_ctl_addr, new_kbd); + adb_error(); + g_kbd_ctl_addr = new_kbd; + } + + + tmp1 = val2 >> 4; + if(tmp1 == 4) { + g_adb_repeat_delay = 0; + } else if(tmp1 < 4) { + g_adb_repeat_delay = (tmp1 + 1) * 15; + } else { + halt_printf("Bad ADB repeat delay: %02x\n", tmp1); + } + + tmp1 = val2 & 0xf; + if(g_rom_version >= 3) { + tmp1 = 9 - tmp1; + } + + switch(tmp1) { + case 0: + g_adb_repeat_rate = 1; break; - default: - break; - } - } + case 1: + g_adb_repeat_rate = 2; + break; + case 2: + g_adb_repeat_rate = 3; + break; + case 3: + g_adb_repeat_rate = 3; + break; + case 4: + g_adb_repeat_rate = 4; + break; + case 5: + g_adb_repeat_rate = 5; + break; + case 6: + g_adb_repeat_rate = 7; + break; + case 7: + g_adb_repeat_rate = 15; + break; + case 8: + /* I don't know what this should be, ROM 03 uses it */ + g_adb_repeat_rate = 30; + break; + case 9: + /* I don't know what this should be, ROM 03 uses it */ + g_adb_repeat_rate = 60; + break; + default: + halt_printf("Bad repeat rate: %02x\n", tmp1); + } - /* CUA clipboard paste - for those that remember ctrl-insert/shift-insert */ - if(is_up == 0 && a2code == 0x72 && SHIFT_DOWN) { - clipboard_paste(); - } +} - /* Only process reset requests here */ - if(is_up == 0 && a2code == 0x7f && CTRL_DOWN) { - /* Reset pressed! */ - glogf("Reset pressed since CTRL_DOWN: %d", CTRL_DOWN); - do_reset(); - return; - } +void adb_set_new_mode(word32 val) { + if(val & 0x03) { + glogf("Disabling keyboard/mouse:%02x!", val); + } - if(special && !is_up) { - switch(special) { - case 0x03: /* F3 - screenshot */ - g_screenshot_requested = 1; - break; - case 0x04: /* F4 - emulator config panel */ - if (CMD_DOWN) { + if(val & 0xa2) { + halt_printf("ADB set mode: %02x!\n", val); + adb_error(); + } + + g_adb_mode = val; +} + + +int adb_read_c026() { + word32 ret; + int i; + + ret = 0; + switch(g_adb_state) { + case ADB_IDLE: + ret = g_adb_interrupt_byte; + g_adb_interrupt_byte = 0; + if(g_irq_pending & IRQ_PENDING_ADB_KBD_SRQ) { + g_adb_interrupt_byte |= 0x08; + } + if(g_adb_data_pending == 0) { + if(ret & 0x80) { + halt_printf("read_c026: ret:%02x, pend:%d\n", + ret, g_adb_data_pending); + } + adb_clear_data_int(); + } + if(g_adb_data_pending) { + if(g_adb_state != ADB_IN_CMD) { + g_adb_state = ADB_SENDING_DATA; + } + } + break; + case ADB_IN_CMD: + ret = 0; + break; + case ADB_SENDING_DATA: + ret = g_adb_data[0]; + for(i = 1; i < g_adb_data_pending; i++) { + g_adb_data[i-1] = g_adb_data[i]; + } + g_adb_data_pending--; + if(g_adb_data_pending <= 0) { + g_adb_data_pending = 0; + g_adb_state = ADB_IDLE; + adb_clear_data_int(); + } + break; + default: + halt_printf("Bad ADB state: %d!\n", g_adb_state); + adb_clear_data_int(); + break; + } + + adb_printf("Reading c026. Returning %02x, st: %02x, pend: %d\n", + ret, g_adb_state, g_adb_data_pending); + + adb_log(0xc026, ret); + return (ret & 0xff); +} + + +void adb_write_c026(int val) { + word32 tmp; + int dev; + + adb_printf("Writing c026 with %02x\n", val); + adb_log(0x1c026, val); + + + switch(g_adb_state) { + case ADB_IDLE: + g_adb_cmd = val; + g_adb_cmd_so_far = 0; + g_adb_cmd_len = 0; + + dev = val & 0xf; + switch(val) { + case 0x01: /* Abort */ + adb_printf("Performing adb abort\n"); + /* adb_abort() */ + break; + case 0x03: /* Flush keyboard buffer */ + adb_printf("Flushing adb keyboard buffer\n"); + /* Do nothing */ + break; + case 0x04: /* Set modes */ + adb_printf("ADB set modes\n"); + g_adb_state = ADB_IN_CMD; + g_adb_cmd_len = 1; + break; + case 0x05: /* Clear modes */ + adb_printf("ADB clear modes\n"); + g_adb_state = ADB_IN_CMD; + g_adb_cmd_len = 1; + break; + case 0x06: /* Set config */ + adb_printf("ADB set config\n"); + g_adb_state = ADB_IN_CMD; + g_adb_cmd_len = 3; + break; + case 0x07: /* Sync */ + adb_printf("Performing sync cmd!\n"); + g_adb_state = ADB_IN_CMD; + if(g_rom_version == 1) { + g_adb_cmd_len = 4; + } else { + g_adb_cmd_len = 8; + } + break; + case 0x08: /* Write mem */ + adb_printf("Starting write_mem cmd\n"); + g_adb_state = ADB_IN_CMD; + g_adb_cmd_len = 2; + break; + case 0x09: /* Read mem */ + adb_printf("Performing read_mem cmd!\n"); + g_adb_state = ADB_IN_CMD; + g_adb_cmd_len = 2; + break; + case 0x0a: /* Read modes byte */ + printf("Performing read_modes cmd!\n"); + /* set_halt(1); */ + adb_send_1byte(g_adb_mode); + break; + case 0x0b: /* Read config bytes */ + printf("Performing read_configs cmd!\n"); + tmp = (g_mouse_ctl_addr << 20) + + (g_kbd_ctl_addr << 16) + + (g_adb_char_set << 12) + + (g_adb_layout_lang << 8) + + (g_adb_repeat_info << 0); + tmp = (0x82U << 24) + tmp; + adb_send_bytes(4, tmp, 0, 0); + break; + case 0x0d: /* Get Version */ + adb_printf("Performing get_version cmd!\n"); + val = 0; + if(g_rom_version == 1) { + /* ROM 01 = revision 5 */ + val = 5; + } else { + /* ROM 03 checks for rev >= 6 */ + val = 6; + } + adb_send_1byte(val); + break; + case 0x0e: /* Read avail char sets */ + adb_printf("Performing read avail char sets cmd!\n"); + adb_send_bytes(2, /* just 2 bytes */ + 0x08000000, /* number of ch sets=0x8 */ + 0, 0); + /* set_halt(1); */ + break; + case 0x0f: /* Read avail kbd layouts */ + adb_printf("Performing read avail kbd layouts cmd!\n"); + adb_send_bytes(0x2, /* number of kbd layouts=0xa */ + 0x0a000000, 0, 0); + /* set_halt(1); */ + break; + case 0x10: /* Reset */ + printf("ADB reset, cmd 0x10\n"); + do_reset(); + break; + case 0x11: /* Send ADB keycodes */ + adb_printf("Sending ADB keycodes\n"); + g_adb_state = ADB_IN_CMD; + g_adb_cmd_len = 1; + break; + case 0x12: /* ADB cmd 12: ROM 03 only! */ + if(g_rom_version >= 3) { + g_adb_state = ADB_IN_CMD; + g_adb_cmd_len = 2; + } else { + printf("ADB cmd 12, but not ROM 3!\n"); + adb_error(); + } + break; + case 0x13: /* ADB cmd 13: ROM 03 only! */ + if(g_rom_version >= 3) { + g_adb_state = ADB_IN_CMD; + g_adb_cmd_len = 2; + } else { + printf("ADB cmd 13, but not ROM 3!\n"); + adb_error(); + } + break; + case 0x73: /* Disable SRQ device 3: mouse */ + adb_printf("Disabling Mouse SRQ's (device 3)\n"); + /* HACK HACK...should deal with SRQs on mouse */ + break; + case 0xb0: case 0xb1: case 0xb2: case 0xb3: + case 0xb4: case 0xb5: case 0xb6: case 0xb7: + case 0xb8: case 0xb9: case 0xba: case 0xbb: + case 0xbc: case 0xbd: case 0xbe: case 0xbf: + /* Listen dev x reg 3 */ + adb_printf("Sending data to dev %x reg 3\n", dev); + g_adb_state = ADB_IN_CMD; + g_adb_cmd_len = 2; + break; + case 0xc0: case 0xc1: case 0xc2: case 0xc3: + case 0xc4: case 0xc5: case 0xc6: case 0xc7: + case 0xc8: case 0xc9: case 0xca: case 0xcb: + case 0xcc: case 0xcd: case 0xce: case 0xcf: + /* Talk dev x reg 0 */ + adb_printf("Performing talk dev %x reg 0\n", dev); + if(dev == g_kbd_dev_addr) { + adb_kbd_talk_reg0(); + } else { + printf("Unknown talk dev %x reg 0!\n", dev); + /* send no data, on SRQ, system polls devs */ + /* so we don't want to send anything */ + adb_error(); + } + break; + case 0xf0: case 0xf1: case 0xf2: case 0xf3: + case 0xf4: case 0xf5: case 0xf6: case 0xf7: + case 0xf8: case 0xf9: case 0xfa: case 0xfb: + case 0xfc: case 0xfd: case 0xfe: case 0xff: + /* Talk dev x reg 3 */ + adb_printf("Performing talk dev %x reg 3\n", dev); + if(dev == g_kbd_dev_addr) { + adb_response_packet(2, g_kbd_reg3_16bit); + } else { + printf("Performing talk dev %x reg 3!!\n", dev); + adb_error(); + } + break; + default: + /* The Gog's says ACS Demo 2 has a bug and writes to */ + /* c026 */ + // OG + if (val==0x84) + printf("ACS Demo2 (3: Colum& Music scroll) : discarding unknown controller command\n"); + else + halt_printf("ADB ucontroller cmd %02x unknown!\n", val); + + break; + } + break; + case ADB_IN_CMD: + adb_printf("Setting byte %d of cmd %02x to %02x\n", + g_adb_cmd_so_far, g_adb_cmd, val); + + g_adb_cmd_data[g_adb_cmd_so_far] = val; + g_adb_cmd_so_far++; + if(g_adb_cmd_so_far >= g_adb_cmd_len) { + adb_printf("Finished cmd %02x\n", g_adb_cmd); + do_adb_cmd(); + } + + break; + default: + printf("adb_state: %02x is unknown! Setting it to ADB_IDLE\n", + g_adb_state); + g_adb_state = ADB_IDLE; + adb_error(); + halt_on_all_c027 = 1; + break; + } + return; +} + +void do_adb_cmd() { + int dev; + int new_kbd; + int addr; + int val; + + dev = g_adb_cmd & 0xf; + + g_adb_state = ADB_IDLE; + + switch(g_adb_cmd) { + case 0x04: /* Set modes */ + adb_printf("Performing ADB set mode: OR'ing in %02x\n", + g_adb_cmd_data[0]); + + val = g_adb_cmd_data[0] | g_adb_mode; + adb_set_new_mode(val); + + break; + case 0x05: /* clear modes */ + adb_printf("Performing ADB clear mode: AND'ing in ~%02x\n", + g_adb_cmd_data[0]); + + val = g_adb_cmd_data[0]; + val = g_adb_mode & (~val); + adb_set_new_mode(val); + break; + case 0x06: /* Set config */ + adb_printf("Set ADB config to %02x %02x %02x\n", + g_adb_cmd_data[0], g_adb_cmd_data[1],g_adb_cmd_data[2]); + + adb_set_config(g_adb_cmd_data[0], g_adb_cmd_data[1], + g_adb_cmd_data[2]); + + break; + case 0x07: /* SYNC */ + adb_printf("Performing ADB SYNC\n"); + adb_printf("data: %02x %02x %02x %02x\n", + g_adb_cmd_data[0], g_adb_cmd_data[1], g_adb_cmd_data[2], + g_adb_cmd_data[3]); + + adb_set_new_mode(g_adb_cmd_data[0]); + adb_set_config(g_adb_cmd_data[1], g_adb_cmd_data[2], + g_adb_cmd_data[3]); + + if(g_rom_version >= 3) { + adb_printf(" and cmd12:%02x %02x cmd13:%02x %02x\n", + g_adb_cmd_data[4], g_adb_cmd_data[5], + g_adb_cmd_data[6], g_adb_cmd_data[7]); + } + break; + case 0x08: /* Write mem */ + addr = g_adb_cmd_data[0]; + val = g_adb_cmd_data[1]; + write_adb_ram(addr, val); + break; + case 0x09: /* Read mem */ + addr = (g_adb_cmd_data[1] << 8) + g_adb_cmd_data[0]; + adb_printf("Performing mem read to addr %04x\n", addr); + adb_send_1byte(read_adb_ram(addr)); + break; + case 0x11: /* Send ADB keycodes */ + val = g_adb_cmd_data[0]; + adb_printf("Performing send ADB keycodes: %02x\n", val); + adb_virtual_key_update(val & 0x7f, val >> 7); + break; + case 0x12: /* ADB cmd12 */ + adb_printf("Performing ADB cmd 12\n"); + adb_printf("data: %02x %02x\n", g_adb_cmd_data[0], + g_adb_cmd_data[1]); + break; + case 0x13: /* ADB cmd13 */ + adb_printf("Performing ADB cmd 13\n"); + adb_printf("data: %02x %02x\n", g_adb_cmd_data[0], + g_adb_cmd_data[1]); + break; + case 0xb0: case 0xb1: case 0xb2: case 0xb3: + case 0xb4: case 0xb5: case 0xb6: case 0xb7: + case 0xb8: case 0xb9: case 0xba: case 0xbb: + case 0xbc: case 0xbd: case 0xbe: case 0xbf: + /* Listen dev x reg 3 */ + if(dev == g_kbd_dev_addr) { + if(g_adb_cmd_data[1] == 0xfe) { + /* change keyboard addr? */ + new_kbd = g_adb_cmd_data[0] & 0xf; + if(new_kbd != dev) { + printf("Moving kbd to dev %x!\n", + new_kbd); + adb_error(); + } + g_kbd_dev_addr = new_kbd; + } else if(g_adb_cmd_data[1] != 1) { + /* see what new device handler id is */ + printf("KBD listen to dev %x reg 3: 1:%02x\n", + dev, g_adb_cmd_data[1]); + adb_error(); + } + if(g_adb_cmd_data[0] != (word32)g_kbd_dev_addr) { + /* see if app is trying to change addr */ + printf("KBD listen to dev %x reg 3: 0:%02x!\n", + dev, g_adb_cmd_data[0]); + adb_error(); + } + g_kbd_reg3_16bit = ((g_adb_cmd_data[0] & 0xf) << 12) + + (g_kbd_reg3_16bit & 0x0fff); + } else if(dev == g_mouse_dev_addr) { + if(g_adb_cmd_data[0] != (word32)dev) { + /* see if app is trying to change mouse addr */ + printf("MOUS listen to dev %x reg3: 0:%02x!\n", + dev, g_adb_cmd_data[0]); + adb_error(); + } + if(g_adb_cmd_data[1] != 1 && g_adb_cmd_data[1] != 2) { + /* see what new device handler id is */ + printf("MOUS listen to dev %x reg 3: 1:%02x\n", + dev, g_adb_cmd_data[1]); + adb_error(); + } + } else { + printf("Listen cmd to dev %x reg3????\n", dev); + printf("data0: %02x, data1: %02x ????\n", + g_adb_cmd_data[0], g_adb_cmd_data[1]); + adb_error(); + } + break; + default: + printf("Doing adb_cmd %02x: UNKNOWN!\n", g_adb_cmd); + break; + } +} + + +int adb_read_c027() { + word32 ret; + + if(halt_on_all_c027) { + halt_printf("halting on all c027 reads!\n"); + } + + if(g_c027_val & (~ADB_C027_NEG_MASK)) { + halt_printf("read_c027: g_c027_val: %02x\n", g_c027_val); + } + + ret = (g_c027_val & ADB_C027_NEG_MASK); + + if(g_adb_mouse_valid_data) { + ret |= ADB_C027_MOUSE_DATA; + } + + if(g_adb_interrupt_byte != 0) { + ret |= ADB_C027_DATA_VALID; + } else if(g_adb_data_pending > 0) { + if((g_adb_state != ADB_IN_CMD)) { + ret |= ADB_C027_DATA_VALID; + } + } + + if(g_adb_mouse_coord) { + ret |= ADB_C027_MOUSE_COORD; + } + +#if 0 + adb_printf("Read c027: %02x, int_byte: %02x, d_pend: %d\n", + ret, g_adb_interrupt_byte, g_adb_data_pending); +#endif + +#if 0 + adb_log(0xc027, ret); +#endif + return ret; +} + +void adb_write_c027(int val) { + word32 old_val; + word32 new_int; + word32 old_int; + + adb_printf("Writing c027 with %02x\n", val); + adb_log(0x1c027, val); + + + old_val = g_c027_val; + + g_c027_val = (val & ADB_C027_NEG_MASK); + new_int = g_c027_val & ADB_C027_MOUSE_INT; + old_int = old_val & ADB_C027_MOUSE_INT; + if(!new_int && old_int) { + adb_clear_mouse_int(); + } + + new_int = g_c027_val & ADB_C027_DATA_INT; + old_int = old_val & ADB_C027_DATA_INT; + if(!new_int && old_int) { + /* ints were on, now off */ + adb_clear_data_int(); + } + + if(g_c027_val & ADB_C027_KBD_INT) { + halt_printf("Can't support kbd interrupts!\n"); + } + + return; +} + +int read_adb_ram(word32 addr) { + int val; + + adb_printf("Reading adb ram addr: %02x\n", addr); + + if(addr >= 0x100) { + if(addr >= 0x1000 && addr < 0x2000) { + /* ROM self-test checksum */ + if(addr == 0x1400) { + val = 0x72; + } else if(addr == 0x1401) { + val = 0xf7; + } else { + val = 0; + } + } else { + printf("adb ram addr out of range: %04x!\n", addr); + val = 0; + } + } else { + val = adb_memory[addr]; + if((addr == 0xb) && (g_rom_version == 1)) { + // read special key state byte for Out of This World + val = (g_c025_val >> 1) & 0x43; + val |= (g_c025_val << 2) & 0x4; + val |= (g_c025_val >> 2) & 0x10; + } + if((addr == 0xc) && (g_rom_version >= 3)) { + // read special key state byte for Out of This World + val = g_c025_val & 0xc7; + printf("val is %02x\n", val); + } + } + + adb_printf("adb_ram returning %02x\n", val); + return val; +} + +void write_adb_ram(word32 addr, int val) { + + adb_printf("Writing adb_ram addr: %02x: %02x\n", addr, val); + + if(addr >= 0x100) { + printf("write adb_ram addr: %02x: %02x!\n", addr, val); + adb_error(); + } else { + adb_memory[addr] = val; + } +} + +int adb_get_keypad_xy(int get_y) { + int x, y; + int key; + int num_keys; + int i, j; + + key = 1; + num_keys = 0; + x = 0; + y = 0; + for(i = 0; i < 3; i++) { + for(j = 0; j < 3; j++) { + if(g_keypad_key_is_down[key]) { + num_keys++; + x = x + (j - 1)*32768; + y = y + (1 - i)*32768; + } + key++; + } + } + if(num_keys == 0) { + num_keys = 1; + } + + adb_printf("get_xy=%d, num_keys: %d, x:%d, y:%d\n", get_y, + num_keys, x, y); + + if(get_y) { + return y / num_keys; + } else { + return x / num_keys; + } +} + +int update_mouse(int x, int y, int button_states, int buttons_valid) { + double dcycs; + int button1_changed; + int mouse_moved; + int unhide; + int pos; + int i; + + dcycs = g_cur_dcycs; + + g_mouse_raw_x = x; + g_mouse_raw_y = y; + + unhide = 0; + if(x < 0) { + x = 0; + unhide = 1; + } + if(x >= 640) { + x = 639; + unhide = 1; + } + if(y < 0) { + y = 0; + unhide = 1; + } + if(y >= 400) { + y = 399; + unhide = 1; + } + + + g_unhide_pointer = unhide && !g_warp_pointer; + + if(!g_warp_pointer) { + if(g_hide_pointer && g_unhide_pointer) { + /* cursor has left a2 window, show it */ + g_hide_pointer = 0; + x_hide_pointer(0); + } + if((g_num_lines_prev_superhires == 200) && + (g_num_lines_prev_superhires640 == 0) && + ((g_slow_memory_ptr[0x19d00] & 0x80) == 0)) { + // In 320-mode superhires, cut mouse range in half + x = x >> 1; + } + y = y >> 1; + } + + mouse_compress_fifo(dcycs); + +#if 0 + printf("Update Mouse called with buttons:%d x,y:%d,%d, fifo:%d,%d, " + " a2: %d,%d\n", buttons_valid, x, y, + g_mouse_fifo[0].x, g_mouse_fifo[0].y, + g_mouse_a2_x, g_mouse_a2_y); +#endif + + if((buttons_valid < 0) && g_warp_pointer) { + /* Warping the pointer causes it to jump here...this is not */ + /* real motion, just update info and get out */ + g_mouse_a2_x += (x - g_mouse_fifo[0].x); + g_mouse_a2_y += (y - g_mouse_fifo[0].y); + g_mouse_fifo[0].x = x; + g_mouse_fifo[0].y = y; + return 0; + } + +#if 0 + printf("...real move, warp: %d, %d, new x: %d, %d, a2:%d,%d\n", + g_mouse_warp_x, g_mouse_warp_y, g_mouse_fifo[0].x, + g_mouse_fifo[0].y, g_mouse_a2_x, g_mouse_a2_y); +#endif + + mouse_moved = (g_mouse_fifo[0].x != x) || (g_mouse_fifo[0].y != y); + + g_mouse_a2_x += g_mouse_warp_x; + g_mouse_a2_y += g_mouse_warp_y; + g_mouse_fifo[0].x = x; + g_mouse_fifo[0].y = y; + g_mouse_fifo[0].dcycs = dcycs; + g_mouse_warp_x = 0; + g_mouse_warp_y = 0; + + button1_changed = (buttons_valid & 1) && + ((button_states & 1) != (g_mouse_fifo[0].buttons & 1)); + + if((button_states & 4) && !(g_mouse_fifo[0].buttons & 4) && + (buttons_valid & 4)) { + /* right button pressed */ + adb_increment_speed(); + } + if((button_states & 2) && !(g_mouse_fifo[0].buttons & 2) && + (buttons_valid & 2)) { + /* middle button pressed */ + adb_increment_speed(); + //halt2_printf("Middle button pressed\n"); + } + + pos = g_mouse_fifo_pos; + if((pos < (ADB_MOUSE_FIFO - 2)) && button1_changed) { + /* copy delta to overflow, set overflow */ + /* overflow ensures the mouse button state is precise at */ + /* button up/down times. Using a mouse event list where */ + /* deltas accumulate until a button change would work, too */ + for(i = pos; i >= 0; i--) { + g_mouse_fifo[i + 1] = g_mouse_fifo[i]; /* copy struct*/ + } + g_mouse_fifo_pos = pos + 1; + } + + g_mouse_fifo[0].buttons = (button_states & buttons_valid) | + (g_mouse_fifo[0].buttons & ~buttons_valid); + + if(mouse_moved || button1_changed) { + if( (g_mouse_ctl_addr == g_mouse_dev_addr) && + ((g_adb_mode & 0x2) == 0)) { + g_adb_mouse_valid_data = 1; + adb_add_mouse_int(); + } + } + + return mouse_moved; +} + +int mouse_read_c024(double dcycs) { + word32 ret; + word32 tool_start; + int em_active; + int target_x, target_y; + int delta_x, delta_y; + int a2_x, a2_y; + int mouse_button; + int clamped; + int pos; + + if(((g_adb_mode & 0x2) != 0) || (g_mouse_dev_addr != g_mouse_ctl_addr)) { + /* mouse is off, return 0, or mouse is not autopoll */ + g_adb_mouse_valid_data = 0; + adb_clear_mouse_int(); + return 0; + } + + mouse_compress_fifo(dcycs); + + pos = g_mouse_fifo_pos; + target_x = g_mouse_fifo[pos].x; + target_y = g_mouse_fifo[pos].y; + mouse_button = (g_mouse_fifo[pos].buttons & 1); + delta_x = target_x - g_mouse_a2_x; + delta_y = target_y - g_mouse_a2_y; + + clamped = 0; + if(delta_x > 0x3f) { + delta_x = 0x3f; + clamped = 1; + } else if(delta_x < -0x3f) { + delta_x = -0x3f; + clamped = 1; + } + if(delta_y > 0x3f) { + delta_y = 0x3f; + clamped = 1; + } else if(delta_y < -0x3f) { + delta_y = -0x3f; + clamped = 1; + } + + if(pos > 0) { + /* peek into next entry's button info if we are not clamped */ + /* and we're returning the y-coord */ + if(!clamped && g_adb_mouse_coord) { + mouse_button = g_mouse_fifo[pos - 1].buttons & 1; + } + } + + if(g_adb_mouse_coord) { + /* y coord */ + delta_x = 0; /* clear unneeded x delta */ + } else { + delta_y = 0; /* clear unneeded y delta */ + } + + + adb_printf(" pre a2_x:%02x,%02x,%02x,%02x\n", + g_slow_memory_ptr[0x100e9], g_slow_memory_ptr[0x100ea], + g_slow_memory_ptr[0x100eb], g_slow_memory_ptr[0x100ec]); + adb_printf(" pre a2_x:%02x,%02x,%02x,%02x\n", + g_slow_memory_ptr[0x10190], g_slow_memory_ptr[0x10192], + g_slow_memory_ptr[0x10191], g_slow_memory_ptr[0x10193]); + + /* Update event manager internal state */ + tool_start = (g_slow_memory_ptr[0x103ca] << 16) + + (g_slow_memory_ptr[0x103c9] << 8) + + g_slow_memory_ptr[0x103c8]; + + em_active = 0; + if((tool_start >= 0x20000) && (tool_start < (g_mem_size_total - 28)) ) { + /* seems to be valid ptr to addr of mem space for tools */ + /* see if event manager appears to be active */ + em_active = g_memory_ptr[tool_start + 6*4] + + (g_memory_ptr[tool_start + 6*4 + 1] << 8); + if(g_warp_pointer) { + em_active = 0; + } + } + + a2_x = g_mouse_a2_x; + a2_y = g_mouse_a2_y; + + if(em_active) { + if((!g_hide_pointer) && (g_num_lines_prev_superhires == 200) && + !g_unhide_pointer) { + /* if super-hires and forcing tracking, then hide */ + g_hide_pointer = 1; + x_hide_pointer(1); + } + if(g_adb_mouse_coord == 0) { + /* update x coord values */ + g_slow_memory_ptr[0x47c] = a2_x & 0xff; + g_slow_memory_ptr[0x57c] = a2_x >> 8; + g_memory_ptr[0x47c] = a2_x & 0xff; + g_memory_ptr[0x57c] = a2_x >> 8; + + g_slow_memory_ptr[0x10190] = a2_x & 0xff; + g_slow_memory_ptr[0x10192] = a2_x >> 8; + } else { + g_slow_memory_ptr[0x4fc] = a2_y & 0xff; + g_slow_memory_ptr[0x5fc] = a2_y >> 8; + g_memory_ptr[0x4fc] = a2_y & 0xff; + g_memory_ptr[0x5fc] = a2_y >> 8; + + g_slow_memory_ptr[0x10191] = a2_y & 0xff; + g_slow_memory_ptr[0x10193] = a2_y >> 8; + } + } else { + if(g_hide_pointer && !g_warp_pointer) { + g_hide_pointer = 0; + x_hide_pointer(0); + } + } + + ret = ((!mouse_button) << 7) + ((delta_x | delta_y) & 0x7f); + if(g_adb_mouse_coord) { + g_mouse_a2_button = mouse_button; /* y coord has button*/ + } else { + ret |= 0x80; /* mouse button not down on x coord rd */ + } + + a2_x += delta_x; + a2_y += delta_y; + g_mouse_a2_x = a2_x; + g_mouse_a2_y = a2_y; + if(g_mouse_fifo_pos) { + if((target_x == a2_x) && (target_y == a2_y) && + (g_mouse_a2_button == mouse_button)) { + g_mouse_fifo_pos--; + } + } + + + adb_printf("Read c024, mouse is_y:%d, %02x, vbl:%08x, dcyc:%f, em:%d\n", + g_adb_mouse_coord, ret, g_vbl_count, dcycs, em_active); + adb_printf("...mouse targ_x:%d,%d delta_x,y:%d,%d fifo:%d, a2:%d,%d\n", + target_x, target_y, delta_x, delta_y, g_mouse_fifo_pos, + a2_x, a2_y); + adb_printf(" post a2_x:%02x,%02x,%02x,%02x\n", + g_slow_memory_ptr[0x10190], g_slow_memory_ptr[0x10192], + g_slow_memory_ptr[0x10191], g_slow_memory_ptr[0x10193]); + + if((g_mouse_fifo_pos == 0) && (g_mouse_fifo[0].x == a2_x) && + (g_mouse_fifo[0].y == a2_y) && + ((g_mouse_fifo[0].buttons & 1) == g_mouse_a2_button)) { + g_adb_mouse_valid_data = 0; + adb_clear_mouse_int(); + } + + g_adb_mouse_coord = !g_adb_mouse_coord; + return ret; +} + +void mouse_compress_fifo(double dcycs) { + int pos; + + /* The mouse fifo exists so that fast button changes don't get lost */ + /* if the emulator lags behind the mouse events */ + /* But the FIFO means really old mouse events are saved if */ + /* the emulated code isn't looking at the mouse registers */ + /* This routine compresses all mouse events > 0.5 seconds old */ + + for(pos = g_mouse_fifo_pos; pos >= 1; pos--) { + if(g_mouse_fifo[pos].dcycs < (dcycs - 500*1000.0)) { + /* Remove this entry */ + adb_printf("Old mouse FIFO pos %d removed\n", pos); + g_mouse_fifo_pos = pos - 1; + continue; + } + /* Else, stop searching the FIFO */ + break; + } +} + +void adb_key_event(int a2code, int is_up) { + word32 special; + word32 vbl_count; + int key; + int hard_key; + int pos; + int tmp_ascii; + int ascii; + + if(is_up) { + adb_printf("adb_key_event, key:%02x, is up, g_key_down: %02x\n", + a2code, g_key_down); + } + + if(a2code < 0 || a2code > 0x7f) { + halt_printf("add_key_event: a2code: %04x!\n", a2code); + return; + } + + if(!is_up && a2code == 0x35) { + /* ESC pressed, see if ctrl & cmd key down */ + if(CTRL_DOWN && CMD_DOWN) { + /* Desk mgr int */ + glog("Desk mgr int!"); + + g_adb_interrupt_byte |= 0x20; + adb_add_data_int(); + } + } + + /* convert key to ascii, if possible */ + hard_key = 0; + if(a2_key_to_ascii[a2code][1] & 0xef00) { + /* special key */ + } else { + /* we have ascii */ + hard_key = 1; + } + + pos = 1; + ascii = a2_key_to_ascii[a2code][1]; + if(CAPS_LOCK_DOWN && (ascii >= 'a' && ascii <= 'z')) { + pos = 2; + if(SHIFT_DOWN && (g_adb_mode & 0x40)) { + /* xor shift mode--capslock and shift == lowercase */ + pos = 1; + } + } else if(SHIFT_DOWN) { + pos = 2; + } + + ascii = a2_key_to_ascii[a2code][pos]; + if(CTRL_DOWN) { + tmp_ascii = a2_key_to_ascii[a2code][3]; + if(tmp_ascii >= 0) { + ascii = tmp_ascii; + } + } + key = (ascii & 0x7f) + 0x80; + + special = (ascii >> 8) & 0xff; + if(ascii < 0) { + glogf("ascii1: %d, a2code: %02x, pos: %d", ascii,a2code,pos); + ascii = 0; + special = 0; + } + + + if(!is_up) { + if(hard_key) { + g_kbd_buf[g_kbd_chars_buffered] = key; + g_kbd_chars_buffered++; + if(g_kbd_chars_buffered >= MAX_KBD_BUF) { + g_kbd_chars_buffered = MAX_KBD_BUF - 1; + } + g_key_down = 1; + g_a2code_down = a2code; + + /* first key down, set up autorepeat */ + vbl_count = g_vbl_count; + if(g_config_control_panel) { + vbl_count = g_cfg_vbl_count; + } + g_adb_repeat_vbl = vbl_count + g_adb_repeat_delay; + if(g_adb_repeat_delay == 0) { + g_key_down = 0; + } + g_hard_key_down = 1; + } + + g_c025_val = g_c025_val | special; + adb_printf("new c025_or: %02x\n", g_c025_val); + } else { + if(hard_key && (a2code == g_a2code_down)) { + g_hard_key_down = 0; + /* Turn off repeat */ + g_key_down = 0; + } + + g_c025_val = g_c025_val & (~special); + adb_printf("new c025_and: %02x\n", g_c025_val); + } + + if(g_key_down) { + g_c025_val = g_c025_val & (~0x20); + } else { + /* If no hard key down, set update mod latch */ + g_c025_val = g_c025_val | 0x20; + } + +} + +word32 adb_read_c000() { + word32 vbl_count; + + if( ((g_kbd_buf[0] & 0x80) == 0) && (g_key_down == 0)) { + /* nothing happening, check clipboard */ + int c = clipboard_get_char(); + if(c) { + /* inject clipboard char into keyboard buffer */ + g_kbd_buf[0] = c; + } + /* just get out */ + return g_kbd_buf[0]; + } + if(g_kbd_buf[0] & 0x80) { + /* got one */ + if((g_kbd_read_no_update++ > 5) && (g_kbd_chars_buffered > 1)) { + /* read 5 times, keys pending, let's move it along */ + glogf("Read %02x %d times, tossing", g_kbd_buf[0], g_kbd_read_no_update); + adb_access_c010(); + } + } else { + vbl_count = g_vbl_count; + if(g_config_control_panel) { + vbl_count = g_cfg_vbl_count; + } + if(g_key_down && vbl_count >= g_adb_repeat_vbl) { + /* repeat the g_key_down */ + g_c025_val |= 0x8; + adb_key_event(g_a2code_down, 0); + g_adb_repeat_vbl = vbl_count + g_adb_repeat_rate; + } + } + + return g_kbd_buf[0]; +} + +word32 adb_access_c010() { + int tmp; + int i; + + g_kbd_read_no_update = 0; + + tmp = g_kbd_buf[0] & 0x7f; + g_kbd_buf[0] = tmp; + + tmp = tmp | (g_hard_key_down << 7); + if(g_kbd_chars_buffered) { + for(i = 1; i < g_kbd_chars_buffered; i++) { + g_kbd_buf[i - 1] = g_kbd_buf[i]; + } + g_kbd_chars_buffered--; + } + + g_c025_val = g_c025_val & (~(0x08)); + + return tmp; +} + +word32 adb_read_c025() { + return g_c025_val; +} + +int adb_is_cmd_key_down() { + return CMD_DOWN; +} + +int adb_is_option_key_down() { + return OPTION_DOWN; +} + +void adb_increment_speed() { + const char *str; + + g_limit_speed++; + if(g_limit_speed > 3) { + g_limit_speed = 0; + } + + str = ""; + switch(g_limit_speed) { + case 0: + str = "... as fast as possible!"; + break; + case 1: + str = "... 1.024MHz"; + break; + case 2: + str = "... 2.8MHz"; + break; + case 3: + str = "... 8.0MHz"; + break; + } + glogf("Setting g_limit_speed = %d %s", g_limit_speed, str); +} + +void adb_physical_key_update(int a2code, int is_up) { + int autopoll; + int special; + int ascii_and_type; + int ascii; + + /* this routine called by xdriver to pass raw codes--handle */ + /* ucontroller and ADB bus protocol issues here */ + /* if autopoll on, pass it on through to c025,c000 regs */ + /* else only put it in kbd reg 3, and pull SRQ if needed */ + + adb_printf("adb_phys_key_update: %02x, %d\n", a2code, is_up); + + adb_printf("Handle a2code: %02x, is_up: %d\n", a2code, is_up); + + if(a2code < 0 || a2code > 0x7f) { + halt_printf("a2code: %04x!\n", a2code); + return; + } + + /* Remap 0x7b-0x7e to 0x3b-0x3e (arrow keys on new mac keyboards) */ + if(a2code >= 0x7b && a2code <= 0x7e) { + a2code = a2code - 0x40; + } + + /* Now check for special keys (function keys, etc) */ + ascii_and_type = a2_key_to_ascii[a2code][1]; + special = 0; + if((ascii_and_type & 0xf000) == 0x8000) { + /* special function key */ + special = ascii_and_type & 0xff; + switch(special) { + case 0x01: /* F1 - remap to cmd */ + a2code = 0x37; + special = 0; + break; + case 0x02: /* F2 - remap to option */ + a2code = 0x3a; + special = 0; + break; + case 0x0c: /* F12 - remap to reset */ + a2code = 0x7f; + special = 0; + break; + default: + break; + } + } + + /* CUA clipboard paste - for those that remember ctrl-insert/shift-insert */ + if(is_up == 0 && a2code == 0x72 && SHIFT_DOWN) { + clipboard_paste(); + } + + /* Only process reset requests here */ + if(is_up == 0 && a2code == 0x7f && CTRL_DOWN) { + /* Reset pressed! */ + glogf("Reset pressed since CTRL_DOWN: %d", CTRL_DOWN); + do_reset(); + return; + } + + if(special && !is_up) { + switch(special) { + case 0x03: /* F3 - screenshot */ + g_screenshot_requested = 1; + break; + case 0x04: /* F4 - emulator config panel */ + if (CMD_DOWN) { glog("Quit!"); iwm_shut(); my_exit(1); - } - else - { + } + else + { cfg_toggle_config_panel(); - } - break; - case 0x05: /* F5 - emulator clipboard paste */ - if (SHIFT_DOWN) { - // reserved - } else { - clipboard_paste(); - } - break; - case 0x06: /* F6 - emulator speed */ - if(SHIFT_DOWN) { - halt2_printf("Shift-F6 pressed\n"); - } else { - adb_increment_speed(); - } - break; - case 0x07: /* F7 - fast disk emul */ - g_fast_disk_emul = !g_fast_disk_emul; - glogf("g_fast_disk_emul is now %d", g_fast_disk_emul); - break; - case 0x08: /* F8 - warp pointer */ - g_warp_pointer = !g_warp_pointer; - if(g_hide_pointer != g_warp_pointer) { - g_hide_pointer = g_warp_pointer; - x_hide_pointer(g_hide_pointer); - } - break; - case 0x09: /* F9 - swap paddles */ - if(SHIFT_DOWN) { - g_swap_paddles = !g_swap_paddles; - glogf("Swap paddles is now: %d", g_swap_paddles); - } else { - g_invert_paddles = !g_invert_paddles; - glogf("Invert paddles is now: %d", g_invert_paddles); - } - break; - case 0x0a: /* F10 - change a2vid paletter */ - if (SHIFT_DOWN) { + } + break; + case 0x05: /* F5 - emulator clipboard paste */ + if (SHIFT_DOWN) { + // reserved + } else { + clipboard_paste(); + } + break; + case 0x06: /* F6 - emulator speed */ + if(SHIFT_DOWN) { + halt2_printf("Shift-F6 pressed\n"); + } else { + adb_increment_speed(); + } + break; + case 0x07: /* F7 - fast disk emul */ + g_fast_disk_emul = !g_fast_disk_emul; + glogf("g_fast_disk_emul is now %d", g_fast_disk_emul); + break; + case 0x08: /* F8 - warp pointer */ + g_warp_pointer = !g_warp_pointer; + if(g_hide_pointer != g_warp_pointer) { + g_hide_pointer = g_warp_pointer; + x_hide_pointer(g_hide_pointer); + } + break; + case 0x09: /* F9 - swap paddles */ + if(SHIFT_DOWN) { + g_swap_paddles = !g_swap_paddles; + glogf("Swap paddles is now: %d", g_swap_paddles); + } else { + g_invert_paddles = !g_invert_paddles; + glogf("Invert paddles is now: %d", g_invert_paddles); + } + break; + case 0x0a: /* F10 - change a2vid paletter */ + if (SHIFT_DOWN) { #ifdef TOGGLE_STATUS - extern void x_toggle_status_lines(); - x_toggle_status_lines(); + extern void x_toggle_status_lines(); + x_toggle_status_lines(); #endif - } else if (CMD_DOWN) { + } else if (CMD_DOWN) { do_reset(); return; - } else { - change_a2vid_palette((g_a2vid_palette + 1) & 0xf); - } - break; - case 0x0b: /* F11 - full screen */ - g_fullscreen = !g_fullscreen; - x_full_screen(g_fullscreen); - break; - } + } else { + change_a2vid_palette((g_a2vid_palette + 1) & 0xf); + } + break; + case 0x0b: /* F11 - full screen */ + g_fullscreen = !g_fullscreen; + x_full_screen(g_fullscreen); + break; + } - return; - } - /* Handle Keypad Joystick here partly...if keypad key pressed */ - /* while in Keypad Joystick mode, do not pass it on as a key press */ - if((ascii_and_type & 0xff00) == 0x1000) { - /* Keep track of keypad number keys being up or down even */ - /* if joystick mode isn't keypad. This avoid funny cases */ - /* if joystick mode is changed while a key is pressed */ - ascii = ascii_and_type & 0xff; - if(ascii > 0x30 && ascii <= 0x39) { - g_keypad_key_is_down[ascii - 0x30] = !is_up; - } - if(g_joystick_type == JOYSTICK_TYPE_KEYPAD) { - /* If Joystick type is keypad, then do not let these */ - /* keypress pass on further, except for cmd/opt */ - if(ascii == 0x30) { - /* remap '0' to cmd */ - a2code = 0x37; - } else if(ascii == 0x2e || ascii == 0x2c) { - /* remap '.' and ',' to option */ - a2code = 0x3a; - } else { - /* Just ignore it in this mode */ - return; - } - } - } + return; + } + /* Handle Keypad Joystick here partly...if keypad key pressed */ + /* while in Keypad Joystick mode, do not pass it on as a key press */ + if((ascii_and_type & 0xff00) == 0x1000) { + /* Keep track of keypad number keys being up or down even */ + /* if joystick mode isn't keypad. This avoid funny cases */ + /* if joystick mode is changed while a key is pressed */ + ascii = ascii_and_type & 0xff; + if(ascii > 0x30 && ascii <= 0x39) { + g_keypad_key_is_down[ascii - 0x30] = !is_up; + } + if(g_joystick_type == JOYSTICK_TYPE_KEYPAD) { + /* If Joystick type is keypad, then do not let these */ + /* keypress pass on further, except for cmd/opt */ + if(ascii == 0x30) { + /* remap '0' to cmd */ + a2code = 0x37; + } else if(ascii == 0x2e || ascii == 0x2c) { + /* remap '.' and ',' to option */ + a2code = 0x3a; + } else { + /* Just ignore it in this mode */ + return; + } + } + } - autopoll = 1; - if(g_adb_mode & 1) { - /* autopoll is explicitly off */ - autopoll = 0; - } - if(g_kbd_dev_addr != g_kbd_ctl_addr) { - /* autopoll is off because ucontroller doesn't know kbd moved */ - autopoll = 0; - } - if(g_config_control_panel) { - /* always do autopoll */ - autopoll = 1; - } + autopoll = 1; + if(g_adb_mode & 1) { + /* autopoll is explicitly off */ + autopoll = 0; + } + if(g_kbd_dev_addr != g_kbd_ctl_addr) { + /* autopoll is off because ucontroller doesn't know kbd moved */ + autopoll = 0; + } + if(g_config_control_panel) { + /* always do autopoll */ + autopoll = 1; + } - if(is_up) { - if(!autopoll) { - /* no auto keys, generate SRQ! */ - adb_kbd_reg0_data(a2code, is_up); - } else { - adb_virtual_key_update(a2code, is_up); - } - } else { - if(!autopoll) { - /* no auto keys, generate SRQ! */ - adb_kbd_reg0_data(a2code, is_up); - } else { - /* was up, now down */ - adb_virtual_key_update(a2code, is_up); - } - } + if(is_up) { + if(!autopoll) { + /* no auto keys, generate SRQ! */ + adb_kbd_reg0_data(a2code, is_up); + } else { + adb_virtual_key_update(a2code, is_up); + } + } else { + if(!autopoll) { + /* no auto keys, generate SRQ! */ + adb_kbd_reg0_data(a2code, is_up); + } else { + /* was up, now down */ + adb_virtual_key_update(a2code, is_up); + } + } } -void -adb_virtual_key_update(int a2code, int is_up) -{ - int i; - int bitpos; - word32 mask; +void adb_virtual_key_update(int a2code, int is_up) { + int i; + int bitpos; + word32 mask; - adb_printf("Virtual handle a2code: %02x, is_up: %d\n", a2code, is_up); + adb_printf("Virtual handle a2code: %02x, is_up: %d\n", a2code, is_up); - if(a2code < 0 || a2code > 0x7f) { - halt_printf("a2code: %04x!\n", a2code); - return; - } + if(a2code < 0 || a2code > 0x7f) { + halt_printf("a2code: %04x!\n", a2code); + return; + } - i = (a2code >> 5) & 3; - bitpos = a2code & 0x1f; - mask = (1 << bitpos); + i = (a2code >> 5) & 3; + bitpos = a2code & 0x1f; + mask = (1 << bitpos); - if(is_up) { - if(g_virtual_key_up[i] & mask) { - /* already up, do nothing */ - } else { - g_virtual_key_up[i] |= mask; - adb_key_event(a2code, is_up); - } - } else { - if(g_virtual_key_up[i] & mask) { - g_virtual_key_up[i] &= (~mask); - adb_key_event(a2code, is_up); - } - } + if(is_up) { + if(g_virtual_key_up[i] & mask) { + /* already up, do nothing */ + } else { + g_virtual_key_up[i] |= mask; + adb_key_event(a2code, is_up); + } + } else { + if(g_virtual_key_up[i] & mask) { + g_virtual_key_up[i] &= (~mask); + adb_key_event(a2code, is_up); + } + } } -void -adb_all_keys_up() -{ - word32 mask; - int i, j; +void adb_all_keys_up() { + word32 mask; + int i, j; - for(i = 0; i < 4; i++) { - for(j = 0; j < 32; j++) { - mask = 1 << j; - if((g_virtual_key_up[i] & mask) == 0) { - /* create key-up event */ - adb_physical_key_update(i*32 + j, 1); - } - } - } + for(i = 0; i < 4; i++) { + for(j = 0; j < 32; j++) { + mask = 1 << j; + if((g_virtual_key_up[i] & mask) == 0) { + /* create key-up event */ + adb_physical_key_update(i*32 + j, 1); + } + } + } } -void -adb_kbd_repeat_off() -{ - g_key_down = 0; +void adb_kbd_repeat_off() { + g_key_down = 0; } diff --git a/src/arch/os2/os2driver.c b/src/arch/os2/os2driver.c index e2c87a4..9d25541 100644 --- a/src/arch/os2/os2driver.c +++ b/src/arch/os2/os2driver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "../../defc.h" #include "../../protos.h" @@ -15,13 +15,13 @@ #include #include "gsportos2.h" /* Resource symbolic identifiers*/ -HAB g_hab; /* PM anchor block handle */ -PSZ pszErrMsg; +HAB g_hab; /* PM anchor block handle */ +PSZ pszErrMsg; QMSG qmsg; /* Message from message queue */ HWND g_hwnd_frame = NULLHANDLE; /* Frame window handle */ HWND g_hwnd_client = NULLHANDLE; /* Client area window handle */ -HMQ g_hmq; /* Message queue handle */ +HMQ g_hmq; /* Message queue handle */ extern int Verbose; @@ -32,15 +32,15 @@ int g_screen_mdepth = 0; extern int g_quit_sim_now; -int g_use_shmem = 1; -int g_has_focus = 0; -int g_auto_repeat_on = -1; +int g_use_shmem = 1; +int g_has_focus = 0; +int g_auto_repeat_on = -1; extern Kimage g_mainwin_kimage; -int g_main_height = 0; +int g_main_height = 0; -int g_win_capslock_down = 0; +int g_win_capslock_down = 0; extern int g_border_sides_refresh_needed; extern int g_border_special_refresh_needed; @@ -68,87 +68,79 @@ extern char *g_status_ptrs[MAX_STATUS_LINES]; VOID DispErrorMessage(); -int -win_nonblock_read_stdin(int fd, char *bufptr, int len) -{ +int win_nonblock_read_stdin(int fd, char *bufptr, int len) { return 0; } -void -x_dialog_create_gsport_conf(const char *str) -{ +void x_dialog_create_gsport_conf(const char *str) { } -int -x_show_alert(int is_fatal, const char *str) -{ - return 0; +int x_show_alert(int is_fatal, const char *str) { + return 0; } -int -main(int argc, char **argv) -{ -DEVOPENSTRUC pszData; -ULONG flCreate; /* Window creation control flags*/ -int height; -SIZEL sizel; +int main(int argc, char **argv) { + DEVOPENSTRUC pszData; + ULONG flCreate; /* Window creation control flags*/ + int height; + SIZEL sizel; - if ((g_hab = WinInitialize(0)) == 0L) /* Initialize PM */ - os2_abort(g_hwnd_frame, g_hwnd_client); /* Terminate the application */ + if ((g_hab = WinInitialize(0)) == 0L) /* Initialize PM */ + os2_abort(g_hwnd_frame, g_hwnd_client); /* Terminate the application */ - if ((g_hmq = WinCreateMsgQueue( g_hab, 0 )) == 0L)/* Create a msg queue */ - os2_abort(g_hwnd_frame, g_hwnd_client); /* Terminate the application */ + if ((g_hmq = WinCreateMsgQueue( g_hab, 0 )) == 0L) /* Create a msg queue */ + os2_abort(g_hwnd_frame, g_hwnd_client); /* Terminate the application */ - if (!WinRegisterClass( /* Register window class */ - g_hab, /* Anchor block handle */ - (PSZ)"MyWindow", /* Window class name */ - (PFNWP)MyWindowProc, /* Address of window procedure */ - CS_SIZEREDRAW, /* Class style */ - 0 /* No extra window words */ - )) - os2_abort(g_hwnd_frame, g_hwnd_client); /* Terminate the application */ + if (!WinRegisterClass( /* Register window class */ + g_hab, /* Anchor block handle */ + (PSZ)"MyWindow", /* Window class name */ + (PFNWP)MyWindowProc, /* Address of window procedure */ + CS_SIZEREDRAW, /* Class style */ + 0 /* No extra window words */ + )) + os2_abort(g_hwnd_frame, g_hwnd_client); /* Terminate the application */ - height = X_A2_WINDOW_HEIGHT + (MAX_STATUS_LINES*16); - g_main_height = height; + height = X_A2_WINDOW_HEIGHT + (MAX_STATUS_LINES*16); + g_main_height = height; - flCreate = FCF_STANDARD & /* Set frame control flags to */ - ~FCF_SHELLPOSITION; /* standard except for shell */ - /* positioning. */ + flCreate = FCF_STANDARD & /* Set frame control flags to */ + ~FCF_SHELLPOSITION; /* standard except for shell */ + /* positioning. */ - if ((g_hwnd_frame = WinCreateStdWindow( - HWND_DESKTOP, /* Desktop window is parent */ - 0, /* STD. window styles */ - &flCreate, /* Frame control flag */ - "MyWindow", /* Client window class name */ - "", /* No window text */ - 0, /* No special class style */ - (HMODULE)0L, /* Resource is in .EXE file */ - ID_WINDOW, /* Frame window identifier */ - &g_hwnd_client /* Client window handle */ - )) == 0L) - os2_abort(HWND_DESKTOP, HWND_DESKTOP); /* Terminate the application */ + if ((g_hwnd_frame = WinCreateStdWindow( + HWND_DESKTOP, /* Desktop window is parent */ + 0, /* STD. window styles */ + &flCreate, /* Frame control flag */ + "MyWindow", /* Client window class name */ + "", /* No window text */ + 0, /* No special class style */ + (HMODULE)0L, /* Resource is in .EXE file */ + ID_WINDOW, /* Frame window identifier */ + &g_hwnd_client /* Client window handle */ + )) == 0L) + os2_abort(HWND_DESKTOP, HWND_DESKTOP); /* Terminate the application */ - WinSetWindowText(g_hwnd_frame, "GSport"); + WinSetWindowText(g_hwnd_frame, "GSport"); - if (!WinSetWindowPos( g_hwnd_frame, /* Shows and activates frame */ - HWND_TOP, /* window at position 100, 100, */ - 100, 100, X_A2_WINDOW_WIDTH, height, /* and size 200, 200. */ - SWP_SIZE | SWP_MOVE | SWP_ACTIVATE | SWP_SHOW - )) - os2_abort(g_hwnd_frame, g_hwnd_client); /* Terminate the application */ + if (!WinSetWindowPos( g_hwnd_frame, /* Shows and activates frame */ + HWND_TOP, /* window at position 100, 100, */ + 100, 100, X_A2_WINDOW_WIDTH, height, /* and size 200, 200. */ + SWP_SIZE | SWP_MOVE | SWP_ACTIVATE | SWP_SHOW + )) + os2_abort(g_hwnd_frame, g_hwnd_client); /* Terminate the application */ - g_hdc_screen = WinOpenWindowDC(g_hwnd_client); - sizel.cx = X_A2_WINDOW_WIDTH; - sizel.cy = height; - g_hps_screen = GpiCreatePS(g_hab,g_hdc_screen, &sizel, PU_PELS | GPIF_LONG | GPIA_ASSOC); + g_hdc_screen = WinOpenWindowDC(g_hwnd_client); + sizel.cx = X_A2_WINDOW_WIDTH; + sizel.cy = height; + g_hps_screen = GpiCreatePS(g_hab,g_hdc_screen, &sizel, PU_PELS | GPIF_LONG | GPIA_ASSOC); - g_hdc_memory = DevOpenDC(g_hab, OD_MEMORY, "*", 4, (PDEVOPENDATA)&pszData, NULL); - g_hps_memory = GpiCreatePS(g_hab,g_hdc_memory, &sizel, PU_ARBITRARY | GPIT_MICRO | GPIA_ASSOC); + g_hdc_memory = DevOpenDC(g_hab, OD_MEMORY, "*", 4, (PDEVOPENDATA)&pszData, NULL); + g_hps_memory = GpiCreatePS(g_hab,g_hdc_memory, &sizel, PU_ARBITRARY | GPIT_MICRO | GPIA_ASSOC); - // Call gsplusmain - return gsplusmain(argc, argv); + // Call gsplusmain + return gsplusmain(argc, argv); } @@ -163,10 +155,9 @@ SIZEL sizel; * the message command and parameters. * *************************************************************************/ -MRESULT EXPENTRY MyWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 ) -{ -HPS hps; -RECTL rcl; +MRESULT EXPENTRY MyWindowProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 ) { + HPS hps; + RECTL rcl; switch( msg ) { @@ -185,7 +176,7 @@ RECTL rcl; * When Exit is chosen, the application posts itself a WM_CLOSE * message. */ - { + { USHORT command; /* WM_COMMAND command value */ command = SHORT1FROMMP(mp1); /* Extract the command value */ switch (command) @@ -198,7 +189,7 @@ RECTL rcl; } break; - } + } case WM_ERASEBACKGROUND: /* * Return TRUE to request PM to paint the window background @@ -209,16 +200,16 @@ RECTL rcl; /* * Window contents are drawn here in WM_PAINT processing. */ - hps = WinBeginPaint(hwnd, NULLHANDLE, &rcl); - WinEndPaint(hps); - g_needfullrefreshfornextframe = 1; + hps = WinBeginPaint(hwnd, NULLHANDLE, &rcl); + WinEndPaint(hps); + g_needfullrefreshfornextframe = 1; break; case WM_CLOSE: /* * This is the place to put your termination routines */ WinPostMsg( hwnd, WM_QUIT, (MPARAM)0,(MPARAM)0 ); /* Cause termination*/ - exit(0); + exit(0); break; default: /* @@ -232,262 +223,232 @@ RECTL rcl; return (MRESULT)FALSE; } /* End of MyWindowProc */ -void -check_input_events() -{ +void check_input_events() { /* * Get and dispatch messages from the application message queue * until WinGetMsg returns FALSE, indicating a WM_QUIT message. */ - while(WinPeekMsg(g_hab, &qmsg, g_hwnd_frame, 0, 0, PM_NOREMOVE)) { - if(WinGetMsg(g_hab, &qmsg, 0L, 0, 0) > 0) { - //TranslateMessage(&qmsg); - WinDispatchMsg(g_hab, &qmsg); - } else { - printf("GetMessage returned <= 0\n"); - my_exit(2); - } - } + while(WinPeekMsg(g_hab, &qmsg, g_hwnd_frame, 0, 0, PM_NOREMOVE)) { + if(WinGetMsg(g_hab, &qmsg, 0L, 0, 0) > 0) { + //TranslateMessage(&qmsg); + WinDispatchMsg(g_hab, &qmsg); + } else { + printf("GetMessage returned <= 0\n"); + my_exit(2); + } + } } -void -x_update_color(int col_num, int red, int green, int blue, word32 rgb) -{ +void x_update_color(int col_num, int red, int green, int blue, word32 rgb) { } -void -x_update_physical_colormap() -{ +void x_update_physical_colormap() { } -void -show_xcolor_array() -{ +void show_xcolor_array() { } -void -xdriver_end() -{ - printf("OS/2 driver_end\n"); +void xdriver_end() { + printf("OS/2 driver_end\n"); } -void -x_get_kimage(Kimage *kimage_ptr) -{ - byte *ptr; - int width; - int height; - int depth, mdepth; - int size; +void x_get_kimage(Kimage *kimage_ptr) { + byte *ptr; + int width; + int height; + int depth, mdepth; + int size; - width = kimage_ptr->width_req; - height = kimage_ptr->height; - depth = kimage_ptr->depth; - mdepth = kimage_ptr->mdepth; + width = kimage_ptr->width_req; + height = kimage_ptr->height; + depth = kimage_ptr->depth; + mdepth = kimage_ptr->mdepth; - size = 0; + size = 0; - if(depth == g_screen_depth) { - /* Use g_bmapinfo_ptr, adjusting width, height */ - g_bmaphdr_ptr->cx = width; - g_bmaphdr_ptr->cy = height; + if(depth == g_screen_depth) { + /* Use g_bmapinfo_ptr, adjusting width, height */ + g_bmaphdr_ptr->cx = width; + g_bmaphdr_ptr->cy = height; - kimage_ptr->dev_handle = GpiCreateBitmap( + kimage_ptr->dev_handle = GpiCreateBitmap( - (HPS)g_hps_memory, (PBITMAPINFOHEADER2)g_bmaphdr_ptr, - 0L, (PBYTE)kimage_ptr->data_ptr, - (PBITMAPINFO2)g_bmapinfo_ptr); + (HPS)g_hps_memory, (PBITMAPINFOHEADER2)g_bmaphdr_ptr, + 0L, (PBYTE)kimage_ptr->data_ptr, + (PBITMAPINFO2)g_bmapinfo_ptr); - size = (width*height*mdepth) >> 3; - ptr = (byte *)malloc(size); + size = (width*height*mdepth) >> 3; + ptr = (byte *)malloc(size); - if(ptr == 0) { - printf("malloc for data failed, mdepth: %d\n", mdepth); - exit(2); - } + if(ptr == 0) { + printf("malloc for data failed, mdepth: %d\n", mdepth); + exit(2); + } - kimage_ptr->data_ptr = ptr; + kimage_ptr->data_ptr = ptr; - } else { - /* allocate buffers for video.c to draw into */ + } else { + /* allocate buffers for video.c to draw into */ - size = (width*height*mdepth) >> 3; - ptr = (byte *)malloc(size); + size = (width*height*mdepth) >> 3; + ptr = (byte *)malloc(size); - if(ptr == 0) { - printf("malloc for data failed, mdepth: %d\n", mdepth); - exit(2); - } + if(ptr == 0) { + printf("malloc for data failed, mdepth: %d\n", mdepth); + exit(2); + } - kimage_ptr->data_ptr = ptr; + kimage_ptr->data_ptr = ptr; - kimage_ptr->dev_handle = (void *)-1; + kimage_ptr->dev_handle = (void *)-1; - } + } - return; + return; } -void -dev_video_init() -{ - int lores_col; - int i; +void dev_video_init() { + int lores_col; + int i; - printf("Preparing graphics system\n"); + printf("Preparing graphics system\n"); - g_screen_depth = 24; - g_screen_mdepth = 32; + g_screen_depth = 24; + g_screen_mdepth = 32; - g_bmapinfo_ptr = (BITMAPINFO2 *)malloc(sizeof(BITMAPINFOHEADER2)); - g_bmaphdr_ptr = (BITMAPINFOHEADER2 *)g_bmapinfo_ptr; - g_bmaphdr_ptr->cbFix = sizeof(BITMAPINFOHEADER2); - g_bmaphdr_ptr->cx = A2_WINDOW_WIDTH; - g_bmaphdr_ptr->cy = A2_WINDOW_HEIGHT; - g_bmaphdr_ptr->cPlanes = 1; - g_bmaphdr_ptr->cBitCount = g_screen_mdepth; - g_bmaphdr_ptr->ulCompression = BCA_UNCOMP; - g_bmaphdr_ptr->cclrUsed = 0; + g_bmapinfo_ptr = (BITMAPINFO2 *)malloc(sizeof(BITMAPINFOHEADER2)); + g_bmaphdr_ptr = (BITMAPINFOHEADER2 *)g_bmapinfo_ptr; + g_bmaphdr_ptr->cbFix = sizeof(BITMAPINFOHEADER2); + g_bmaphdr_ptr->cx = A2_WINDOW_WIDTH; + g_bmaphdr_ptr->cy = A2_WINDOW_HEIGHT; + g_bmaphdr_ptr->cPlanes = 1; + g_bmaphdr_ptr->cBitCount = g_screen_mdepth; + g_bmaphdr_ptr->ulCompression = BCA_UNCOMP; + g_bmaphdr_ptr->cclrUsed = 0; - video_get_kimages(); + video_get_kimages(); - if(g_screen_depth != 8) { - // Allocate g_mainwin_kimage - video_get_kimage(&g_mainwin_kimage, 0, g_screen_depth, - g_screen_mdepth); - } + if(g_screen_depth != 8) { + // Allocate g_mainwin_kimage + video_get_kimage(&g_mainwin_kimage, 0, g_screen_depth, + g_screen_mdepth); + } - for(i = 0; i < 256; i++) { - lores_col = g_lores_colors[i & 0xf]; - video_update_color_raw(i, lores_col); - g_a2palette_8to1624[i] = g_palette_8to1624[i]; - } + for(i = 0; i < 256; i++) { + lores_col = g_lores_colors[i & 0xf]; + video_update_color_raw(i, lores_col); + g_a2palette_8to1624[i] = g_palette_8to1624[i]; + } - g_installed_full_superhires_colormap = 1; + g_installed_full_superhires_colormap = 1; - printf("Done with dev_video_init\n"); - fflush(stdout); + printf("Done with dev_video_init\n"); + fflush(stdout); } -void -x_redraw_status_lines() -{ +void x_redraw_status_lines() { - int line,len,height; - POINTL pt; - char *buf; + int line,len,height; + POINTL pt; + char *buf; - printf("x_redraw_status_lines() called\n"); + printf("x_redraw_status_lines() called\n"); /* - if (g_status_ptrs[0] != NULL) - { - height = 16; - pt.x = 5; pt.y = 0; - GpiSetColor( g_hps_screen, CLR_NEUTRAL ); - GpiSetBackColor( g_hps_screen, CLR_BACKGROUND ); - GpiSetBackMix( g_hps_screen, BM_OVERPAINT ); + if (g_status_ptrs[0] != NULL) + { + height = 16; + pt.x = 5; pt.y = 0; + GpiSetColor( g_hps_screen, CLR_NEUTRAL ); + GpiSetBackColor( g_hps_screen, CLR_BACKGROUND ); + GpiSetBackMix( g_hps_screen, BM_OVERPAINT ); - for (line = 0; line < MAX_STATUS_LINES; line++) - { - buf = g_status_ptrs[line]; - if (buf != 0) - { - pt.y = height * (line+1); - len = strlen(buf); - GpiCharStringAt( g_hps_screen, &pt, (LONG)strlen( buf ), buf ); - } - } - } -*/ + for (line = 0; line < MAX_STATUS_LINES; line++) + { + buf = g_status_ptrs[line]; + if (buf != 0) + { + pt.y = height * (line+1); + len = strlen(buf); + GpiCharStringAt( g_hps_screen, &pt, (LONG)strlen( buf ), buf ); + } + } + } + */ } -void -x_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, - int width, int height) -{ - RECTL rc; - POINTL pt[4]; - HBITMAP hbmOld, hbmNew; +void x_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, + int width, int height) { + RECTL rc; + POINTL pt[4]; + HBITMAP hbmOld, hbmNew; - char *szString = "Hello, world!\0"; + char *szString = "Hello, world!\0"; - printf("x_push_kimage() called: Src: (%d,%d) Dest: (%d,%d) Width: %d Height: %d\n",srcx,srcy,destx,desty,width,height); - pt[0].x = destx; /* Target X1 */ - pt[0].y = desty+(MAX_STATUS_LINES*16); /* Target Y1 */ - pt[1].x = destx+width; /* Target X2 */ - pt[1].y = desty+height+(MAX_STATUS_LINES*16); /* Target Y2: Translate up, make room for status border */ - pt[2].x = srcx; /* Source X */ - pt[2].y = srcy; /* Source Y */ - pt[3].x = srcx+width; - pt[3].y = srcy+height; + printf("x_push_kimage() called: Src: (%d,%d) Dest: (%d,%d) Width: %d Height: %d\n",srcx,srcy,destx,desty,width,height); + pt[0].x = destx; /* Target X1 */ + pt[0].y = desty+(MAX_STATUS_LINES*16); /* Target Y1 */ + pt[1].x = destx+width; /* Target X2 */ + pt[1].y = desty+height+(MAX_STATUS_LINES*16); /* Target Y2: Translate up, make room for status border */ + pt[2].x = srcx; /* Source X */ + pt[2].y = srcy; /* Source Y */ + pt[3].x = srcx+width; + pt[3].y = srcy+height; -if (width == 560) -{ - /* Paint a known-good bitmap until we can figure out why images aren't showing up */ - hbmNew = GpiLoadBitmap(g_hps_memory,NULLHANDLE,ID_BITMAP,560,400); - hbmOld = GpiSetBitmap(g_hps_memory, hbmNew); - GpiBitBlt(g_hps_screen,g_hps_memory,4L,pt,ROP_SRCCOPY, BBO_IGNORE); - GpiSetBitmap(g_hps_memory, hbmOld); - GpiDeleteBitmap(hbmNew); -} -else -{ - hbmOld = GpiSetBitmap(g_hps_memory, (HBITMAP)kimage_ptr->dev_handle); - GpiBitBlt(g_hps_screen,g_hps_memory,4L,pt,ROP_SRCCOPY, BBO_IGNORE); - GpiSetBitmap(g_hps_memory, hbmOld); -} + if (width == 560) + { + /* Paint a known-good bitmap until we can figure out why images aren't showing up */ + hbmNew = GpiLoadBitmap(g_hps_memory,NULLHANDLE,ID_BITMAP,560,400); + hbmOld = GpiSetBitmap(g_hps_memory, hbmNew); + GpiBitBlt(g_hps_screen,g_hps_memory,4L,pt,ROP_SRCCOPY, BBO_IGNORE); + GpiSetBitmap(g_hps_memory, hbmOld); + GpiDeleteBitmap(hbmNew); + } + else + { + hbmOld = GpiSetBitmap(g_hps_memory, (HBITMAP)kimage_ptr->dev_handle); + GpiBitBlt(g_hps_screen,g_hps_memory,4L,pt,ROP_SRCCOPY, BBO_IGNORE); + GpiSetBitmap(g_hps_memory, hbmOld); + } } // OG Adding release -void x_release_kimage(Kimage* kimage_ptr) -{ - if (kimage_ptr->dev_handle == (void*)-1) - { - free(kimage_ptr->data_ptr); - kimage_ptr->data_ptr = NULL; - } - else - { - } +void x_release_kimage(Kimage* kimage_ptr) { + if (kimage_ptr->dev_handle == (void*)-1) + { + free(kimage_ptr->data_ptr); + kimage_ptr->data_ptr = NULL; + } + else + { + } } -void -x_push_done() -{ +void x_push_done() { } -void -x_auto_repeat_on(int must) -{ +void x_auto_repeat_on(int must) { } -void -x_auto_repeat_off(int must) -{ +void x_auto_repeat_off(int must) { } -void -x_hide_pointer(int do_hide) -{ +void x_hide_pointer(int do_hide) { } -void -x_full_screen(int do_full) -{ - return; +void x_full_screen(int do_full) { + return; } -int x_calc_ratio(float ratiox,float ratioy) -{ - return 0; // not stretched +int x_calc_ratio(float ratiox,float ratioy) { + return 0; // not stretched } /**************************************************************************/ @@ -496,33 +457,30 @@ int x_calc_ratio(float ratiox,float ratioy) /* The error message is displayed using a message box */ /* */ /**************************************************************************/ -VOID DispErrorMessage() -{ - PERRINFO pErrInfoBlk; - PSZ pszOffSet, pszErrMsg; - ERRORID ErrorId; - PCH ErrorStr; +VOID DispErrorMessage() { + PERRINFO pErrInfoBlk; + PSZ pszOffSet, pszErrMsg; + ERRORID ErrorId; + PCH ErrorStr; - ErrorId = WinGetLastError(g_hab); + ErrorId = WinGetLastError(g_hab); - if ((pErrInfoBlk = WinGetErrorInfo(g_hab)) != (PERRINFO)NULL) - { - pszOffSet = ((PSZ)pErrInfoBlk) + pErrInfoBlk->offaoffszMsg; - pszErrMsg = ((PSZ)pErrInfoBlk) + *((PULONG)pszOffSet); + if ((pErrInfoBlk = WinGetErrorInfo(g_hab)) != (PERRINFO)NULL) + { + pszOffSet = ((PSZ)pErrInfoBlk) + pErrInfoBlk->offaoffszMsg; + pszErrMsg = ((PSZ)pErrInfoBlk) + *((PULONG)pszOffSet); - WinMessageBox(HWND_DESKTOP, /* Parent window is desk top */ - g_hwnd_frame, /* Owner window is our frame */ - pszErrMsg, /* PMWIN Error message */ - "Error", /* Title bar message */ - 0, /* Message identifier */ - MB_MOVEABLE | MB_CANCEL ); /* Flags */ + WinMessageBox(HWND_DESKTOP, /* Parent window is desk top */ + g_hwnd_frame, /* Owner window is our frame */ + pszErrMsg, /* PMWIN Error message */ + "Error", /* Title bar message */ + 0, /* Message identifier */ + MB_MOVEABLE | MB_CANCEL ); /* Flags */ - WinFreeErrorInfo(pErrInfoBlk); - } + WinFreeErrorInfo(pErrInfoBlk); + } } -void -os2_abort(HWND g_hwnd_frame, HWND g_hwnd_client) -{ +void os2_abort(HWND g_hwnd_frame, HWND g_hwnd_client) { exit(-1); } diff --git a/src/arch/os2/os2snd_driver.c b/src/arch/os2/os2snd_driver.c index 3b63f02..dcab44f 100644 --- a/src/arch/os2/os2snd_driver.c +++ b/src/arch/os2/os2snd_driver.c @@ -1,8 +1,8 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ int g_preferred_rate = 48000; diff --git a/src/arch/os2/src/dirport.c b/src/arch/os2/src/dirport.c index 2873391..37dc01a 100644 --- a/src/arch/os2/src/dirport.c +++ b/src/arch/os2/src/dirport.c @@ -18,48 +18,45 @@ * 02111-1307 USA. * */ - + #include #include #include "dirport.h" -DIR *opendir (char *path) -{ - APIRET ulrc; - ULONG cnt = 1; - DIR *dir; - char *name; - - if (!(dir = (DIR*)calloc(1, sizeof(DIR)))) { - return NULL; - } - if (name = (char*)calloc(1, strlen(path) + 3)) { - strcat(strcpy(name, path),path[strlen(path) - 1] == '\\' ? "*" : "\\*"); - dir->handle = HDIR_CREATE; - ulrc = DosFindFirst(name, &(dir->handle), _A_ANY, &(dir->buffer), sizeof(struct _FILEFINDBUF3), &cnt, FIL_STANDARD); - free(name); - } - if (!name || ulrc) { - DosFindClose (dir->handle); - free(dir); - dir = NULL; - } - return dir; -} - -struct dirent *readdir (DIR *dir) -{ - ULONG cnt = 1; - - return DosFindNext (dir->handle, &(dir->buffer), sizeof(struct _FILEFINDBUF), &cnt) ? NULL : &(dir->buffer); -} - -int closedir (DIR *dir) -{ - APIRET ulrc = DosFindClose (dir->handle); +DIR *opendir (char *path) { + APIRET ulrc; + ULONG cnt = 1; + DIR *dir; + char *name; + if (!(dir = (DIR*)calloc(1, sizeof(DIR)))) { + return NULL; + } + if (name = (char*)calloc(1, strlen(path) + 3)) { + strcat(strcpy(name, path),path[strlen(path) - 1] == '\\' ? "*" : "\\*"); + dir->handle = HDIR_CREATE; + ulrc = DosFindFirst(name, &(dir->handle), _A_ANY, &(dir->buffer), sizeof(struct _FILEFINDBUF3), &cnt, FIL_STANDARD); + free(name); + } + if (!name || ulrc) { + DosFindClose (dir->handle); free(dir); - return (int)ulrc; + dir = NULL; + } + return dir; +} + +struct dirent *readdir (DIR *dir) { + ULONG cnt = 1; + + return DosFindNext (dir->handle, &(dir->buffer), sizeof(struct _FILEFINDBUF), &cnt) ? NULL : &(dir->buffer); +} + +int closedir (DIR *dir) { + APIRET ulrc = DosFindClose (dir->handle); + + free(dir); + return (int)ulrc; } @@ -71,7 +68,7 @@ int closedir (DIR *dir) snprintf.c - a portable implementation of snprintf, including vsnprintf.c, asnprintf, vasnprintf, asprintf, vasprintf - + snprintf is a routine to convert numeric and string arguments to formatted strings. It is similar to sprintf(3) provided in a system's C library, yet it requires an additional argument - the buffer size - @@ -81,10 +78,10 @@ int closedir (DIR *dir) not or do provide an inadequate (slow or idiosyncratic) version, which calls for a portable implementation of this routine. -Author + Author Mark Martinec , April 1999, June 2000 - Copyright © 1999, Mark Martinec + Copyright � 1999, Mark Martinec */ @@ -167,14 +164,14 @@ Author #define fast_memcpy(d,s,n) \ { register size_t nn = (size_t)(n); \ if (nn >= breakeven_point) memcpy((d), (s), nn); \ - else if (nn > 0) { /* proc call overhead is worth only for large strings*/\ + else if (nn > 0) { /* proc call overhead is worth only for large strings*/ \ register char *dd; register const char *ss; \ for (ss=(s), dd=(d); nn>0; nn--) *dd++ = *ss++; } } #define fast_memset(d,c,n) \ { register size_t nn = (size_t)(n); \ if (nn >= breakeven_point) memset((d), (int)(c), nn); \ - else if (nn > 0) { /* proc call overhead is worth only for large strings*/\ + else if (nn > 0) { /* proc call overhead is worth only for large strings*/ \ register char *dd; register const int cc=(int)(c); \ for (dd=(d); nn>0; nn--) *dd++ = cc; } } @@ -251,8 +248,7 @@ int vasprintf(char **ptr, const char *fmt, va_list ap) { { va_list ap2; va_copy(ap2, ap); /* don't consume the original ap, we'll need it again */ str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap2);/*get required size*/ - va_end(ap2); - } + va_end(ap2);} assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */ *ptr = (char *) malloc(str_m = (size_t)str_l + 1); if (*ptr == NULL) { errno = ENOMEM; str_l = -1; } @@ -300,8 +296,7 @@ int vasnprintf (char **ptr, size_t str_m, const char *fmt, va_list ap) { { va_list ap2; va_copy(ap2, ap); /* don't consume the original ap, we'll need it again */ str_l = portable_vsnprintf(NULL, (size_t)0, fmt, ap2);/*get required size*/ - va_end(ap2); - } + va_end(ap2);} assert(str_l >= 0); /* possible integer overflow if str_m > INT_MAX */ if ((size_t)str_l + 1 < str_m) str_m = (size_t)str_l + 1; /* truncate */ /* if str_m is 0, no buffer is allocated, just set *ptr to NULL */ @@ -358,14 +353,14 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { if (!p) p = ""; while (*p) { if (*p != '%') { - /* if (str_l < str_m) str[str_l++] = *p++; -- this would be sufficient */ - /* but the following code achieves better performance for cases - * where format string is long and contains few conversions */ + /* if (str_l < str_m) str[str_l++] = *p++; -- this would be sufficient */ + /* but the following code achieves better performance for cases + * where format string is long and contains few conversions */ const char *q = strchr(p+1,'%'); size_t n = !q ? strlen(p) : (q-p); if (str_l < str_m) { size_t avail = str_m-str_l; - fast_memcpy(str+str_l, p, (n>avail?avail:n)); + fast_memcpy(str+str_l, p, (n>avail ? avail : n)); } p += n; str_l += n; } else { @@ -382,45 +377,45 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { size_t str_arg_l; /* natural field width of arg without padding and sign */ unsigned char uchar_arg; - /* unsigned char argument value - only defined for c conversion. - N.B. standard explicitly states the char argument for - the c conversion is unsigned */ + /* unsigned char argument value - only defined for c conversion. + N.B. standard explicitly states the char argument for + the c conversion is unsigned */ size_t number_of_zeros_to_pad = 0; - /* number of zeros to be inserted for numeric conversions - as required by the precision or minimal field width */ + /* number of zeros to be inserted for numeric conversions + as required by the precision or minimal field width */ size_t zero_padding_insertion_ind = 0; - /* index into tmp where zero padding is to be inserted */ + /* index into tmp where zero padding is to be inserted */ char fmt_spec = '\0'; - /* current conversion specifier character */ + /* current conversion specifier character */ str_arg = credits;/* just to make compiler happy (defined but not used)*/ str_arg = NULL; starting_p = p; p++; /* skip '%' */ - /* parse flags */ + /* parse flags */ while (*p == '0' || *p == '-' || *p == '+' || *p == ' ' || *p == '#' || *p == '\'') { switch (*p) { - case '0': zero_padding = 1; break; - case '-': justify_left = 1; break; - case '+': force_sign = 1; space_for_positive = 0; break; - case ' ': force_sign = 1; - /* If both the ' ' and '+' flags appear, the ' ' flag should be ignored */ + case '0': zero_padding = 1; break; + case '-': justify_left = 1; break; + case '+': force_sign = 1; space_for_positive = 0; break; + case ' ': force_sign = 1; + /* If both the ' ' and '+' flags appear, the ' ' flag should be ignored */ #ifdef PERL_COMPATIBLE - /* ... but in Perl the last of ' ' and '+' applies */ - space_for_positive = 1; + /* ... but in Perl the last of ' ' and '+' applies */ + space_for_positive = 1; #endif - break; - case '#': alternate_form = 1; break; - case '\'': break; + break; + case '#': alternate_form = 1; break; + case '\'': break; } p++; } - /* If the '0' and '-' flags both appear, the '0' flag should be ignored. */ + /* If the '0' and '-' flags both appear, the '0' flag should be ignored. */ - /* parse field width */ + /* parse field width */ if (*p == '*') { int j; p++; j = va_arg(ap, int); @@ -433,7 +428,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { while (isdigit((int)(*p))) uj = 10*uj + (unsigned int)(*p++ - '0'); min_field_width = uj; } - /* parse precision */ + /* parse precision */ if (*p == '.') { p++; precision_specified = 1; if (*p == '*') { @@ -442,12 +437,12 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { if (j >= 0) precision = j; else { precision_specified = 0; precision = 0; - /* NOTE: - * Solaris 2.6 man page claims that in this case the precision - * should be set to 0. Digital Unix 4.0, HPUX 10 and BSD man page - * claim that this case should be treated as unspecified precision, - * which is what we do here. - */ + /* NOTE: + * Solaris 2.6 man page claims that in this case the precision + * should be set to 0. Digital Unix 4.0, HPUX 10 and BSD man page + * claim that this case should be treated as unspecified precision, + * which is what we do here. + */ } } else if (isdigit((int)(*p))) { /* size_t could be wider than unsigned int; @@ -457,7 +452,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { precision = uj; } } - /* parse 'h', 'l' and 'll' length modifiers */ + /* parse 'h', 'l' and 'll' length modifiers */ if (*p == 'h' || *p == 'l') { length_modifier = *p; p++; if (length_modifier == 'l' && *p == 'l') { /* double l = long long */ @@ -470,336 +465,334 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { } } fmt_spec = *p; - /* common synonyms: */ + /* common synonyms: */ switch (fmt_spec) { - case 'i': fmt_spec = 'd'; break; - case 'D': fmt_spec = 'd'; length_modifier = 'l'; break; - case 'U': fmt_spec = 'u'; length_modifier = 'l'; break; - case 'O': fmt_spec = 'o'; length_modifier = 'l'; break; - default: break; + case 'i': fmt_spec = 'd'; break; + case 'D': fmt_spec = 'd'; length_modifier = 'l'; break; + case 'U': fmt_spec = 'u'; length_modifier = 'l'; break; + case 'O': fmt_spec = 'o'; length_modifier = 'l'; break; + default: break; } - /* get parameter value, do initial processing */ + /* get parameter value, do initial processing */ switch (fmt_spec) { - case '%': /* % behaves similar to 's' regarding flags and field widths */ - case 'c': /* c behaves similar to 's' regarding flags and field widths */ - case 's': - length_modifier = '\0'; /* wint_t and wchar_t not supported */ - /* the result of zero padding flag with non-numeric conversion specifier*/ - /* is undefined. Solaris and HPUX 10 does zero padding in this case, */ - /* Digital Unix and Linux does not. */ -#if !defined(SOLARIS_COMPATIBLE) && !defined(HPUX_COMPATIBLE) - zero_padding = 0; /* turn zero padding off for string conversions */ -#endif - str_arg_l = 1; - switch (fmt_spec) { - case '%': - str_arg = p; break; - case 'c': { - int j = va_arg(ap, int); - uchar_arg = (unsigned char) j; /* standard demands unsigned char */ - str_arg = (const char *) &uchar_arg; - break; - } + case '%': /* % behaves similar to 's' regarding flags and field widths */ + case 'c': /* c behaves similar to 's' regarding flags and field widths */ case 's': - str_arg = va_arg(ap, const char *); - if (!str_arg) str_arg_l = 0; - /* make sure not to address string beyond the specified precision !!! */ - else if (!precision_specified) str_arg_l = strlen(str_arg); - /* truncate string if necessary as requested by precision */ - else if (precision == 0) str_arg_l = 0; - else { - /* memchr on HP does not like n > 2^31 !!! */ - const char *q = memchr(str_arg, '\0', - precision <= 0x7fffffff ? precision : 0x7fffffff); - str_arg_l = !q ? precision : (q-str_arg); + length_modifier = '\0'; /* wint_t and wchar_t not supported */ + /* the result of zero padding flag with non-numeric conversion specifier*/ + /* is undefined. Solaris and HPUX 10 does zero padding in this case, */ + /* Digital Unix and Linux does not. */ +#if !defined(SOLARIS_COMPATIBLE) && !defined(HPUX_COMPATIBLE) + zero_padding = 0; /* turn zero padding off for string conversions */ +#endif + str_arg_l = 1; + switch (fmt_spec) { + case '%': + str_arg = p; break; + case 'c': { + int j = va_arg(ap, int); + uchar_arg = (unsigned char) j; /* standard demands unsigned char */ + str_arg = (const char *) &uchar_arg; + break; + } + case 's': + str_arg = va_arg(ap, const char *); + if (!str_arg) str_arg_l = 0; + /* make sure not to address string beyond the specified precision !!! */ + else if (!precision_specified) str_arg_l = strlen(str_arg); + /* truncate string if necessary as requested by precision */ + else if (precision == 0) str_arg_l = 0; + else { + /* memchr on HP does not like n > 2^31 !!! */ + const char *q = memchr(str_arg, '\0', + precision <= 0x7fffffff ? precision : 0x7fffffff); + str_arg_l = !q ? precision : (q-str_arg); + } + break; + default: break; } break; - default: break; - } - break; - case 'd': case 'u': case 'o': case 'x': case 'X': case 'p': { - /* NOTE: the u, o, x, X and p conversion specifiers imply - the value is unsigned; d implies a signed value */ + case 'd': case 'u': case 'o': case 'x': case 'X': case 'p': { + /* NOTE: the u, o, x, X and p conversion specifiers imply + the value is unsigned; d implies a signed value */ - int arg_sign = 0; + int arg_sign = 0; /* 0 if numeric argument is zero (or if pointer is NULL for 'p'), - +1 if greater than zero (or nonzero for unsigned arguments), - -1 if negative (unsigned argument is never negative) */ + +1 if greater than zero (or nonzero for unsigned arguments), + -1 if negative (unsigned argument is never negative) */ - int int_arg = 0; unsigned int uint_arg = 0; + int int_arg = 0; unsigned int uint_arg = 0; /* only defined for length modifier h, or for no length modifiers */ - long int long_arg = 0; unsigned long int ulong_arg = 0; + long int long_arg = 0; unsigned long int ulong_arg = 0; /* only defined for length modifier l */ - void *ptr_arg = NULL; + void *ptr_arg = NULL; /* pointer argument value -only defined for p conversion */ #ifdef SNPRINTF_LONGLONG_SUPPORT - long long int long_long_arg = 0; - unsigned long long int ulong_long_arg = 0; + long long int long_long_arg = 0; + unsigned long long int ulong_long_arg = 0; /* only defined for length modifier ll */ #endif - if (fmt_spec == 'p') { - /* HPUX 10: An l, h, ll or L before any other conversion character - * (other than d, i, u, o, x, or X) is ignored. - * Digital Unix: - * not specified, but seems to behave as HPUX does. - * Solaris: If an h, l, or L appears before any other conversion - * specifier (other than d, i, u, o, x, or X), the behavior - * is undefined. (Actually %hp converts only 16-bits of address - * and %llp treats address as 64-bit data which is incompatible - * with (void *) argument on a 32-bit system). - */ + if (fmt_spec == 'p') { + /* HPUX 10: An l, h, ll or L before any other conversion character + * (other than d, i, u, o, x, or X) is ignored. + * Digital Unix: + * not specified, but seems to behave as HPUX does. + * Solaris: If an h, l, or L appears before any other conversion + * specifier (other than d, i, u, o, x, or X), the behavior + * is undefined. (Actually %hp converts only 16-bits of address + * and %llp treats address as 64-bit data which is incompatible + * with (void *) argument on a 32-bit system). + */ #ifdef SOLARIS_COMPATIBLE # ifdef SOLARIS_BUG_COMPATIBLE - /* keep length modifiers even if it represents 'll' */ + /* keep length modifiers even if it represents 'll' */ # else - if (length_modifier == '2') length_modifier = '\0'; + if (length_modifier == '2') length_modifier = '\0'; # endif #else - length_modifier = '\0'; + length_modifier = '\0'; #endif - ptr_arg = va_arg(ap, void *); - if (ptr_arg != NULL) arg_sign = 1; - } else if (fmt_spec == 'd') { /* signed */ - switch (length_modifier) { - case '\0': - case 'h': - /* It is non-portable to specify a second argument of char or short - * to va_arg, because arguments seen by the called function - * are not char or short. C converts char and short arguments - * to int before passing them to a function. - */ - int_arg = va_arg(ap, int); - if (int_arg > 0) arg_sign = 1; - else if (int_arg < 0) arg_sign = -1; - break; - case 'l': - long_arg = va_arg(ap, long int); - if (long_arg > 0) arg_sign = 1; - else if (long_arg < 0) arg_sign = -1; - break; + ptr_arg = va_arg(ap, void *); + if (ptr_arg != NULL) arg_sign = 1; + } else if (fmt_spec == 'd') { /* signed */ + switch (length_modifier) { + case '\0': + case 'h': + /* It is non-portable to specify a second argument of char or short + * to va_arg, because arguments seen by the called function + * are not char or short. C converts char and short arguments + * to int before passing them to a function. + */ + int_arg = va_arg(ap, int); + if (int_arg > 0) arg_sign = 1; + else if (int_arg < 0) arg_sign = -1; + break; + case 'l': + long_arg = va_arg(ap, long int); + if (long_arg > 0) arg_sign = 1; + else if (long_arg < 0) arg_sign = -1; + break; #ifdef SNPRINTF_LONGLONG_SUPPORT - case '2': - long_long_arg = va_arg(ap, long long int); - if (long_long_arg > 0) arg_sign = 1; - else if (long_long_arg < 0) arg_sign = -1; - break; + case '2': + long_long_arg = va_arg(ap, long long int); + if (long_long_arg > 0) arg_sign = 1; + else if (long_long_arg < 0) arg_sign = -1; + break; #endif - } - } else { /* unsigned */ - switch (length_modifier) { - case '\0': - case 'h': - uint_arg = va_arg(ap, unsigned int); - if (uint_arg) arg_sign = 1; - break; - case 'l': - ulong_arg = va_arg(ap, unsigned long int); - if (ulong_arg) arg_sign = 1; - break; + } + } else { /* unsigned */ + switch (length_modifier) { + case '\0': + case 'h': + uint_arg = va_arg(ap, unsigned int); + if (uint_arg) arg_sign = 1; + break; + case 'l': + ulong_arg = va_arg(ap, unsigned long int); + if (ulong_arg) arg_sign = 1; + break; #ifdef SNPRINTF_LONGLONG_SUPPORT - case '2': - ulong_long_arg = va_arg(ap, unsigned long long int); - if (ulong_long_arg) arg_sign = 1; - break; + case '2': + ulong_long_arg = va_arg(ap, unsigned long long int); + if (ulong_long_arg) arg_sign = 1; + break; #endif + } } - } - str_arg = tmp; str_arg_l = 0; - /* NOTE: - * For d, i, u, o, x, and X conversions, if precision is specified, - * the '0' flag should be ignored. This is so with Solaris 2.6, - * Digital UNIX 4.0, HPUX 10, Linux, FreeBSD, NetBSD; but not with Perl. - */ + str_arg = tmp; str_arg_l = 0; + /* NOTE: + * For d, i, u, o, x, and X conversions, if precision is specified, + * the '0' flag should be ignored. This is so with Solaris 2.6, + * Digital UNIX 4.0, HPUX 10, Linux, FreeBSD, NetBSD; but not with Perl. + */ #ifndef PERL_COMPATIBLE - if (precision_specified) zero_padding = 0; + if (precision_specified) zero_padding = 0; #endif - if (fmt_spec == 'd') { - if (force_sign && arg_sign >= 0) - tmp[str_arg_l++] = space_for_positive ? ' ' : '+'; - /* leave negative numbers for sprintf to handle, - to avoid handling tricky cases like (short int)(-32768) */ + if (fmt_spec == 'd') { + if (force_sign && arg_sign >= 0) + tmp[str_arg_l++] = space_for_positive ? ' ' : '+'; + /* leave negative numbers for sprintf to handle, + to avoid handling tricky cases like (short int)(-32768) */ #ifdef LINUX_COMPATIBLE - } else if (fmt_spec == 'p' && force_sign && arg_sign > 0) { - tmp[str_arg_l++] = space_for_positive ? ' ' : '+'; + } else if (fmt_spec == 'p' && force_sign && arg_sign > 0) { + tmp[str_arg_l++] = space_for_positive ? ' ' : '+'; #endif - } else if (alternate_form) { - if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X') ) + } else if (alternate_form) { + if (arg_sign != 0 && (fmt_spec == 'x' || fmt_spec == 'X') ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = fmt_spec; } - /* alternate form should have no effect for p conversion, but ... */ + /* alternate form should have no effect for p conversion, but ... */ #ifdef HPUX_COMPATIBLE - else if (fmt_spec == 'p' - /* HPUX 10: for an alternate form of p conversion, - * a nonzero result is prefixed by 0x. */ + else if (fmt_spec == 'p' + /* HPUX 10: for an alternate form of p conversion, + * a nonzero result is prefixed by 0x. */ #ifndef HPUX_BUG_COMPATIBLE - /* Actually it uses 0x prefix even for a zero value. */ - && arg_sign != 0 + /* Actually it uses 0x prefix even for a zero value. */ + && arg_sign != 0 #endif - ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; } + ) { tmp[str_arg_l++] = '0'; tmp[str_arg_l++] = 'x'; } #endif - } - zero_padding_insertion_ind = str_arg_l; - if (!precision_specified) precision = 1; /* default precision is 1 */ - if (precision == 0 && arg_sign == 0 + } + zero_padding_insertion_ind = str_arg_l; + if (!precision_specified) precision = 1; /* default precision is 1 */ + if (precision == 0 && arg_sign == 0 #if defined(HPUX_BUG_COMPATIBLE) || defined(LINUX_COMPATIBLE) - && fmt_spec != 'p' - /* HPUX 10 man page claims: With conversion character p the result of - * converting a zero value with a precision of zero is a null string. - * Actually HP returns all zeroes, and Linux returns "(nil)". */ + && fmt_spec != 'p' + /* HPUX 10 man page claims: With conversion character p the result of + * converting a zero value with a precision of zero is a null string. + * Actually HP returns all zeroes, and Linux returns "(nil)". */ #endif - ) { - /* converted to null string */ - /* When zero value is formatted with an explicit precision 0, - the resulting formatted string is empty (d, i, u, o, x, X, p). */ - } else { - char f[5]; int f_l = 0; - f[f_l++] = '%'; /* construct a simple format string for sprintf */ - if (!length_modifier) { } - else if (length_modifier=='2') { f[f_l++] = 'l'; f[f_l++] = 'l'; } - else f[f_l++] = length_modifier; - f[f_l++] = fmt_spec; f[f_l++] = '\0'; - if (fmt_spec == 'p') str_arg_l += sprintf(tmp+str_arg_l, f, ptr_arg); - else if (fmt_spec == 'd') { /* signed */ - switch (length_modifier) { - case '\0': - case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, int_arg); break; - case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, long_arg); break; + ) { + /* converted to null string */ + /* When zero value is formatted with an explicit precision 0, + the resulting formatted string is empty (d, i, u, o, x, X, p). */ + } else { + char f[5]; int f_l = 0; + f[f_l++] = '%'; /* construct a simple format string for sprintf */ + if (!length_modifier) { } + else if (length_modifier=='2') { f[f_l++] = 'l'; f[f_l++] = 'l'; } + else f[f_l++] = length_modifier; + f[f_l++] = fmt_spec; f[f_l++] = '\0'; + if (fmt_spec == 'p') str_arg_l += sprintf(tmp+str_arg_l, f, ptr_arg); + else if (fmt_spec == 'd') { /* signed */ + switch (length_modifier) { + case '\0': + case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, int_arg); break; + case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, long_arg); break; #ifdef SNPRINTF_LONGLONG_SUPPORT - case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,long_long_arg); break; + case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,long_long_arg); break; #endif + } + } else { /* unsigned */ + switch (length_modifier) { + case '\0': + case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, uint_arg); break; + case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, ulong_arg); break; +#ifdef SNPRINTF_LONGLONG_SUPPORT + case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,ulong_long_arg); break; +#endif + } } - } else { /* unsigned */ - switch (length_modifier) { - case '\0': - case 'h': str_arg_l+=sprintf(tmp+str_arg_l, f, uint_arg); break; - case 'l': str_arg_l+=sprintf(tmp+str_arg_l, f, ulong_arg); break; -#ifdef SNPRINTF_LONGLONG_SUPPORT - case '2': str_arg_l+=sprintf(tmp+str_arg_l,f,ulong_long_arg);break; -#endif + /* include the optional minus sign and possible "0x" + in the region before the zero padding insertion point */ + if (zero_padding_insertion_ind < str_arg_l && + tmp[zero_padding_insertion_ind] == '-') { + zero_padding_insertion_ind++; + } + if (zero_padding_insertion_ind+1 < str_arg_l && + tmp[zero_padding_insertion_ind] == '0' && + (tmp[zero_padding_insertion_ind+1] == 'x' || + tmp[zero_padding_insertion_ind+1] == 'X') ) { + zero_padding_insertion_ind += 2; } } - /* include the optional minus sign and possible "0x" - in the region before the zero padding insertion point */ - if (zero_padding_insertion_ind < str_arg_l && - tmp[zero_padding_insertion_ind] == '-') { - zero_padding_insertion_ind++; - } - if (zero_padding_insertion_ind+1 < str_arg_l && - tmp[zero_padding_insertion_ind] == '0' && - (tmp[zero_padding_insertion_ind+1] == 'x' || - tmp[zero_padding_insertion_ind+1] == 'X') ) { - zero_padding_insertion_ind += 2; - } - } - { size_t num_of_digits = str_arg_l - zero_padding_insertion_ind; - if (alternate_form && fmt_spec == 'o' + { size_t num_of_digits = str_arg_l - zero_padding_insertion_ind; + if (alternate_form && fmt_spec == 'o' #ifdef HPUX_COMPATIBLE /* ("%#.o",0) -> "" */ - && (str_arg_l > 0) + && (str_arg_l > 0) #endif #ifdef DIGITAL_UNIX_BUG_COMPATIBLE /* ("%#o",0) -> "00" */ #else - /* unless zero is already the first character */ - && !(zero_padding_insertion_ind < str_arg_l - && tmp[zero_padding_insertion_ind] == '0') + /* unless zero is already the first character */ + && !(zero_padding_insertion_ind < str_arg_l + && tmp[zero_padding_insertion_ind] == '0') #endif - ) { /* assure leading zero for alternate-form octal numbers */ - if (!precision_specified || precision < num_of_digits+1) { - /* precision is increased to force the first character to be zero, - except if a zero value is formatted with an explicit precision - of zero */ - precision = num_of_digits+1; precision_specified = 1; + ) { /* assure leading zero for alternate-form octal numbers */ + if (!precision_specified || precision < num_of_digits+1) { + /* precision is increased to force the first character to be zero, + except if a zero value is formatted with an explicit precision + of zero */ + precision = num_of_digits+1; precision_specified = 1; + } } + /* zero padding to specified precision? */ + if (num_of_digits < precision) + number_of_zeros_to_pad = precision - num_of_digits; } + /* zero padding to specified minimal field width? */ + if (!justify_left && zero_padding) { + int n = min_field_width - (str_arg_l+number_of_zeros_to_pad); + if (n > 0) number_of_zeros_to_pad += n; } - /* zero padding to specified precision? */ - if (num_of_digits < precision) - number_of_zeros_to_pad = precision - num_of_digits; + break; } - /* zero padding to specified minimal field width? */ - if (!justify_left && zero_padding) { - int n = min_field_width - (str_arg_l+number_of_zeros_to_pad); - if (n > 0) number_of_zeros_to_pad += n; - } - break; - } - default: /* unrecognized conversion specifier, keep format string as-is*/ - zero_padding = 0; /* turn zero padding off for non-numeric convers. */ + default: /* unrecognized conversion specifier, keep format string as-is*/ + zero_padding = 0; /* turn zero padding off for non-numeric convers. */ #ifndef DIGITAL_UNIX_COMPATIBLE - justify_left = 1; min_field_width = 0; /* reset flags */ + justify_left = 1; min_field_width = 0; /* reset flags */ #endif #if defined(PERL_COMPATIBLE) || defined(LINUX_COMPATIBLE) - /* keep the entire format string unchanged */ - str_arg = starting_p; str_arg_l = p - starting_p; - /* well, not exactly so for Linux, which does something inbetween, - * and I don't feel an urge to imitate it: "%+++++hy" -> "%+y" */ + /* keep the entire format string unchanged */ + str_arg = starting_p; str_arg_l = p - starting_p; + /* well, not exactly so for Linux, which does something inbetween, + * and I don't feel an urge to imitate it: "%+++++hy" -> "%+y" */ #else - /* discard the unrecognized conversion, just keep * - * the unrecognized conversion character */ - str_arg = p; str_arg_l = 0; + /* discard the unrecognized conversion, just keep * + * the unrecognized conversion character */ + str_arg = p; str_arg_l = 0; #endif - if (*p) str_arg_l++; /* include invalid conversion specifier unchanged - if not at end-of-string */ - break; + if (*p) str_arg_l++; /* include invalid conversion specifier unchanged + if not at end-of-string */ + break; } if (*p) p++; /* step over the just processed conversion specifier */ - /* insert padding to the left as requested by min_field_width; - this does not include the zero padding in case of numerical conversions*/ + /* insert padding to the left as requested by min_field_width; + this does not include the zero padding in case of numerical conversions*/ if (!justify_left) { /* left padding with blank or zero */ int n = min_field_width - (str_arg_l+number_of_zeros_to_pad); if (n > 0) { if (str_l < str_m) { size_t avail = str_m-str_l; - fast_memset(str+str_l, (zero_padding?'0':' '), (n>avail?avail:n)); + fast_memset(str+str_l, (zero_padding ? '0' : ' '), (n>avail ? avail : n)); } str_l += n; } } - /* zero padding as requested by the precision or by the minimal field width - * for numeric conversions required? */ + /* zero padding as requested by the precision or by the minimal field width + * for numeric conversions required? */ if (number_of_zeros_to_pad <= 0) { - /* will not copy first part of numeric right now, * - * force it to be copied later in its entirety */ + /* will not copy first part of numeric right now, * + * force it to be copied later in its entirety */ zero_padding_insertion_ind = 0; } else { - /* insert first part of numerics (sign or '0x') before zero padding */ + /* insert first part of numerics (sign or '0x') before zero padding */ int n = zero_padding_insertion_ind; if (n > 0) { if (str_l < str_m) { size_t avail = str_m-str_l; - fast_memcpy(str+str_l, str_arg, (n>avail?avail:n)); + fast_memcpy(str+str_l, str_arg, (n>avail ? avail : n)); } str_l += n; } - /* insert zero padding as requested by the precision or min field width */ + /* insert zero padding as requested by the precision or min field width */ n = number_of_zeros_to_pad; if (n > 0) { if (str_l < str_m) { size_t avail = str_m-str_l; - fast_memset(str+str_l, '0', (n>avail?avail:n)); + fast_memset(str+str_l, '0', (n>avail ? avail : n)); } str_l += n; } } - /* insert formatted string - * (or as-is conversion specifier for unknown conversions) */ + /* insert formatted string + * (or as-is conversion specifier for unknown conversions) */ { int n = str_arg_l - zero_padding_insertion_ind; if (n > 0) { if (str_l < str_m) { size_t avail = str_m-str_l; fast_memcpy(str+str_l, str_arg+zero_padding_insertion_ind, - (n>avail?avail:n)); + (n>avail ? avail : n)); } str_l += n; - } - } - /* insert right padding */ + }} + /* insert right padding */ if (justify_left) { /* right blank padding to the field width */ int n = min_field_width - (str_arg_l+number_of_zeros_to_pad); if (n > 0) { if (str_l < str_m) { size_t avail = str_m-str_l; - fast_memset(str+str_l, ' ', (n>avail?avail:n)); + fast_memset(str+str_l, ' ', (n>avail ? avail : n)); } str_l += n; } @@ -829,7 +822,7 @@ int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { #endif #endif /* ndef HAVE_SNPRINTF */ /* - Local Variables: - tab-width: 3 - end: -*/ + Local Variables: + tab-width: 3 + end: + */ diff --git a/src/atbridge/aarp.c b/src/atbridge/aarp.c index b5face6..edbb925 100644 --- a/src/atbridge/aarp.c +++ b/src/atbridge/aarp.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /** This module implements AARP, a necessary protocol for ELAP communication. **/ @@ -24,10 +24,10 @@ struct amt_entry_t { - struct at_addr_t protocol; - struct ether_addr_t hardware; + struct at_addr_t protocol; + struct ether_addr_t hardware; - struct amt_entry_t* next; + struct amt_entry_t* next; }; typedef struct amt_entry_t* amt_t; @@ -38,253 +38,237 @@ static unsigned int retry_count; static clock_t retry_timer; -void aarp_init() -{ - aarp_retry_reset(); +void aarp_init() { + aarp_retry_reset(); } -void aarp_shutdown() -{ - struct amt_entry_t* entry = amt; - while (entry) - { - struct amt_entry_t* next = entry->next; - free(entry); - entry = next; - } +void aarp_shutdown() { + struct amt_entry_t* entry = amt; + while (entry) + { + struct amt_entry_t* next = entry->next; + free(entry); + entry = next; + } } //// -static void aarp_send_packet(enum AARP_FUNCTION function, const struct at_addr_t* source_at_addr, const struct at_addr_t* dest_at_addr, const struct ether_addr_t* dest_hw_addr) -{ - if (source_at_addr && dest_at_addr && dest_hw_addr) - { - struct aarp_header_t response; - response.hardware_type = htons(AARP_HARDWARE_ETHER); - response.protocol_type = htons(AARP_PROTOCOL_TYPE); - response.hw_addr_len = AARP_HW_ADDR_LEN; - response.protocol_addr_len = AARP_PROTOCOL_ADDR_LEN; - response.function = htons(function); +static void aarp_send_packet(enum AARP_FUNCTION function, const struct at_addr_t* source_at_addr, const struct at_addr_t* dest_at_addr, const struct ether_addr_t* dest_hw_addr) { + if (source_at_addr && dest_at_addr && dest_hw_addr) + { + struct aarp_header_t response; + response.hardware_type = htons(AARP_HARDWARE_ETHER); + response.protocol_type = htons(AARP_PROTOCOL_TYPE); + response.hw_addr_len = AARP_HW_ADDR_LEN; + response.protocol_addr_len = AARP_PROTOCOL_ADDR_LEN; + response.function = htons(function); - memcpy(&response.source_proto_addr.addr, source_at_addr, sizeof(response.source_proto_addr.addr)); - response.source_proto_addr.addr.network = htons(response.source_proto_addr.addr.network); - response.source_proto_addr.zero = 0x00; + memcpy(&response.source_proto_addr.addr, source_at_addr, sizeof(response.source_proto_addr.addr)); + response.source_proto_addr.addr.network = htons(response.source_proto_addr.addr.network); + response.source_proto_addr.zero = 0x00; - memcpy(&response.dest_proto_addr.addr, dest_at_addr, sizeof(response.dest_proto_addr.addr)); - response.dest_proto_addr.addr.network = htons(response.dest_proto_addr.addr.network); - response.dest_proto_addr.zero = 0x00; + memcpy(&response.dest_proto_addr.addr, dest_at_addr, sizeof(response.dest_proto_addr.addr)); + response.dest_proto_addr.addr.network = htons(response.dest_proto_addr.addr.network); + response.dest_proto_addr.zero = 0x00; - memcpy(response.source_hw_addr.mac, elap_get_mac()->mac, sizeof(response.source_hw_addr.mac)); + memcpy(response.source_hw_addr.mac, elap_get_mac()->mac, sizeof(response.source_hw_addr.mac)); - memcpy(response.dest_hw_addr.mac, &dest_hw_addr->mac, sizeof(response.dest_hw_addr.mac)); + memcpy(response.dest_hw_addr.mac, &dest_hw_addr->mac, sizeof(response.dest_hw_addr.mac)); - if (dest_hw_addr == &HW_ZERO) - elap_send(&HW_APPLETALK_BROADCAST, &SNAP_AARP, sizeof(struct aarp_header_t), (byte*)&response); - else - elap_send(&response.dest_hw_addr, &SNAP_AARP, sizeof(struct aarp_header_t), (byte*)&response); - } + if (dest_hw_addr == &HW_ZERO) + elap_send(&HW_APPLETALK_BROADCAST, &SNAP_AARP, sizeof(struct aarp_header_t), (byte*)&response); + else + elap_send(&response.dest_hw_addr, &SNAP_AARP, sizeof(struct aarp_header_t), (byte*)&response); + } } -void aarp_probe(const struct at_addr_t* addr) -{ - if (addr) - { - aarp_send_packet(AARP_FUNCTION_PROBE, addr, addr, &HW_ZERO); - } +void aarp_probe(const struct at_addr_t* addr) { + if (addr) + { + aarp_send_packet(AARP_FUNCTION_PROBE, addr, addr, &HW_ZERO); + } } -static void aarp_request(const struct at_addr_t* addr) -{ - if (addr) - { - aarp_send_packet(AARP_FUNCTION_REQUEST, atbridge_get_addr(), addr, &HW_ZERO); - } +static void aarp_request(const struct at_addr_t* addr) { + if (addr) + { + aarp_send_packet(AARP_FUNCTION_REQUEST, atbridge_get_addr(), addr, &HW_ZERO); + } } //// -static struct amt_entry_t* amt_lookup_entry_hardware(const struct ether_addr_t* hardware) -{ - if (hardware) - { - struct amt_entry_t* entry = amt; - while (entry) - { - if (memcmp(&entry->hardware, hardware, sizeof(entry->hardware)) == 0) - return entry; - entry = entry->next; - } - } - return 0; +static struct amt_entry_t* amt_lookup_entry_hardware(const struct ether_addr_t* hardware) { + if (hardware) + { + struct amt_entry_t* entry = amt; + while (entry) + { + if (memcmp(&entry->hardware, hardware, sizeof(entry->hardware)) == 0) + return entry; + entry = entry->next; + } + } + return 0; } -static struct amt_entry_t* amt_lookup_entry_protocol(const struct at_addr_t* protocol) -{ - if (protocol) - { - struct amt_entry_t* entry = amt; - while (entry) - { - if (memcmp(&entry->protocol, protocol, sizeof(entry->protocol)) == 0) - return entry; - entry = entry->next; - } - } - return 0; +static struct amt_entry_t* amt_lookup_entry_protocol(const struct at_addr_t* protocol) { + if (protocol) + { + struct amt_entry_t* entry = amt; + while (entry) + { + if (memcmp(&entry->protocol, protocol, sizeof(entry->protocol)) == 0) + return entry; + entry = entry->next; + } + } + return 0; } -static void amt_delete_entry_protocol(const struct at_addr_t* protocol) -{ - if (protocol) - { - struct amt_entry_t* entry = amt; - struct amt_entry_t* previous = amt; - while (entry) - { - if (memcmp(&entry->protocol, protocol, sizeof(entry->protocol)) == 0) - { - previous->next = entry->next; - free(entry); - break; - } - previous = entry; - entry = entry->next; - } - } +static void amt_delete_entry_protocol(const struct at_addr_t* protocol) { + if (protocol) + { + struct amt_entry_t* entry = amt; + struct amt_entry_t* previous = amt; + while (entry) + { + if (memcmp(&entry->protocol, protocol, sizeof(entry->protocol)) == 0) + { + previous->next = entry->next; + free(entry); + break; + } + previous = entry; + entry = entry->next; + } + } } -static void amt_add(const struct at_addr_t* protocol, const struct ether_addr_t* hardware) -{ - // Does an entry matching one of the protocol or hardware addresses exist? If so, update it. - struct amt_entry_t* entry = amt_lookup_entry_protocol(protocol); - if (entry) - { - memcpy(&entry->hardware, hardware, sizeof(entry->hardware)); - return; - } +static void amt_add(const struct at_addr_t* protocol, const struct ether_addr_t* hardware) { + // Does an entry matching one of the protocol or hardware addresses exist? If so, update it. + struct amt_entry_t* entry = amt_lookup_entry_protocol(protocol); + if (entry) + { + memcpy(&entry->hardware, hardware, sizeof(entry->hardware)); + return; + } - entry = amt_lookup_entry_hardware(hardware); - if (entry) - { - memcpy(&entry->protocol, protocol, sizeof(entry->protocol)); - return; - } + entry = amt_lookup_entry_hardware(hardware); + if (entry) + { + memcpy(&entry->protocol, protocol, sizeof(entry->protocol)); + return; + } - // Otherwise, add a new entry. - entry = (struct amt_entry_t*)malloc(sizeof(struct amt_entry_t)); - memcpy(&entry->hardware, hardware, sizeof(entry->hardware)); - memcpy(&entry->protocol, protocol, sizeof(entry->protocol)); - entry->next = amt; - amt = entry; + // Otherwise, add a new entry. + entry = (struct amt_entry_t*)malloc(sizeof(struct amt_entry_t)); + memcpy(&entry->hardware, hardware, sizeof(entry->hardware)); + memcpy(&entry->protocol, protocol, sizeof(entry->protocol)); + entry->next = amt; + amt = entry; } -const struct ether_addr_t* aarp_request_hardware(const struct at_addr_t* protocol) -{ - struct amt_entry_t* entry = amt_lookup_entry_protocol(protocol); - if (entry) - { - aarp_retry_reset(); - return (const struct ether_addr_t*)&entry->hardware; - } - else - { - // The AMT doesn't have this protocol address so issue a request at no more than the AARP_PROBE_INTERVAL period. - if (((clock() - retry_timer) >= (AARP_REQUEST_INTERVAL * CLOCKS_PER_SEC / 1000)) && - (retry_count > 0)) - { - aarp_request(protocol); - - retry_count--; - retry_timer = clock(); - - //atbridge_printf("AARP request count %d timer %d.\n", retry_count, retry_timer); - } +const struct ether_addr_t* aarp_request_hardware(const struct at_addr_t* protocol) { + struct amt_entry_t* entry = amt_lookup_entry_protocol(protocol); + if (entry) + { + aarp_retry_reset(); + return (const struct ether_addr_t*)&entry->hardware; + } + else + { + // The AMT doesn't have this protocol address so issue a request at no more than the AARP_PROBE_INTERVAL period. + if (((clock() - retry_timer) >= (AARP_REQUEST_INTERVAL * CLOCKS_PER_SEC / 1000)) && + (retry_count > 0)) + { + aarp_request(protocol); - return 0; - } + retry_count--; + retry_timer = clock(); + + //atbridge_printf("AARP request count %d timer %d.\n", retry_count, retry_timer); + } + + return 0; + } } -const struct at_addr_t* aarp_request_protocol(const struct ether_addr_t* hardware) -{ - struct amt_entry_t* entry = amt_lookup_entry_hardware(hardware); - if (entry) - return (const struct at_addr_t*)&entry->protocol; - else - return 0; +const struct at_addr_t* aarp_request_protocol(const struct ether_addr_t* hardware) { + struct amt_entry_t* entry = amt_lookup_entry_hardware(hardware); + if (entry) + return (const struct at_addr_t*)&entry->protocol; + else + return 0; } -bool aarp_retry() -{ - return retry_count > 0; +bool aarp_retry() { + return retry_count > 0; } -void aarp_retry_reset() -{ - retry_count = AARP_REQUEST_COUNT; - retry_timer = clock(); +void aarp_retry_reset() { + retry_count = AARP_REQUEST_COUNT; + retry_timer = clock(); } -void aarp_glean(const struct at_addr_t* protocol, const struct ether_addr_t* hardware) -{ - amt_add(protocol, hardware); +void aarp_glean(const struct at_addr_t* protocol, const struct ether_addr_t* hardware) { + amt_add(protocol, hardware); } -bool aarp_address_used(const struct at_addr_t* protocol) -{ - // reference 2-8 - if (protocol) - { - // Check for reserved node numbers, per reference 3-9. - if (protocol->node == 0x00 || protocol->node == 0xfe || protocol->node == 0xff) - return true; +bool aarp_address_used(const struct at_addr_t* protocol) { + // reference 2-8 + if (protocol) + { + // Check for reserved node numbers, per reference 3-9. + if (protocol->node == 0x00 || protocol->node == 0xfe || protocol->node == 0xff) + return true; - // Look for the address in the AMT. If it's there, another node is using this address. - struct amt_entry_t* entry = amt_lookup_entry_protocol(protocol); - if (entry) - return true; + // Look for the address in the AMT. If it's there, another node is using this address. + struct amt_entry_t* entry = amt_lookup_entry_protocol(protocol); + if (entry) + return true; - // Try a probe. If this address is in use, another node will reply with an AARP RESPONSE packet. - // Return true to advise the caller that the address is not known to be in use. The caller should - // retry aarp_try_address() every 200 ms (AARP_PROBE_INTERVAL) and 10 times (AARP_PROBE_COUNT), - // per the AARP protocol definition, before choosing this address. - aarp_probe(protocol); - return false; - } - return false; + // Try a probe. If this address is in use, another node will reply with an AARP RESPONSE packet. + // Return true to advise the caller that the address is not known to be in use. The caller should + // retry aarp_try_address() every 200 ms (AARP_PROBE_INTERVAL) and 10 times (AARP_PROBE_COUNT), + // per the AARP protocol definition, before choosing this address. + aarp_probe(protocol); + return false; + } + return false; } //// -void aarp_handle_packet(const struct aarp_header_t* aarp) -{ - if (aarp && - aarp->hardware_type == AARP_HARDWARE_ETHER && - aarp->protocol_type == AARP_PROTOCOL_TYPE && - aarp->hw_addr_len == AARP_HW_ADDR_LEN && - aarp->protocol_addr_len == AARP_PROTOCOL_ADDR_LEN) - { - switch (aarp->function) - { - case AARP_FUNCTION_REQUEST: - if (((aarp->dest_proto_addr.addr.network == atbridge_get_net()) || - (aarp->dest_proto_addr.addr.network == 0x00 /* reference 4-6 */)) && - (aarp->dest_proto_addr.addr.node == atbridge_get_node())) - { - // Generate a response for the AARP request. - aarp_send_packet(AARP_FUNCTION_RESPONSE, &aarp->dest_proto_addr.addr, &aarp->source_proto_addr.addr, &aarp->source_hw_addr); - } - break; - case AARP_FUNCTION_RESPONSE: - aarp_glean(&aarp->source_proto_addr.addr, &aarp->source_hw_addr); - aarp_glean(&aarp->dest_proto_addr.addr, &aarp->dest_hw_addr); - break; - case AARP_FUNCTION_PROBE: - // AMT entry aging, method 2, reference 2-11 - amt_delete_entry_protocol(&aarp->dest_proto_addr.addr); - break; - default: - break; - } - } +void aarp_handle_packet(const struct aarp_header_t* aarp) { + if (aarp && + aarp->hardware_type == AARP_HARDWARE_ETHER && + aarp->protocol_type == AARP_PROTOCOL_TYPE && + aarp->hw_addr_len == AARP_HW_ADDR_LEN && + aarp->protocol_addr_len == AARP_PROTOCOL_ADDR_LEN) + { + switch (aarp->function) + { + case AARP_FUNCTION_REQUEST: + if (((aarp->dest_proto_addr.addr.network == atbridge_get_net()) || + (aarp->dest_proto_addr.addr.network == 0x00 /* reference 4-6 */)) && + (aarp->dest_proto_addr.addr.node == atbridge_get_node())) + { + // Generate a response for the AARP request. + aarp_send_packet(AARP_FUNCTION_RESPONSE, &aarp->dest_proto_addr.addr, &aarp->source_proto_addr.addr, &aarp->source_hw_addr); + } + break; + case AARP_FUNCTION_RESPONSE: + aarp_glean(&aarp->source_proto_addr.addr, &aarp->source_hw_addr); + aarp_glean(&aarp->dest_proto_addr.addr, &aarp->dest_hw_addr); + break; + case AARP_FUNCTION_PROBE: + // AMT entry aging, method 2, reference 2-11 + amt_delete_entry_protocol(&aarp->dest_proto_addr.addr); + break; + default: + break; + } + } } diff --git a/src/atbridge/atbridge.c b/src/atbridge/atbridge.c index b1bfbcf..f908f2c 100644 --- a/src/atbridge/atbridge.c +++ b/src/atbridge/atbridge.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /** This module is the "heart" of the bridge and provides the connection between the ELAP and LLAP ports. **/ @@ -37,378 +37,357 @@ static const at_node_t NODE_STARTUP_HIGH = 0xFE; static void send_rtmp_request(); -bool atbridge_init() -{ - // If the GS reboots, we may try to reinitialize the bridge. If this is the case, keep the old address and AMT. - if (local_address.network == 0) - { - // Obtain a provisional node address and startup range network. - // - // This isn't correct for an extended network (like ELAP) but works adequately on small networks. - // The bridge should follow the complicated process on page 4-9 to obtain the network and node number. - srand((unsigned int)time(0)); - local_address.network = (at_network_t)((double)rand()/RAND_MAX * (NET_STARTUP_HIGH - NET_STARTUP_LOW) + NET_STARTUP_LOW); - local_address.node = (at_node_t)((double)rand()/RAND_MAX + (NODE_STARTUP_HIGH - NODE_STARTUP_LOW) + 0x01); +bool atbridge_init() { + // If the GS reboots, we may try to reinitialize the bridge. If this is the case, keep the old address and AMT. + if (local_address.network == 0) + { + // Obtain a provisional node address and startup range network. + // + // This isn't correct for an extended network (like ELAP) but works adequately on small networks. + // The bridge should follow the complicated process on page 4-9 to obtain the network and node number. + srand((unsigned int)time(0)); + local_address.network = (at_network_t)((double)rand()/RAND_MAX * (NET_STARTUP_HIGH - NET_STARTUP_LOW) + NET_STARTUP_LOW); + local_address.node = (at_node_t)((double)rand()/RAND_MAX + (NODE_STARTUP_HIGH - NODE_STARTUP_LOW) + 0x01); - aarp_init(); - llap_init(); - if (!elap_init()) - { - atbridge_shutdown(); - return false; - } - } - return true; + aarp_init(); + llap_init(); + if (!elap_init()) + { + atbridge_shutdown(); + return false; + } + } + return true; } -void atbridge_shutdown() -{ - llap_shutdown(); - elap_shutdown(); - aarp_shutdown(); +void atbridge_shutdown() { + llap_shutdown(); + elap_shutdown(); + aarp_shutdown(); } -void atbridge_set_diagnostics(bool enabled) -{ - diagnostics = enabled; +void atbridge_set_diagnostics(bool enabled) { + diagnostics = enabled; } -bool atbridge_get_diagnostics() -{ - return diagnostics; +bool atbridge_get_diagnostics() { + return diagnostics; } -void atbridge_printf(const char *fmt, ...) -{ - if (atbridge_get_diagnostics()) - { - va_list args; - va_start(args, fmt); - vprintf(fmt, args); - va_end(args); - } +void atbridge_printf(const char *fmt, ...) { + if (atbridge_get_diagnostics()) + { + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); + } } -const struct at_addr_t* atbridge_get_addr() -{ - return &local_address; +const struct at_addr_t* atbridge_get_addr() { + return &local_address; } -const at_network_t atbridge_get_net() -{ - return local_address.network; +const at_network_t atbridge_get_net() { + return local_address.network; } -const at_node_t atbridge_get_node() -{ - return local_address.node; +const at_node_t atbridge_get_node() { + return local_address.node; } -void atbridge_set_net(at_network_t net) -{ - local_address.network = net; +void atbridge_set_net(at_network_t net) { + local_address.network = net; } -void atbridge_set_node(at_node_t node) -{ - local_address.node = node; +void atbridge_set_node(at_node_t node) { + local_address.node = node; } -bool atbridge_address_used(const struct at_addr_t* addr) -{ - if (!sent_rtmp_request) - send_rtmp_request(); - return aarp_address_used(addr); +bool atbridge_address_used(const struct at_addr_t* addr) { + if (!sent_rtmp_request) + send_rtmp_request(); + return aarp_address_used(addr); } /* Calculate a DDP checksum, per Apple's documented algorithm in 4-17 of "Inside AppleTalk". */ -static word16 get_checksum(size_t size, byte data[]) -{ - word16 cksum = 0; - for (unsigned int i = 0; i < size; i++) - { - cksum += data[i]; - cksum = (cksum << 1) | ((cksum & 0x8000) >> 15); // roll left - } - if (cksum == 0) - cksum = 0xffff; - return cksum; +static word16 get_checksum(size_t size, byte data[]) { + word16 cksum = 0; + for (unsigned int i = 0; i < size; i++) + { + cksum += data[i]; + cksum = (cksum << 1) | ((cksum & 0x8000) >> 15); // roll left + } + if (cksum == 0) + cksum = 0xffff; + return cksum; } -static void calculate_checksum(struct packet_t* packet) -{ - if (packet && packet->data && (packet->size >= sizeof(struct DDP_LONG)) && (packet->type == LAP_DDP_LONG)) - { - struct DDP_LONG* header = (struct DDP_LONG*)(packet->data); - header->checksum = htons(get_checksum( - packet->size - offsetof(struct DDP_LONG, dest_net), - (byte*)&header->dest_net)); - } +static void calculate_checksum(struct packet_t* packet) { + if (packet && packet->data && (packet->size >= sizeof(struct DDP_LONG)) && (packet->type == LAP_DDP_LONG)) + { + struct DDP_LONG* header = (struct DDP_LONG*)(packet->data); + header->checksum = htons(get_checksum( + packet->size - offsetof(struct DDP_LONG, dest_net), + (byte*)&header->dest_net)); + } } /* Convert a long-form DDP header to a short-form header. This function only converts the headers. */ -static word16 convert_ddp_header_to_short(const struct DDP_LONG* in, struct DDP_SHORT* out) -{ - word16 size; +static word16 convert_ddp_header_to_short(const struct DDP_LONG* in, struct DDP_SHORT* out) { + word16 size; - if (!in || !out) - return 0; - - size = ((in->length[0] & 0x3) << 8) + (in->length[1]) - (sizeof(struct DDP_LONG) - sizeof(struct DDP_SHORT)); + if (!in || !out) + return 0; - out->length[0] = (size >> 8) & 0x03; - out->length[1] = size & 0xff; + size = ((in->length[0] & 0x3) << 8) + (in->length[1]) - (sizeof(struct DDP_LONG) - sizeof(struct DDP_SHORT)); - out->dest_socket = in->dest_socket; - out->source_socket = in->source_socket; - - out->type = in->type; + out->length[0] = (size >> 8) & 0x03; + out->length[1] = size & 0xff; - return size; + out->dest_socket = in->dest_socket; + out->source_socket = in->source_socket; + + out->type = in->type; + + return size; } /* Convert a short-form DDP header to a long-form header. ELAP requires long-form, but LLAP often uses short-form. */ /* This function only converts the headers. */ -static word16 convert_ddp_header_to_long(const struct at_addr_t dest, const struct at_addr_t source, const struct DDP_SHORT* in, struct DDP_LONG* out) -{ - word16 size; +static word16 convert_ddp_header_to_long(const struct at_addr_t dest, const struct at_addr_t source, const struct DDP_SHORT* in, struct DDP_LONG* out) { + word16 size; - if (!in || !out) - return 0; - - size = ((in->length[0] & 0x3) << 8) + (in->length[1]) + (sizeof(struct DDP_LONG) - sizeof(struct DDP_SHORT)); - out->length[0] = (size >> 8) & 0x03; - out->length[1] = size & 0xff; - - out->checksum = 0x0000; /* 0x0000 == no checksum calculated, reference 4-17 */ - - if (dest.network) - out->dest_net = dest.network; - else - out->dest_net = atbridge_get_net(); - out->dest_net = (at_network_t)htons(out->dest_net); + if (!in || !out) + return 0; - if (source.network) - out->source_net = source.network; - else - out->source_net = atbridge_get_net(); - out->source_net = (at_network_t)htons(out->source_net); + size = ((in->length[0] & 0x3) << 8) + (in->length[1]) + (sizeof(struct DDP_LONG) - sizeof(struct DDP_SHORT)); + out->length[0] = (size >> 8) & 0x03; + out->length[1] = size & 0xff; - out->dest_node = dest.node; - out->source_node = source.node; + out->checksum = 0x0000; /* 0x0000 == no checksum calculated, reference 4-17 */ - out->dest_socket = in->dest_socket; - out->source_socket = in->source_socket; + if (dest.network) + out->dest_net = dest.network; + else + out->dest_net = atbridge_get_net(); + out->dest_net = (at_network_t)htons(out->dest_net); - out->type = in->type; + if (source.network) + out->source_net = source.network; + else + out->source_net = atbridge_get_net(); + out->source_net = (at_network_t)htons(out->source_net); - return size; + out->dest_node = dest.node; + out->source_node = source.node; + + out->dest_socket = in->dest_socket; + out->source_socket = in->source_socket; + + out->type = in->type; + + return size; } /* Convert a short-form DDP packet to a long-form packet. */ /* This function converts an entire packet, not just the header. */ -static void convert_ddp_packet_to_long(struct packet_t* packet) -{ - if (packet && (packet->type == LAP_DDP_SHORT) && packet->data && (packet->size >= sizeof(struct DDP_SHORT))) - { - struct DDP_SHORT* header_short = (struct DDP_SHORT*)packet->data; +static void convert_ddp_packet_to_long(struct packet_t* packet) { + if (packet && (packet->type == LAP_DDP_SHORT) && packet->data && (packet->size >= sizeof(struct DDP_SHORT))) + { + struct DDP_SHORT* header_short = (struct DDP_SHORT*)packet->data; - const size_t payload_size = packet->size - sizeof(struct DDP_SHORT); - byte* data = (byte*)malloc(payload_size + sizeof(struct DDP_LONG)); - struct DDP_LONG* header_long = (struct DDP_LONG*)data; + const size_t payload_size = packet->size - sizeof(struct DDP_SHORT); + byte* data = (byte*)malloc(payload_size + sizeof(struct DDP_LONG)); + struct DDP_LONG* header_long = (struct DDP_LONG*)data; - const word16 size = convert_ddp_header_to_long(packet->dest, packet->source, header_short, header_long); - packet->dest.network = ntohs(header_long->dest_net); - packet->source.network = ntohs(header_long->source_net); + const word16 size = convert_ddp_header_to_long(packet->dest, packet->source, header_short, header_long); + packet->dest.network = ntohs(header_long->dest_net); + packet->source.network = ntohs(header_long->source_net); - memcpy(data + sizeof(struct DDP_LONG), packet->data + sizeof(struct DDP_SHORT), payload_size); + memcpy(data + sizeof(struct DDP_LONG), packet->data + sizeof(struct DDP_SHORT), payload_size); - packet->type = LAP_DDP_LONG; - packet->size = size; + packet->type = LAP_DDP_LONG; + packet->size = size; - // Replace the original short-form packet data. - free(packet->data); - packet->data = data; + // Replace the original short-form packet data. + free(packet->data); + packet->data = data; - calculate_checksum(packet); - } + calculate_checksum(packet); + } } /* Convert a long-form DDP packet to short-form. */ -static void convert_ddp_packet_to_short(struct packet_t* packet) -{ - if (packet && (packet->type == LAP_DDP_LONG) && packet->data) - { - struct DDP_LONG* header_long = (struct DDP_LONG*)packet->data; +static void convert_ddp_packet_to_short(struct packet_t* packet) { + if (packet && (packet->type == LAP_DDP_LONG) && packet->data) + { + struct DDP_LONG* header_long = (struct DDP_LONG*)packet->data; - const size_t payload_size = packet->size - sizeof(struct DDP_LONG); - byte* data = (byte*)malloc(payload_size + sizeof(struct DDP_SHORT)); - struct DDP_SHORT* header_short = (struct DDP_SHORT*)data; + const size_t payload_size = packet->size - sizeof(struct DDP_LONG); + byte* data = (byte*)malloc(payload_size + sizeof(struct DDP_SHORT)); + struct DDP_SHORT* header_short = (struct DDP_SHORT*)data; - const word16 size = convert_ddp_header_to_short(header_long, header_short); + const word16 size = convert_ddp_header_to_short(header_long, header_short); - memcpy(data + sizeof(struct DDP_SHORT), packet->data + sizeof(struct DDP_LONG), payload_size); + memcpy(data + sizeof(struct DDP_SHORT), packet->data + sizeof(struct DDP_LONG), payload_size); - packet->type = LAP_DDP_SHORT; - packet->size = size; + packet->type = LAP_DDP_SHORT; + packet->size = size; - free(packet->data); - packet->data = data; - } + free(packet->data); + packet->data = data; + } } /*static void convert_rtmp_to_extended(struct packet_t* packet) -{ - if (packet && (packet->type == LAP_DDP_SHORT) && packet->data) - { - struct DDP_SHORT* header_short = (struct DDP_SHORT*)packet->data; - if (header_short->type != DDP_TYPE_RTMP || header_short->dest_socket != DDP_SOCKET_RTMP) - return; + { + if (packet && (packet->type == LAP_DDP_SHORT) && packet->data) + { + struct DDP_SHORT* header_short = (struct DDP_SHORT*)packet->data; + if (header_short->type != DDP_TYPE_RTMP || header_short->dest_socket != DDP_SOCKET_RTMP) + return; - struct rtmp_nonextended_data_t* in = (struct rtmp_nonextended_data_t*)(packet->data + sizeof(struct DDP_SHORT)); + struct rtmp_nonextended_data_t* in = (struct rtmp_nonextended_data_t*)(packet->data + sizeof(struct DDP_SHORT)); - // Construct a new long-form DDP packet header. - size_t size = sizeof(struct DDP_LONG) + sizeof(struct rtmp_extended_data_t); - byte* data = (byte*)malloc(size); - struct DDP_LONG* header_long = (struct DDP_LONG*)data; - convert_ddp_header_to_long(packet->dest, packet->source, header_short, header_long); - - struct rtmp_extended_data_t* out = (struct rtmp_extended_data_t*)(data + sizeof(struct DDP_LONG)); - out->net = in->net; - out->id_length = in->id_length; - out->node = in->node; + // Construct a new long-form DDP packet header. + size_t size = sizeof(struct DDP_LONG) + sizeof(struct rtmp_extended_data_t); + byte* data = (byte*)malloc(size); + struct DDP_LONG* header_long = (struct DDP_LONG*)data; + convert_ddp_header_to_long(packet->dest, packet->source, header_short, header_long); - // Copy the routing tuples. - struct rtmp_nonextended_tuple_t* in_tuple = (struct rtmp_nonextended_tuple_t*)(packet->data + sizeof(struct DDP_SHORT) + sizeof(struct rtmp_nonextended_data_t)); - struct rtmp_extended_tuple_t* out_tuple = (struct rtmp_extended_tuple_t*)(data + size); - while ((byte*)in_tuple < (packet->data + packet->size)) - { - size += sizeof(struct rtmp_extended_tuple_t); - realloc(data, size); - out_tuple->range_start = in_tuple->net; - out_tuple->distance = in_tuple->distance | 0x80; - out_tuple->range_end = in_tuple->net; - out_tuple->delimiter = RTMP_TUPLE_DELIMITER; - in_tuple++; - } + struct rtmp_extended_data_t* out = (struct rtmp_extended_data_t*)(data + sizeof(struct DDP_LONG)); + out->net = in->net; + out->id_length = in->id_length; + out->node = in->node; - free(packet->data); - packet->data = data; - packet->size = size; - packet->type = LAP_DDP_LONG; - } -}*/ + // Copy the routing tuples. + struct rtmp_nonextended_tuple_t* in_tuple = (struct rtmp_nonextended_tuple_t*)(packet->data + sizeof(struct DDP_SHORT) + sizeof(struct rtmp_nonextended_data_t)); + struct rtmp_extended_tuple_t* out_tuple = (struct rtmp_extended_tuple_t*)(data + size); + while ((byte*)in_tuple < (packet->data + packet->size)) + { + size += sizeof(struct rtmp_extended_tuple_t); + realloc(data, size); + out_tuple->range_start = in_tuple->net; + out_tuple->distance = in_tuple->distance | 0x80; + out_tuple->range_end = in_tuple->net; + out_tuple->delimiter = RTMP_TUPLE_DELIMITER; + in_tuple++; + } -static void convert_rtmp_to_nonextended(struct packet_t* packet) -{ - if (packet && (packet->type == LAP_DDP_LONG) && packet->data) - { - struct DDP_LONG* header_long = (struct DDP_LONG*)packet->data; - if (header_long->type != DDP_TYPE_RTMP || header_long->dest_socket != DDP_SOCKET_RTMP) - return; + free(packet->data); + packet->data = data; + packet->size = size; + packet->type = LAP_DDP_LONG; + } + }*/ - struct rtmp_extended_data_t* in = (struct rtmp_extended_data_t*)(packet->data + sizeof(struct DDP_LONG)); +static void convert_rtmp_to_nonextended(struct packet_t* packet) { + if (packet && (packet->type == LAP_DDP_LONG) && packet->data) + { + struct DDP_LONG* header_long = (struct DDP_LONG*)packet->data; + if (header_long->type != DDP_TYPE_RTMP || header_long->dest_socket != DDP_SOCKET_RTMP) + return; - size_t size = sizeof(struct DDP_SHORT) + sizeof(struct rtmp_nonextended_response_t); - byte* data = (byte*)malloc(size); - struct DDP_SHORT* header_short = (struct DDP_SHORT*)data; - convert_ddp_header_to_short(header_long, header_short); - header_short->length[0] = (size >> 8) & 0x03; - header_short->length[1] = size & 0xff; + struct rtmp_extended_data_t* in = (struct rtmp_extended_data_t*)(packet->data + sizeof(struct DDP_LONG)); - struct rtmp_nonextended_response_t* out = (struct rtmp_nonextended_response_t*)(data + sizeof(struct DDP_SHORT)); - out->net = in->net; - out->id_length = in->id_length; - out->node = in->node; + size_t size = sizeof(struct DDP_SHORT) + sizeof(struct rtmp_nonextended_response_t); + byte* data = (byte*)malloc(size); + struct DDP_SHORT* header_short = (struct DDP_SHORT*)data; + convert_ddp_header_to_short(header_long, header_short); + header_short->length[0] = (size >> 8) & 0x03; + header_short->length[1] = size & 0xff; - /*rtmp_extended_tuple_t* in_tuple = (rtmp_extended_tuple_t*)(packet->data + sizeof(DDP_LONG) + sizeof(rtmp_extended_data_t)); - rtmp_nonextended_tuple_t* out_tuple = (rtmp_nonextended_tuple_t*)(data + size); - while ((byte*)in_tuple < (packet->data + packet->size)) - { - size += sizeof(rtmp_nonextended_tuple_t); - realloc(data, size); - out_tuple->net = in_tuple->range_start; - out_tuple->distance = in_tuple->distance & 0x7f; - in_tuple++; - }*/ + struct rtmp_nonextended_response_t* out = (struct rtmp_nonextended_response_t*)(data + sizeof(struct DDP_SHORT)); + out->net = in->net; + out->id_length = in->id_length; + out->node = in->node; - free(packet->data); - packet->data = data; - packet->size = size; - packet->type = LAP_DDP_SHORT; - } + /*rtmp_extended_tuple_t* in_tuple = (rtmp_extended_tuple_t*)(packet->data + sizeof(DDP_LONG) + sizeof(rtmp_extended_data_t)); + rtmp_nonextended_tuple_t* out_tuple = (rtmp_nonextended_tuple_t*)(data + size); + while ((byte*)in_tuple < (packet->data + packet->size)) + { + size += sizeof(rtmp_nonextended_tuple_t); + realloc(data, size); + out_tuple->net = in_tuple->range_start; + out_tuple->distance = in_tuple->distance & 0x7f; + in_tuple++; + }*/ + + free(packet->data); + packet->data = data; + packet->size = size; + packet->type = LAP_DDP_SHORT; + } } /* Learn our network number from RTMP packets. */ /* "Inside AppleTalk", section 4-8, describes this approach for non-extended networks. Technically, we probably should be doing the more complicated extended network approach (also on 4-8), but the easy approach using RTMP seems adequate for now. */ -static void glean_net_from_rtmp(struct packet_t* packet) -{ - if (packet && (packet->type == LAP_DDP_LONG) && packet->data) - { - struct DDP_LONG* header_long = (struct DDP_LONG*)packet->data; - if (header_long->type != DDP_TYPE_RTMP || header_long->dest_socket != DDP_SOCKET_RTMP) - return; +static void glean_net_from_rtmp(struct packet_t* packet) { + if (packet && (packet->type == LAP_DDP_LONG) && packet->data) + { + struct DDP_LONG* header_long = (struct DDP_LONG*)packet->data; + if (header_long->type != DDP_TYPE_RTMP || header_long->dest_socket != DDP_SOCKET_RTMP) + return; - struct rtmp_extended_data_t* in = (struct rtmp_extended_data_t*)(packet->data + sizeof(struct DDP_LONG)); + struct rtmp_extended_data_t* in = (struct rtmp_extended_data_t*)(packet->data + sizeof(struct DDP_LONG)); - atbridge_set_net(ntohs(in->net)); - } + atbridge_set_net(ntohs(in->net)); + } } -static void send_rtmp_request() -{ - struct packet_t* packet = (struct packet_t*)malloc(sizeof(struct packet_t)); - - packet->type = LAP_DDP_LONG; - packet->dest.network = atbridge_get_net(); - packet->dest.node = 255; - packet->source.network = atbridge_get_net(); - packet->source.node = atbridge_get_node(); - packet->next = 0; - packet->size = sizeof(struct DDP_LONG) + sizeof(struct rtmp_request_t); - packet->data = (byte*)malloc(packet->size); +static void send_rtmp_request() { + struct packet_t* packet = (struct packet_t*)malloc(sizeof(struct packet_t)); - struct DDP_LONG* header = (struct DDP_LONG*)packet->data; - header->type = DDP_TYPE_RTMP_REQUEST; - header->source_net = htons(packet->source.network); - header->source_node = packet->source.node; - header->source_socket = DDP_SOCKET_RTMP; - header->dest_net = htons(packet->dest.network); - header->dest_node = packet->dest.node; - header->dest_socket = DDP_SOCKET_RTMP; - header->length[0] = (packet->size >> 8) & 0x03; - header->length[1] = packet->size & 0xff; + packet->type = LAP_DDP_LONG; + packet->dest.network = atbridge_get_net(); + packet->dest.node = 255; + packet->source.network = atbridge_get_net(); + packet->source.node = atbridge_get_node(); + packet->next = 0; + packet->size = sizeof(struct DDP_LONG) + sizeof(struct rtmp_request_t); + packet->data = (byte*)malloc(packet->size); - struct rtmp_request_t* request = (struct rtmp_request_t*)(packet->data + sizeof(struct DDP_LONG)); - request->function = RTMP_FUNCTION_REQUEST; + struct DDP_LONG* header = (struct DDP_LONG*)packet->data; + header->type = DDP_TYPE_RTMP_REQUEST; + header->source_net = htons(packet->source.network); + header->source_node = packet->source.node; + header->source_socket = DDP_SOCKET_RTMP; + header->dest_net = htons(packet->dest.network); + header->dest_node = packet->dest.node; + header->dest_socket = DDP_SOCKET_RTMP; + header->length[0] = (packet->size >> 8) & 0x03; + header->length[1] = packet->size & 0xff; - calculate_checksum(packet); - - elap_enqueue_out(packet); - sent_rtmp_request = true; + struct rtmp_request_t* request = (struct rtmp_request_t*)(packet->data + sizeof(struct DDP_LONG)); + request->function = RTMP_FUNCTION_REQUEST; + + calculate_checksum(packet); + + elap_enqueue_out(packet); + sent_rtmp_request = true; } -void atbridge_process() -{ - elap_process(); - //llap_process(); - - struct packet_t* packet = elap_dequeue_in(); - if (packet) - { - glean_net_from_rtmp(packet); - convert_rtmp_to_nonextended(packet); - // The GS should understand long-form DDP, but converting to short-form ought to slightly improve performance (fewer bytes for the GS to process). - convert_ddp_packet_to_short(packet); - llap_enqueue_out(packet); - } - packet = llap_dequeue_in(); - if (packet) - { - // ELAP does not support short-form DDP, so convert such packets to long-form. - convert_ddp_packet_to_long(packet); - elap_enqueue_out(packet); - } +void atbridge_process() { + elap_process(); + //llap_process(); + + struct packet_t* packet = elap_dequeue_in(); + if (packet) + { + glean_net_from_rtmp(packet); + convert_rtmp_to_nonextended(packet); + // The GS should understand long-form DDP, but converting to short-form ought to slightly improve performance (fewer bytes for the GS to process). + convert_ddp_packet_to_short(packet); + llap_enqueue_out(packet); + } + packet = llap_dequeue_in(); + if (packet) + { + // ELAP does not support short-form DDP, so convert such packets to long-form. + convert_ddp_packet_to_long(packet); + elap_enqueue_out(packet); + } } diff --git a/src/atbridge/elap.c b/src/atbridge/elap.c index 856da27..3774df4 100644 --- a/src/atbridge/elap.c +++ b/src/atbridge/elap.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /** This module implements the ELAP port of the bridge. **/ @@ -37,350 +37,338 @@ static struct packet_port_t elap_port; static struct ether_addr_t HW_LOCAL; /*static void dump_device_list(pcap_if_t* devices) -{ - int i = 0; - for(pcap_if_t* device = devices; device; device = device->next) - { - printf("%d. %s", ++i, device->name); - if (device->description) - printf(" (%s)\n", device->description); - else - printf(" (No description available)\n"); - } -}*/ + { + int i = 0; + for(pcap_if_t* device = devices; device; device = device->next) + { + printf("%d. %s", ++i, device->name); + if (device->description) + printf(" (%s)\n", device->description); + else + printf(" (No description available)\n"); + } + }*/ -static void elap_clone_host_mac(pcap_if_t* device) -{ - if (!device) - return; +static void elap_clone_host_mac(pcap_if_t* device) { + if (!device) + return; #ifdef WIN32 - //// - // Extract the device GUID, which Windows uses to identify the device. - char* name = device->name; - while (name && *name != '{') - name++; - size_t guidLen = strlen(name); + //// + // Extract the device GUID, which Windows uses to identify the device. + char* name = device->name; + while (name && *name != '{') + name++; + size_t guidLen = strlen(name); - //// - // Find and copy the device MAC address. - ULONG size = sizeof(IP_ADAPTER_ADDRESSES) * 15; - IP_ADAPTER_ADDRESSES* addresses = malloc(size); - - ULONG result = GetAdaptersAddresses(AF_UNSPEC, - GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER, NULL, addresses, &size); - - if (result == ERROR_BUFFER_OVERFLOW) - { - // The addresses buffer is too small. Allocate a bigger buffer. - free(addresses); - addresses = malloc(size); - result = GetAdaptersAddresses(AF_UNSPEC, - GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER, NULL, addresses, &size); - } + //// + // Find and copy the device MAC address. + ULONG size = sizeof(IP_ADAPTER_ADDRESSES) * 15; + IP_ADAPTER_ADDRESSES* addresses = malloc(size); - if (result == NO_ERROR) - { - // Search for the desired adapter address. - IP_ADAPTER_ADDRESSES* current = addresses; - while (current) - { - if (current->PhysicalAddressLength == ETHER_ADDR_LEN && memcmp(current->AdapterName, name, guidLen) == 0) - { - memcpy(&HW_LOCAL.mac, ¤t->PhysicalAddress, sizeof(HW_LOCAL.mac)); - break; - } - current = current->Next; - } - } - else - { - halt_printf("ATBridge: Failed to find host MAC address (%d).", result); - } + ULONG result = GetAdaptersAddresses(AF_UNSPEC, + GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER, NULL, addresses, &size); - free(addresses); + if (result == ERROR_BUFFER_OVERFLOW) + { + // The addresses buffer is too small. Allocate a bigger buffer. + free(addresses); + addresses = malloc(size); + result = GetAdaptersAddresses(AF_UNSPEC, + GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER, NULL, addresses, &size); + } + + if (result == NO_ERROR) + { + // Search for the desired adapter address. + IP_ADAPTER_ADDRESSES* current = addresses; + while (current) + { + if (current->PhysicalAddressLength == ETHER_ADDR_LEN && memcmp(current->AdapterName, name, guidLen) == 0) + { + memcpy(&HW_LOCAL.mac, ¤t->PhysicalAddress, sizeof(HW_LOCAL.mac)); + break; + } + current = current->Next; + } + } + else + { + halt_printf("ATBridge: Failed to find host MAC address (%d).", result); + } + + free(addresses); #else #ifdef AF_PACKET - struct pcap_addr* address; - for (address = device->addresses; address != 0; address = address->next) - if (address->addr->sa_family == AF_PACKET) - { - struct sockaddr_ll* ll = (struct sockaddr_ll*)address->addr; - memcpy(&HW_LOCAL.mac, ll->sll_addr, sizeof(HW_LOCAL.mac)); - } + struct pcap_addr* address; + for (address = device->addresses; address != 0; address = address->next) + if (address->addr->sa_family == AF_PACKET) + { + struct sockaddr_ll* ll = (struct sockaddr_ll*)address->addr; + memcpy(&HW_LOCAL.mac, ll->sll_addr, sizeof(HW_LOCAL.mac)); + } #endif #endif } -const struct ether_addr_t* elap_get_mac() -{ - return &HW_LOCAL; +const struct ether_addr_t* elap_get_mac() { + return &HW_LOCAL; } -bool elap_init() -{ - port_init(&elap_port); +bool elap_init() { + port_init(&elap_port); - memcpy(&HW_LOCAL, &HW_LOCAL_DEFAULT, sizeof(HW_LOCAL)); + memcpy(&HW_LOCAL, &HW_LOCAL_DEFAULT, sizeof(HW_LOCAL)); - pcap_if_t* device; - pcap_if_t* alldevs; - int i = 0; - - char errbuf[PCAP_ERRBUF_SIZE]; - - // Load the PCAP library. - if (!pcapdelay_load()) - { - halt_printf("ATBridge: PCAP not available.\n"); - return false; - } + pcap_if_t* device; + pcap_if_t* alldevs; + int i = 0; - // Retrieve the device list. - if(pcapdelay_findalldevs(&alldevs, errbuf) == -1) - { - atbridge_printf("ATBridge: Error enumerating PCAP devices: %s\n", errbuf); - return false; - } - //dump_device_list(alldevs); + char errbuf[PCAP_ERRBUF_SIZE]; - // Jump to the selected adapter. - for (device = alldevs, i = 0; i < g_ethernet_interface; device = device->next, i++); - if (!device) - { - halt_printf("ATBridge: PCAP device not found. Check interface number in settings.\n"); - return false; - } + // Load the PCAP library. + if (!pcapdelay_load()) + { + halt_printf("ATBridge: PCAP not available.\n"); + return false; + } - // Clone the MAC address of the underlying interface. In certain configurations (e.g. Windows with an MS Loopback - // interface), the interface, even in promiscous mode, filters foreign MAC addresses. - elap_clone_host_mac(device); + // Retrieve the device list. + if(pcapdelay_findalldevs(&alldevs, errbuf) == -1) + { + atbridge_printf("ATBridge: Error enumerating PCAP devices: %s\n", errbuf); + return false; + } + //dump_device_list(alldevs); - // Open the adapter, - if ((pcap_session = pcapdelay_open_live(device->name, // name of the device - 65536, // portion of the packet to capture. - // 65536 grants that the whole packet will be captured on all the MACs. - 1, // promiscuous mode (nonzero means promiscuous) - 1, // read timeout - errbuf // error buffer - )) == NULL) - { - halt_printf("ATBridge: Unable to open the adapter. Pcap does not support %s.\n", device->name); - pcapdelay_freealldevs(alldevs); - return false; - } + // Jump to the selected adapter. + for (device = alldevs, i = 0; i < g_ethernet_interface; device = device->next, i++); + if (!device) + { + halt_printf("ATBridge: PCAP device not found. Check interface number in settings.\n"); + return false; + } - // The device must support Ethernet because the bridge "speaks" EtherTalk. - if (pcapdelay_datalink(pcap_session) == DLT_EN10MB) - { - pcapdelay_setnonblock(pcap_session, 1, errbuf); + // Clone the MAC address of the underlying interface. In certain configurations (e.g. Windows with an MS Loopback + // interface), the interface, even in promiscous mode, filters foreign MAC addresses. + elap_clone_host_mac(device); - atbridge_printf("ATBridge: AppleTalk bridging using network device '%s' with Ethernet address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X.\n", - device->description, HW_LOCAL.mac[0], HW_LOCAL.mac[1], HW_LOCAL.mac[2], HW_LOCAL.mac[3], HW_LOCAL.mac[4], HW_LOCAL.mac[5]); - - pcapdelay_freealldevs(alldevs); - return true; - } - else - { - pcapdelay_close(pcap_session); - pcap_session = 0; - halt_printf("ATBridge: Selected network device %s must support Ethernet.\n", device->description); - pcapdelay_freealldevs(alldevs); - return false; - } + // Open the adapter, + if ((pcap_session = pcapdelay_open_live(device->name, // name of the device + 65536, // portion of the packet to capture. + // 65536 grants that the whole packet will be captured on all the MACs. + 1, // promiscuous mode (nonzero means promiscuous) + 1, // read timeout + errbuf // error buffer + )) == NULL) + { + halt_printf("ATBridge: Unable to open the adapter. Pcap does not support %s.\n", device->name); + pcapdelay_freealldevs(alldevs); + return false; + } + + // The device must support Ethernet because the bridge "speaks" EtherTalk. + if (pcapdelay_datalink(pcap_session) == DLT_EN10MB) + { + pcapdelay_setnonblock(pcap_session, 1, errbuf); + + atbridge_printf("ATBridge: AppleTalk bridging using network device '%s' with Ethernet address %.2X:%.2X:%.2X:%.2X:%.2X:%.2X.\n", + device->description, HW_LOCAL.mac[0], HW_LOCAL.mac[1], HW_LOCAL.mac[2], HW_LOCAL.mac[3], HW_LOCAL.mac[4], HW_LOCAL.mac[5]); + + pcapdelay_freealldevs(alldevs); + return true; + } + else + { + pcapdelay_close(pcap_session); + pcap_session = 0; + halt_printf("ATBridge: Selected network device %s must support Ethernet.\n", device->description); + pcapdelay_freealldevs(alldevs); + return false; + } } -void elap_shutdown() -{ - port_shutdown(&elap_port); - if (pcap_session) - { - pcapdelay_close(pcap_session); - pcap_session = 0; - } - pcapdelay_unload(); +void elap_shutdown() { + port_shutdown(&elap_port); + if (pcap_session) + { + pcapdelay_close(pcap_session); + pcap_session = 0; + } + pcapdelay_unload(); } -void elap_enqueue_out(struct packet_t* packet) -{ - enqueue_packet(&elap_port.out, packet); +void elap_enqueue_out(struct packet_t* packet) { + enqueue_packet(&elap_port.out, packet); } -struct packet_t* elap_dequeue_in() -{ - return dequeue(&elap_port.in); +struct packet_t* elap_dequeue_in() { + return dequeue(&elap_port.in); } -void elap_send(const struct ether_addr_t* dest, const struct snap_discriminator_t* discriminator, size_t size, byte data[]) -{ - if (pcap_session && dest && discriminator) - { - // Allocate heap space for the frame. - const size_t frame_size = sizeof(struct ethernet_header_t) + sizeof(struct snap_header_t) + size; - u_char* frame_data; - size_t pad = 0; - if (frame_size < ETHER_MIN_SIZE) - pad = ETHER_MIN_SIZE - frame_size; - frame_data = (u_char*)malloc(frame_size + pad); +void elap_send(const struct ether_addr_t* dest, const struct snap_discriminator_t* discriminator, size_t size, byte data[]) { + if (pcap_session && dest && discriminator) + { + // Allocate heap space for the frame. + const size_t frame_size = sizeof(struct ethernet_header_t) + sizeof(struct snap_header_t) + size; + u_char* frame_data; + size_t pad = 0; + if (frame_size < ETHER_MIN_SIZE) + pad = ETHER_MIN_SIZE - frame_size; + frame_data = (u_char*)malloc(frame_size + pad); - // Build the 802.3 header. - struct ethernet_header_t* ether = (struct ethernet_header_t*)frame_data; - memcpy(ether->dest.mac, dest, sizeof(ether->dest.mac)); - memcpy(ether->source.mac, HW_LOCAL.mac, sizeof(ether->source.mac)); - ether->length = htons(frame_size - sizeof(struct ethernet_header_t)); + // Build the 802.3 header. + struct ethernet_header_t* ether = (struct ethernet_header_t*)frame_data; + memcpy(ether->dest.mac, dest, sizeof(ether->dest.mac)); + memcpy(ether->source.mac, HW_LOCAL.mac, sizeof(ether->source.mac)); + ether->length = htons(frame_size - sizeof(struct ethernet_header_t)); - // Build the 802.2 header. - struct snap_header_t* snap = (struct snap_header_t*)(frame_data + sizeof(struct ethernet_header_t)); - snap->dsap = SNAP_DSAP; - snap->ssap = SNAP_SSAP; - snap->control = SNAP_CONTROL; - memcpy(&snap->discriminator, discriminator, sizeof(snap->discriminator)); + // Build the 802.2 header. + struct snap_header_t* snap = (struct snap_header_t*)(frame_data + sizeof(struct ethernet_header_t)); + snap->dsap = SNAP_DSAP; + snap->ssap = SNAP_SSAP; + snap->control = SNAP_CONTROL; + memcpy(&snap->discriminator, discriminator, sizeof(snap->discriminator)); - // Add the data payload. - struct snap_header_t* payload = (struct snap_header_t*)(frame_data + sizeof(struct ethernet_header_t) + sizeof(struct snap_header_t)); - memcpy(payload, data, size); + // Add the data payload. + struct snap_header_t* payload = (struct snap_header_t*)(frame_data + sizeof(struct ethernet_header_t) + sizeof(struct snap_header_t)); + memcpy(payload, data, size); - // Add padding to meet minimum Ethernet frame size. - if (pad > 0) - memset(frame_data + frame_size, 0, pad); + // Add padding to meet minimum Ethernet frame size. + if (pad > 0) + memset(frame_data + frame_size, 0, pad); - pcapdelay_sendpacket(pcap_session, frame_data, frame_size + pad); - } + pcapdelay_sendpacket(pcap_session, frame_data, frame_size + pad); + } } -static bool elap_send_one_queued() -{ - // Attempt to send one packet out the host network interface. - struct packet_t* packet = queue_peek(&elap_port.out); - if (packet) - { - // Find the MAC address. - const struct ether_addr_t* dest; - if (packet->dest.node == at_broadcast_node) - dest = &HW_APPLETALK_BROADCAST; - else - dest = aarp_request_hardware(&packet->dest); +static bool elap_send_one_queued() { + // Attempt to send one packet out the host network interface. + struct packet_t* packet = queue_peek(&elap_port.out); + if (packet) + { + // Find the MAC address. + const struct ether_addr_t* dest; + if (packet->dest.node == at_broadcast_node) + dest = &HW_APPLETALK_BROADCAST; + else + dest = aarp_request_hardware(&packet->dest); - // Send it. - if (dest) - { - elap_send(dest, &SNAP_APPLETALK, packet->size, packet->data); + // Send it. + if (dest) + { + elap_send(dest, &SNAP_APPLETALK, packet->size, packet->data); - dequeue(&elap_port.out); - free(packet->data); - free(packet); - } - else - { - // AARP does not have the needed hardware address. Give AARP time to obtain the address and keep the current packet. - if (!aarp_retry()) - { - // However, if AARP has reached the retry limit, the packet is undeliverable. Discard the packet and move on. - atbridge_printf("ATBridge: AARP failed to find MAC address for network %d node %d. Dropping packet.\n", packet->dest.network, packet->dest.node); - aarp_retry_reset(); - dequeue(&elap_port.out); - free(packet->data); - free(packet); - } - } - return true; - } - else - return false; + dequeue(&elap_port.out); + free(packet->data); + free(packet); + } + else + { + // AARP does not have the needed hardware address. Give AARP time to obtain the address and keep the current packet. + if (!aarp_retry()) + { + // However, if AARP has reached the retry limit, the packet is undeliverable. Discard the packet and move on. + atbridge_printf("ATBridge: AARP failed to find MAC address for network %d node %d. Dropping packet.\n", packet->dest.network, packet->dest.node); + aarp_retry_reset(); + dequeue(&elap_port.out); + free(packet->data); + free(packet); + } + } + return true; + } + else + return false; } -static void elap_send_all_queued() -{ - while (elap_send_one_queued()); +static void elap_send_all_queued() { + while (elap_send_one_queued()); } -static bool elap_receive_one() -{ - if (!pcap_session) - return false; +static bool elap_receive_one() { + if (!pcap_session) + return false; - struct pcap_pkthdr header; - const u_char* packet = pcapdelay_next(pcap_session, &header); - if (packet) - { - // Receive and process one packet from the host network interface. - //// + struct pcap_pkthdr header; + const u_char* packet = pcapdelay_next(pcap_session, &header); + if (packet) + { + // Receive and process one packet from the host network interface. + //// - // Check the Ethernet 802.3 header. - const struct ethernet_header_t* ether = (struct ethernet_header_t*)packet; - if (header.len > sizeof(struct ethernet_header_t) && - ntohs(ether->length) <= ETHER_MAX_SIZE && - ntohs(ether->length) > sizeof(struct snap_header_t) && - (memcmp(ðer->source, &HW_LOCAL, sizeof(ether->source)) != 0) && /* Ignore packets sent from our node. */ - (memcmp(ðer->dest, &HW_LOCAL, sizeof(ether->dest)) == 0 || /* Accept packets destined for our node ... */ - memcmp(ðer->dest, &HW_APPLETALK_BROADCAST, sizeof(ether->dest)) == 0 /* ... or for broadcast. */) - ) - { - // Check the 802.2 SNAP header. - const struct snap_header_t* snap = (struct snap_header_t*)(packet + sizeof(struct ethernet_header_t)); - if (snap->dsap == SNAP_DSAP && - snap->ssap == SNAP_SSAP && - snap->control == SNAP_CONTROL) - { - if (memcmp(&snap->discriminator, &SNAP_APPLETALK, sizeof(snap->discriminator)) == 0) - { - // Handle an AppleTalk packet. - const size_t payload_size = ntohs(ether->length) - sizeof(struct snap_header_t); - const u_char* payload = packet + sizeof(struct ethernet_header_t) + sizeof(struct snap_header_t); + // Check the Ethernet 802.3 header. + const struct ethernet_header_t* ether = (struct ethernet_header_t*)packet; + if (header.len > sizeof(struct ethernet_header_t) && + ntohs(ether->length) <= ETHER_MAX_SIZE && + ntohs(ether->length) > sizeof(struct snap_header_t) && + (memcmp(ðer->source, &HW_LOCAL, sizeof(ether->source)) != 0) && /* Ignore packets sent from our node. */ + (memcmp(ðer->dest, &HW_LOCAL, sizeof(ether->dest)) == 0 || /* Accept packets destined for our node ... */ + memcmp(ðer->dest, &HW_APPLETALK_BROADCAST, sizeof(ether->dest)) == 0 /* ... or for broadcast. */) + ) + { + // Check the 802.2 SNAP header. + const struct snap_header_t* snap = (struct snap_header_t*)(packet + sizeof(struct ethernet_header_t)); + if (snap->dsap == SNAP_DSAP && + snap->ssap == SNAP_SSAP && + snap->control == SNAP_CONTROL) + { + if (memcmp(&snap->discriminator, &SNAP_APPLETALK, sizeof(snap->discriminator)) == 0) + { + // Handle an AppleTalk packet. + const size_t payload_size = ntohs(ether->length) - sizeof(struct snap_header_t); + const u_char* payload = packet + sizeof(struct ethernet_header_t) + sizeof(struct snap_header_t); - byte* copy = (byte*)malloc(payload_size); - memcpy(copy, payload, payload_size); + byte* copy = (byte*)malloc(payload_size); + memcpy(copy, payload, payload_size); - // ELAP does not support short-form DDP, so this must be a long-form DDP packet. - struct at_addr_t source, dest; - if (payload_size >= sizeof(struct DDP_LONG)) - { - // Extract the protocol address from the header. - // - // ELAP really shouldn't be looking at the header for the next level of the protocol stack, - // but this is a convenient place to glean addresses for the AMT, a process that needs both - // hardware and protocol addresses. - struct DDP_LONG* header = (struct DDP_LONG*)copy; - dest.network = (at_network_t)ntohs(header->dest_net); - source.network = (at_network_t)ntohs(header->source_net); - dest.node = header->dest_node; - source.node = header->source_node; + // ELAP does not support short-form DDP, so this must be a long-form DDP packet. + struct at_addr_t source, dest; + if (payload_size >= sizeof(struct DDP_LONG)) + { + // Extract the protocol address from the header. + // + // ELAP really shouldn't be looking at the header for the next level of the protocol stack, + // but this is a convenient place to glean addresses for the AMT, a process that needs both + // hardware and protocol addresses. + struct DDP_LONG* header = (struct DDP_LONG*)copy; + dest.network = (at_network_t)ntohs(header->dest_net); + source.network = (at_network_t)ntohs(header->source_net); + dest.node = header->dest_node; + source.node = header->source_node; - enqueue(&elap_port.in, dest, source, LAP_DDP_LONG, payload_size, copy); + enqueue(&elap_port.in, dest, source, LAP_DDP_LONG, payload_size, copy); - aarp_glean(&source, ðer->source); - } - else - atbridge_printf("ATBridge: Dropping invalid short ELAP frame.\n"); - } - else if (memcmp(&snap->discriminator, &SNAP_AARP, sizeof(snap->discriminator)) == 0) - { - // Handle an AARP packet. - struct aarp_header_t* aarp = (struct aarp_header_t*)(packet + sizeof(struct ethernet_header_t) + sizeof(struct snap_header_t)); - aarp->dest_proto_addr.addr.network = ntohs(aarp->dest_proto_addr.addr.network); - aarp->source_proto_addr.addr.network = ntohs(aarp->source_proto_addr.addr.network); - aarp->function = ntohs(aarp->function); - aarp->hardware_type = ntohs(aarp->hardware_type); - aarp->protocol_type = ntohs(aarp->protocol_type); - aarp_handle_packet(aarp); - } - } - } + aarp_glean(&source, ðer->source); + } + else + atbridge_printf("ATBridge: Dropping invalid short ELAP frame.\n"); + } + else if (memcmp(&snap->discriminator, &SNAP_AARP, sizeof(snap->discriminator)) == 0) + { + // Handle an AARP packet. + struct aarp_header_t* aarp = (struct aarp_header_t*)(packet + sizeof(struct ethernet_header_t) + sizeof(struct snap_header_t)); + aarp->dest_proto_addr.addr.network = ntohs(aarp->dest_proto_addr.addr.network); + aarp->source_proto_addr.addr.network = ntohs(aarp->source_proto_addr.addr.network); + aarp->function = ntohs(aarp->function); + aarp->hardware_type = ntohs(aarp->hardware_type); + aarp->protocol_type = ntohs(aarp->protocol_type); + aarp_handle_packet(aarp); + } + } + } - return true; - } - else - return false; + return true; + } + else + return false; } -static void elap_receive_all() -{ - while (elap_receive_one()); +static void elap_receive_all() { + while (elap_receive_one()); } -void elap_process() -{ - elap_receive_all(); - elap_send_all_queued(); +void elap_process() { + elap_receive_all(); + elap_send_all_queued(); } diff --git a/src/atbridge/llap.c b/src/atbridge/llap.c index 84177b4..c783282 100644 --- a/src/atbridge/llap.c +++ b/src/atbridge/llap.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /** This module implements the LLAP port of the bridge. **/ @@ -14,12 +14,12 @@ #include "llap.h" typedef enum { - LLAP_DDP_SHORT = 0x01, - LLAP_DDP_LONG = 0x02, - LLAP_ENQ = 0x81, - LLAP_ACK = 0x82, - LLAP_RTS = 0x84, - LLAP_CTS = 0x85 + LLAP_DDP_SHORT = 0x01, + LLAP_DDP_LONG = 0x02, + LLAP_ENQ = 0x81, + LLAP_ACK = 0x82, + LLAP_RTS = 0x84, + LLAP_CTS = 0x85 } LLAP_TYPES; const unsigned int LLAP_PACKET_MAX = 603 /* bytes */; @@ -31,290 +31,279 @@ const double GAP_TOLERANCE = 4.0; static struct packet_port_t llap_port; typedef enum { - DIALOG_READY, - DIALOG_GOT_CTS, - DIALOG_WAIT_IDG + DIALOG_READY, + DIALOG_GOT_CTS, + DIALOG_WAIT_IDG } DIALOG_STATE; static DIALOG_STATE dialog_state; static double dialog_end_dcycs; static double last_frame_dcycs; -void llap_init() -{ - dialog_state = DIALOG_READY; - last_frame_dcycs = 0; - port_init(&llap_port); +void llap_init() { + dialog_state = DIALOG_READY; + last_frame_dcycs = 0; + port_init(&llap_port); } -void llap_shutdown() -{ - port_shutdown(&llap_port); +void llap_shutdown() { + port_shutdown(&llap_port); } /** Queue one data packet out from the bridge's LLAP port and into the guest. **/ -void llap_enqueue_out(struct packet_t* packet) -{ - // Generate the RTS. - struct packet_t* rts = (struct packet_t*)malloc(sizeof(struct packet_t)); - rts->source.network = packet->source.network; - rts->source.node = packet->source.node; - rts->dest.network = packet->dest.network; - rts->dest.node = packet->dest.node; - rts->size = 0; - rts->data = 0; - rts->type = LLAP_RTS; - enqueue_packet(&llap_port.out, rts); +void llap_enqueue_out(struct packet_t* packet) { + // Generate the RTS. + struct packet_t* rts = (struct packet_t*)malloc(sizeof(struct packet_t)); + rts->source.network = packet->source.network; + rts->source.node = packet->source.node; + rts->dest.network = packet->dest.network; + rts->dest.node = packet->dest.node; + rts->size = 0; + rts->data = 0; + rts->type = LLAP_RTS; + enqueue_packet(&llap_port.out, rts); - // Enqueue the data. - enqueue_packet(&llap_port.out, packet); + // Enqueue the data. + enqueue_packet(&llap_port.out, packet); } -struct packet_t* llap_dequeue_in() -{ - return dequeue(&llap_port.in); +struct packet_t* llap_dequeue_in() { + return dequeue(&llap_port.in); } -static void llap_dump_packet(size_t size, byte data[]) -{ - if (size < LLAP_PACKET_MIN) - atbridge_printf("LLAP short packet.\n"); - else if (size > LLAP_PACKET_MAX) - atbridge_printf("LLAP long packet.\n"); - else - { - at_node_t dest = data[0]; - at_node_t source = data[1]; - LLAP_TYPES type = (LLAP_TYPES)(data[2]); - - const char* typeName = 0; - switch (type) - { - case LLAP_DDP_SHORT: - typeName = "DDP (short)"; - break; - case LLAP_DDP_LONG: - typeName = "DDP (long)"; - break; - case LLAP_ENQ: - typeName = "lapENQ"; - break; - case LLAP_ACK: - typeName = "lapACK"; - break; - case LLAP_RTS: - typeName = "lapRTS"; - break; - case LLAP_CTS: - typeName = "lapCTS"; - break; - } +static void llap_dump_packet(size_t size, byte data[]) { + if (size < LLAP_PACKET_MIN) + atbridge_printf("LLAP short packet.\n"); + else if (size > LLAP_PACKET_MAX) + atbridge_printf("LLAP long packet.\n"); + else + { + at_node_t dest = data[0]; + at_node_t source = data[1]; + LLAP_TYPES type = (LLAP_TYPES)(data[2]); - if (typeName) - atbridge_printf("LLAP[%d->%d] %s: %d bytes.\n", source, dest, typeName, size); - else - atbridge_printf("LLAP[%d->%d] %x: %d bytes.\n", source, dest, type, size); - - /*for (size_t i = 0; i < size; i++) - atbridge_printf("%02x ", data[i]); - atbridge_printf("\n");*/ - } + const char* typeName = 0; + switch (type) + { + case LLAP_DDP_SHORT: + typeName = "DDP (short)"; + break; + case LLAP_DDP_LONG: + typeName = "DDP (long)"; + break; + case LLAP_ENQ: + typeName = "lapENQ"; + break; + case LLAP_ACK: + typeName = "lapACK"; + break; + case LLAP_RTS: + typeName = "lapRTS"; + break; + case LLAP_CTS: + typeName = "lapCTS"; + break; + } + + if (typeName) + atbridge_printf("LLAP[%d->%d] %s: %d bytes.\n", source, dest, typeName, size); + else + atbridge_printf("LLAP[%d->%d] %x: %d bytes.\n", source, dest, type, size); + + /*for (size_t i = 0; i < size; i++) + atbridge_printf("%02x ", data[i]); + atbridge_printf("\n");*/ + } } /** Reply to a control packet from the GS **/ -static void llap_reply_control(at_node_t dest, at_node_t source, LLAP_TYPES type) -{ - struct at_addr_t dest_addr = { 0, dest }; - struct at_addr_t source_addr = { 0, source }; +static void llap_reply_control(at_node_t dest, at_node_t source, LLAP_TYPES type) { + struct at_addr_t dest_addr = { 0, dest }; + struct at_addr_t source_addr = { 0, source }; - // Insert control packets at the head of the queue contrary to normal FIFO queue operation - // to ensure that control frames arrive in the intended order. - insert(&llap_port.out, dest_addr, source_addr, type, 0, 0); + // Insert control packets at the head of the queue contrary to normal FIFO queue operation + // to ensure that control frames arrive in the intended order. + insert(&llap_port.out, dest_addr, source_addr, type, 0, 0); } /** Accept a data packet from the GS. **/ -static void llap_handle_data(size_t size, byte data[]) -{ - at_node_t dest = data[0]; - at_node_t source = data[1]; - LLAP_TYPES type = (LLAP_TYPES)(data[2]); +static void llap_handle_data(size_t size, byte data[]) { + at_node_t dest = data[0]; + at_node_t source = data[1]; + LLAP_TYPES type = (LLAP_TYPES)(data[2]); - const size_t data_size = size - 3; - byte* data_copy = (byte*)malloc(data_size); - memcpy(data_copy, data + 3, data_size); + const size_t data_size = size - 3; + byte* data_copy = (byte*)malloc(data_size); + memcpy(data_copy, data + 3, data_size); - struct at_addr_t dest_addr = { 0, dest }; - struct at_addr_t source_addr = { 0, source }; - enqueue(&llap_port.in, dest_addr, source_addr, type, data_size, data_copy); + struct at_addr_t dest_addr = { 0, dest }; + struct at_addr_t source_addr = { 0, source }; + enqueue(&llap_port.in, dest_addr, source_addr, type, data_size, data_copy); } /** Accept a control packet from the GS. **/ -static void llap_handle_control(size_t size, byte data[]) -{ - at_node_t dest = data[0]; - at_node_t source = data[1]; - LLAP_TYPES type = (LLAP_TYPES)(data[2]); +static void llap_handle_control(size_t size, byte data[]) { + at_node_t dest = data[0]; + at_node_t source = data[1]; + LLAP_TYPES type = (LLAP_TYPES)(data[2]); - struct at_addr_t addr = { atbridge_get_net(), dest }; + struct at_addr_t addr = { atbridge_get_net(), dest }; - switch (type) - { - case LLAP_ENQ: - // Require the GS to take a valid "client" address not known to be in use. - if (dest > 127 || dest == 0 || atbridge_address_used(&addr)) - llap_reply_control(source, dest, LLAP_ACK); - break; - case LLAP_ACK: - break; - case LLAP_RTS: - if (dest != at_broadcast_node) - // The GS is trying to make a directed transmission. Provide the required RTS/CTS handshake. - // Note that broadcast packets do not require a CTS. - llap_reply_control(source, dest, LLAP_CTS); - break; - case LLAP_CTS: - // The GS sent a CTS. If the bridge has pending data, prepare to deliver the packet. - dialog_state = DIALOG_GOT_CTS; - break; - default: - break; - } + switch (type) + { + case LLAP_ENQ: + // Require the GS to take a valid "client" address not known to be in use. + if (dest > 127 || dest == 0 || atbridge_address_used(&addr)) + llap_reply_control(source, dest, LLAP_ACK); + break; + case LLAP_ACK: + break; + case LLAP_RTS: + if (dest != at_broadcast_node) + // The GS is trying to make a directed transmission. Provide the required RTS/CTS handshake. + // Note that broadcast packets do not require a CTS. + llap_reply_control(source, dest, LLAP_CTS); + break; + case LLAP_CTS: + // The GS sent a CTS. If the bridge has pending data, prepare to deliver the packet. + dialog_state = DIALOG_GOT_CTS; + break; + default: + break; + } } /** Occassionally, we receive an invalid packet from the GS. I'm unsure if this is due to a bug in GS/OS - or, more likely, a bug in the SCC emulation. Regardless, when such a thing does occur, discard the - current, corrupted dialog. Link errors are routine in real LocalTalk networks, and LocalTalk will recover. - **/ -static void llap_reset_dialog() -{ - dialog_state = DIALOG_READY; - last_frame_dcycs = 0; + or, more likely, a bug in the SCC emulation. Regardless, when such a thing does occur, discard the + current, corrupted dialog. Link errors are routine in real LocalTalk networks, and LocalTalk will recover. + **/ +static void llap_reset_dialog() { + dialog_state = DIALOG_READY; + last_frame_dcycs = 0; - // Discard packets until the queue is either empty or the next dialog starts (and dialogs begin with an RTS). - while (true) - { - struct packet_t* packet = queue_peek(&llap_port.out); - - if (packet && (packet->type != LLAP_RTS)) - { - packet = dequeue(&llap_port.out); - if (packet->data) - free(packet->data); - free(packet); - } - else - break; - } + // Discard packets until the queue is either empty or the next dialog starts (and dialogs begin with an RTS). + while (true) + { + struct packet_t* packet = queue_peek(&llap_port.out); + + if (packet && (packet->type != LLAP_RTS)) + { + packet = dequeue(&llap_port.out); + if (packet->data) + free(packet->data); + free(packet); + } + else + break; + } } /** Transfer (send) one LLAP packet from the GS. **/ -void llap_enqueue_in(double dcycs, size_t size, byte data[]) -{ - atbridge_printf("<%0.0f> TX: ", dcycs); - llap_dump_packet(size, data); +void llap_enqueue_in(double dcycs, size_t size, byte data[]) { + atbridge_printf("<%0.0f> TX: ", dcycs); + llap_dump_packet(size, data); - if (size < LLAP_PACKET_MIN) - atbridge_printf("ATBridge: Dropping LLAP short packet.\n"); - else if (size > LLAP_PACKET_MAX) - atbridge_printf("ATBridge: Dropping LLAP long packet.\n"); - else - { - last_frame_dcycs = dcycs; - LLAP_TYPES type = (LLAP_TYPES)(data[2]); + if (size < LLAP_PACKET_MIN) + atbridge_printf("ATBridge: Dropping LLAP short packet.\n"); + else if (size > LLAP_PACKET_MAX) + atbridge_printf("ATBridge: Dropping LLAP long packet.\n"); + else + { + last_frame_dcycs = dcycs; + LLAP_TYPES type = (LLAP_TYPES)(data[2]); - switch (type) - { - case LLAP_DDP_SHORT: - case LLAP_DDP_LONG: - llap_handle_data(size, data); - break; - case LLAP_ENQ: - case LLAP_ACK: - case LLAP_RTS: - case LLAP_CTS: - llap_handle_control(size, data); - break; - default: - // Intentionally check for valid types and ingore packets with invalid types. - // Sometimes, the bridge gets invalid packets from the GS, which tends to break the bridge. - atbridge_printf("ATBridge: Dropping LLAP packet with invalid type.\n"); - llap_reset_dialog(); - } - } + switch (type) + { + case LLAP_DDP_SHORT: + case LLAP_DDP_LONG: + llap_handle_data(size, data); + break; + case LLAP_ENQ: + case LLAP_ACK: + case LLAP_RTS: + case LLAP_CTS: + llap_handle_control(size, data); + break; + default: + // Intentionally check for valid types and ingore packets with invalid types. + // Sometimes, the bridge gets invalid packets from the GS, which tends to break the bridge. + atbridge_printf("ATBridge: Dropping LLAP packet with invalid type.\n"); + llap_reset_dialog(); + } + } } /** Transfer (receive) one LLAP packet to the GS. **/ -void llap_dequeue_out(double dcycs, size_t* size, byte* data[]) -{ - *size = 0; +void llap_dequeue_out(double dcycs, size_t* size, byte* data[]) { + *size = 0; - // The LocalTalk protocol requires a minimum 400us gap between dialogs (called the IDG). - // If necessary, wait for the IDG. - if (dialog_state == DIALOG_WAIT_IDG) + // The LocalTalk protocol requires a minimum 400us gap between dialogs (called the IDG). + // If necessary, wait for the IDG. + if (dialog_state == DIALOG_WAIT_IDG) + { + if ((dcycs - dialog_end_dcycs) >= LLAP_IDG) + // The IDG is done. + dialog_state = DIALOG_READY; + else + // Continue waiting for the IDG. + return; + } + // The LocalTalk protocols requires a maximum 200us gap between frames within a dialog (called the IFG). + // If we exceed the IFG, the bridge must be stuck in an incomplete or corrupt dialog. In this case, + // discard the current dialog and try again. + if ((dialog_state != DIALOG_READY) && (last_frame_dcycs != 0) && ((dcycs - last_frame_dcycs) >= (GAP_TOLERANCE*LLAP_IFG))) + { + llap_reset_dialog(); + atbridge_printf("ATBridge: Dialog reset due to IFG violation.\n"); + } + + struct packet_t* packet = queue_peek(&llap_port.out); + + if ((dialog_state == DIALOG_READY) && (packet) && !(packet->type & 0x80) && (last_frame_dcycs != 0) && ((dcycs - last_frame_dcycs) >= (GAP_TOLERANCE*LLAP_IDG))) + { + llap_reset_dialog(); + packet = queue_peek(&llap_port.out); + atbridge_printf("ATBridge: Dialog reset due to IDG violation.\n"); + } + + if (packet && + ((packet->type & 0x80) || /* Pass along control frames without waiting for a CTS. */ + (!(packet->type & 0x80) && (packet->dest.node == at_broadcast_node) && (dialog_state == DIALOG_READY)) || /* Pass along broadcast frames, which don't wait for CTS frames. */ + (!(packet->type & 0x80) && (packet->dest.node != at_broadcast_node) && (dialog_state == DIALOG_GOT_CTS)))) /* Pass along directed frames only after receiving a CTS handshake. */ + { + dequeue(&llap_port.out); + + // Prepend the LLAP header. + *size = packet->size + 3 + 2; + *data = (byte*)malloc(*size); + (*data)[0] = packet->dest.node; + (*data)[1] = packet->source.node; + (*data)[2] = packet->type; + + // Insert the data into the new LLAP packet. + if (*size) + memcpy((*data) + 3, packet->data, packet->size); + + // Fake a frame check sequence (FCS). Since our SCC emulation doesn't actually + // check the FCS, the value of the FCS doesn't matter. + (*data)[packet->size + 3 + 0] = 0xff; + (*data)[packet->size + 3 + 1] = 0xff; + + atbridge_printf("<%0.0f> RX: ", dcycs); + llap_dump_packet(*size, *data); + + if (packet->type & 0x80) + dialog_state = DIALOG_READY; + else { - if ((dcycs - dialog_end_dcycs) >= LLAP_IDG) - // The IDG is done. - dialog_state = DIALOG_READY; - else - // Continue waiting for the IDG. - return; + // This was the last packet in the dialog. + dialog_state = DIALOG_WAIT_IDG; + dialog_end_dcycs = dcycs; } - // The LocalTalk protocols requires a maximum 200us gap between frames within a dialog (called the IFG). - // If we exceed the IFG, the bridge must be stuck in an incomplete or corrupt dialog. In this case, - // discard the current dialog and try again. - if ((dialog_state != DIALOG_READY) && (last_frame_dcycs != 0) && ((dcycs - last_frame_dcycs) >= (GAP_TOLERANCE*LLAP_IFG))) - { - llap_reset_dialog(); - atbridge_printf("ATBridge: Dialog reset due to IFG violation.\n"); - } - struct packet_t* packet = queue_peek(&llap_port.out); + last_frame_dcycs = dcycs; - if ((dialog_state == DIALOG_READY) && (packet) && !(packet->type & 0x80) && (last_frame_dcycs != 0) && ((dcycs - last_frame_dcycs) >= (GAP_TOLERANCE*LLAP_IDG))) - { - llap_reset_dialog(); - packet = queue_peek(&llap_port.out); - atbridge_printf("ATBridge: Dialog reset due to IDG violation.\n"); - } - - if (packet && - ((packet->type & 0x80) || /* Pass along control frames without waiting for a CTS. */ - (!(packet->type & 0x80) && (packet->dest.node == at_broadcast_node) && (dialog_state == DIALOG_READY)) || /* Pass along broadcast frames, which don't wait for CTS frames. */ - (!(packet->type & 0x80) && (packet->dest.node != at_broadcast_node) && (dialog_state == DIALOG_GOT_CTS)))) /* Pass along directed frames only after receiving a CTS handshake. */ - { - dequeue(&llap_port.out); - - // Prepend the LLAP header. - *size = packet->size + 3 + 2; - *data = (byte*)malloc(*size); - (*data)[0] = packet->dest.node; - (*data)[1] = packet->source.node; - (*data)[2] = packet->type; - - // Insert the data into the new LLAP packet. - if (*size) - memcpy((*data) + 3, packet->data, packet->size); - - // Fake a frame check sequence (FCS). Since our SCC emulation doesn't actually - // check the FCS, the value of the FCS doesn't matter. - (*data)[packet->size + 3 + 0] = 0xff; - (*data)[packet->size + 3 + 1] = 0xff; - - atbridge_printf("<%0.0f> RX: ", dcycs); - llap_dump_packet(*size, *data); - - if (packet->type & 0x80) - dialog_state = DIALOG_READY; - else - { - // This was the last packet in the dialog. - dialog_state = DIALOG_WAIT_IDG; - dialog_end_dcycs = dcycs; - } - - last_frame_dcycs = dcycs; - - free(packet->data); - free(packet); - } + free(packet->data); + free(packet); + } } diff --git a/src/atbridge/pcap_delay.c b/src/atbridge/pcap_delay.c index 0c2d31e..7cc1846 100644 --- a/src/atbridge/pcap_delay.c +++ b/src/atbridge/pcap_delay.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include #include "pcap_delay.h" @@ -17,143 +17,130 @@ static void* module = 0; #endif -bool pcapdelay_load() -{ - if (!pcapdelay_is_loaded()) - { +bool pcapdelay_load() { + if (!pcapdelay_is_loaded()) + { #ifdef WIN32 - module = LoadLibrary("wpcap.dll"); + module = LoadLibrary("wpcap.dll"); #elif __linux__ - module = dlopen("libpcap.so", RTLD_LAZY); + module = dlopen("libpcap.so", RTLD_LAZY); #endif - } - return pcapdelay_is_loaded(); + } + return pcapdelay_is_loaded(); } -bool pcapdelay_is_loaded() -{ +bool pcapdelay_is_loaded() { #ifdef WIN32 - return module != NULL; + return module != NULL; #elif __linux__ - return module != 0; + return module != 0; #endif return 0; } -void pcapdelay_unload() -{ - if (pcapdelay_is_loaded()) - { +void pcapdelay_unload() { + if (pcapdelay_is_loaded()) + { #ifdef WIN32 - FreeLibrary(module); - module = NULL; + FreeLibrary(module); + module = NULL; #elif __linux__ - dlclose(module); - module = 0; + dlclose(module); + module = 0; #endif - } + } } typedef void (*PFNVOID)(); -static PFNVOID delay_load(const char* proc, PFNVOID* ppfn) -{ - if (pcapdelay_load() && proc && ppfn && !*ppfn) - { +static PFNVOID delay_load(const char* proc, PFNVOID* ppfn) { + if (pcapdelay_load() && proc && ppfn && !*ppfn) + { #ifdef WIN32 - *ppfn = (PFNVOID)GetProcAddress(module, proc); + *ppfn = (PFNVOID)GetProcAddress(module, proc); #elif __linux__ - *ppfn = (PFNVOID)dlsym(module, proc); + *ppfn = (PFNVOID)dlsym(module, proc); #endif - } - if (ppfn) - return *ppfn; - else - return 0; + } + if (ppfn) + return *ppfn; + else + return 0; } -void pcapdelay_freealldevs(pcap_if_t* a0) -{ - typedef void (*PFN)(pcap_if_t*); - static PFN pfn = 0; - if ((pfn = (PFN)delay_load("pcap_freealldevs", (PFNVOID*)&pfn))) - (*pfn)(a0); +void pcapdelay_freealldevs(pcap_if_t* a0) { + typedef void (*PFN)(pcap_if_t*); + static PFN pfn = 0; + if ((pfn = (PFN)delay_load("pcap_freealldevs", (PFNVOID*)&pfn))) + (*pfn)(a0); } -pcap_t* pcapdelay_open_live(const char* a0, int a1, int a2, int a3, char* a4) -{ - typedef pcap_t* (*PFN)(const char*, int, int, int, char*); - static PFN pfn = 0; - if ((pfn = (PFN)delay_load("pcap_open_live", (PFNVOID*)&pfn))) - return (*pfn)(a0, a1, a2, a3, a4); - else - return 0; +pcap_t* pcapdelay_open_live(const char* a0, int a1, int a2, int a3, char* a4) { + typedef pcap_t* (*PFN)(const char*, int, int, int, char*); + static PFN pfn = 0; + if ((pfn = (PFN)delay_load("pcap_open_live", (PFNVOID*)&pfn))) + return (*pfn)(a0, a1, a2, a3, a4); + else + return 0; } -void pcapdelay_close(pcap_t* a0) -{ - typedef void (*PFN)(pcap_t*); - static PFN pfn = 0; - if ((pfn = (PFN)delay_load("pcap_close", (PFNVOID*)&pfn))) - (*pfn)(a0); +void pcapdelay_close(pcap_t* a0) { + typedef void (*PFN)(pcap_t*); + static PFN pfn = 0; + if ((pfn = (PFN)delay_load("pcap_close", (PFNVOID*)&pfn))) + (*pfn)(a0); } -int pcapdelay_findalldevs(pcap_if_t** a0, char* a1) -{ - typedef int (*PFN)(pcap_if_t**, char*); - static PFN pfn = 0; - if ((pfn = (PFN)delay_load("pcap_findalldevs", (PFNVOID*)&pfn))) - return (*pfn)(a0, a1); - else - return 0; +int pcapdelay_findalldevs(pcap_if_t** a0, char* a1) { + typedef int (*PFN)(pcap_if_t**, char*); + static PFN pfn = 0; + if ((pfn = (PFN)delay_load("pcap_findalldevs", (PFNVOID*)&pfn))) + return (*pfn)(a0, a1); + else + return 0; } -int pcapdelay_datalink(pcap_t* a0) -{ - typedef int(*PFN)(pcap_t*); - static PFN pfn = 0; - if ((pfn = (PFN)delay_load("pcap_datalink", (PFNVOID*)&pfn))) - return (*pfn)(a0); - else - return 0; +int pcapdelay_datalink(pcap_t* a0) { + typedef int (*PFN)(pcap_t*); + static PFN pfn = 0; + if ((pfn = (PFN)delay_load("pcap_datalink", (PFNVOID*)&pfn))) + return (*pfn)(a0); + else + return 0; } -int pcapdelay_setnonblock(pcap_t* a0, int a1, char* a2) -{ - typedef int(*PFN)(pcap_t*, int, char*); - static PFN pfn = 0; - if ((pfn = (PFN)delay_load("pcap_setnonblock", (PFNVOID*)&pfn))) - return (*pfn)(a0, a1, a2); - else - return 0; +int pcapdelay_setnonblock(pcap_t* a0, int a1, char* a2) { + typedef int (*PFN)(pcap_t*, int, char*); + static PFN pfn = 0; + if ((pfn = (PFN)delay_load("pcap_setnonblock", (PFNVOID*)&pfn))) + return (*pfn)(a0, a1, a2); + else + return 0; } -int pcapdelay_sendpacket(pcap_t* a0, u_char* a1, int a2) -{ - typedef int(*PFN)(pcap_t*, u_char*, int); - static PFN pfn = 0; - if ((pfn = (PFN)delay_load("pcap_sendpacket", (PFNVOID*)&pfn))) - return (*pfn)(a0, a1, a2); - else - return 0; +int pcapdelay_sendpacket(pcap_t* a0, u_char* a1, int a2) { + typedef int (*PFN)(pcap_t*, u_char*, int); + static PFN pfn = 0; + if ((pfn = (PFN)delay_load("pcap_sendpacket", (PFNVOID*)&pfn))) + return (*pfn)(a0, a1, a2); + else + return 0; } -const u_char* pcapdelay_next(pcap_t* a0, struct pcap_pkthdr* a1) -{ - typedef const u_char*(*PFN)(pcap_t*, struct pcap_pkthdr*); - static PFN pfn = 0; - if ((pfn = (PFN)delay_load("pcap_next", (PFNVOID*)&pfn))) - return (*pfn)(a0, a1); - else - return 0; +const u_char* pcapdelay_next(pcap_t* a0, struct pcap_pkthdr* a1) { + typedef const u_char*(*PFN)(pcap_t*, struct pcap_pkthdr*); + static PFN pfn = 0; + if ((pfn = (PFN)delay_load("pcap_next", (PFNVOID*)&pfn))) + return (*pfn)(a0, a1); + else + return 0; } -int pcapdelay_dispatch(pcap_t* a0, int a1, pcap_handler a2, u_char* a3) -{ - typedef const int(*PFN)(pcap_t *, int, pcap_handler, u_char *); - static PFN pfn = 0; - if ((pfn = (PFN)delay_load("pcap_dispatch", (PFNVOID*)&pfn))) - return (*pfn)(a0, a1, a2, a3); - else - return 0; +int pcapdelay_dispatch(pcap_t* a0, int a1, pcap_handler a2, u_char* a3) { + typedef const int (*PFN)(pcap_t *, int, pcap_handler, u_char *); + static PFN pfn = 0; + if ((pfn = (PFN)delay_load("pcap_dispatch", (PFNVOID*)&pfn))) + return (*pfn)(a0, a1, a2, a3); + else + return 0; } diff --git a/src/atbridge/port.c b/src/atbridge/port.c index 7b7e611..aed18ef 100644 --- a/src/atbridge/port.c +++ b/src/atbridge/port.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /** This module implements queues for storing and transferring packets within the bridge. **/ @@ -11,117 +11,107 @@ #include "atalk.h" #include "port.h" -void port_init(struct packet_port_t* port) -{ - if (port) - { - queue_init(&port->in); - queue_init(&port->out); - } +void port_init(struct packet_port_t* port) { + if (port) + { + queue_init(&port->in); + queue_init(&port->out); + } } -void port_shutdown(struct packet_port_t* port) -{ - if (port) - { - queue_shutdown(&port->in); - queue_shutdown(&port->out); - } +void port_shutdown(struct packet_port_t* port) { + if (port) + { + queue_shutdown(&port->in); + queue_shutdown(&port->out); + } } -void queue_init(struct packet_queue_t* queue) -{ - if (queue) - { - queue->head = queue->tail = 0; - } +void queue_init(struct packet_queue_t* queue) { + if (queue) + { + queue->head = queue->tail = 0; + } } -void queue_shutdown(struct packet_queue_t* queue) -{ - if (queue) - { - struct packet_t* packet = dequeue(queue); - while (packet) - { - if (packet->data) - free(packet->data); - free(packet); - packet = dequeue(queue); - } - } +void queue_shutdown(struct packet_queue_t* queue) { + if (queue) + { + struct packet_t* packet = dequeue(queue); + while (packet) + { + if (packet->data) + free(packet->data); + free(packet); + packet = dequeue(queue); + } + } } -void enqueue(struct packet_queue_t* queue, struct at_addr_t dest, struct at_addr_t source, byte type, size_t size, byte data[]) -{ - if (!queue) - return; +void enqueue(struct packet_queue_t* queue, struct at_addr_t dest, struct at_addr_t source, byte type, size_t size, byte data[]) { + if (!queue) + return; - struct packet_t* packet = (struct packet_t*)malloc(sizeof(struct packet_t)); - packet->dest.network = dest.network; - packet->dest.node = dest.node; - packet->source.network = source.network; - packet->source.node = source.node; - packet->type = type; - packet->size = size; - packet->data = data; - enqueue_packet(queue, packet); + struct packet_t* packet = (struct packet_t*)malloc(sizeof(struct packet_t)); + packet->dest.network = dest.network; + packet->dest.node = dest.node; + packet->source.network = source.network; + packet->source.node = source.node; + packet->type = type; + packet->size = size; + packet->data = data; + enqueue_packet(queue, packet); } -void enqueue_packet(struct packet_queue_t* queue, struct packet_t* packet) -{ - packet->next = 0; +void enqueue_packet(struct packet_queue_t* queue, struct packet_t* packet) { + packet->next = 0; - if (queue->tail) - queue->tail->next = packet; - else - queue->head = packet; - queue->tail = packet; + if (queue->tail) + queue->tail->next = packet; + else + queue->head = packet; + queue->tail = packet; } -void insert(struct packet_queue_t* queue, struct at_addr_t dest, struct at_addr_t source, byte type, size_t size, byte data[]) -{ - if (!queue) - return; +void insert(struct packet_queue_t* queue, struct at_addr_t dest, struct at_addr_t source, byte type, size_t size, byte data[]) { + if (!queue) + return; - struct packet_t* packet = (struct packet_t*)malloc(sizeof(struct packet_t)); - packet->dest.network = dest.network; - packet->dest.node = dest.node; - packet->source.network = source.network; - packet->source.node = source.node; - packet->type = type; - packet->size = size; - packet->data = data; - insert_packet(queue, packet); + struct packet_t* packet = (struct packet_t*)malloc(sizeof(struct packet_t)); + packet->dest.network = dest.network; + packet->dest.node = dest.node; + packet->source.network = source.network; + packet->source.node = source.node; + packet->type = type; + packet->size = size; + packet->data = data; + insert_packet(queue, packet); } -void insert_packet(struct packet_queue_t* queue, struct packet_t* packet) -{ - packet->next = queue->head; - queue->head = packet; - if (!queue->tail) - queue->tail = queue->head; +void insert_packet(struct packet_queue_t* queue, struct packet_t* packet) { + packet->next = queue->head; + queue->head = packet; + if (!queue->tail) + queue->tail = queue->head; } -struct packet_t* dequeue(struct packet_queue_t* queue) -{ - if (queue && queue->head) - { - struct packet_t* packet = queue->head; - if (queue->tail == queue->head) - queue->tail = queue->head = 0; - else - queue->head = packet->next; - return packet; - } - else - return 0; +struct packet_t* dequeue(struct packet_queue_t* queue) { + if (queue && queue->head) + { + struct packet_t* packet = queue->head; + if (queue->tail == queue->head) + queue->tail = queue->head = 0; + else + queue->head = packet->next; + return packet; + } + else + return 0; } -struct packet_t* queue_peek(struct packet_queue_t* queue) -{ - if (queue) - return queue->head; - else - return 0; +struct packet_t* queue_peek(struct packet_queue_t* queue) { + if (queue) + return queue->head; + else + return 0; } diff --git a/src/clock.c b/src/clock.c index 79d1edc..755e0d9 100644 --- a/src/clock.c +++ b/src/clock.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include @@ -16,419 +16,396 @@ #endif extern int Verbose; -extern word32 g_vbl_count; // OG change int to word32 +extern word32 g_vbl_count; // OG change int to word32 extern int g_rom_version; extern int g_config_gsplus_update_needed; -#define CLK_IDLE 1 -#define CLK_TIME 2 -#define CLK_INTERNAL 3 -#define CLK_BRAM1 4 -#define CLK_BRAM2 5 +#define CLK_IDLE 1 +#define CLK_TIME 2 +#define CLK_INTERNAL 3 +#define CLK_BRAM1 4 +#define CLK_BRAM2 5 -int g_clk_mode = CLK_IDLE; -int g_clk_read = 0; -int g_clk_reg1 = 0; +int g_clk_mode = CLK_IDLE; +int g_clk_read = 0; +int g_clk_reg1 = 0; extern int g_c033_data; extern int g_c034_val; -byte g_bram[2][256]; -byte *g_bram_ptr = &(g_bram[0][0]); -byte g_temp_boot_slot = 254; -byte g_orig_boot_slot = 0; +byte g_bram[2][256]; +byte *g_bram_ptr = &(g_bram[0][0]); +byte g_temp_boot_slot = 254; +byte g_orig_boot_slot = 0; -word32 g_clk_cur_time = 0xa0000000; -int g_clk_next_vbl_update = 0; +word32 g_clk_cur_time = 0xa0000000; +int g_clk_next_vbl_update = 0; -double -get_dtime() -{ +double get_dtime() { #ifndef _WIN32 - struct timeval tp1; - double dsec; - double dusec; + struct timeval tp1; + double dsec; + double dusec; #endif - double dtime; + double dtime; - /* Routine used to return actual system time as a double */ - /* No routine cares about the absolute value, only deltas--maybe */ - /* take advantage of that in future to increase usec accuracy */ + /* Routine used to return actual system time as a double */ + /* No routine cares about the absolute value, only deltas--maybe */ + /* take advantage of that in future to increase usec accuracy */ #ifdef _WIN32 - dtime = timeGetTime() / 1000.0; + dtime = timeGetTime() / 1000.0; #else # ifdef SOLARIS - gettimeofday(&tp1, (void *)0); + gettimeofday(&tp1, (void *)0); # else - gettimeofday(&tp1, (struct timezone *)0); + gettimeofday(&tp1, (struct timezone *)0); # endif - dsec = (double)tp1.tv_sec; - dusec = (double)tp1.tv_usec; + dsec = (double)tp1.tv_sec; + dusec = (double)tp1.tv_usec; - dtime = dsec + (dusec / (1000.0 * 1000.0)); + dtime = dsec + (dusec / (1000.0 * 1000.0)); #endif - return dtime; + return dtime; } -int -micro_sleep(double dtime) -{ +int micro_sleep(double dtime) { #ifndef _WIN32 - struct timeval Timer; - int ret; + struct timeval Timer; + int ret; #endif - if(dtime <= 0.0) { - return 0; - } - if(dtime >= 1.0) { - halt_printf("micro_sleep called with %f!!\n", dtime); - return -1; - } + if(dtime <= 0.0) { + return 0; + } + if(dtime >= 1.0) { + halt_printf("micro_sleep called with %f!!\n", dtime); + return -1; + } #if 0 - printf("usleep: %f\n", dtime); + printf("usleep: %f\n", dtime); #endif #ifdef _WIN32 - Sleep((DWORD)(dtime * 1000)); + Sleep((DWORD)(dtime * 1000)); #else - Timer.tv_sec = 0; - Timer.tv_usec = (dtime * 1000000.0); - if( (ret = select(0, 0, 0, 0, &Timer)) < 0) { - glogf("micro_sleep (select) ret: %d, errno: %d", ret, errno); - return -1; - } + Timer.tv_sec = 0; + Timer.tv_usec = (dtime * 1000000.0); + if( (ret = select(0, 0, 0, 0, &Timer)) < 0) { + glogf("micro_sleep (select) ret: %d, errno: %d", ret, errno); + return -1; + } #endif - return 0; + return 0; } -void -clk_bram_zero() -{ - int i, j; +void clk_bram_zero() { + int i, j; - /* zero out all bram */ - for(i = 0; i < 2; i++) { - for(j = 0; j < 256; j++) { - g_bram[i][j] = 0; - } - } - g_bram_ptr = &(g_bram[0][0]); + /* zero out all bram */ + for(i = 0; i < 2; i++) { + for(j = 0; j < 256; j++) { + g_bram[i][j] = 0; + } + } + g_bram_ptr = &(g_bram[0][0]); } -void -clk_bram_set(int bram_num, int offset, int val) -{ - g_bram[bram_num][offset] = val; +void clk_bram_set(int bram_num, int offset, int val) { + g_bram[bram_num][offset] = val; } extern void x_clk_setup_bram_version(); -void -clk_setup_bram_version() -{ - x_clk_setup_bram_version(); +void clk_setup_bram_version() { + x_clk_setup_bram_version(); } -void -clk_write_bram(FILE *fconf) -{ - int i, j, k; +void clk_write_bram(FILE *fconf) { + int i, j, k; - for(i = 0; i < 2; i++) { - fprintf(fconf, "\n"); - for(j = 0; j < 256; j += 16) { - fprintf(fconf, "bram%d[%02x] =", 2*i + 1, j); - for(k = 0; k < 16; k++) { - fprintf(fconf, " %02x", g_bram[i][j+k]); - } - fprintf(fconf, "\n"); - } - } + for(i = 0; i < 2; i++) { + fprintf(fconf, "\n"); + for(j = 0; j < 256; j += 16) { + fprintf(fconf, "bram%d[%02x] =", 2*i + 1, j); + for(k = 0; k < 16; k++) { + fprintf(fconf, " %02x", g_bram[i][j+k]); + } + fprintf(fconf, "\n"); + } + } } -void -clk_calculate_bram_checksum(void) { - int checksum = 0; - int i; - if (g_bram_ptr[251] == 0xff) { - // Only make the checksum valid if we have non-zeron data! - // Otherwise you have very valid zeroes, which confuses the GS firmware mightily. - for (i = 250; i >= 0; i--) { - checksum = (checksum & 0xFFFF) << 1; - checksum = (checksum & 0xFFFF) - + g_bram_ptr[i] - + ((int)g_bram_ptr[i + 1] << 8) - + (checksum >> 16); - } - checksum &= 0xFFFF; - checksum += ((checksum ^ 0xAAAA) << 16); +void clk_calculate_bram_checksum(void) { + int checksum = 0; + int i; + if (g_bram_ptr[251] == 0xff) { + // Only make the checksum valid if we have non-zeron data! + // Otherwise you have very valid zeroes, which confuses the GS firmware mightily. + for (i = 250; i >= 0; i--) { + checksum = (checksum & 0xFFFF) << 1; + checksum = (checksum & 0xFFFF) + + g_bram_ptr[i] + + ((int)g_bram_ptr[i + 1] << 8) + + (checksum >> 16); + } + checksum &= 0xFFFF; + checksum += ((checksum ^ 0xAAAA) << 16); #if defined(GSPLUS_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) // OSX needs to calculate endianness mid-compilation, can't be passed on compile command - g_bram_ptr[252] = (checksum & 0xFF); - g_bram_ptr[253] = (checksum >> 8); - g_bram_ptr[254] = (checksum >> 16); - g_bram_ptr[255] = (checksum >> 24); + g_bram_ptr[252] = (checksum & 0xFF); + g_bram_ptr[253] = (checksum >> 8); + g_bram_ptr[254] = (checksum >> 16); + g_bram_ptr[255] = (checksum >> 24); #else - g_bram_ptr[255] = (checksum & 0xFF); - g_bram_ptr[254] = (checksum >> 8); - g_bram_ptr[253] = (checksum >> 16); - g_bram_ptr[252] = (checksum >> 24); + g_bram_ptr[255] = (checksum & 0xFF); + g_bram_ptr[254] = (checksum >> 8); + g_bram_ptr[253] = (checksum >> 16); + g_bram_ptr[252] = (checksum >> 24); #endif - } + } } -void -update_cur_time() -{ - struct tm *tm_ptr; - time_t cur_time; - unsigned int secs, secs2; +void update_cur_time() { + struct tm *tm_ptr; + time_t cur_time; + unsigned int secs, secs2; -#ifdef UNDER_CE // OG Not supported on WIndows CE - /* - SYSTEMTIME stime; - FILETIME ftime; - GetLocalTime(&stime); - SystemTimeToFileTime(&stime,&ftime); - cur_time = ftime.dwLowDateTime; - */ - cur_time = time(0); +#ifdef UNDER_CE // OG Not supported on WIndows CE + /* + SYSTEMTIME stime; + FILETIME ftime; + GetLocalTime(&stime); + SystemTimeToFileTime(&stime,&ftime); + cur_time = ftime.dwLowDateTime; + */ + cur_time = time(0); - secs=0; - secs2=0; + secs=0; + secs2=0; #else - cur_time = time(0); + cur_time = time(0); - /* Figure out the timezone (effectively) by diffing two times. */ - /* this is probably not right for a few hours around daylight savings*/ - /* time transition */ - secs2 = (unsigned int)mktime(gmtime(&cur_time)); - tm_ptr = localtime(&cur_time); - secs = (unsigned int)mktime(tm_ptr); + /* Figure out the timezone (effectively) by diffing two times. */ + /* this is probably not right for a few hours around daylight savings*/ + /* time transition */ + secs2 = (unsigned int)mktime(gmtime(&cur_time)); + tm_ptr = localtime(&cur_time); + secs = (unsigned int)mktime(tm_ptr); #ifdef MAC - /* Mac OS X's mktime function modifies the tm_ptr passed in for */ - /* the CDT timezone and breaks this algorithm. So on a Mac, we */ - /* will use the tm_ptr->gmtoff member to correct the time */ - secs = secs + tm_ptr->tm_gmtoff; + /* Mac OS X's mktime function modifies the tm_ptr passed in for */ + /* the CDT timezone and breaks this algorithm. So on a Mac, we */ + /* will use the tm_ptr->gmtoff member to correct the time */ + secs = secs + tm_ptr->tm_gmtoff; #else - secs = (unsigned int)cur_time - (secs2 - secs); + secs = (unsigned int)cur_time - (secs2 - secs); - if(tm_ptr->tm_isdst) { - /* adjust for daylight savings time */ - secs += 3600; - } + if(tm_ptr->tm_isdst) { + /* adjust for daylight savings time */ + secs += 3600; + } #endif #endif - /* add in secs to make date based on Apple Jan 1, 1904 instead of */ - /* Unix's Jan 1, 1970 */ - /* So add in 66 years and 17 leap year days (1904 is a leap year) */ - secs += ((66*365) + 17) * (24*3600); + /* add in secs to make date based on Apple Jan 1, 1904 instead of */ + /* Unix's Jan 1, 1970 */ + /* So add in 66 years and 17 leap year days (1904 is a leap year) */ + secs += ((66*365) + 17) * (24*3600); - g_clk_cur_time = secs; + g_clk_cur_time = secs; - clk_printf("Update g_clk_cur_time to %08x\n", g_clk_cur_time); - g_clk_next_vbl_update = g_vbl_count + 5; + clk_printf("Update g_clk_cur_time to %08x\n", g_clk_cur_time); + g_clk_next_vbl_update = g_vbl_count + 5; } /* clock_update called by sim65816 every VBL */ -void -clock_update() -{ - /* Nothing to do */ +void clock_update() { + /* Nothing to do */ } -void -clock_update_if_needed() -{ - int diff; +void clock_update_if_needed() { + int diff; - diff = g_clk_next_vbl_update - g_vbl_count; - if(diff < 0 || diff > 60) { - /* Been a while, re-read the clock */ - update_cur_time(); - } + diff = g_clk_next_vbl_update - g_vbl_count; + if(diff < 0 || diff > 60) { + /* Been a while, re-read the clock */ + update_cur_time(); + } } -void -clock_write_c034(word32 val) -{ - g_c034_val = val & 0x7f; - if((val & 0x80) != 0) { - if((val & 0x20) == 0) { - printf("c034 write not last = 1\n"); - /* set_halt(1); */ - } - do_clock_data(); - } +void clock_write_c034(word32 val) { + g_c034_val = val & 0x7f; + if((val & 0x80) != 0) { + if((val & 0x20) == 0) { + printf("c034 write not last = 1\n"); + /* set_halt(1); */ + } + do_clock_data(); + } } -void -do_clock_data() -{ - word32 mask; - int read; - int op; +void do_clock_data() { + word32 mask; + int read; + int op; - clk_printf("In do_clock_data, g_clk_mode: %02x\n", g_clk_mode); + clk_printf("In do_clock_data, g_clk_mode: %02x\n", g_clk_mode); - read = g_c034_val & 0x40; - switch(g_clk_mode) { - case CLK_IDLE: - g_clk_read = (g_c033_data >> 7) & 1; - g_clk_reg1 = (g_c033_data >> 2) & 3; - op = (g_c033_data >> 4) & 7; - if(!read) { - /* write */ - switch(op) { - case 0x0: /* Read/write seconds register */ - g_clk_mode = CLK_TIME; - clock_update_if_needed(); - break; - case 0x3: /* internal registers */ - g_clk_mode = CLK_INTERNAL; - if(g_clk_reg1 & 0x2) { - /* extend BRAM read */ - g_clk_mode = CLK_BRAM2; - g_clk_reg1 = (g_c033_data & 7) << 5; - } - break; - case 0x2: /* read/write ram 0x10-0x13 */ - g_clk_mode = CLK_BRAM1; - g_clk_reg1 += 0x10; - break; - case 0x4: /* read/write ram 0x00-0x0f */ - case 0x5: case 0x6: case 0x7: - g_clk_mode = CLK_BRAM1; - g_clk_reg1 = (g_c033_data >> 2) & 0xf; - break; - default: - halt_printf("Bad c033_data in CLK_IDLE: %02x\n", - g_c033_data); - } - } else { - printf("clk read from IDLE mode!\n"); - /* set_halt(1); */ - g_clk_mode = CLK_IDLE; - } - break; - case CLK_BRAM2: - if(!read) { - /* get more bits of bram addr */ - if((g_c033_data & 0x83) == 0x00) { - /* more address bits */ - g_clk_reg1 |= ((g_c033_data >> 2) & 0x1f); - g_clk_mode = CLK_BRAM1; - } else { - halt_printf("CLK_BRAM2: c033_data: %02x!\n", - g_c033_data); - g_clk_mode = CLK_IDLE; - } - } else { - halt_printf("CLK_BRAM2: clock read!\n"); - g_clk_mode = CLK_IDLE; - } - break; - case CLK_BRAM1: - /* access battery ram addr g_clk_reg1 */ - if(read) { - if(g_clk_read) { - /* Yup, read */ - if ((g_clk_reg1 == 0x28) && (g_temp_boot_slot != 254)){ - // Modify boot slot - g_c033_data = g_temp_boot_slot; - clk_calculate_bram_checksum(); - } else { - g_c033_data = g_bram_ptr[g_clk_reg1]; - } - clk_printf("Reading BRAM loc %02x: %02x\n", - g_clk_reg1, g_c033_data); - } else { - halt_printf("CLK_BRAM1: said wr, now read\n"); - } - } else { - if(g_clk_read) { - halt_printf("CLK_BRAM1: said rd, now write\n"); - } else { - /* Yup, write */ - if ((g_clk_reg1 == 0x28) && (g_temp_boot_slot != 254)) { - // Modify boot slot - g_bram_ptr[g_clk_reg1] = g_temp_boot_slot; - clk_calculate_bram_checksum(); - } else { - g_bram_ptr[g_clk_reg1] = g_c033_data; - } - clk_printf("Writing BRAM loc %02x with %02x\n", - g_clk_reg1, g_c033_data); - g_config_gsplus_update_needed = 1; - } - } - g_clk_mode = CLK_IDLE; - break; - case CLK_TIME: - if(read) { - if(g_clk_read == 0) { - halt_printf("Reading time, but in set mode!\n"); - } - g_c033_data = (g_clk_cur_time >> (g_clk_reg1 * 8)) & - 0xff; - clk_printf("Returning time byte %d: %02x\n", - g_clk_reg1, g_c033_data); - } else { - /* Write */ - if(g_clk_read) { - halt_printf("Write time, but in read mode!\n"); - } - clk_printf("Writing TIME loc %d with %02x\n", - g_clk_reg1, g_c033_data); - mask = 0xff << (8 * g_clk_reg1); + read = g_c034_val & 0x40; + switch(g_clk_mode) { + case CLK_IDLE: + g_clk_read = (g_c033_data >> 7) & 1; + g_clk_reg1 = (g_c033_data >> 2) & 3; + op = (g_c033_data >> 4) & 7; + if(!read) { + /* write */ + switch(op) { + case 0x0: /* Read/write seconds register */ + g_clk_mode = CLK_TIME; + clock_update_if_needed(); + break; + case 0x3: /* internal registers */ + g_clk_mode = CLK_INTERNAL; + if(g_clk_reg1 & 0x2) { + /* extend BRAM read */ + g_clk_mode = CLK_BRAM2; + g_clk_reg1 = (g_c033_data & 7) << 5; + } + break; + case 0x2: /* read/write ram 0x10-0x13 */ + g_clk_mode = CLK_BRAM1; + g_clk_reg1 += 0x10; + break; + case 0x4: /* read/write ram 0x00-0x0f */ + case 0x5: case 0x6: case 0x7: + g_clk_mode = CLK_BRAM1; + g_clk_reg1 = (g_c033_data >> 2) & 0xf; + break; + default: + halt_printf("Bad c033_data in CLK_IDLE: %02x\n", + g_c033_data); + } + } else { + printf("clk read from IDLE mode!\n"); + /* set_halt(1); */ + g_clk_mode = CLK_IDLE; + } + break; + case CLK_BRAM2: + if(!read) { + /* get more bits of bram addr */ + if((g_c033_data & 0x83) == 0x00) { + /* more address bits */ + g_clk_reg1 |= ((g_c033_data >> 2) & 0x1f); + g_clk_mode = CLK_BRAM1; + } else { + halt_printf("CLK_BRAM2: c033_data: %02x!\n", + g_c033_data); + g_clk_mode = CLK_IDLE; + } + } else { + halt_printf("CLK_BRAM2: clock read!\n"); + g_clk_mode = CLK_IDLE; + } + break; + case CLK_BRAM1: + /* access battery ram addr g_clk_reg1 */ + if(read) { + if(g_clk_read) { + /* Yup, read */ + if ((g_clk_reg1 == 0x28) && (g_temp_boot_slot != 254)) { + // Modify boot slot + g_c033_data = g_temp_boot_slot; + clk_calculate_bram_checksum(); + } else { + g_c033_data = g_bram_ptr[g_clk_reg1]; + } + clk_printf("Reading BRAM loc %02x: %02x\n", + g_clk_reg1, g_c033_data); + } else { + halt_printf("CLK_BRAM1: said wr, now read\n"); + } + } else { + if(g_clk_read) { + halt_printf("CLK_BRAM1: said rd, now write\n"); + } else { + /* Yup, write */ + if ((g_clk_reg1 == 0x28) && (g_temp_boot_slot != 254)) { + // Modify boot slot + g_bram_ptr[g_clk_reg1] = g_temp_boot_slot; + clk_calculate_bram_checksum(); + } else { + g_bram_ptr[g_clk_reg1] = g_c033_data; + } + clk_printf("Writing BRAM loc %02x with %02x\n", + g_clk_reg1, g_c033_data); + g_config_gsplus_update_needed = 1; + } + } + g_clk_mode = CLK_IDLE; + break; + case CLK_TIME: + if(read) { + if(g_clk_read == 0) { + halt_printf("Reading time, but in set mode!\n"); + } + g_c033_data = (g_clk_cur_time >> (g_clk_reg1 * 8)) & + 0xff; + clk_printf("Returning time byte %d: %02x\n", + g_clk_reg1, g_c033_data); + } else { + /* Write */ + if(g_clk_read) { + halt_printf("Write time, but in read mode!\n"); + } + clk_printf("Writing TIME loc %d with %02x\n", + g_clk_reg1, g_c033_data); + mask = 0xff << (8 * g_clk_reg1); - g_clk_cur_time = (g_clk_cur_time & (~mask)) | - ((g_c033_data & 0xff) << (8 * g_clk_reg1)); - } - g_clk_mode = CLK_IDLE; - break; - case CLK_INTERNAL: - if(read) { - printf("Attempting to read internal reg %02x!\n", - g_clk_reg1); - } else { - switch(g_clk_reg1) { - case 0x0: /* test register */ - if(g_c033_data & 0xc0) { - printf("Writing test reg: %02x!\n", - g_c033_data); - /* set_halt(1); */ - } - break; - case 0x1: /* write protect reg */ - clk_printf("Writing clk wr_protect: %02x\n", - g_c033_data); - if(g_c033_data & 0x80) { - printf("Stop, wr clk wr_prot: %02x\n", - g_c033_data); - /* set_halt(1); */ - } - break; - default: - halt_printf("Writing int reg: %02x with %02x\n", - g_clk_reg1, g_c033_data); - } - } - g_clk_mode = CLK_IDLE; - break; - default: - halt_printf("clk mode: %d unknown!\n", g_clk_mode); - g_clk_mode = CLK_IDLE; - break; - } + g_clk_cur_time = (g_clk_cur_time & (~mask)) | + ((g_c033_data & 0xff) << (8 * g_clk_reg1)); + } + g_clk_mode = CLK_IDLE; + break; + case CLK_INTERNAL: + if(read) { + printf("Attempting to read internal reg %02x!\n", + g_clk_reg1); + } else { + switch(g_clk_reg1) { + case 0x0: /* test register */ + if(g_c033_data & 0xc0) { + printf("Writing test reg: %02x!\n", + g_c033_data); + /* set_halt(1); */ + } + break; + case 0x1: /* write protect reg */ + clk_printf("Writing clk wr_protect: %02x\n", + g_c033_data); + if(g_c033_data & 0x80) { + printf("Stop, wr clk wr_prot: %02x\n", + g_c033_data); + /* set_halt(1); */ + } + break; + default: + halt_printf("Writing int reg: %02x with %02x\n", + g_clk_reg1, g_c033_data); + } + } + g_clk_mode = CLK_IDLE; + break; + default: + halt_printf("clk mode: %d unknown!\n", g_clk_mode); + g_clk_mode = CLK_IDLE; + break; + } } diff --git a/src/compile_time.c b/src/compile_time.c index 4ce7f0f..ae829b7 100644 --- a/src/compile_time.c +++ b/src/compile_time.c @@ -1,8 +1,8 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ -char g_compile_time[] = "Compiled: " __DATE__ " " __TIME__ ; +char g_compile_time[] = "Compiled: " __DATE__ " " __TIME__; diff --git a/src/config.c b/src/config.c index 16eaa42..7b58392 100644 --- a/src/config.c +++ b/src/config.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include @@ -154,29 +154,29 @@ int g_config_gsplus_auto_update = 1; int g_config_gsplus_update_needed = 0; const char *g_config_gsplus_name_list[] = { - "config.txt", "config.gsp", ".config.gsp",0 + "config.txt", "config.gsp", ".config.gsp",0 }; -int g_highest_smartport_unit = -1; -int g_reparse_delay = 0; -int g_user_page2_shadow = 1; +int g_highest_smartport_unit = -1; +int g_reparse_delay = 0; +int g_user_page2_shadow = 1; -byte g_save_text_screen_bytes[0x800]; -int g_save_cur_a2_stat = 0; -char g_cfg_printf_buf[CFG_PRINTF_BUFSIZE]; -char g_config_gsplus_buf[CONF_BUF_LEN]; +byte g_save_text_screen_bytes[0x800]; +int g_save_cur_a2_stat = 0; +char g_cfg_printf_buf[CFG_PRINTF_BUFSIZE]; +char g_config_gsplus_buf[CONF_BUF_LEN]; -word32 g_cfg_vbl_count = 0; +word32 g_cfg_vbl_count = 0; -int g_cfg_curs_x = 0; -int g_cfg_curs_y = 0; -int g_cfg_curs_inv = 0; -int g_cfg_curs_mousetext = 0; +int g_cfg_curs_x = 0; +int g_cfg_curs_y = 0; +int g_cfg_curs_inv = 0; +int g_cfg_curs_mousetext = 0; int g_cfg_triggeriwreset = 0; #define CFG_PG_SCROLL_AMT 15 -#define CFG_MAX_OPTS 16 -#define CFG_OPT_MAXSTR 100 +#define CFG_MAX_OPTS 16 +#define CFG_OPT_MAXSTR 100 int g_cfg_opts_vals[CFG_MAX_OPTS]; char g_cfg_opts_strs[CFG_MAX_OPTS][CFG_OPT_MAXSTR]; @@ -191,66 +191,66 @@ int g_cfg_file_max_size = 2047*1024*1024; int g_cfg_file_dir_only = 0; -#define MAX_PARTITION_BLK_SIZE 65536 +#define MAX_PARTITION_BLK_SIZE 65536 extern Cfg_menu g_cfg_main_menu[]; -#define KNMP(a) &a, #a, 0 +#define KNMP(a) &a, #a, 0 // This first menu is not a menu, but a list of config options that are // represented here so they will be parsed correctly out of the config files. Cfg_menu g_cfg_uiless_menu[] = { - { "", KNMP(g_audio_enable), CFGTYPE_INT }, - { "", KNMP(g_preferred_rate), CFGTYPE_INT }, - { "", KNMP(g_fullscreen), CFGTYPE_INT }, - { "", KNMP(g_highdpi), CFGTYPE_INT }, - { "", KNMP(g_borderless), CFGTYPE_INT }, - { "", KNMP(g_resizeable), CFGTYPE_INT }, - { "", KNMP(g_screen_redraw_skip_amt), CFGTYPE_INT }, - { "", KNMP(g_use_dhr140), CFGTYPE_INT }, - { "", KNMP(g_use_bw_hires), CFGTYPE_INT }, - { "", KNMP(g_scanline_simulator), CFGTYPE_INT }, - { "", KNMP(g_startx), CFGTYPE_INT }, - { "", KNMP(g_starty), CFGTYPE_INT }, - { "", KNMP(g_joystick_number), CFGTYPE_INT }, - { "", KNMP(g_joystick_x_axis), CFGTYPE_INT }, - { "", KNMP(g_joystick_y_axis), CFGTYPE_INT }, - { "", KNMP(g_joystick_x2_axis), CFGTYPE_INT }, - { "", KNMP(g_joystick_y2_axis), CFGTYPE_INT }, - { "", KNMP(g_joystick_button_0), CFGTYPE_INT }, - { "", KNMP(g_joystick_button_1), CFGTYPE_INT }, - { "", KNMP(g_joystick_button_2), CFGTYPE_INT }, - { "", KNMP(g_joystick_button_3), CFGTYPE_INT }, - { "", KNMP(g_ethernet), CFGTYPE_INT }, - { "", KNMP(g_halt_on_bad_read), CFGTYPE_INT }, - { "", KNMP(g_ignore_bad_acc), CFGTYPE_INT }, - { "", KNMP(g_ignore_halts), CFGTYPE_INT }, - { 0, 0, 0, 0, 0 }, + { "", KNMP(g_audio_enable), CFGTYPE_INT }, + { "", KNMP(g_preferred_rate), CFGTYPE_INT }, + { "", KNMP(g_fullscreen), CFGTYPE_INT }, + { "", KNMP(g_highdpi), CFGTYPE_INT }, + { "", KNMP(g_borderless), CFGTYPE_INT }, + { "", KNMP(g_resizeable), CFGTYPE_INT }, + { "", KNMP(g_screen_redraw_skip_amt), CFGTYPE_INT }, + { "", KNMP(g_use_dhr140), CFGTYPE_INT }, + { "", KNMP(g_use_bw_hires), CFGTYPE_INT }, + { "", KNMP(g_scanline_simulator), CFGTYPE_INT }, + { "", KNMP(g_startx), CFGTYPE_INT }, + { "", KNMP(g_starty), CFGTYPE_INT }, + { "", KNMP(g_joystick_number), CFGTYPE_INT }, + { "", KNMP(g_joystick_x_axis), CFGTYPE_INT }, + { "", KNMP(g_joystick_y_axis), CFGTYPE_INT }, + { "", KNMP(g_joystick_x2_axis), CFGTYPE_INT }, + { "", KNMP(g_joystick_y2_axis), CFGTYPE_INT }, + { "", KNMP(g_joystick_button_0), CFGTYPE_INT }, + { "", KNMP(g_joystick_button_1), CFGTYPE_INT }, + { "", KNMP(g_joystick_button_2), CFGTYPE_INT }, + { "", KNMP(g_joystick_button_3), CFGTYPE_INT }, + { "", KNMP(g_ethernet), CFGTYPE_INT }, + { "", KNMP(g_halt_on_bad_read), CFGTYPE_INT }, + { "", KNMP(g_ignore_bad_acc), CFGTYPE_INT }, + { "", KNMP(g_ignore_halts), CFGTYPE_INT }, + { 0, 0, 0, 0, 0 }, }; Cfg_menu g_cfg_disk_menu[] = { -{ "Disk Configuration", g_cfg_disk_menu, 0, 0, CFGTYPE_MENU }, -{ "s5d1 = ", 0, 0, 0, CFGTYPE_DISK + 0x5000 }, -{ "s5d2 = ", 0, 0, 0, CFGTYPE_DISK + 0x5010 }, -{ "", 0, 0, 0, 0 }, -{ "s6d1 = ", 0, 0, 0, CFGTYPE_DISK + 0x6000 }, -{ "s6d2 = ", 0, 0, 0, CFGTYPE_DISK + 0x6010 }, -{ "", 0, 0, 0, 0 }, -{ "s7d1 = ", 0, 0, 0, CFGTYPE_DISK + 0x7000 }, -{ "s7d2 = ", 0, 0, 0, CFGTYPE_DISK + 0x7010 }, -{ "s7d3 = ", 0, 0, 0, CFGTYPE_DISK + 0x7020 }, -{ "s7d4 = ", 0, 0, 0, CFGTYPE_DISK + 0x7030 }, -{ "s7d5 = ", 0, 0, 0, CFGTYPE_DISK + 0x7040 }, -{ "s7d6 = ", 0, 0, 0, CFGTYPE_DISK + 0x7050 }, -{ "s7d7 = ", 0, 0, 0, CFGTYPE_DISK + 0x7060 }, -{ "s7d8 = ", 0, 0, 0, CFGTYPE_DISK + 0x7070 }, -{ "s7d9 = ", 0, 0, 0, CFGTYPE_DISK + 0x7080 }, -{ "s7d10 = ", 0, 0, 0, CFGTYPE_DISK + 0x7090 }, -{ "s7d11 = ", 0, 0, 0, CFGTYPE_DISK + 0x70a0 }, -{ "", 0, 0, 0, 0 }, -{ "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, -{ 0, 0, 0, 0, 0 }, + { "Disk Configuration", g_cfg_disk_menu, 0, 0, CFGTYPE_MENU }, + { "s5d1 = ", 0, 0, 0, CFGTYPE_DISK + 0x5000 }, + { "s5d2 = ", 0, 0, 0, CFGTYPE_DISK + 0x5010 }, + { "", 0, 0, 0, 0 }, + { "s6d1 = ", 0, 0, 0, CFGTYPE_DISK + 0x6000 }, + { "s6d2 = ", 0, 0, 0, CFGTYPE_DISK + 0x6010 }, + { "", 0, 0, 0, 0 }, + { "s7d1 = ", 0, 0, 0, CFGTYPE_DISK + 0x7000 }, + { "s7d2 = ", 0, 0, 0, CFGTYPE_DISK + 0x7010 }, + { "s7d3 = ", 0, 0, 0, CFGTYPE_DISK + 0x7020 }, + { "s7d4 = ", 0, 0, 0, CFGTYPE_DISK + 0x7030 }, + { "s7d5 = ", 0, 0, 0, CFGTYPE_DISK + 0x7040 }, + { "s7d6 = ", 0, 0, 0, CFGTYPE_DISK + 0x7050 }, + { "s7d7 = ", 0, 0, 0, CFGTYPE_DISK + 0x7060 }, + { "s7d8 = ", 0, 0, 0, CFGTYPE_DISK + 0x7070 }, + { "s7d9 = ", 0, 0, 0, CFGTYPE_DISK + 0x7080 }, + { "s7d10 = ", 0, 0, 0, CFGTYPE_DISK + 0x7090 }, + { "s7d11 = ", 0, 0, 0, CFGTYPE_DISK + 0x70a0 }, + { "", 0, 0, 0, 0 }, + { "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, + { 0, 0, 0, 0, 0 }, }; // OG Use define instead of const for joystick_types @@ -258,219 +258,219 @@ Cfg_menu g_cfg_disk_menu[] = { #define TOSTRING(x) STRINGIFY(x) Cfg_menu g_cfg_joystick_menu[] = { -{ "Joystick Configuration", g_cfg_joystick_menu, 0, 0, CFGTYPE_MENU }, -{ "Joystick Emulation,"TOSTRING(JOYSTICK_TYPE_KEYPAD)",Keypad Joystick,"TOSTRING(JOYSTICK_TYPE_MOUSE)",Mouse Joystick,"TOSTRING(JOYSTICK_TYPE_NATIVE_1)",Native Joystick 1," - TOSTRING(JOYSTICK_TYPE_NATIVE_2)",Native Joystick 2,"TOSTRING(JOYSTICK_TYPE_NONE)",No Joystick", KNMP(g_joystick_type), CFGTYPE_INT }, -{ "Joystick Scale X,0x100,Standard,0x119,+10%,0x133,+20%," - "0x150,+30%,0xb0,-30%,0xcd,-20%,0xe7,-10%", - KNMP(g_joystick_scale_factor_x), CFGTYPE_INT }, -{ "Joystick Scale Y,0x100,Standard,0x119,+10%,0x133,+20%," - "0x150,+30%,0xb0,-30%,0xcd,-20%,0xe7,-10%", - KNMP(g_joystick_scale_factor_y), CFGTYPE_INT }, -{ "Joystick Trim X", KNMP(g_joystick_trim_amount_x), CFGTYPE_INT }, -{ "Joystick Trim Y", KNMP(g_joystick_trim_amount_y), CFGTYPE_INT }, -{ "Swap Joystick X and Y,0,Normal operation,1,Paddle 1 and Paddle 0 swapped", - KNMP(g_swap_paddles), CFGTYPE_INT }, -{ "Invert Joystick,0,Normal operation,1,Left becomes right and up becomes down", - KNMP(g_invert_paddles), CFGTYPE_INT }, -{ "", 0, 0, 0, 0 }, -{ "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, -{ 0, 0, 0, 0, 0 }, + { "Joystick Configuration", g_cfg_joystick_menu, 0, 0, CFGTYPE_MENU }, + { "Joystick Emulation,"TOSTRING (JOYSTICK_TYPE_KEYPAD)",Keypad Joystick,"TOSTRING (JOYSTICK_TYPE_MOUSE)",Mouse Joystick,"TOSTRING (JOYSTICK_TYPE_NATIVE_1)",Native Joystick 1," + TOSTRING(JOYSTICK_TYPE_NATIVE_2) ",Native Joystick 2,"TOSTRING (JOYSTICK_TYPE_NONE)",No Joystick", KNMP(g_joystick_type), CFGTYPE_INT }, + { "Joystick Scale X,0x100,Standard,0x119,+10%,0x133,+20%," + "0x150,+30%,0xb0,-30%,0xcd,-20%,0xe7,-10%", + KNMP(g_joystick_scale_factor_x), CFGTYPE_INT }, + { "Joystick Scale Y,0x100,Standard,0x119,+10%,0x133,+20%," + "0x150,+30%,0xb0,-30%,0xcd,-20%,0xe7,-10%", + KNMP(g_joystick_scale_factor_y), CFGTYPE_INT }, + { "Joystick Trim X", KNMP(g_joystick_trim_amount_x), CFGTYPE_INT }, + { "Joystick Trim Y", KNMP(g_joystick_trim_amount_y), CFGTYPE_INT }, + { "Swap Joystick X and Y,0,Normal operation,1,Paddle 1 and Paddle 0 swapped", + KNMP(g_swap_paddles), CFGTYPE_INT }, + { "Invert Joystick,0,Normal operation,1,Left becomes right and up becomes down", + KNMP(g_invert_paddles), CFGTYPE_INT }, + { "", 0, 0, 0, 0 }, + { "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, + { 0, 0, 0, 0, 0 }, }; Cfg_menu g_cfg_rom_menu[] = { -{ "ROM File Selection", g_cfg_rom_menu, 0, 0, CFGTYPE_MENU }, -{ "ROM File", KNMP(g_cfg_rom_path), CFGTYPE_FILE }, -{ "", 0, 0, 0, 0 }, -{ "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, -{ 0, 0, 0, 0, 0 }, + { "ROM File Selection", g_cfg_rom_menu, 0, 0, CFGTYPE_MENU }, + { "ROM File", KNMP(g_cfg_rom_path), CFGTYPE_FILE }, + { "", 0, 0, 0, 0 }, + { "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, + { 0, 0, 0, 0, 0 }, }; Cfg_menu g_cfg_host_menu[] = { -{ "Host FST Configuration", g_cfg_host_menu, 0, 0, CFGTYPE_MENU }, -{ "Shared Host Folder", KNMP(g_cfg_host_path), CFGTYPE_DIR }, -{ "Read Only,0,No,1,Yes", KNMP(g_cfg_host_read_only), CFGTYPE_INT }, -{ "CR/LF conversion,0,No,1,Yes", KNMP(g_cfg_host_crlf), CFGTYPE_INT }, -{ "Merlin conversion,0,No,1,Yes", KNMP(g_cfg_host_merlin), CFGTYPE_INT }, -{ "", 0, 0, 0, 0 }, -{ "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, -{ 0, 0, 0, 0, 0 }, + { "Host FST Configuration", g_cfg_host_menu, 0, 0, CFGTYPE_MENU }, + { "Shared Host Folder", KNMP(g_cfg_host_path), CFGTYPE_DIR }, + { "Read Only,0,No,1,Yes", KNMP(g_cfg_host_read_only), CFGTYPE_INT }, + { "CR/LF conversion,0,No,1,Yes", KNMP(g_cfg_host_crlf), CFGTYPE_INT }, + { "Merlin conversion,0,No,1,Yes", KNMP(g_cfg_host_merlin), CFGTYPE_INT }, + { "", 0, 0, 0, 0 }, + { "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, + { 0, 0, 0, 0, 0 }, }; Cfg_menu g_cfg_serial_menu[] = { -{ "Serial Port Configuration", g_cfg_serial_menu, 0, 0, CFGTYPE_MENU }, + { "Serial Port Configuration", g_cfg_serial_menu, 0, 0, CFGTYPE_MENU }, #ifdef HAVE_SDL -{ "Port 0 (slot 1),0,Only use socket 6501,1,Use real port if avail,2,Virtual ImageWriter", - KNMP(g_serial_type[0]), CFGTYPE_INT }, -{ "Port 1 (slot 2),0,Only use socket 6502,1,Use real port if avail,2,Virtual ImageWriter", - KNMP(g_serial_type[1]), CFGTYPE_INT }, + { "Port 0 (slot 1),0,Only use socket 6501,1,Use real port if avail,2,Virtual ImageWriter", + KNMP(g_serial_type[0]), CFGTYPE_INT }, + { "Port 1 (slot 2),0,Only use socket 6502,1,Use real port if avail,2,Virtual ImageWriter", + KNMP(g_serial_type[1]), CFGTYPE_INT }, #else -{ "Port 0 (slot 1),0,Only use socket 6501,1,Use real port if avail", - KNMP(g_serial_type[0]), CFGTYPE_INT }, -{ "Port 1 (slot 2),0,Only use socket 6502,1,Use real port if avail", - KNMP(g_serial_type[1]), CFGTYPE_INT }, + { "Port 0 (slot 1),0,Only use socket 6501,1,Use real port if avail", + KNMP(g_serial_type[0]), CFGTYPE_INT }, + { "Port 1 (slot 2),0,Only use socket 6502,1,Use real port if avail", + KNMP(g_serial_type[1]), CFGTYPE_INT }, #endif -{ "Serial Output,0,Send full 8-bit data,1,Mask off high bit", - KNMP(g_serial_out_masking), CFGTYPE_INT }, -{ "Modem on port 0 (slot 1),0,Simple socket emulation mode,1,Modem with " - "incoming and outgoing emulation", KNMP(g_serial_modem[0]), - CFGTYPE_INT }, -{ "Modem on port 1 (slot 2),0,Simple socket emulation mode,1,Modem with " - "incoming and outgoing emulation", KNMP(g_serial_modem[1]), - CFGTYPE_INT }, -{ "", 0, 0, 0, 0 }, -{ "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, -{ 0, 0, 0, 0, 0 }, + { "Serial Output,0,Send full 8-bit data,1,Mask off high bit", + KNMP(g_serial_out_masking), CFGTYPE_INT }, + { "Modem on port 0 (slot 1),0,Simple socket emulation mode,1,Modem with " + "incoming and outgoing emulation", KNMP(g_serial_modem[0]), + CFGTYPE_INT }, + { "Modem on port 1 (slot 2),0,Simple socket emulation mode,1,Modem with " + "incoming and outgoing emulation", KNMP(g_serial_modem[1]), + CFGTYPE_INT }, + { "", 0, 0, 0, 0 }, + { "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, + { 0, 0, 0, 0, 0 }, }; Cfg_menu g_cfg_parallel_menu[] = { -{ "Parallel Card Configuration", g_cfg_parallel_menu, 0, 0, CFGTYPE_MENU }, -{ "Parallel Card in Slot 1,0,Off,1,On", - KNMP(g_parallel), CFGTYPE_INT }, -{ "Parallel Output,0,Send full 8-bit data,1,Mask off high bit", - KNMP(g_parallel_out_masking), CFGTYPE_INT }, -{ "", 0, 0, 0, 0 }, -{ "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, -{ 0, 0, 0, 0, 0 }, + { "Parallel Card Configuration", g_cfg_parallel_menu, 0, 0, CFGTYPE_MENU }, + { "Parallel Card in Slot 1,0,Off,1,On", + KNMP(g_parallel), CFGTYPE_INT }, + { "Parallel Output,0,Send full 8-bit data,1,Mask off high bit", + KNMP(g_parallel_out_masking), CFGTYPE_INT }, + { "", 0, 0, 0, 0 }, + { "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, + { 0, 0, 0, 0, 0 }, }; Cfg_menu g_cfg_ethernet_menu[] = { -{ "Ethernet Card Configuration", g_cfg_ethernet_menu, 0, 0, CFGTYPE_MENU }, -{ "Use Interface Number,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10", - KNMP(g_ethernet_interface), CFGTYPE_INT }, -{ "", 0, 0, 0, 0 }, -{ "Uthernet Card in Slot 3,0,Off,1,On", - KNMP(g_ethernet), CFGTYPE_INT }, + { "Ethernet Card Configuration", g_cfg_ethernet_menu, 0, 0, CFGTYPE_MENU }, + { "Use Interface Number,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10", + KNMP(g_ethernet_interface), CFGTYPE_INT }, + { "", 0, 0, 0, 0 }, + { "Uthernet Card in Slot 3,0,Off,1,On", + KNMP(g_ethernet), CFGTYPE_INT }, #ifdef HAVE_ATBRIDGE -{ "", 0, 0, 0, 0 }, -{ "AppleTalk Bridging,0,Off,1,On", - KNMP(g_appletalk_bridging), CFGTYPE_INT }, -{ "AppleTalk Speed,0,Normal (230.4 kbps),1,Turbo", - KNMP(g_appletalk_turbo), CFGTYPE_INT }, + { "", 0, 0, 0, 0 }, + { "AppleTalk Bridging,0,Off,1,On", + KNMP(g_appletalk_bridging), CFGTYPE_INT }, + { "AppleTalk Speed,0,Normal (230.4 kbps),1,Turbo", + KNMP(g_appletalk_turbo), CFGTYPE_INT }, #endif -{ "", 0, 0, 0, 0 }, -{ "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, -{ 0, 0, 0, 0, 0 }, + { "", 0, 0, 0, 0 }, + { "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, + { 0, 0, 0, 0, 0 }, }; #ifdef HAVE_SDL Cfg_menu g_cfg_printer_menu[] = { -{ "Virtual Epson Configuration", g_cfg_printer_menu, 0, 0, CFGTYPE_MENU }, -{ "Virtual Printer Type,0,Epson LQ", - KNMP(g_printer), CFGTYPE_INT }, -{ "Printer DPI,60,60x60 dpi,180,180x180 dpi,360,360x360 dpi", - KNMP(g_printer_dpi), CFGTYPE_INT }, -{ "Printer Output Type,bmp,Windows Bitmap,ps,Postscript (B&W),printer,Direct to host printer,text,Text file", - KNMP(g_printer_output), CFGTYPE_STR }, -{ "Multipage Files? (PS and Direct to Host Only),0,No,1,Yes", - KNMP(g_printer_multipage), CFGTYPE_INT }, -{ "Printer Timeout,0,Never,2,2 sec.,15,15 sec.,30,30 sec.,60, 1 min.", - KNMP(g_printer_timeout), CFGTYPE_INT }, -{ "", 0, 0, 0, 0 }, -{ "Epson LQ Fonts", 0, 0, 0, 0 }, -{ "--------------", 0, 0, 0, 0 }, -{ "", 0, 0, 0, 0 }, -{ "Roman", KNMP(g_printer_font_roman), CFGTYPE_FILE }, -{ "Sans Serif", KNMP(g_printer_font_sans), CFGTYPE_FILE }, -{ "Courier", KNMP(g_printer_font_courier), CFGTYPE_FILE }, -{ "Prestige", KNMP(g_printer_font_prestige), CFGTYPE_FILE }, -{ "Script", KNMP(g_printer_font_script), CFGTYPE_FILE }, -{ "OCR A/B", KNMP(g_printer_font_ocra), CFGTYPE_FILE }, -{ "", 0, 0, 0, 0 }, -{ "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, -{ 0, 0, 0, 0, 0 }, + { "Virtual Epson Configuration", g_cfg_printer_menu, 0, 0, CFGTYPE_MENU }, + { "Virtual Printer Type,0,Epson LQ", + KNMP(g_printer), CFGTYPE_INT }, + { "Printer DPI,60,60x60 dpi,180,180x180 dpi,360,360x360 dpi", + KNMP(g_printer_dpi), CFGTYPE_INT }, + { "Printer Output Type,bmp,Windows Bitmap,ps,Postscript (B&W),printer,Direct to host printer,text,Text file", + KNMP(g_printer_output), CFGTYPE_STR }, + { "Multipage Files? (PS and Direct to Host Only),0,No,1,Yes", + KNMP(g_printer_multipage), CFGTYPE_INT }, + { "Printer Timeout,0,Never,2,2 sec.,15,15 sec.,30,30 sec.,60, 1 min.", + KNMP(g_printer_timeout), CFGTYPE_INT }, + { "", 0, 0, 0, 0 }, + { "Epson LQ Fonts", 0, 0, 0, 0 }, + { "--------------", 0, 0, 0, 0 }, + { "", 0, 0, 0, 0 }, + { "Roman", KNMP(g_printer_font_roman), CFGTYPE_FILE }, + { "Sans Serif", KNMP(g_printer_font_sans), CFGTYPE_FILE }, + { "Courier", KNMP(g_printer_font_courier), CFGTYPE_FILE }, + { "Prestige", KNMP(g_printer_font_prestige), CFGTYPE_FILE }, + { "Script", KNMP(g_printer_font_script), CFGTYPE_FILE }, + { "OCR A/B", KNMP(g_printer_font_ocra), CFGTYPE_FILE }, + { "", 0, 0, 0, 0 }, + { "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, + { 0, 0, 0, 0, 0 }, }; Cfg_menu g_cfg_imagewriter_menu[] = { -{ "Virtual ImageWriter Configuration", g_cfg_imagewriter_menu, 0, 0, CFGTYPE_MENU }, -{ "Virtual Printer Type,0,ImageWriter II,1,ImageWriter LQ", - KNMP(g_imagewriter), CFGTYPE_INT }, -{ "Paper Size,0,US Letter (8.5x11in),1,US Legal (8.5x14in),2,ISO A4 (210 x 297mm),3,ISO B5 (176 x 250mm),4,Wide Fanfold (14 x 11in),5,Ledger (11 x 17in),6,ISO A3 (297 x 420mm)", - KNMP(g_imagewriter_paper), CFGTYPE_INT }, -{ "Printer DPI,360,360x360 dpi (Best for 8-bit software),720,720x720 dpi (Best for GS/OS & IW LQ Modes),1440,1440x1440 dpi", - KNMP(g_imagewriter_dpi), CFGTYPE_INT }, -{ "Banner Printing (Limited To 144x144 dpi Output),0,Banner Printing Off,3,3 Pages Long,4,4 Pages Long,5,5 Pages Long,6,6 Pages Long,7,7 Pages Long,8,8 Pages Long,9,9 Pages Long,10,10 Pages Long", - KNMP(g_imagewriter_banner), CFGTYPE_INT }, -{ "Printer Output Type,bmp,Windows Bitmap,ps,Postscript (B&W),colorps,Postscript (Color),printer,Direct to host printer,text,Text file", - KNMP(g_imagewriter_output), CFGTYPE_STR }, -{ "Multipage Files? (PS and Direct to Host Only),0,No,1,Yes", - KNMP(g_imagewriter_multipage), CFGTYPE_INT }, -{ "Printer Timeout,0,Never,2,2 sec.,15,15 sec.,30,30 sec.,60, 1 min.", - KNMP(g_imagewriter_timeout), CFGTYPE_INT }, -{ "", 0, 0, 0, 0 }, -{ "ImageWriter Fonts", 0, 0, 0, 0 }, -{ "-----------------", 0, 0, 0, 0 }, -{ "", 0, 0, 0, 0 }, -{ "Fixed Width Font", KNMP(g_imagewriter_fixed_font), CFGTYPE_FILE }, -{ "", 0, 0, 0, 0 }, -{ "Proportional Font", KNMP(g_imagewriter_prop_font), CFGTYPE_FILE }, -{ "", 0, 0, 0, 0 }, -{ "", 0, 0, 0, 0 }, -{ "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, -{ 0, 0, 0, 0, 0 }, + { "Virtual ImageWriter Configuration", g_cfg_imagewriter_menu, 0, 0, CFGTYPE_MENU }, + { "Virtual Printer Type,0,ImageWriter II,1,ImageWriter LQ", + KNMP(g_imagewriter), CFGTYPE_INT }, + { "Paper Size,0,US Letter (8.5x11in),1,US Legal (8.5x14in),2,ISO A4 (210 x 297mm),3,ISO B5 (176 x 250mm),4,Wide Fanfold (14 x 11in),5,Ledger (11 x 17in),6,ISO A3 (297 x 420mm)", + KNMP(g_imagewriter_paper), CFGTYPE_INT }, + { "Printer DPI,360,360x360 dpi (Best for 8-bit software),720,720x720 dpi (Best for GS/OS & IW LQ Modes),1440,1440x1440 dpi", + KNMP(g_imagewriter_dpi), CFGTYPE_INT }, + { "Banner Printing (Limited To 144x144 dpi Output),0,Banner Printing Off,3,3 Pages Long,4,4 Pages Long,5,5 Pages Long,6,6 Pages Long,7,7 Pages Long,8,8 Pages Long,9,9 Pages Long,10,10 Pages Long", + KNMP(g_imagewriter_banner), CFGTYPE_INT }, + { "Printer Output Type,bmp,Windows Bitmap,ps,Postscript (B&W),colorps,Postscript (Color),printer,Direct to host printer,text,Text file", + KNMP(g_imagewriter_output), CFGTYPE_STR }, + { "Multipage Files? (PS and Direct to Host Only),0,No,1,Yes", + KNMP(g_imagewriter_multipage), CFGTYPE_INT }, + { "Printer Timeout,0,Never,2,2 sec.,15,15 sec.,30,30 sec.,60, 1 min.", + KNMP(g_imagewriter_timeout), CFGTYPE_INT }, + { "", 0, 0, 0, 0 }, + { "ImageWriter Fonts", 0, 0, 0, 0 }, + { "-----------------", 0, 0, 0, 0 }, + { "", 0, 0, 0, 0 }, + { "Fixed Width Font", KNMP(g_imagewriter_fixed_font), CFGTYPE_FILE }, + { "", 0, 0, 0, 0 }, + { "Proportional Font", KNMP(g_imagewriter_prop_font), CFGTYPE_FILE }, + { "", 0, 0, 0, 0 }, + { "", 0, 0, 0, 0 }, + { "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, + { 0, 0, 0, 0, 0 }, }; #endif Cfg_menu g_cfg_devel_menu[] = { -{ "Developer Options", g_cfg_devel_menu, 0, 0, CFGTYPE_MENU }, + { "Developer Options", g_cfg_devel_menu, 0, 0, CFGTYPE_MENU }, #if defined(_WIN32) || defined(__CYGWIN__) && !defined(WIN_SDL) -{ "Status lines,0,Hide,1,Show", KNMP(g_win_status_debug_request), CFGTYPE_INT }, -{ "Console,0,Hide,1,Show", KNMP(g_win_show_console_request), CFGTYPE_INT }, + { "Status lines,0,Hide,1,Show", KNMP(g_win_status_debug_request), CFGTYPE_INT }, + { "Console,0,Hide,1,Show", KNMP(g_win_show_console_request), CFGTYPE_INT }, #endif #ifdef HAVE_ATBRIDGE -{ "", 0, 0, 0, 0 }, -{ "Show AppleTalk Diagnostics,0,No,1,Yes", KNMP(g_appletalk_diagnostics), CFGTYPE_INT }, -{ "AppleTalk Network Hint", KNMP(g_appletalk_network_hint), CFGTYPE_INT }, + { "", 0, 0, 0, 0 }, + { "Show AppleTalk Diagnostics,0,No,1,Yes", KNMP(g_appletalk_diagnostics), CFGTYPE_INT }, + { "AppleTalk Network Hint", KNMP(g_appletalk_network_hint), CFGTYPE_INT }, #endif -{ "", 0, 0, 0, 0 }, + { "", 0, 0, 0, 0 }, #ifndef _WIN32 -{ "Force X-windows display depth", KNMP(g_force_depth), CFGTYPE_INT }, + { "Force X-windows display depth", KNMP(g_force_depth), CFGTYPE_INT }, #endif -{ "Code Red Halts,0,Do not stop on bad accesses,1,Enter debugger on bad accesses", KNMP(g_user_halt_bad), CFGTYPE_INT }, -{ "3200 Color Enable,0,Auto (Full if fast enough),1,Full (Update every line),8,Off (Update video every 8 lines)", KNMP(g_video_line_update_interval), CFGTYPE_INT }, -{ "Keyboard and mouse poll rate,0,60 times per second,1,240 times per second", KNMP(g_video_extra_check_inputs), CFGTYPE_INT }, -{ "Enable Text Page 2 Shadow,0,Disabled on ROM 01 (matches real hardware),1,Enabled on ROM 01 and 03", KNMP(g_user_page2_shadow), CFGTYPE_INT }, -{ "", 0, 0, 0, 0 }, -{ "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, -{ 0, 0, 0, 0, 0 }, + { "Code Red Halts,0,Do not stop on bad accesses,1,Enter debugger on bad accesses", KNMP(g_user_halt_bad), CFGTYPE_INT }, + { "3200 Color Enable,0,Auto (Full if fast enough),1,Full (Update every line),8,Off (Update video every 8 lines)", KNMP(g_video_line_update_interval), CFGTYPE_INT }, + { "Keyboard and mouse poll rate,0,60 times per second,1,240 times per second", KNMP(g_video_extra_check_inputs), CFGTYPE_INT }, + { "Enable Text Page 2 Shadow,0,Disabled on ROM 01 (matches real hardware),1,Enabled on ROM 01 and 03", KNMP(g_user_page2_shadow), CFGTYPE_INT }, + { "", 0, 0, 0, 0 }, + { "Back to Main Config", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, + { 0, 0, 0, 0, 0 }, }; Cfg_menu g_cfg_main_menu[] = { -{ "GSplus Configuration", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, -{ "Disk Configuration", g_cfg_disk_menu, 0, 0, CFGTYPE_MENU }, -{ "Joystick Configuration", g_cfg_joystick_menu, 0, 0, CFGTYPE_MENU }, -{ "ROM File Selection", g_cfg_rom_menu, 0, 0, CFGTYPE_MENU }, -{ "HOST FST Configuration", g_cfg_host_menu, 0, 0, CFGTYPE_MENU }, -{ "Serial Port Configuration", g_cfg_serial_menu, 0, 0, CFGTYPE_MENU }, -{ "Ethernet Card Configuration", g_cfg_ethernet_menu, 0, 0, CFGTYPE_MENU }, -{ "Parallel Card Configuration", g_cfg_parallel_menu, 0, 0, CFGTYPE_MENU }, + { "GSplus Configuration", g_cfg_main_menu, 0, 0, CFGTYPE_MENU }, + { "Disk Configuration", g_cfg_disk_menu, 0, 0, CFGTYPE_MENU }, + { "Joystick Configuration", g_cfg_joystick_menu, 0, 0, CFGTYPE_MENU }, + { "ROM File Selection", g_cfg_rom_menu, 0, 0, CFGTYPE_MENU }, + { "HOST FST Configuration", g_cfg_host_menu, 0, 0, CFGTYPE_MENU }, + { "Serial Port Configuration", g_cfg_serial_menu, 0, 0, CFGTYPE_MENU }, + { "Ethernet Card Configuration", g_cfg_ethernet_menu, 0, 0, CFGTYPE_MENU }, + { "Parallel Card Configuration", g_cfg_parallel_menu, 0, 0, CFGTYPE_MENU }, #ifdef HAVE_SDL -{ "Virtual Epson Configuration", g_cfg_printer_menu, 0, 0, CFGTYPE_MENU }, -{ "Virtual ImageWriter Configuration", g_cfg_imagewriter_menu, 0, 0, CFGTYPE_MENU }, + { "Virtual Epson Configuration", g_cfg_printer_menu, 0, 0, CFGTYPE_MENU }, + { "Virtual ImageWriter Configuration", g_cfg_imagewriter_menu, 0, 0, CFGTYPE_MENU }, #endif -{ "Developer Options", g_cfg_devel_menu, 0, 0, CFGTYPE_MENU }, -{ "Auto-update configuration file,0,Manual,1,Immediately", KNMP(g_config_gsplus_auto_update), CFGTYPE_INT }, -{ "Speed,0,Unlimited,1,1.0MHz,2,2.8MHz,3,8.0MHz (Zip)", KNMP(g_limit_speed), CFGTYPE_INT }, -{ "Expansion Mem Size,0,0MB,0x100000,1MB,0x200000,2MB,0x300000,3MB," - "0x400000,4MB,0x600000,6MB,0x800000,8MB,0xa00000,10MB,0xc00000,12MB," - "0xe00000,14MB", KNMP(g_mem_size_exp), CFGTYPE_INT }, -{ "Dump text screen to file", (void *)cfg_text_screen_dump, 0, 0, CFGTYPE_FUNC}, + { "Developer Options", g_cfg_devel_menu, 0, 0, CFGTYPE_MENU }, + { "Auto-update configuration file,0,Manual,1,Immediately", KNMP(g_config_gsplus_auto_update), CFGTYPE_INT }, + { "Speed,0,Unlimited,1,1.0MHz,2,2.8MHz,3,8.0MHz (Zip)", KNMP(g_limit_speed), CFGTYPE_INT }, + { "Expansion Mem Size,0,0MB,0x100000,1MB,0x200000,2MB,0x300000,3MB," + "0x400000,4MB,0x600000,6MB,0x800000,8MB,0xa00000,10MB,0xc00000,12MB," + "0xe00000,14MB", KNMP(g_mem_size_exp), CFGTYPE_INT }, + { "Dump text screen to file", (void *)cfg_text_screen_dump, 0, 0, CFGTYPE_FUNC}, #ifdef HAVE_SDL -{ "Reset Virtual ImageWriter", (void *)cfg_iwreset, 0, 0, CFGTYPE_FUNC }, + { "Reset Virtual ImageWriter", (void *)cfg_iwreset, 0, 0, CFGTYPE_FUNC }, #endif -{ "", 0, 0, 0, 0 }, -{ "Save changes to configuration file", (void *)config_write_config_gsplus_file, 0, 0, - CFGTYPE_FUNC }, -{ "", 0, 0, 0, 0 }, -{ "Exit Config (or press F4)", (void *)cfg_exit, 0, 0, CFGTYPE_FUNC }, -{ 0, 0, 0, 0, 0 }, + { "", 0, 0, 0, 0 }, + { "Save changes to configuration file", (void *)config_write_config_gsplus_file, 0, 0, + CFGTYPE_FUNC }, + { "", 0, 0, 0, 0 }, + { "Exit Config (or press F4)", (void *)cfg_exit, 0, 0, CFGTYPE_FUNC }, + { 0, 0, 0, 0, 0 }, }; -#define CFG_MAX_DEFVALS 128 +#define CFG_MAX_DEFVALS 128 Cfg_defval g_cfg_defvals[CFG_MAX_DEFVALS]; int g_cfg_defval_index = 0; @@ -490,7 +490,7 @@ Cfg_listhdr g_cfg_partitionlist = { 0 }; int g_cfg_file_pathfield = 0; const char *g_gsplus_rom_names[] = { "ROM", "ROM", "ROM01", "ROM03", "ROM.01", "ROM.03", 0 }; - /* First entry is special--it will be overwritten by g_cfg_rom_path */ +/* First entry is special--it will be overwritten by g_cfg_rom_path */ const char *g_gsplus_c1rom_names[] = { "parallel.rom", 0 }; const char *g_gsplus_c2rom_names[] = { 0 }; @@ -501,3009 +501,2909 @@ const char *g_gsplus_c6rom_names[] = { "c600.rom", "controller.rom", "disk.rom", const char *g_gsplus_c7rom_names[] = { 0 }; const char **g_gsplus_rom_card_list[8] = { - 0, g_gsplus_c1rom_names, - g_gsplus_c2rom_names, g_gsplus_c3rom_names, - g_gsplus_c4rom_names, g_gsplus_c5rom_names, - g_gsplus_c6rom_names, g_gsplus_c7rom_names }; + 0, g_gsplus_c1rom_names, + g_gsplus_c2rom_names, g_gsplus_c3rom_names, + g_gsplus_c4rom_names, g_gsplus_c5rom_names, + g_gsplus_c6rom_names, g_gsplus_c7rom_names +}; byte g_rom_c600_rom01_diffs[256] = { - 0x00, 0x00, 0x00, 0x00, 0xc6, 0x00, 0xe2, 0x00, - 0xd0, 0x50, 0x0f, 0x77, 0x5b, 0xb9, 0xc3, 0xb1, - 0xb1, 0xf8, 0xcb, 0x4e, 0xb8, 0x60, 0xc7, 0x2e, - 0xfc, 0xe0, 0xbf, 0x1f, 0x66, 0x37, 0x4a, 0x70, - 0x55, 0x2c, 0x3c, 0xfc, 0xc2, 0xa5, 0x08, 0x29, - 0xac, 0x21, 0xcc, 0x09, 0x55, 0x03, 0x17, 0x35, - 0x4e, 0xe2, 0x0c, 0xe9, 0x3f, 0x9d, 0xc2, 0x06, - 0x18, 0x88, 0x0d, 0x58, 0x57, 0x6d, 0x83, 0x8c, - 0x22, 0xd3, 0x4f, 0x0a, 0xe5, 0xb7, 0x9f, 0x7d, - 0x2c, 0x3e, 0xae, 0x7f, 0x24, 0x78, 0xfd, 0xd0, - 0xb5, 0xd6, 0xe5, 0x26, 0x85, 0x3d, 0x8d, 0xc9, - 0x79, 0x0c, 0x75, 0xec, 0x98, 0xcc, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x39, 0x00, 0x35, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, - 0x6c, 0x44, 0xce, 0x4c, 0x01, 0x08, 0x00, 0x00 + 0x00, 0x00, 0x00, 0x00, 0xc6, 0x00, 0xe2, 0x00, + 0xd0, 0x50, 0x0f, 0x77, 0x5b, 0xb9, 0xc3, 0xb1, + 0xb1, 0xf8, 0xcb, 0x4e, 0xb8, 0x60, 0xc7, 0x2e, + 0xfc, 0xe0, 0xbf, 0x1f, 0x66, 0x37, 0x4a, 0x70, + 0x55, 0x2c, 0x3c, 0xfc, 0xc2, 0xa5, 0x08, 0x29, + 0xac, 0x21, 0xcc, 0x09, 0x55, 0x03, 0x17, 0x35, + 0x4e, 0xe2, 0x0c, 0xe9, 0x3f, 0x9d, 0xc2, 0x06, + 0x18, 0x88, 0x0d, 0x58, 0x57, 0x6d, 0x83, 0x8c, + 0x22, 0xd3, 0x4f, 0x0a, 0xe5, 0xb7, 0x9f, 0x7d, + 0x2c, 0x3e, 0xae, 0x7f, 0x24, 0x78, 0xfd, 0xd0, + 0xb5, 0xd6, 0xe5, 0x26, 0x85, 0x3d, 0x8d, 0xc9, + 0x79, 0x0c, 0x75, 0xec, 0x98, 0xcc, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x39, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x39, 0x00, 0x35, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, + 0x6c, 0x44, 0xce, 0x4c, 0x01, 0x08, 0x00, 0x00 }; -void -config_init_menus(Cfg_menu *menuptr) -{ - void *voidptr; - const char *name_str; - Cfg_defval *defptr; - char **str_ptr; - char *str; - int type; - int pos; - int val; +void config_init_menus(Cfg_menu *menuptr) { + void *voidptr; + const char *name_str; + Cfg_defval *defptr; + char **str_ptr; + char *str; + int type; + int pos; + int val; - if(menuptr[0].defptr != 0) { - return; - } - menuptr[0].defptr = (void *)1; - pos = 0; - while(pos < 100) { - type = menuptr->cfgtype; - voidptr = menuptr->ptr; - name_str = menuptr->name_str; - if(menuptr->str == 0) { - break; - } - if(name_str != 0) { - defptr = &(g_cfg_defvals[g_cfg_defval_index++]); - if(g_cfg_defval_index >= CFG_MAX_DEFVALS) { - fatal_printf("CFG_MAX_DEFVAL overflow\n"); - my_exit(5); - } - defptr->menuptr = menuptr; - defptr->intval = 0; - defptr->strval = 0; - switch(type) { - case CFGTYPE_INT: - val = *((int *)voidptr); - defptr->intval = val; - menuptr->defptr = &(defptr->intval); - break; - case CFGTYPE_STR: - case CFGTYPE_FILE: - case CFGTYPE_DIR: - str_ptr = (char **)menuptr->ptr; - str = *str_ptr; - // We need to malloc this string since all - // string values must be dynamically alloced - defptr->strval = str; // this can have a copy - *str_ptr = gsplus_malloc_str(str); - menuptr->defptr = &(defptr->strval); - break; - default: - fatal_printf("name_str is %p = %s, but type: " - "%d\n", name_str, name_str, type); - my_exit(5); - } - } - if(type == CFGTYPE_MENU) { - config_init_menus((Cfg_menu *)voidptr); - } - pos++; - menuptr++; - } + if(menuptr[0].defptr != 0) { + return; + } + menuptr[0].defptr = (void *)1; + pos = 0; + while(pos < 100) { + type = menuptr->cfgtype; + voidptr = menuptr->ptr; + name_str = menuptr->name_str; + if(menuptr->str == 0) { + break; + } + if(name_str != 0) { + defptr = &(g_cfg_defvals[g_cfg_defval_index++]); + if(g_cfg_defval_index >= CFG_MAX_DEFVALS) { + fatal_printf("CFG_MAX_DEFVAL overflow\n"); + my_exit(5); + } + defptr->menuptr = menuptr; + defptr->intval = 0; + defptr->strval = 0; + switch(type) { + case CFGTYPE_INT: + val = *((int *)voidptr); + defptr->intval = val; + menuptr->defptr = &(defptr->intval); + break; + case CFGTYPE_STR: + case CFGTYPE_FILE: + case CFGTYPE_DIR: + str_ptr = (char **)menuptr->ptr; + str = *str_ptr; + // We need to malloc this string since all + // string values must be dynamically alloced + defptr->strval = str; // this can have a copy + *str_ptr = gsplus_malloc_str(str); + menuptr->defptr = &(defptr->strval); + break; + default: + fatal_printf("name_str is %p = %s, but type: " + "%d\n", name_str, name_str, type); + my_exit(5); + } + } + if(type == CFGTYPE_MENU) { + config_init_menus((Cfg_menu *)voidptr); + } + pos++; + menuptr++; + } } -void -config_init() -{ - int can_create; +void config_init() { + int can_create; - config_init_menus(g_cfg_main_menu); - config_init_menus(g_cfg_uiless_menu); + config_init_menus(g_cfg_main_menu); + config_init_menus(g_cfg_uiless_menu); - // Find the configuration file - g_config_gsplus_name[0] = 0; - can_create = 1; - setup_gsplus_file(&g_config_gsplus_name[0], sizeof(g_config_gsplus_name), 0, can_create, &g_config_gsplus_name_list[0]); + // Find the configuration file + g_config_gsplus_name[0] = 0; + can_create = 1; + setup_gsplus_file(&g_config_gsplus_name[0], sizeof(g_config_gsplus_name), 0, can_create, &g_config_gsplus_name_list[0]); - config_parse_config_gsplus_file(); + config_parse_config_gsplus_file(); } -void -cfg_exit() -{ - if(g_rom_version >= 1) { - g_config_control_panel = 0; - } +void cfg_exit() { + if(g_rom_version >= 1) { + g_config_control_panel = 0; + } } -void -cfg_toggle_config_panel() -{ - g_config_control_panel = !g_config_control_panel; - if(g_rom_version < 0) { - g_config_control_panel = 1; /* Stay in config mode */ - } +void cfg_toggle_config_panel() { + g_config_control_panel = !g_config_control_panel; + if(g_rom_version < 0) { + g_config_control_panel = 1; /* Stay in config mode */ + } } -void -cfg_text_screen_dump() -{ - char buf[85]; - char *filename; - FILE *ofile; - int offset; - int c; - int pos; - int i, j; +void cfg_text_screen_dump() { + char buf[85]; + char *filename; + FILE *ofile; + int offset; + int c; + int pos; + int i, j; - filename = "gsplus.screen.dump"; - glogf("Writing text screen to the file %s", filename); - ofile = fopen(filename, "w"); - if(ofile == 0) { - fatal_printf("Could not write to file %s, (%d)\n", filename, errno); - return; - } + filename = "gsplus.screen.dump"; + glogf("Writing text screen to the file %s", filename); + ofile = fopen(filename, "w"); + if(ofile == 0) { + fatal_printf("Could not write to file %s, (%d)\n", filename, errno); + return; + } - for(i = 0; i < 24; i++) { - pos = 0; - for(j = 0; j < 40; j++) { - offset = g_screen_index[i] + j; - if(g_save_cur_a2_stat & ALL_STAT_VID80) { - c = g_save_text_screen_bytes[0x400+offset]; - c = c & 0x7f; - if(c < 0x20) { - c += 0x40; - } - buf[pos++] = c; - } - c = g_save_text_screen_bytes[offset] & 0x7f; - if(c < 0x20) { - c += 0x40; - } - buf[pos++] = c; - } - while((pos > 0) && (buf[pos-1] == ' ')) { - /* try to strip out trailing spaces */ - pos--; - } - buf[pos++] = '\n'; - buf[pos++] = 0; - fputs(buf, ofile); - } - fclose(ofile); + for(i = 0; i < 24; i++) { + pos = 0; + for(j = 0; j < 40; j++) { + offset = g_screen_index[i] + j; + if(g_save_cur_a2_stat & ALL_STAT_VID80) { + c = g_save_text_screen_bytes[0x400+offset]; + c = c & 0x7f; + if(c < 0x20) { + c += 0x40; + } + buf[pos++] = c; + } + c = g_save_text_screen_bytes[offset] & 0x7f; + if(c < 0x20) { + c += 0x40; + } + buf[pos++] = c; + } + while((pos > 0) && (buf[pos-1] == ' ')) { + /* try to strip out trailing spaces */ + pos--; + } + buf[pos++] = '\n'; + buf[pos++] = 0; + fputs(buf, ofile); + } + fclose(ofile); } -void -cfg_iwreset() -{ - imagewriter_feed(); - imagewriter_close(); - imagewriter_init(g_imagewriter_dpi,g_imagewriter_paper,g_imagewriter_banner, g_imagewriter_output,g_imagewriter_multipage); - return; +void cfg_iwreset() { + imagewriter_feed(); + imagewriter_close(); + imagewriter_init(g_imagewriter_dpi,g_imagewriter_paper,g_imagewriter_banner, g_imagewriter_output,g_imagewriter_multipage); + return; } #ifdef HAVE_TFE -void -cfg_get_tfe_name() -{ - int i = 0; - char *ppname = NULL; - char *ppdes = NULL; - cfg_htab_vtab(0,11); - if (tfe_enumadapter_open()) - { - cfg_printf("Interface List:\n---------------"); - while(tfe_enumadapter(&ppname,&ppdes)) - { - cfg_htab_vtab(0, 13+i); - cfg_printf("%2d: %s",i,ppdes); - i++; - lib_free(ppname); - lib_free(ppdes); - } - tfe_enumadapter_close(); - } - else - { - #if defined(_WIN32) || defined(__CYGWIN__) - cfg_printf("ERROR: Install/Enable WinPcap for Ethernet Support!!"); - #else - cfg_printf("ERROR: Install/Enable LibPcap for Ethernet Support!!"); - #endif - } - return; +void cfg_get_tfe_name() { + int i = 0; + char *ppname = NULL; + char *ppdes = NULL; + cfg_htab_vtab(0,11); + if (tfe_enumadapter_open()) + { + cfg_printf("Interface List:\n---------------"); + while(tfe_enumadapter(&ppname,&ppdes)) + { + cfg_htab_vtab(0, 13+i); + cfg_printf("%2d: %s",i,ppdes); + i++; + lib_free(ppname); + lib_free(ppdes); + } + tfe_enumadapter_close(); + } + else + { + #if defined(_WIN32) || defined(__CYGWIN__) + cfg_printf("ERROR: Install/Enable WinPcap for Ethernet Support!!"); + #else + cfg_printf("ERROR: Install/Enable LibPcap for Ethernet Support!!"); + #endif + } + return; } #endif -void -config_vbl_update(int doit_3_persec) -{ - if(doit_3_persec) { - if(g_config_gsplus_auto_update && g_config_gsplus_update_needed) { - config_write_config_gsplus_file(); - } - } - return; +void config_vbl_update(int doit_3_persec) { + if(doit_3_persec) { + if(g_config_gsplus_auto_update && g_config_gsplus_update_needed) { + config_write_config_gsplus_file(); + } + } + return; } -void -config_parse_option(char *buf, int pos, int len, int line) -{ - Cfg_menu *menuptr; - Cfg_defval *defptr; - char *nameptr; - char **strptr; - int *iptr; - int num_equals; - int type; - int val; - int c; - int i; +void config_parse_option(char *buf, int pos, int len, int line) { + Cfg_menu *menuptr; + Cfg_defval *defptr; + char *nameptr; + char **strptr; + int *iptr; + int num_equals; + int type; + int val; + int c; + int i; // warning: modifies buf (turns spaces to nulls) // parse buf from pos into option, "=" and then "rest" - if(pos >= len) { - /* blank line */ - return; - } + if(pos >= len) { + /* blank line */ + return; + } - if(strncmp(&buf[pos], "bram", 4) == 0) { - config_parse_bram(buf, pos+4, len); - return; - } + if(strncmp(&buf[pos], "bram", 4) == 0) { + config_parse_bram(buf, pos+4, len); + return; + } - // find "name" as first contiguous string - glogf("%s line %d, len:%d \"%s\"", parse_log_prefix_file, line, len, &buf[pos]); + // find "name" as first contiguous string + glogf("%s line %d, len:%d \"%s\"", parse_log_prefix_file, line, len, &buf[pos]); - nameptr = &buf[pos]; - while(pos < len) { - c = buf[pos]; - if(c == 0 || c == ' ' || c == '\t' || c == '\n') { - break; - } - pos++; - } - buf[pos] = 0; - pos++; + nameptr = &buf[pos]; + while(pos < len) { + c = buf[pos]; + if(c == 0 || c == ' ' || c == '\t' || c == '\n') { + break; + } + pos++; + } + buf[pos] = 0; + pos++; - // Eat up all whitespace and '=' - num_equals = 0; - while(pos < len) { - c = buf[pos]; - if((c == '=') && num_equals == 0) { - pos++; - num_equals++; - } else if(c == ' ' || c == '\t') { - pos++; - } else { - break; - } - } + // Eat up all whitespace and '=' + num_equals = 0; + while(pos < len) { + c = buf[pos]; + if((c == '=') && num_equals == 0) { + pos++; + num_equals++; + } else if(c == ' ' || c == '\t') { + pos++; + } else { + break; + } + } - /* Look up nameptr to find type */ - type = -1; - defptr = 0; - menuptr = 0; - for(i = 0; i < g_cfg_defval_index; i++) { - defptr = &(g_cfg_defvals[i]); - menuptr = defptr->menuptr; - if(strcmp(menuptr->name_str, nameptr) == 0) { - type = menuptr->cfgtype; - break; - } - } + /* Look up nameptr to find type */ + type = -1; + defptr = 0; + menuptr = 0; + for(i = 0; i < g_cfg_defval_index; i++) { + defptr = &(g_cfg_defvals[i]); + menuptr = defptr->menuptr; + if(strcmp(menuptr->name_str, nameptr) == 0) { + type = menuptr->cfgtype; + break; + } + } - switch(type) { - case CFGTYPE_INT: - /* use strtol */ - val = (int)strtol(&buf[pos], 0, 0); - iptr = (int *)menuptr->ptr; - *iptr = val; - break; - case CFGTYPE_STR: - case CFGTYPE_FILE: - case CFGTYPE_DIR: - strptr = (char **)menuptr->ptr; - if(strptr && *strptr) { - free(*strptr); - } - *strptr = gsplus_malloc_str(&buf[pos]); - break; - default: - glogf("Config file variable %s is unknown type: %d", nameptr, type); - } + switch(type) { + case CFGTYPE_INT: + /* use strtol */ + val = (int)strtol(&buf[pos], 0, 0); + iptr = (int *)menuptr->ptr; + *iptr = val; + break; + case CFGTYPE_STR: + case CFGTYPE_FILE: + case CFGTYPE_DIR: + strptr = (char **)menuptr->ptr; + if(strptr && *strptr) { + free(*strptr); + } + *strptr = gsplus_malloc_str(&buf[pos]); + break; + default: + glogf("Config file variable %s is unknown type: %d", nameptr, type); + } } -void -config_parse_bram(char *buf, int pos, int len) -{ - int bram_num; - int offset; - int val; +void config_parse_bram(char *buf, int pos, int len) { + int bram_num; + int offset; + int val; - if((len < (pos+5)) || (buf[pos+1] != '[') || (buf[pos+4] != ']')) { - fatal_printf("While reading configuration file, found malformed bram " - "statement: %s\n", buf); - return; - } - bram_num = buf[pos] - '0'; - if(bram_num != 1 && bram_num != 3) { - fatal_printf("While reading configuration file, found bad bram " - "num: %s\n", buf); - return; - } + if((len < (pos+5)) || (buf[pos+1] != '[') || (buf[pos+4] != ']')) { + fatal_printf("While reading configuration file, found malformed bram " + "statement: %s\n", buf); + return; + } + bram_num = buf[pos] - '0'; + if(bram_num != 1 && bram_num != 3) { + fatal_printf("While reading configuration file, found bad bram " + "num: %s\n", buf); + return; + } - bram_num = bram_num >> 1; // turn 3->1 and 1->0 + bram_num = bram_num >> 1; // turn 3->1 and 1->0 - offset = strtoul(&(buf[pos+2]), 0, 16); - pos += 5; - while(pos < len) { - while(buf[pos] == ' ' || buf[pos] == '\t' || buf[pos] == 0x0a || - buf[pos] == 0x0d || buf[pos] == '=') { - pos++; - } - val = strtoul(&buf[pos], 0, 16); - clk_bram_set(bram_num, offset, val); - offset++; - pos += 2; - } + offset = strtoul(&(buf[pos+2]), 0, 16); + pos += 5; + while(pos < len) { + while(buf[pos] == ' ' || buf[pos] == '\t' || buf[pos] == 0x0a || + buf[pos] == 0x0d || buf[pos] == '=') { + pos++; + } + val = strtoul(&buf[pos], 0, 16); + clk_bram_set(bram_num, offset, val); + offset++; + pos += 2; + } } -void -config_load_roms() -{ - struct stat stat_buf; - const char **names_ptr; - int more_than_8mb; - int changed_rom; - int len; - FILE *file; - int ret; - int i; +void config_load_roms() { + struct stat stat_buf; + const char **names_ptr; + int more_than_8mb; + int changed_rom; + int len; + FILE *file; + int ret; + int i; - g_rom_version = -1; + g_rom_version = -1; - /* set first entry of g_gsplus_rom_names[] to g_cfg_rom_path so that */ - /* it becomes the first place searched. */ - g_gsplus_rom_names[0] = g_cfg_rom_path; - setup_gsplus_file(&g_cfg_tmp_path[0], CFG_PATH_MAX, -1, 0, - &g_gsplus_rom_names[0]); + /* set first entry of g_gsplus_rom_names[] to g_cfg_rom_path so that */ + /* it becomes the first place searched. */ + g_gsplus_rom_names[0] = g_cfg_rom_path; + setup_gsplus_file(&g_cfg_tmp_path[0], CFG_PATH_MAX, -1, 0, + &g_gsplus_rom_names[0]); - if(g_cfg_tmp_path[0] == 0) { - // Just get out, let config interface select ROM - g_config_control_panel = 1; - return; - } - file = fopen(&g_cfg_tmp_path[0], "rb"); - if(!file) { - fatal_printf("Open ROM file %s failed; errno:%d\n", - &g_cfg_tmp_path[0], errno); - g_config_control_panel = 1; - return; - } + if(g_cfg_tmp_path[0] == 0) { + // Just get out, let config interface select ROM + g_config_control_panel = 1; + return; + } + file = fopen(&g_cfg_tmp_path[0], "rb"); + if(!file) { + fatal_printf("Open ROM file %s failed; errno:%d\n", + &g_cfg_tmp_path[0], errno); + g_config_control_panel = 1; + return; + } - ret = stat(&g_cfg_tmp_path[0], &stat_buf); - if(ret != 0) { - fatal_printf("stat returned %d; errno: %d\n", - ret, errno); - g_config_control_panel = 1; - return; - } + ret = stat(&g_cfg_tmp_path[0], &stat_buf); + if(ret != 0) { + fatal_printf("stat returned %d; errno: %d\n", + ret, errno); + g_config_control_panel = 1; + return; + } - len = stat_buf.st_size; - if(len == 128*1024) { - g_rom_version = 1; - g_mem_size_base = 256*1024; - memset(&g_rom_fc_ff_ptr[0], 0, 2*65536); - /* Clear banks fc and fd to 0 */ - ret = fread(&g_rom_fc_ff_ptr[2*65536], 1, len, file); - } else if(len == 256*1024) { - g_rom_version = 3; - g_mem_size_base = 1024*1024; - ret = fread(&g_rom_fc_ff_ptr[0], 1, len, file); - } else { - fatal_printf("The ROM size should be 128K or 256K, this file " - "is %d bytes\n", len); - g_config_control_panel = 1; - return; - } + len = stat_buf.st_size; + if(len == 128*1024) { + g_rom_version = 1; + g_mem_size_base = 256*1024; + memset(&g_rom_fc_ff_ptr[0], 0, 2*65536); + /* Clear banks fc and fd to 0 */ + ret = fread(&g_rom_fc_ff_ptr[2*65536], 1, len, file); + } else if(len == 256*1024) { + g_rom_version = 3; + g_mem_size_base = 1024*1024; + ret = fread(&g_rom_fc_ff_ptr[0], 1, len, file); + } else { + fatal_printf("The ROM size should be 128K or 256K, this file " + "is %d bytes\n", len); + g_config_control_panel = 1; + return; + } - glogf("Read %d bytes (%dK) of ROM", ret, ret/1024); - if(ret != len) { - fatal_printf("errno: %d\n", errno); - g_config_control_panel = 1; // THIS DOESN'T DO ANYTHING? - return; - } - fclose(file); + glogf("Read %d bytes (%dK) of ROM", ret, ret/1024); + if(ret != len) { + fatal_printf("errno: %d\n", errno); + g_config_control_panel = 1; // THIS DOESN'T DO ANYTHING? + return; + } + fclose(file); - memset(&g_rom_cards_ptr[0], 0, 256*16); + memset(&g_rom_cards_ptr[0], 0, 256*16); - /* initialize c600 rom to be diffs from the real ROM, to build-in */ - /* Apple II compatibility without distributing ROMs */ - for(i = 0; i < 256; i++) { - g_rom_cards_ptr[0x600 + i] = g_rom_fc_ff_ptr[0x3c600 + i] ^ - g_rom_c600_rom01_diffs[i]; - } - if(g_rom_version >= 3) { - /* some patches */ - g_rom_cards_ptr[0x61b] ^= 0x40; - g_rom_cards_ptr[0x61c] ^= 0x33; - g_rom_cards_ptr[0x632] ^= 0xc0; - g_rom_cards_ptr[0x633] ^= 0x33; - } + /* initialize c600 rom to be diffs from the real ROM, to build-in */ + /* Apple II compatibility without distributing ROMs */ + for(i = 0; i < 256; i++) { + g_rom_cards_ptr[0x600 + i] = g_rom_fc_ff_ptr[0x3c600 + i] ^ + g_rom_c600_rom01_diffs[i]; + } + if(g_rom_version >= 3) { + /* some patches */ + g_rom_cards_ptr[0x61b] ^= 0x40; + g_rom_cards_ptr[0x61c] ^= 0x33; + g_rom_cards_ptr[0x632] ^= 0xc0; + g_rom_cards_ptr[0x633] ^= 0x33; + } - for(i = 1; i < 8; i++) { - names_ptr = g_gsplus_rom_card_list[i]; - if(names_ptr == 0) { - continue; - } - if(*names_ptr == 0) { - continue; - } - setup_gsplus_file(&g_cfg_tmp_path[0], CFG_PATH_MAX, 1, 0, names_ptr); - if(g_cfg_tmp_path[0] != 0) { - file = fopen(&(g_cfg_tmp_path[0]), "rb"); - if(!file) { - fatal_printf("Open card ROM file %s failed; errno:%d\n", - &g_cfg_tmp_path[0], errno); - continue; - } + for(i = 1; i < 8; i++) { + names_ptr = g_gsplus_rom_card_list[i]; + if(names_ptr == 0) { + continue; + } + if(*names_ptr == 0) { + continue; + } + setup_gsplus_file(&g_cfg_tmp_path[0], CFG_PATH_MAX, 1, 0, names_ptr); + if(g_cfg_tmp_path[0] != 0) { + file = fopen(&(g_cfg_tmp_path[0]), "rb"); + if(!file) { + fatal_printf("Open card ROM file %s failed; errno:%d\n", + &g_cfg_tmp_path[0], errno); + continue; + } - len = 256; - ret = fread(&g_rom_cards_ptr[i*0x100], 1, len, file); + len = 256; + ret = fread(&g_rom_cards_ptr[i*0x100], 1, len, file); - if(ret != len) { - fatal_printf("While reading card ROM %s, file " - "is too short. (%d) Expected %d bytes, " - "read %d bytes\n", &g_cfg_tmp_path[0], errno, len, ret); - continue; - } - glogf("Read: %d bytes of ROM in slot %d from file %s.", ret, i, &g_cfg_tmp_path[0]); - fclose(file); - } - } - more_than_8mb = (g_mem_size_exp > 0x800000); - /* Only do the patch if users wants more than 8MB of expansion mem */ + if(ret != len) { + fatal_printf("While reading card ROM %s, file " + "is too short. (%d) Expected %d bytes, " + "read %d bytes\n", &g_cfg_tmp_path[0], errno, len, ret); + continue; + } + glogf("Read: %d bytes of ROM in slot %d from file %s.", ret, i, &g_cfg_tmp_path[0]); + fclose(file); + } + } + more_than_8mb = (g_mem_size_exp > 0x800000); + /* Only do the patch if users wants more than 8MB of expansion mem */ - changed_rom = 0; - if(g_rom_version == 1) { - /* make some patches to ROM 01 */ + changed_rom = 0; + if(g_rom_version == 1) { + /* make some patches to ROM 01 */ #if 0 - /* 1: Patch ROM selftest to not do speed test */ - printf("Patching out speed test failures from ROM 01\n"); - g_rom_fc_ff_ptr[0x3785a] = 0x18; - changed_rom = 1; + /* 1: Patch ROM selftest to not do speed test */ + printf("Patching out speed test failures from ROM 01\n"); + g_rom_fc_ff_ptr[0x3785a] = 0x18; + changed_rom = 1; #endif #if 0 - /* 2: Patch ROM selftests not to do tests 2,4 */ - /* 0 = skip, 1 = do it, test 1 is bit 0 of LSByte */ - g_rom_fc_ff_ptr[0x371e9] = 0xf5; - g_rom_fc_ff_ptr[0x371ea] = 0xff; - changed_rom = 1; + /* 2: Patch ROM selftests not to do tests 2,4 */ + /* 0 = skip, 1 = do it, test 1 is bit 0 of LSByte */ + g_rom_fc_ff_ptr[0x371e9] = 0xf5; + g_rom_fc_ff_ptr[0x371ea] = 0xff; + changed_rom = 1; #endif - if(more_than_8mb) { - /* Geoff Weiss patch to use up to 14MB of RAM */ - g_rom_fc_ff_ptr[0x30302] = 0xdf; - g_rom_fc_ff_ptr[0x30314] = 0xdf; - g_rom_fc_ff_ptr[0x3031c] = 0x00; - changed_rom = 1; - } + if(more_than_8mb) { + /* Geoff Weiss patch to use up to 14MB of RAM */ + g_rom_fc_ff_ptr[0x30302] = 0xdf; + g_rom_fc_ff_ptr[0x30314] = 0xdf; + g_rom_fc_ff_ptr[0x3031c] = 0x00; + changed_rom = 1; + } - /* Patch ROM selftest to not do ROM cksum if any changes*/ - if(changed_rom) { - g_rom_fc_ff_ptr[0x37a06] = 0x18; - g_rom_fc_ff_ptr[0x37a07] = 0x18; - } - } else if(g_rom_version == 3) { - /* patch ROM 03 */ - glog("Patching ROM 03 smartport bug"); - /* 1: Patch Smartport code to fix a stupid bug */ - /* that causes it to write the IWM status reg into c036, */ - /* which is the system speed reg...it's "safe" since */ - /* IWM status reg bit 4 must be 0 (7MHz)..., otherwise */ - /* it might have turned on shadowing in all banks! */ - g_rom_fc_ff_ptr[0x357c9] = 0x00; - changed_rom = 1; + /* Patch ROM selftest to not do ROM cksum if any changes*/ + if(changed_rom) { + g_rom_fc_ff_ptr[0x37a06] = 0x18; + g_rom_fc_ff_ptr[0x37a07] = 0x18; + } + } else if(g_rom_version == 3) { + /* patch ROM 03 */ + glog("Patching ROM 03 smartport bug"); + /* 1: Patch Smartport code to fix a stupid bug */ + /* that causes it to write the IWM status reg into c036, */ + /* which is the system speed reg...it's "safe" since */ + /* IWM status reg bit 4 must be 0 (7MHz)..., otherwise */ + /* it might have turned on shadowing in all banks! */ + g_rom_fc_ff_ptr[0x357c9] = 0x00; + changed_rom = 1; #if 0 - /* patch ROM 03 to not to speed test */ - /* skip fast speed test */ - g_rom_fc_ff_ptr[0x36ad7] = 0x18; - g_rom_fc_ff_ptr[0x36ad8] = 0x18; - changed_rom = 1; + /* patch ROM 03 to not to speed test */ + /* skip fast speed test */ + g_rom_fc_ff_ptr[0x36ad7] = 0x18; + g_rom_fc_ff_ptr[0x36ad8] = 0x18; + changed_rom = 1; #endif #if 0 - /* skip slow speed test */ - g_rom_fc_ff_ptr[0x36ae7] = 0x18; - g_rom_fc_ff_ptr[0x36ae8] = 0x6b; - changed_rom = 1; + /* skip slow speed test */ + g_rom_fc_ff_ptr[0x36ae7] = 0x18; + g_rom_fc_ff_ptr[0x36ae8] = 0x6b; + changed_rom = 1; #endif #if 0 - /* 4: Patch ROM 03 selftests not to do tests 1-4 */ - g_rom_fc_ff_ptr[0x364a9] = 0xf0; - g_rom_fc_ff_ptr[0x364aa] = 0xff; - changed_rom = 1; + /* 4: Patch ROM 03 selftests not to do tests 1-4 */ + g_rom_fc_ff_ptr[0x364a9] = 0xf0; + g_rom_fc_ff_ptr[0x364aa] = 0xff; + changed_rom = 1; #endif - /* ROM tests are in ff/6403-642x, where 6403 = addr of */ - /* test 1, etc. */ + /* ROM tests are in ff/6403-642x, where 6403 = addr of */ + /* test 1, etc. */ - if(more_than_8mb) { - /* Geoff Weiss patch to use up to 14MB of RAM */ - g_rom_fc_ff_ptr[0x30b] = 0xdf; - g_rom_fc_ff_ptr[0x31d] = 0xdf; - g_rom_fc_ff_ptr[0x325] = 0x00; - changed_rom = 1; - } + if(more_than_8mb) { + /* Geoff Weiss patch to use up to 14MB of RAM */ + g_rom_fc_ff_ptr[0x30b] = 0xdf; + g_rom_fc_ff_ptr[0x31d] = 0xdf; + g_rom_fc_ff_ptr[0x325] = 0x00; + changed_rom = 1; + } - if(changed_rom) { - /* patch ROM 03 selftest to not do ROM cksum */ - g_rom_fc_ff_ptr[0x36cb0] = 0x18; - g_rom_fc_ff_ptr[0x36cb1] = 0x18; - } + if(changed_rom) { + /* patch ROM 03 selftest to not do ROM cksum */ + g_rom_fc_ff_ptr[0x36cb0] = 0x18; + g_rom_fc_ff_ptr[0x36cb1] = 0x18; + } - } + } } -void -config_parse_config_gsplus_file() -{ - FILE *fconf; - char *buf; - char *ptr; - char *name_ptr; - char *partition_name; - int part_num; - int ejected; - int line; - int pos; - int slot; - int drive; - int size; - int len; - int ret; - int i; +void config_parse_config_gsplus_file() { + FILE *fconf; + char *buf; + char *ptr; + char *name_ptr; + char *partition_name; + int part_num; + int ejected; + int line; + int pos; + int slot; + int drive; + int size; + int len; + int ret; + int i; - glogf("Parsing configuration file '%s'", g_config_gsplus_name); + glogf("Parsing configuration file '%s'", g_config_gsplus_name); - clk_bram_zero(); + clk_bram_zero(); - g_highest_smartport_unit = -1; + g_highest_smartport_unit = -1; - cfg_get_base_path(&g_cfg_cwd_str[0], g_config_gsplus_name, 0); + cfg_get_base_path(&g_cfg_cwd_str[0], g_config_gsplus_name, 0); - // I think this code is wrong. It breaks relative config paths on the "-config" - // option. ie "./gsplus -config foo/bar.gsp" - // It's possible it was needed for some of the autodiscovery stuff, but I'm - // not really a fan of that either and think it should be take out. - // Especially now that you can pass a filename. + // I think this code is wrong. It breaks relative config paths on the "-config" + // option. ie "./gsplus -config foo/bar.gsp" + // It's possible it was needed for some of the autodiscovery stuff, but I'm + // not really a fan of that either and think it should be take out. + // Especially now that you can pass a filename. - fconf = fopen(g_config_gsplus_name, "r"); - if(fconf == 0) { - perror("ERROR"); - fatal_printf("Cannot open configuration file at %s! Stopping!\n",g_config_gsplus_name); - my_exit(3); - } + fconf = fopen(g_config_gsplus_name, "r"); + if(fconf == 0) { + perror("ERROR"); + fatal_printf("Cannot open configuration file at %s! Stopping!\n",g_config_gsplus_name); + my_exit(3); + } - line = 0; - while(1) { - buf = &g_config_gsplus_buf[0]; - ptr = fgets(buf, CONF_BUF_LEN, fconf); - if(ptr == 0) { - iwm_printf("Done reading disk_conf\n"); - break; - } + line = 0; + while(1) { + buf = &g_config_gsplus_buf[0]; + ptr = fgets(buf, CONF_BUF_LEN, fconf); + if(ptr == 0) { + iwm_printf("Done reading disk_conf\n"); + break; + } - line++; - /* strip off newline(s) */ - len = strlen(buf); - for(i = len - 1; i >= 0; i--) { - if((buf[i] != 0x0d) && (buf[i] != 0x0a)) { - break; - } - len = i; - buf[i] = 0; - } + line++; + /* strip off newline(s) */ + len = strlen(buf); + for(i = len - 1; i >= 0; i--) { + if((buf[i] != 0x0d) && (buf[i] != 0x0a)) { + break; + } + len = i; + buf[i] = 0; + } - iwm_printf("disk_conf[%d]: %s\n", line, buf); - if(len > 0 && buf[0] == '#') { - iwm_printf("Skipping comment\n"); - continue; - } + iwm_printf("disk_conf[%d]: %s\n", line, buf); + if(len > 0 && buf[0] == '#') { + iwm_printf("Skipping comment\n"); + continue; + } - /* determine what this is */ - pos = 0; + /* determine what this is */ + pos = 0; - while(pos < len && (buf[pos] == ' ' || buf[pos] == '\t') ) { - pos++; - } - if((pos + 4) > len || buf[pos] != 's' || buf[pos+2] != 'd' || - buf[pos+1] > '9' || buf[pos+1] < '0') { - config_parse_option(buf, pos, len, line); - continue; - } + while(pos < len && (buf[pos] == ' ' || buf[pos] == '\t') ) { + pos++; + } + if((pos + 4) > len || buf[pos] != 's' || buf[pos+2] != 'd' || + buf[pos+1] > '9' || buf[pos+1] < '0') { + config_parse_option(buf, pos, len, line); + continue; + } - slot = buf[pos+1] - '0'; - drive = buf[pos+3] - '0'; + slot = buf[pos+1] - '0'; + drive = buf[pos+3] - '0'; - /* skip over slot, drive */ - pos += 4; - if(buf[pos] >= '0' && buf[pos] <= '9') { - drive = drive * 10 + buf[pos] - '0'; - pos++; - } + /* skip over slot, drive */ + pos += 4; + if(buf[pos] >= '0' && buf[pos] <= '9') { + drive = drive * 10 + buf[pos] - '0'; + pos++; + } - /* make s6d1 mean index 0 */ - drive--; + /* make s6d1 mean index 0 */ + drive--; - while(pos < len && (buf[pos] == ' ' || buf[pos] == '\t' || - buf[pos] == '=') ) { - pos++; - } + while(pos < len && (buf[pos] == ' ' || buf[pos] == '\t' || + buf[pos] == '=') ) { + pos++; + } - ejected = 0; - if(buf[pos] == '#') { - /* disk is ejected, but read all the info anyway */ - ejected = 1; - pos++; - } + ejected = 0; + if(buf[pos] == '#') { + /* disk is ejected, but read all the info anyway */ + ejected = 1; + pos++; + } - size = 0; - if(buf[pos] == ',') { - /* read optional size parameter */ - pos++; - while(pos < len && buf[pos] >= '0' && buf[pos] <= '9'){ - size = size * 10 + buf[pos] - '0'; - pos++; - } - size = size * 1024; - if(buf[pos] == ',') { - pos++; /* eat trailing ',' */ - } - } + size = 0; + if(buf[pos] == ',') { + /* read optional size parameter */ + pos++; + while(pos < len && buf[pos] >= '0' && buf[pos] <= '9') { + size = size * 10 + buf[pos] - '0'; + pos++; + } + size = size * 1024; + if(buf[pos] == ',') { + pos++; /* eat trailing ',' */ + } + } - /* see if it has a partition name */ - partition_name = 0; - part_num = -1; - if(buf[pos] == ':') { - pos++; - /* yup, it's got a partition name! */ - partition_name = &buf[pos]; - while((pos < len) && (buf[pos] != ':')) { - pos++; - } - buf[pos] = 0; /* null terminate partition name */ - pos++; - } - if(buf[pos] == ';') { - pos++; - /* it's got a partition number */ - part_num = 0; - while((pos < len) && (buf[pos] != ':')) { - part_num = (10*part_num) + buf[pos] - '0'; - pos++; - } - pos++; - } + /* see if it has a partition name */ + partition_name = 0; + part_num = -1; + if(buf[pos] == ':') { + pos++; + /* yup, it's got a partition name! */ + partition_name = &buf[pos]; + while((pos < len) && (buf[pos] != ':')) { + pos++; + } + buf[pos] = 0; /* null terminate partition name */ + pos++; + } + if(buf[pos] == ';') { + pos++; + /* it's got a partition number */ + part_num = 0; + while((pos < len) && (buf[pos] != ':')) { + part_num = (10*part_num) + buf[pos] - '0'; + pos++; + } + pos++; + } - /* Get filename */ - name_ptr = &(buf[pos]); - if(name_ptr[0] == 0) { - continue; - } + /* Get filename */ + name_ptr = &(buf[pos]); + if(name_ptr[0] == 0) { + continue; + } - insert_disk(slot, drive, name_ptr, ejected, size, - partition_name, part_num); + insert_disk(slot, drive, name_ptr, ejected, size, + partition_name, part_num); - } + } - ret = fclose(fconf); - if(ret != 0) { - fatal_printf("Closing configuration file ret: %d, errno: %d\n", ret, - errno); - my_exit(4); - } + ret = fclose(fconf); + if(ret != 0) { + fatal_printf("Closing configuration file ret: %d, errno: %d\n", ret, + errno); + my_exit(4); + } - iwm_printf("Done parsing disk_conf file\n"); + iwm_printf("Done parsing disk_conf file\n"); } -Disk * -cfg_get_dsk_from_slot_drive(int slot, int drive) -{ - Disk *dsk; - int max_drive; +Disk *cfg_get_dsk_from_slot_drive(int slot, int drive) { + Disk *dsk; + int max_drive; - /* Get dsk */ - max_drive = 2; - switch(slot) { - case 5: - dsk = &(iwm.drive35[drive]); - break; - case 6: - dsk = &(iwm.drive525[drive]); - break; - default: - max_drive = MAX_C7_DISKS; - dsk = &(iwm.smartport[drive]); - } + /* Get dsk */ + max_drive = 2; + switch(slot) { + case 5: + dsk = &(iwm.drive35[drive]); + break; + case 6: + dsk = &(iwm.drive525[drive]); + break; + default: + max_drive = MAX_C7_DISKS; + dsk = &(iwm.smartport[drive]); + } - if(drive >= max_drive) { - dsk -= drive; /* move back to drive 0 effectively */ - } + if(drive >= max_drive) { + dsk -= drive; /* move back to drive 0 effectively */ + } - return dsk; + return dsk; } -void -config_generate_config_gsplus_name(char *outstr, int maxlen, Disk *dsk, - int with_extras) -{ - char *str; +void config_generate_config_gsplus_name(char *outstr, int maxlen, Disk *dsk, + int with_extras) { + char *str; - str = outstr; + str = outstr; - if(with_extras && (!dsk->file)) { - snprintf(str, maxlen - (str - outstr), "#"); - str = &outstr[strlen(outstr)]; - } - if(with_extras && dsk->force_size > 0) { - snprintf(str, maxlen - (str - outstr), ",%d,", dsk->force_size); - str = &outstr[strlen(outstr)]; - } - if(with_extras && dsk->partition_name != 0) { - snprintf(str, maxlen - (str - outstr), ":%s:", - dsk->partition_name); - str = &outstr[strlen(outstr)]; - } else if(with_extras && dsk->partition_num >= 0) { - snprintf(str, maxlen - (str - outstr), ";%d:", - dsk->partition_num); - str = &outstr[strlen(outstr)]; - } - snprintf(str, maxlen - (str - outstr), "%s", dsk->name_ptr); + if(with_extras && (!dsk->file)) { + snprintf(str, maxlen - (str - outstr), "#"); + str = &outstr[strlen(outstr)]; + } + if(with_extras && dsk->force_size > 0) { + snprintf(str, maxlen - (str - outstr), ",%d,", dsk->force_size); + str = &outstr[strlen(outstr)]; + } + if(with_extras && dsk->partition_name != 0) { + snprintf(str, maxlen - (str - outstr), ":%s:", + dsk->partition_name); + str = &outstr[strlen(outstr)]; + } else if(with_extras && dsk->partition_num >= 0) { + snprintf(str, maxlen - (str - outstr), ";%d:", + dsk->partition_num); + str = &outstr[strlen(outstr)]; + } + snprintf(str, maxlen - (str - outstr), "%s", dsk->name_ptr); } -void -config_write_config_gsplus_file() -{ - FILE *fconf; - Disk *dsk; - Cfg_defval *defptr; - Cfg_menu *menuptr; - char *curstr, *defstr; - int defval, curval; - int type; - int slot, drive; - int i; +void config_write_config_gsplus_file() { + FILE *fconf; + Disk *dsk; + Cfg_defval *defptr; + Cfg_menu *menuptr; + char *curstr, *defstr; + int defval, curval; + int type; + int slot, drive; + int i; - glogf("Writing configuration file to %s", g_config_gsplus_name); + glogf("Writing configuration file to %s", g_config_gsplus_name); - fconf = fopen(g_config_gsplus_name, "w+"); - if(fconf == 0) { - halt_printf("cannot open %s! Stopping!\n",g_config_gsplus_name); - return; - } + fconf = fopen(g_config_gsplus_name, "w+"); + if(fconf == 0) { + halt_printf("cannot open %s! Stopping!\n",g_config_gsplus_name); + return; + } - fprintf(fconf, "# GSplus configuration file version %s\n", - g_gsplus_version_str); + fprintf(fconf, "# GSplus configuration file version %s\n", + g_gsplus_version_str); - for(i = 0; i < MAX_C7_DISKS + 4; i++) { - slot = 7; - drive = i - 4; - if(i < 4) { - slot = (i >> 1) + 5; - drive = i & 1; - } - if(drive == 0) { - fprintf(fconf, "\n"); /* an extra blank line */ - } + for(i = 0; i < MAX_C7_DISKS + 4; i++) { + slot = 7; + drive = i - 4; + if(i < 4) { + slot = (i >> 1) + 5; + drive = i & 1; + } + if(drive == 0) { + fprintf(fconf, "\n"); /* an extra blank line */ + } - dsk = cfg_get_dsk_from_slot_drive(slot, drive); - if(dsk->name_ptr == 0 && (i > 4)) { - /* No disk, not even ejected--just skip */ - continue; - } - fprintf(fconf, "s%dd%d = ", slot, drive + 1); - if(dsk->name_ptr == 0) { - fprintf(fconf, "\n"); - continue; - } - config_generate_config_gsplus_name(&g_cfg_tmp_path[0], - CFG_PATH_MAX, dsk, 1); - fprintf(fconf, "%s\n", &g_cfg_tmp_path[0]); - } + dsk = cfg_get_dsk_from_slot_drive(slot, drive); + if(dsk->name_ptr == 0 && (i > 4)) { + /* No disk, not even ejected--just skip */ + continue; + } + fprintf(fconf, "s%dd%d = ", slot, drive + 1); + if(dsk->name_ptr == 0) { + fprintf(fconf, "\n"); + continue; + } + config_generate_config_gsplus_name(&g_cfg_tmp_path[0], + CFG_PATH_MAX, dsk, 1); + fprintf(fconf, "%s\n", &g_cfg_tmp_path[0]); + } - fprintf(fconf, "\n"); + fprintf(fconf, "\n"); - /* See if any variables are different than their default */ - for(i = 0; i < g_cfg_defval_index; i++) { - defptr = &(g_cfg_defvals[i]); - menuptr = defptr->menuptr; - defval = defptr->intval; - type = menuptr->cfgtype; + /* See if any variables are different than their default */ + for(i = 0; i < g_cfg_defval_index; i++) { + defptr = &(g_cfg_defvals[i]); + menuptr = defptr->menuptr; + defval = defptr->intval; + type = menuptr->cfgtype; - switch (type) { - case CFGTYPE_INT: - curval = *((int *)menuptr->ptr); - if(curval != defval) { - fprintf(fconf, "%s = %d\n", menuptr->name_str, - curval); - } - break; - case CFGTYPE_STR: - case CFGTYPE_FILE: - case CFGTYPE_DIR: - curstr = *((char **)menuptr->ptr); - defstr = *((char **)menuptr->defptr); - if(strcmp(curstr, defstr) != 0) { - fprintf(fconf, "%s = %s\n", menuptr->name_str, - curstr); - } - break; - } - } + switch (type) { + case CFGTYPE_INT: + curval = *((int *)menuptr->ptr); + if(curval != defval) { + fprintf(fconf, "%s = %d\n", menuptr->name_str, + curval); + } + break; + case CFGTYPE_STR: + case CFGTYPE_FILE: + case CFGTYPE_DIR: + curstr = *((char **)menuptr->ptr); + defstr = *((char **)menuptr->defptr); + if(strcmp(curstr, defstr) != 0) { + fprintf(fconf, "%s = %s\n", menuptr->name_str, + curstr); + } + break; + } + } - fprintf(fconf, "\n"); + fprintf(fconf, "\n"); - /* write bram state */ - clk_write_bram(fconf); + /* write bram state */ + clk_write_bram(fconf); - fclose(fconf); + fclose(fconf); - g_config_gsplus_update_needed = 0; + g_config_gsplus_update_needed = 0; } -void -insert_disk(int slot, int drive, const char *name, int ejected, int force_size, - const char *partition_name, int part_num) -{ - byte buf_2img[512]; - Disk *dsk; - char *name_ptr, *uncomp_ptr, *system_str; - char *part_ptr; - int size; - int system_len; - int part_len; - int cmp_o, cmp_p, cmp_dot; - int cmp_b, cmp_i, cmp_n; - int can_write; - int len = 0; - int nibs; - int unix_pos; - int name_len; - int image_identified; - int exp_size; - int save_track; - int ret; - int tmp; - int i; +void insert_disk(int slot, int drive, const char *name, int ejected, int force_size, + const char *partition_name, int part_num) { + byte buf_2img[512]; + Disk *dsk; + char *name_ptr, *uncomp_ptr, *system_str; + char *part_ptr; + int size; + int system_len; + int part_len; + int cmp_o, cmp_p, cmp_dot; + int cmp_b, cmp_i, cmp_n; + int can_write; + int len = 0; + int nibs; + int unix_pos; + int name_len; + int image_identified; + int exp_size; + int save_track; + int ret; + int tmp; + int i; - g_config_gsplus_update_needed = 1; + g_config_gsplus_update_needed = 1; - if((slot < 5) || (slot > 7)) { - fatal_printf("Invalid slot for inserting disk: %d\n", slot); - return; - } - if(drive < 0 || ((slot == 7) && (drive >= MAX_C7_DISKS)) || - ((slot < 7) && (drive > 1))) { - fatal_printf("Invalid drive for inserting disk: %d\n", drive); - return; - } + if((slot < 5) || (slot > 7)) { + fatal_printf("Invalid slot for inserting disk: %d\n", slot); + return; + } + if(drive < 0 || ((slot == 7) && (drive >= MAX_C7_DISKS)) || + ((slot < 7) && (drive > 1))) { + fatal_printf("Invalid drive for inserting disk: %d\n", drive); + return; + } - dsk = cfg_get_dsk_from_slot_drive(slot, drive); + dsk = cfg_get_dsk_from_slot_drive(slot, drive); #if 0 - printf("Inserting disk %s (%s or %d) in slot %d, drive: %d\n", name, - partition_name, part_num, slot, drive); + printf("Inserting disk %s (%s or %d) in slot %d, drive: %d\n", name, + partition_name, part_num, slot, drive); #endif - dsk->just_ejected = 0; - dsk->force_size = force_size; + dsk->just_ejected = 0; + dsk->force_size = force_size; - if(!dsk->file) { - eject_disk(dsk); - } + if(!dsk->file) { + eject_disk(dsk); + } - /* Before opening, make sure no other mounted disk has this name */ - /* If so, unmount it */ - if(!ejected) { - for(i = 0; i < 2; i++) { - eject_named_disk(&iwm.drive525[i], name,partition_name); - eject_named_disk(&iwm.drive35[i], name, partition_name); - } - for(i = 0; i < MAX_C7_DISKS; i++) { - eject_named_disk(&iwm.smartport[i],name,partition_name); - } - } + /* Before opening, make sure no other mounted disk has this name */ + /* If so, unmount it */ + if(!ejected) { + for(i = 0; i < 2; i++) { + eject_named_disk(&iwm.drive525[i], name,partition_name); + eject_named_disk(&iwm.drive35[i], name, partition_name); + } + for(i = 0; i < MAX_C7_DISKS; i++) { + eject_named_disk(&iwm.smartport[i],name,partition_name); + } + } - if(dsk->name_ptr != 0) { - /* free old name_ptr */ - free(dsk->name_ptr); - } + if(dsk->name_ptr != 0) { + /* free old name_ptr */ + free(dsk->name_ptr); + } - name_len = strlen(name); - name_ptr = (char *)malloc(name_len + 1); + name_len = strlen(name); + name_ptr = (char *)malloc(name_len + 1); #if defined(_WIN32) || defined(__CYGWIN__) - // On Windows, we need to change backslashes to forward slashes. - for (i = 0; i < name_len; i++) { - if (name[i] == '\\') { - name_ptr[i] = '/'; - } else { - name_ptr[i] = name[i]; - } - } - name_ptr[name_len] = 0; + // On Windows, we need to change backslashes to forward slashes. + for (i = 0; i < name_len; i++) { + if (name[i] == '\\') { + name_ptr[i] = '/'; + } else { + name_ptr[i] = name[i]; + } + } + name_ptr[name_len] = 0; #else - strncpy(name_ptr, name, name_len + 1); + strncpy(name_ptr, name, name_len + 1); #endif - dsk->name_ptr = name_ptr; + dsk->name_ptr = name_ptr; - dsk->partition_name = 0; - if(partition_name != 0) { - part_len = strlen(partition_name) + 1; - part_ptr = (char *)malloc(part_len); - strncpy(part_ptr, partition_name, part_len); - dsk->partition_name = part_ptr; - } - dsk->partition_num = part_num; + dsk->partition_name = 0; + if(partition_name != 0) { + part_len = strlen(partition_name) + 1; + part_ptr = (char *)malloc(part_len); + strncpy(part_ptr, partition_name, part_len); + dsk->partition_name = part_ptr; + } + dsk->partition_num = part_num; - iwm_printf("Opening up disk image named: %s\n", name_ptr); + iwm_printf("Opening up disk image named: %s\n", name_ptr); - if(ejected) { - /* just get out of here */ - dsk->file = 0; - return; - } + if(ejected) { + /* just get out of here */ + dsk->file = 0; + return; + } - dsk->file = 0; - can_write = 1; + dsk->file = 0; + can_write = 1; - if((name_len > 3) && (strcmp(&name_ptr[name_len - 3], ".gz") == 0)) { + if((name_len > 3) && (strcmp(&name_ptr[name_len - 3], ".gz") == 0)) { - /* it's gzip'ed, try to gunzip it, then unlink the */ - /* uncompressed file */ + /* it's gzip'ed, try to gunzip it, then unlink the */ + /* uncompressed file */ - can_write = 0; + can_write = 0; - uncomp_ptr = (char *)malloc(name_len + 1); - strncpy(uncomp_ptr, name_ptr, name_len + 1); - uncomp_ptr[name_len - 3] = 0; + uncomp_ptr = (char *)malloc(name_len + 1); + strncpy(uncomp_ptr, name_ptr, name_len + 1); + uncomp_ptr[name_len - 3] = 0; - system_len = 2*name_len + 100; - system_str = (char *)malloc(system_len + 1); - snprintf(system_str, system_len, - "set -o noclobber;gunzip -c %c%s%c > %c%s%c", - 0x22, name_ptr, 0x22, - 0x22, uncomp_ptr, 0x22); - /* 0x22 are " to allow spaces in filenames */ - printf("I am uncompressing %s into %s for mounting\n", - name_ptr, uncomp_ptr); - ret = system(system_str); - if(ret == 0) { - /* successfully ran */ - dsk->file = fopen(uncomp_ptr, "rb"); - iwm_printf("Opening .gz file %s\n", uncomp_ptr); + system_len = 2*name_len + 100; + system_str = (char *)malloc(system_len + 1); + snprintf(system_str, system_len, + "set -o noclobber;gunzip -c %c%s%c > %c%s%c", + 0x22, name_ptr, 0x22, + 0x22, uncomp_ptr, 0x22); + /* 0x22 are " to allow spaces in filenames */ + printf("I am uncompressing %s into %s for mounting\n", + name_ptr, uncomp_ptr); + ret = system(system_str); + if(ret == 0) { + /* successfully ran */ + dsk->file = fopen(uncomp_ptr, "rb"); + iwm_printf("Opening .gz file %s\n", uncomp_ptr); - /* and, unlink the temporary file */ - (void)unlink(uncomp_ptr); - } - free(system_str); - free(uncomp_ptr); - /* Reduce name_len by 3 so that subsequent compares for .po */ - /* look at the correct chars */ - name_len -= 3; - } + /* and, unlink the temporary file */ + (void)unlink(uncomp_ptr); + } + free(system_str); + free(uncomp_ptr); + /* Reduce name_len by 3 so that subsequent compares for .po */ + /* look at the correct chars */ + name_len -= 3; + } - if((!dsk->file) && can_write) { - dsk->file = fopen(name_ptr, "rb+"); - } + if((!dsk->file) && can_write) { + dsk->file = fopen(name_ptr, "rb+"); + } - if((!dsk->file) && can_write) { - printf("Trying to open %s read-only, errno: %d\n", name_ptr, - errno); - dsk->file = fopen(name_ptr, "rb"); - can_write = 0; - } + if((!dsk->file) && can_write) { + printf("Trying to open %s read-only, errno: %d\n", name_ptr, + errno); + dsk->file = fopen(name_ptr, "rb"); + can_write = 0; + } - if(!dsk->file) { - fatal_printf("Disk image %s does not exist!\n", name_ptr); - return; - } + if(!dsk->file) { + fatal_printf("Disk image %s does not exist!\n", name_ptr); + return; + } - if(can_write != 0) { - dsk->write_prot = 0; - dsk->write_through_to_unix = 1; - } else { - dsk->write_prot = 1; - dsk->write_through_to_unix = 0; - } + if(can_write != 0) { + dsk->write_prot = 0; + dsk->write_through_to_unix = 1; + } else { + dsk->write_prot = 1; + dsk->write_through_to_unix = 0; + } - save_track = dsk->cur_qtr_track; /* save arm position */ - dsk->image_type = DSK_TYPE_PRODOS; - dsk->image_start = 0; + save_track = dsk->cur_qtr_track; /* save arm position */ + dsk->image_type = DSK_TYPE_PRODOS; + dsk->image_start = 0; - /* See if it is in 2IMG format */ - ret = fread((char *)&buf_2img[0], 1, 512, dsk->file); - size = force_size; - if(size <= 0) { - size = cfg_get_fd_size(name_ptr); - } + /* See if it is in 2IMG format */ + ret = fread((char *)&buf_2img[0], 1, 512, dsk->file); + size = force_size; + if(size <= 0) { + size = cfg_get_fd_size(name_ptr); + } - /* Try to guess that there is a Mac Binary header of 128 bytes */ - /* See if image size & 0xfff = 0x080 which indicates extra 128 bytes */ - if((size & 0xfff) == 0x080) { - printf("Assuming Mac Binary header on %s\n", dsk->name_ptr); - dsk->image_start += 0x80; - } - image_identified = 0; - if(buf_2img[0] == '2' && buf_2img[1] == 'I' && buf_2img[2] == 'M' && - buf_2img[3] == 'G') { - /* It's a 2IMG disk */ - glogf("Image named %s is in 2IMG format", dsk->name_ptr); - image_identified = 1; + /* Try to guess that there is a Mac Binary header of 128 bytes */ + /* See if image size & 0xfff = 0x080 which indicates extra 128 bytes */ + if((size & 0xfff) == 0x080) { + printf("Assuming Mac Binary header on %s\n", dsk->name_ptr); + dsk->image_start += 0x80; + } + image_identified = 0; + if(buf_2img[0] == '2' && buf_2img[1] == 'I' && buf_2img[2] == 'M' && + buf_2img[3] == 'G') { + /* It's a 2IMG disk */ + glogf("Image named %s is in 2IMG format", dsk->name_ptr); + image_identified = 1; - if(buf_2img[12] == 0) { - glog("2IMG is in DOS 3.3 sector order"); - dsk->image_type = DSK_TYPE_DOS33; - } - if(buf_2img[19] & 0x80) { - /* disk is locked */ - glog("2IMG is write protected"); - dsk->write_prot = 1; - dsk->write_through_to_unix = 0; - } - if((buf_2img[17] & 1) && (dsk->image_type == DSK_TYPE_DOS33)) { - dsk->vol_num = buf_2img[16]; - glogf("Setting DOS 3.3 vol num to %d", dsk->vol_num); - } - // Some 2IMG archives have the size byte reversed - size = (buf_2img[31] << 24) + (buf_2img[30] << 16) + - (buf_2img[29] << 8) + buf_2img[28]; - unix_pos = (buf_2img[27] << 24) + (buf_2img[26] << 16) + - (buf_2img[25] << 8) + buf_2img[24]; - if(size == 0x800c00) { - // Byte reversed 0x0c8000 - size = 0x0c8000; - } - dsk->image_start = unix_pos; - dsk->image_size = size; - } - exp_size = 800*1024; - if(dsk->disk_525) { - exp_size = 140*1024; - } - if(!image_identified) { - /* See if it might be the Mac diskcopy format */ - tmp = (buf_2img[0x40] << 24) + (buf_2img[0x41] << 16) + - (buf_2img[0x42] << 8) + buf_2img[0x43]; - if((size >= (exp_size + 0x54)) && (tmp == exp_size)) { - /* It's diskcopy since data size field matches */ - glogf("Image named %s is in Mac diskcopy format", dsk->name_ptr); - image_identified = 1; - dsk->image_start += 0x54; - dsk->image_size = exp_size; - dsk->image_type = DSK_TYPE_PRODOS; /* ProDOS */ - } - } - if(!image_identified) { - /* Assume raw image */ - dsk->image_size = size; - dsk->image_type = DSK_TYPE_PRODOS; - if(dsk->disk_525) { - dsk->image_type = DSK_TYPE_DOS33; - if(name_len >= 4) { - cmp_o = dsk->name_ptr[name_len-1]; - cmp_p = dsk->name_ptr[name_len-2]; - cmp_dot = dsk->name_ptr[name_len-3]; - if(cmp_dot == '.' && - (cmp_p == 'p' || cmp_p == 'P') && - (cmp_o == 'o' || cmp_o == 'O')) { - dsk->image_type = DSK_TYPE_PRODOS; - } + if(buf_2img[12] == 0) { + glog("2IMG is in DOS 3.3 sector order"); + dsk->image_type = DSK_TYPE_DOS33; + } + if(buf_2img[19] & 0x80) { + /* disk is locked */ + glog("2IMG is write protected"); + dsk->write_prot = 1; + dsk->write_through_to_unix = 0; + } + if((buf_2img[17] & 1) && (dsk->image_type == DSK_TYPE_DOS33)) { + dsk->vol_num = buf_2img[16]; + glogf("Setting DOS 3.3 vol num to %d", dsk->vol_num); + } + // Some 2IMG archives have the size byte reversed + size = (buf_2img[31] << 24) + (buf_2img[30] << 16) + + (buf_2img[29] << 8) + buf_2img[28]; + unix_pos = (buf_2img[27] << 24) + (buf_2img[26] << 16) + + (buf_2img[25] << 8) + buf_2img[24]; + if(size == 0x800c00) { + // Byte reversed 0x0c8000 + size = 0x0c8000; + } + dsk->image_start = unix_pos; + dsk->image_size = size; + } + exp_size = 800*1024; + if(dsk->disk_525) { + exp_size = 140*1024; + } + if(!image_identified) { + /* See if it might be the Mac diskcopy format */ + tmp = (buf_2img[0x40] << 24) + (buf_2img[0x41] << 16) + + (buf_2img[0x42] << 8) + buf_2img[0x43]; + if((size >= (exp_size + 0x54)) && (tmp == exp_size)) { + /* It's diskcopy since data size field matches */ + glogf("Image named %s is in Mac diskcopy format", dsk->name_ptr); + image_identified = 1; + dsk->image_start += 0x54; + dsk->image_size = exp_size; + dsk->image_type = DSK_TYPE_PRODOS; /* ProDOS */ + } + } + if(!image_identified) { + /* Assume raw image */ + dsk->image_size = size; + dsk->image_type = DSK_TYPE_PRODOS; + if(dsk->disk_525) { + dsk->image_type = DSK_TYPE_DOS33; + if(name_len >= 4) { + cmp_o = dsk->name_ptr[name_len-1]; + cmp_p = dsk->name_ptr[name_len-2]; + cmp_dot = dsk->name_ptr[name_len-3]; + if(cmp_dot == '.' && + (cmp_p == 'p' || cmp_p == 'P') && + (cmp_o == 'o' || cmp_o == 'O')) { + dsk->image_type = DSK_TYPE_PRODOS; + } - cmp_b = dsk->name_ptr[name_len-1]; - cmp_i = dsk->name_ptr[name_len-2]; - cmp_n = dsk->name_ptr[name_len-3]; - cmp_dot = dsk->name_ptr[name_len-4]; - if(cmp_dot == '.' && - (cmp_n == 'n' || cmp_n == 'N') && - (cmp_i == 'i' || cmp_i == 'I') && - (cmp_b == 'b' || cmp_b == 'B')) { - dsk->image_type = DSK_TYPE_NIB; - dsk->write_prot = 1; - dsk->write_through_to_unix = 0; - } - } - } - } + cmp_b = dsk->name_ptr[name_len-1]; + cmp_i = dsk->name_ptr[name_len-2]; + cmp_n = dsk->name_ptr[name_len-3]; + cmp_dot = dsk->name_ptr[name_len-4]; + if(cmp_dot == '.' && + (cmp_n == 'n' || cmp_n == 'N') && + (cmp_i == 'i' || cmp_i == 'I') && + (cmp_b == 'b' || cmp_b == 'B')) { + dsk->image_type = DSK_TYPE_NIB; + dsk->write_prot = 1; + dsk->write_through_to_unix = 0; + } + } + } + } - dsk->disk_dirty = 0; - dsk->nib_pos = 0; - dsk->trks = 0; + dsk->disk_dirty = 0; + dsk->nib_pos = 0; + dsk->trks = 0; - if(dsk->smartport) { - g_highest_smartport_unit = MAX(dsk->drive, - g_highest_smartport_unit); + if(dsk->smartport) { + g_highest_smartport_unit = MAX(dsk->drive, + g_highest_smartport_unit); - if(partition_name != 0 || part_num >= 0) { - ret = cfg_partition_find_by_name_or_num(dsk->file, - partition_name, part_num, dsk); - printf("partition %s (num %d) mounted, wr_prot: %d\n", - partition_name, part_num, dsk->write_prot); + if(partition_name != 0 || part_num >= 0) { + ret = cfg_partition_find_by_name_or_num(dsk->file, + partition_name, part_num, dsk); + printf("partition %s (num %d) mounted, wr_prot: %d\n", + partition_name, part_num, dsk->write_prot); - if(ret < 0) { - fclose(dsk->file); - dsk->file = 0; - return; - } - } - iwm_printf("adding smartport device[%d], size:%08x, " - "img_sz:%08x\n", dsk->drive, dsk->trks[0].unix_len, - dsk->image_size); - } else if(dsk->disk_525) { - unix_pos = dsk->image_start; - size = dsk->image_size; - disk_set_num_tracks(dsk, 4*35); - len = 0x1000; - nibs = NIB_LEN_525; - if(dsk->image_type == DSK_TYPE_NIB) { - len = dsk->image_size / 35;; - nibs = len; - } - if(size != 35*len) { - glogf("Warning - Disk 5.25 error: size is %d, not 140K. Will try to mount anyway", size, 35*len); - } - for(i = 0; i < 35; i++) { - iwm_move_to_track(dsk, 4*i); - disk_unix_to_nib(dsk, 4*i, unix_pos, len, nibs); - unix_pos += len; - } - } else { - /* disk_35 */ - unix_pos = dsk->image_start; - size = dsk->image_size; - if(size != 800*1024) { - glogf("Warning - Disk 3.5 error: size is %d, not 800K. Will try to mount anyway", size, 35*len); - } - disk_set_num_tracks(dsk, 2*80); - for(i = 0; i < 2*80; i++) { - iwm_move_to_track(dsk, i); - len = g_track_bytes_35[i >> 5]; - nibs = g_track_nibs_35[i >> 5]; - iwm_printf("Trk: %d.%d = unix: %08x, %04x, %04x\n", - i>>1, i & 1, unix_pos, len, nibs); - disk_unix_to_nib(dsk, i, unix_pos, len, nibs); - unix_pos += len; + if(ret < 0) { + fclose(dsk->file); + dsk->file = 0; + return; + } + } + iwm_printf("adding smartport device[%d], size:%08x, " + "img_sz:%08x\n", dsk->drive, dsk->trks[0].unix_len, + dsk->image_size); + } else if(dsk->disk_525) { + unix_pos = dsk->image_start; + size = dsk->image_size; + disk_set_num_tracks(dsk, 4*35); + len = 0x1000; + nibs = NIB_LEN_525; + if(dsk->image_type == DSK_TYPE_NIB) { + len = dsk->image_size / 35;; + nibs = len; + } + if(size != 35*len) { + glogf("Warning - Disk 5.25 error: size is %d, not 140K. Will try to mount anyway", size, 35*len); + } + for(i = 0; i < 35; i++) { + iwm_move_to_track(dsk, 4*i); + disk_unix_to_nib(dsk, 4*i, unix_pos, len, nibs); + unix_pos += len; + } + } else { + /* disk_35 */ + unix_pos = dsk->image_start; + size = dsk->image_size; + if(size != 800*1024) { + glogf("Warning - Disk 3.5 error: size is %d, not 800K. Will try to mount anyway", size, 35*len); + } + disk_set_num_tracks(dsk, 2*80); + for(i = 0; i < 2*80; i++) { + iwm_move_to_track(dsk, i); + len = g_track_bytes_35[i >> 5]; + nibs = g_track_nibs_35[i >> 5]; + iwm_printf("Trk: %d.%d = unix: %08x, %04x, %04x\n", + i>>1, i & 1, unix_pos, len, nibs); + disk_unix_to_nib(dsk, i, unix_pos, len, nibs); + unix_pos += len; - iwm_printf(" trk_len:%05x\n", dsk->trks[i].track_len); - } - } + iwm_printf(" trk_len:%05x\n", dsk->trks[i].track_len); + } + } - iwm_move_to_track(dsk, save_track); + iwm_move_to_track(dsk, save_track); } -void -eject_named_disk(Disk *dsk, const char *name, const char *partition_name) -{ +void eject_named_disk(Disk *dsk, const char *name, const char *partition_name) { - if(!dsk->file) { - return; - } + if(!dsk->file) { + return; + } - /* If name matches, eject the disk! */ - if(!strcmp(dsk->name_ptr, name)) { - /* It matches, eject it */ - if((partition_name != 0) && (dsk->partition_name != 0)) { - /* If both have partitions, and they differ, then */ - /* don't eject. Otherwise, eject */ - if(strcmp(dsk->partition_name, partition_name) != 0) { - /* Don't eject */ - return; - } - } - eject_disk(dsk); - } + /* If name matches, eject the disk! */ + if(!strcmp(dsk->name_ptr, name)) { + /* It matches, eject it */ + if((partition_name != 0) && (dsk->partition_name != 0)) { + /* If both have partitions, and they differ, then */ + /* don't eject. Otherwise, eject */ + if(strcmp(dsk->partition_name, partition_name) != 0) { + /* Don't eject */ + return; + } + } + eject_disk(dsk); + } } -void -eject_disk_by_num(int slot, int drive) -{ - Disk *dsk; +void eject_disk_by_num(int slot, int drive) { + Disk *dsk; - dsk = cfg_get_dsk_from_slot_drive(slot, drive); + dsk = cfg_get_dsk_from_slot_drive(slot, drive); - eject_disk(dsk); + eject_disk(dsk); } -void -eject_disk(Disk *dsk) -{ - int motor_on; - int i; +void eject_disk(Disk *dsk) { + int motor_on; + int i; - if(!dsk->file) { - return; - } + if(!dsk->file) { + return; + } - g_config_gsplus_update_needed = 1; + g_config_gsplus_update_needed = 1; - motor_on = iwm.motor_on; - if(g_c031_disk35 & 0x40) { - motor_on = iwm.motor_on35; - } - if(motor_on) { - halt_printf("Try eject dsk:%s, but motor_on!\n", dsk->name_ptr); - } + motor_on = iwm.motor_on; + if(g_c031_disk35 & 0x40) { + motor_on = iwm.motor_on35; + } + if(motor_on) { + halt_printf("Try eject dsk:%s, but motor_on!\n", dsk->name_ptr); + } - iwm_flush_disk_to_unix(dsk); + iwm_flush_disk_to_unix(dsk); - glogf("Ejecting disk: %s", dsk->name_ptr); + glogf("Ejecting disk: %s", dsk->name_ptr); - /* Free all memory, close file */ + /* Free all memory, close file */ - /* free the tracks first */ - if(dsk->trks != 0) { - for(i = 0; i < dsk->num_tracks; i++) { - if(dsk->trks[i].nib_area) { - free(dsk->trks[i].nib_area); - } - dsk->trks[i].nib_area = 0; - dsk->trks[i].track_len = 0; - } - free(dsk->trks); - } - dsk->num_tracks = 0; - dsk->trks = 0; + /* free the tracks first */ + if(dsk->trks != 0) { + for(i = 0; i < dsk->num_tracks; i++) { + if(dsk->trks[i].nib_area) { + free(dsk->trks[i].nib_area); + } + dsk->trks[i].nib_area = 0; + dsk->trks[i].track_len = 0; + } + free(dsk->trks); + } + dsk->num_tracks = 0; + dsk->trks = 0; - /* close file, clean up dsk struct */ - fclose(dsk->file); + /* close file, clean up dsk struct */ + fclose(dsk->file); - dsk->image_start = 0; - dsk->image_size = 0; - dsk->nib_pos = 0; - dsk->disk_dirty = 0; - dsk->write_through_to_unix = 0; - dsk->write_prot = 1; - dsk->file = 0; - dsk->just_ejected = 1; + dsk->image_start = 0; + dsk->image_size = 0; + dsk->nib_pos = 0; + dsk->disk_dirty = 0; + dsk->write_through_to_unix = 0; + dsk->write_prot = 1; + dsk->file = 0; + dsk->just_ejected = 1; - /* Leave name_ptr valid */ + /* Leave name_ptr valid */ } -int -cfg_get_fd_size(char *filename) -{ - struct stat stat_buf; - int ret; +int cfg_get_fd_size(char *filename) { + struct stat stat_buf; + int ret; - ret = stat(filename, &stat_buf); - if(ret != 0) { - fprintf(stderr,"stat %s returned errno: %d\n", - filename, errno); - stat_buf.st_size = 0; - } + ret = stat(filename, &stat_buf); + if(ret != 0) { + fprintf(stderr,"stat %s returned errno: %d\n", + filename, errno); + stat_buf.st_size = 0; + } - return stat_buf.st_size; + return stat_buf.st_size; } -int -cfg_partition_read_block(FILE *file, void *buf, int blk, int blk_size) -{ - int ret; +int cfg_partition_read_block(FILE *file, void *buf, int blk, int blk_size) { + int ret; - ret = fseek(file, blk * blk_size, SEEK_SET); - if(ret != 0) { - printf("fseek: wanted: %08x, errno: %d\n", - blk * blk_size, errno); - return 0; - } + ret = fseek(file, blk * blk_size, SEEK_SET); + if(ret != 0) { + printf("fseek: wanted: %08x, errno: %d\n", + blk * blk_size, errno); + return 0; + } - ret = fread((char *)buf, 1, blk_size, file); - if(ret != blk_size) { - printf("ret: %08x, wanted %08x, errno: %d\n", ret, blk_size, - errno); - return 0; - } - return ret; + ret = fread((char *)buf, 1, blk_size, file); + if(ret != blk_size) { + printf("ret: %08x, wanted %08x, errno: %d\n", ret, blk_size, + errno); + return 0; + } + return ret; } -int -cfg_partition_find_by_name_or_num(FILE *file, const char *partnamestr, int part_num, - Disk *dsk) -{ - Cfg_dirent *direntptr; - int match; - int num_parts; - int i; +int cfg_partition_find_by_name_or_num(FILE *file, const char *partnamestr, int part_num, + Disk *dsk) { + Cfg_dirent *direntptr; + int match; + int num_parts; + int i; - num_parts = cfg_partition_make_list(dsk->name_ptr, file); + num_parts = cfg_partition_make_list(dsk->name_ptr, file); - if(num_parts <= 0) { - return -1; - } + if(num_parts <= 0) { + return -1; + } - for(i = 0; i < g_cfg_partitionlist.last; i++) { - direntptr = &(g_cfg_partitionlist.direntptr[i]); - match = 0; - if((strncmp(partnamestr, direntptr->name, 32) == 0) && - (part_num < 0)) { - //printf("partition, match1, name:%s %s, part_num:%d\n", - // partnamestr, direntptr->name, part_num); + for(i = 0; i < g_cfg_partitionlist.last; i++) { + direntptr = &(g_cfg_partitionlist.direntptr[i]); + match = 0; + if((strncmp(partnamestr, direntptr->name, 32) == 0) && + (part_num < 0)) { + //printf("partition, match1, name:%s %s, part_num:%d\n", + // partnamestr, direntptr->name, part_num); - match = 1; - } - if((partnamestr == 0) && (direntptr->part_num == part_num)) { - //printf("partition, match2, n:%s, part_num:%d == %d\n", - // direntptr->name, direntptr->part_num, part_num); - match = 1; - } - if(match) { - dsk->image_start = direntptr->image_start; - dsk->image_size = direntptr->size; - //printf("match with image_start: %08x, image_size: " - // "%08x\n", dsk->image_start, dsk->image_size); + match = 1; + } + if((partnamestr == 0) && (direntptr->part_num == part_num)) { + //printf("partition, match2, n:%s, part_num:%d == %d\n", + // direntptr->name, direntptr->part_num, part_num); + match = 1; + } + if(match) { + dsk->image_start = direntptr->image_start; + dsk->image_size = direntptr->size; + //printf("match with image_start: %08x, image_size: " + // "%08x\n", dsk->image_start, dsk->image_size); - return i; - } - } + return i; + } + } - return -1; + return -1; } -int -cfg_partition_make_list(char *filename, FILE *file) -{ - Driver_desc *driver_desc_ptr; - Part_map *part_map_ptr; - word32 *blk_bufptr; - word32 start; - word32 len; - word32 data_off; - word32 data_len; - word32 sig; - int size; - int image_start, image_size; - int is_dir; - int block_size; - int map_blks; - int cur_blk; +int cfg_partition_make_list(char *filename, FILE *file) { + Driver_desc *driver_desc_ptr; + Part_map *part_map_ptr; + word32 *blk_bufptr; + word32 start; + word32 len; + word32 data_off; + word32 data_len; + word32 sig; + int size; + int image_start, image_size; + int is_dir; + int block_size; + int map_blks; + int cur_blk; - block_size = 512; + block_size = 512; - cfg_free_alldirents(&g_cfg_partitionlist); + cfg_free_alldirents(&g_cfg_partitionlist); - blk_bufptr = (word32 *)malloc(MAX_PARTITION_BLK_SIZE); + blk_bufptr = (word32 *)malloc(MAX_PARTITION_BLK_SIZE); - cfg_partition_read_block(file, blk_bufptr, 0, block_size); + cfg_partition_read_block(file, blk_bufptr, 0, block_size); - driver_desc_ptr = (Driver_desc *)blk_bufptr; - sig = GET_BE_WORD16(driver_desc_ptr->sig); - block_size = GET_BE_WORD16(driver_desc_ptr->blk_size); - if(block_size == 0) { - block_size = 512; - } - if(sig != 0x4552 || block_size < 0x200 || - (block_size > MAX_PARTITION_BLK_SIZE)) { - cfg_printf("Partition error: No driver descriptor map found\n"); - free(blk_bufptr); - return 0; - } + driver_desc_ptr = (Driver_desc *)blk_bufptr; + sig = GET_BE_WORD16(driver_desc_ptr->sig); + block_size = GET_BE_WORD16(driver_desc_ptr->blk_size); + if(block_size == 0) { + block_size = 512; + } + if(sig != 0x4552 || block_size < 0x200 || + (block_size > MAX_PARTITION_BLK_SIZE)) { + cfg_printf("Partition error: No driver descriptor map found\n"); + free(blk_bufptr); + return 0; + } - map_blks = 1; - cur_blk = 0; - size = cfg_get_fd_size(filename); - cfg_file_add_dirent(&g_cfg_partitionlist, "None - Whole image", - is_dir=0, size, 0, -1); + map_blks = 1; + cur_blk = 0; + size = cfg_get_fd_size(filename); + cfg_file_add_dirent(&g_cfg_partitionlist, "None - Whole image", + is_dir=0, size, 0, -1); - while(cur_blk < map_blks) { - cur_blk++; - cfg_partition_read_block(file, blk_bufptr, cur_blk, block_size); - part_map_ptr = (Part_map *)blk_bufptr; - sig = GET_BE_WORD16(part_map_ptr->sig); - if(cur_blk <= 1) { - map_blks = MIN(20, - GET_BE_WORD32(part_map_ptr->map_blk_cnt)); - } - if(sig != 0x504d) { - printf("Partition entry %d bad signature:%04x\n", - cur_blk, sig); - free(blk_bufptr); - return g_cfg_partitionlist.last; - } + while(cur_blk < map_blks) { + cur_blk++; + cfg_partition_read_block(file, blk_bufptr, cur_blk, block_size); + part_map_ptr = (Part_map *)blk_bufptr; + sig = GET_BE_WORD16(part_map_ptr->sig); + if(cur_blk <= 1) { + map_blks = MIN(20, + GET_BE_WORD32(part_map_ptr->map_blk_cnt)); + } + if(sig != 0x504d) { + printf("Partition entry %d bad signature:%04x\n", + cur_blk, sig); + free(blk_bufptr); + return g_cfg_partitionlist.last; + } - /* found it, check for consistency */ - start = GET_BE_WORD32(part_map_ptr->phys_part_start); - len = GET_BE_WORD32(part_map_ptr->part_blk_cnt); - data_off = GET_BE_WORD32(part_map_ptr->data_start); - data_len = GET_BE_WORD32(part_map_ptr->data_cnt); - if(data_off + data_len > len) { - printf("Poorly formed entry\n"); - continue; - } + /* found it, check for consistency */ + start = GET_BE_WORD32(part_map_ptr->phys_part_start); + len = GET_BE_WORD32(part_map_ptr->part_blk_cnt); + data_off = GET_BE_WORD32(part_map_ptr->data_start); + data_len = GET_BE_WORD32(part_map_ptr->data_cnt); + if(data_off + data_len > len) { + printf("Poorly formed entry\n"); + continue; + } - if(data_len < 10 || start < 1) { - printf("Poorly formed entry %d, datalen:%d, " - "start:%08x\n", cur_blk, data_len, start); - continue; - } + if(data_len < 10 || start < 1) { + printf("Poorly formed entry %d, datalen:%d, " + "start:%08x\n", cur_blk, data_len, start); + continue; + } - image_size = data_len * block_size; - image_start = (start + data_off) * block_size; - is_dir = 2*(image_size < 800*1024); + image_size = data_len * block_size; + image_start = (start + data_off) * block_size; + is_dir = 2*(image_size < 800*1024); #if 0 - printf(" partition add entry %d = %s %d %08x %08x\n", - cur_blk, part_map_ptr->part_name, is_dir, - image_size, image_start); + printf(" partition add entry %d = %s %d %08x %08x\n", + cur_blk, part_map_ptr->part_name, is_dir, + image_size, image_start); #endif - cfg_file_add_dirent(&g_cfg_partitionlist, - part_map_ptr->part_name, is_dir, image_size, - image_start, cur_blk); - } + cfg_file_add_dirent(&g_cfg_partitionlist, + part_map_ptr->part_name, is_dir, image_size, + image_start, cur_blk); + } - free(blk_bufptr); - return g_cfg_partitionlist.last; + free(blk_bufptr); + return g_cfg_partitionlist.last; } -int -cfg_maybe_insert_disk(int slot, int drive, const char *namestr) -{ - int num_parts; - FILE *file; +int cfg_maybe_insert_disk(int slot, int drive, const char *namestr) { + int num_parts; + FILE *file; - file = fopen(namestr, "rb"); - if(!file) { - fatal_printf("Cannot open disk image: %s\n", namestr); - return 0; - } + file = fopen(namestr, "rb"); + if(!file) { + fatal_printf("Cannot open disk image: %s\n", namestr); + return 0; + } - num_parts = cfg_partition_make_list((char*)namestr, file); - fclose(file); + num_parts = cfg_partition_make_list((char*)namestr, file); + fclose(file); - if(num_parts > 0) { - printf("Choose a partition\n"); - g_cfg_select_partition = 1; - } else { - insert_disk(slot, drive, namestr, 0, 0, 0, -1); - return 1; - } - return 0; + if(num_parts > 0) { + printf("Choose a partition\n"); + g_cfg_select_partition = 1; + } else { + insert_disk(slot, drive, namestr, 0, 0, 0, -1); + return 1; + } + return 0; } -int -cfg_stat(char *path, struct stat *sb) -{ - int removed_slash; - int len; - int ret; +int cfg_stat(char *path, struct stat *sb) { + int removed_slash; + int len; + int ret; - removed_slash = 0; - len = 0; + removed_slash = 0; + len = 0; #ifdef _WIN32 - /* Windows doesn't like to stat paths ending in a /, so remove it */ - len = strlen(path); - if((len > 1) && (path[len - 1] == '/') ) { - path[len - 1] = 0; /* remove the slash */ - removed_slash = 1; - } + /* Windows doesn't like to stat paths ending in a /, so remove it */ + len = strlen(path); + if((len > 1) && (path[len - 1] == '/') ) { + path[len - 1] = 0; /* remove the slash */ + removed_slash = 1; + } #endif - ret = stat(path, sb); + ret = stat(path, sb); #ifdef _WIN32 - /* put the slash back */ - if(removed_slash) { - path[len - 1] = '/'; - } + /* put the slash back */ + if(removed_slash) { + path[len - 1] = '/'; + } #endif - return ret; + return ret; } -void -cfg_htab_vtab(int x, int y) -{ - if(x > 79) { - x = 0; - } - if(y > 23) { - y = 0; - } - g_cfg_curs_x = x; - g_cfg_curs_y = y; - g_cfg_curs_inv = 0; - g_cfg_curs_mousetext = 0; +void cfg_htab_vtab(int x, int y) { + if(x > 79) { + x = 0; + } + if(y > 23) { + y = 0; + } + g_cfg_curs_x = x; + g_cfg_curs_y = y; + g_cfg_curs_inv = 0; + g_cfg_curs_mousetext = 0; } -void -cfg_home() -{ - int i; +void cfg_home() { + int i; - cfg_htab_vtab(0, 0); - for(i = 0; i < 24; i++) { - cfg_cleol(); - } + cfg_htab_vtab(0, 0); + for(i = 0; i < 24; i++) { + cfg_cleol(); + } } -void -cfg_cleol() -{ - g_cfg_curs_inv = 0; - g_cfg_curs_mousetext = 0; - cfg_putchar(' '); - while(g_cfg_curs_x != 0) { - cfg_putchar(' '); - } +void cfg_cleol() { + g_cfg_curs_inv = 0; + g_cfg_curs_mousetext = 0; + cfg_putchar(' '); + while(g_cfg_curs_x != 0) { + cfg_putchar(' '); + } } -void -cfg_putchar(int c) -{ - int offset; - int x, y; +void cfg_putchar(int c) { + int offset; + int x, y; - if(c == '\n') { - cfg_cleol(); - return; - } - if(c == '\b') { - g_cfg_curs_inv = !g_cfg_curs_inv; - return; - } - if(c == '\t') { - g_cfg_curs_mousetext = !g_cfg_curs_mousetext; - return; - } - y = g_cfg_curs_y; - x = g_cfg_curs_x; + if(c == '\n') { + cfg_cleol(); + return; + } + if(c == '\b') { + g_cfg_curs_inv = !g_cfg_curs_inv; + return; + } + if(c == '\t') { + g_cfg_curs_mousetext = !g_cfg_curs_mousetext; + return; + } + y = g_cfg_curs_y; + x = g_cfg_curs_x; - offset = g_screen_index[g_cfg_curs_y]; - if((x & 1) == 0) { - offset += 0x10000; - } - if(g_cfg_curs_inv) { - if(c >= 0x40 && c < 0x60) { - c = c & 0x1f; - } - } else { - c = c | 0x80; - } - if(g_cfg_curs_mousetext) { - c = (c & 0x1f) | 0x40; - } - set_memory_c(0xe00400 + offset + (x >> 1), c, 0); - x++; - if(x >= 80) { - x = 0; - y++; - if(y >= 24) { - y = 0; - } - } - g_cfg_curs_y = y; - g_cfg_curs_x = x; + offset = g_screen_index[g_cfg_curs_y]; + if((x & 1) == 0) { + offset += 0x10000; + } + if(g_cfg_curs_inv) { + if(c >= 0x40 && c < 0x60) { + c = c & 0x1f; + } + } else { + c = c | 0x80; + } + if(g_cfg_curs_mousetext) { + c = (c & 0x1f) | 0x40; + } + set_memory_c(0xe00400 + offset + (x >> 1), c, 0); + x++; + if(x >= 80) { + x = 0; + y++; + if(y >= 24) { + y = 0; + } + } + g_cfg_curs_y = y; + g_cfg_curs_x = x; } -void -cfg_printf(const char *fmt, ...) -{ - va_list ap; - int c; - int i; +void cfg_printf(const char *fmt, ...) { + va_list ap; + int c; + int i; - va_start(ap, fmt); - (void)vsnprintf(g_cfg_printf_buf, CFG_PRINTF_BUFSIZE, fmt, ap); - va_end(ap); + va_start(ap, fmt); + (void)vsnprintf(g_cfg_printf_buf, CFG_PRINTF_BUFSIZE, fmt, ap); + va_end(ap); - for(i = 0; i < CFG_PRINTF_BUFSIZE; i++) { - c = g_cfg_printf_buf[i]; - if(c == 0) { - return; - } - cfg_putchar(c); - } + for(i = 0; i < CFG_PRINTF_BUFSIZE; i++) { + c = g_cfg_printf_buf[i]; + if(c == 0) { + return; + } + cfg_putchar(c); + } } -void -cfg_print_num(int num, int max_len) -{ - char buf[64]; - char buf2[64]; - int len; - int cnt; - int c; - int i, j; +void cfg_print_num(int num, int max_len) { + char buf[64]; + char buf2[64]; + int len; + int cnt; + int c; + int i, j; - /* Prints right-adjusted "num" in field "max_len" wide */ - snprintf(&buf[0], 64, "%d", num); - len = strlen(buf); - for(i = 0; i < 64; i++) { - buf2[i] = ' '; - } - j = max_len + 1; - buf2[j] = 0; - j--; - cnt = 0; - for(i = len - 1; (i >= 0) && (j >= 1); i--) { - c = buf[i]; - if(c >= '0' && c <= '9') { - if(cnt >= 3) { - buf2[j--] = ','; - cnt = 0; - } - cnt++; - } - buf2[j--] = c; - } - cfg_printf(&buf2[1]); + /* Prints right-adjusted "num" in field "max_len" wide */ + snprintf(&buf[0], 64, "%d", num); + len = strlen(buf); + for(i = 0; i < 64; i++) { + buf2[i] = ' '; + } + j = max_len + 1; + buf2[j] = 0; + j--; + cnt = 0; + for(i = len - 1; (i >= 0) && (j >= 1); i--) { + c = buf[i]; + if(c >= '0' && c <= '9') { + if(cnt >= 3) { + buf2[j--] = ','; + cnt = 0; + } + cnt++; + } + buf2[j--] = c; + } + cfg_printf(&buf2[1]); } -void -cfg_get_disk_name(char *outstr, int maxlen, int type_ext, int with_extras) -{ - Disk *dsk; - int slot, drive; +void cfg_get_disk_name(char *outstr, int maxlen, int type_ext, int with_extras) { + Disk *dsk; + int slot, drive; - slot = type_ext >> 8; - drive = type_ext & 0xff; - dsk = cfg_get_dsk_from_slot_drive(slot, drive); + slot = type_ext >> 8; + drive = type_ext & 0xff; + dsk = cfg_get_dsk_from_slot_drive(slot, drive); - outstr[0] = 0; - if(dsk->name_ptr == 0) { - return; - } + outstr[0] = 0; + if(dsk->name_ptr == 0) { + return; + } - config_generate_config_gsplus_name(outstr, maxlen, dsk, with_extras); + config_generate_config_gsplus_name(outstr, maxlen, dsk, with_extras); } -void -cfg_parse_menu(Cfg_menu *menuptr, int menu_pos, int highlight_pos, int change) -{ - char valbuf[CFG_OPT_MAXSTR]; - char **str_ptr; - const char *menustr; - char *curstr, *defstr; - char *str; - char *outstr; - int *iptr; - int val; - int num_opts; - int opt_num; - int bufpos, outpos; - int curval, defval; - int type; - int type_ext; - int opt_get_str; - int separator; - int len; - int c; - int i; +void cfg_parse_menu(Cfg_menu *menuptr, int menu_pos, int highlight_pos, int change) { + char valbuf[CFG_OPT_MAXSTR]; + char **str_ptr; + const char *menustr; + char *curstr, *defstr; + char *str; + char *outstr; + int *iptr; + int val; + int num_opts; + int opt_num; + int bufpos, outpos; + int curval, defval; + int type; + int type_ext; + int opt_get_str; + int separator; + int len; + int c; + int i; - g_cfg_opt_buf[0] = 0; + g_cfg_opt_buf[0] = 0; - num_opts = 0; - opt_get_str = 0; - separator = ','; + num_opts = 0; + opt_get_str = 0; + separator = ','; - menuptr += menu_pos; /* move forward to entry menu_pos */ + menuptr += menu_pos; /* move forward to entry menu_pos */ - menustr = menuptr->str; - type = menuptr->cfgtype; - type_ext = (type >> 4); - type = type & 0xf; - len = strlen(menustr) + 1; + menustr = menuptr->str; + type = menuptr->cfgtype; + type_ext = (type >> 4); + type = type & 0xf; + len = strlen(menustr) + 1; - bufpos = 0; - outstr = &(g_cfg_opt_buf[0]); + bufpos = 0; + outstr = &(g_cfg_opt_buf[0]); - outstr[bufpos++] = ' '; - outstr[bufpos++] = ' '; - outstr[bufpos++] = '\t'; - outstr[bufpos++] = '\t'; - outstr[bufpos++] = ' '; - outstr[bufpos++] = ' '; + outstr[bufpos++] = ' '; + outstr[bufpos++] = ' '; + outstr[bufpos++] = '\t'; + outstr[bufpos++] = '\t'; + outstr[bufpos++] = ' '; + outstr[bufpos++] = ' '; - if(menu_pos == highlight_pos) { - outstr[bufpos++] = '\b'; - } + if(menu_pos == highlight_pos) { + outstr[bufpos++] = '\b'; + } - opt_get_str = 2; - i = -1; - outpos = bufpos; + opt_get_str = 2; + i = -1; + outpos = bufpos; #if 0 - printf("cfg menu_pos: %d str len: %d\n", menu_pos, len); + printf("cfg menu_pos: %d str len: %d\n", menu_pos, len); #endif - while(++i < len) { - c = menustr[i]; - if(c == separator) { - if(i == 0) { - continue; - } - c = 0; - } - outstr[outpos++] = c; - outstr[outpos] = 0; - if(outpos >= CFG_OPT_MAXSTR) { - fprintf(stderr, "CFG_OPT_MAXSTR exceeded\n"); - my_exit(1); - } - if(c == 0) { - if(opt_get_str == 2) { - outstr = &(valbuf[0]); - bufpos = outpos - 1; - opt_get_str = 0; - } else if(opt_get_str) { + while(++i < len) { + c = menustr[i]; + if(c == separator) { + if(i == 0) { + continue; + } + c = 0; + } + outstr[outpos++] = c; + outstr[outpos] = 0; + if(outpos >= CFG_OPT_MAXSTR) { + fprintf(stderr, "CFG_OPT_MAXSTR exceeded\n"); + my_exit(1); + } + if(c == 0) { + if(opt_get_str == 2) { + outstr = &(valbuf[0]); + bufpos = outpos - 1; + opt_get_str = 0; + } else if(opt_get_str) { #if 0 - if(menu_pos == highlight_pos) { - printf("menu_pos %d opt %d = %s=%d\n", - menu_pos, num_opts, - g_cfg_opts_strs[num_opts], - g_cfg_opts_vals[num_opts]); - } + if(menu_pos == highlight_pos) { + printf("menu_pos %d opt %d = %s=%d\n", + menu_pos, num_opts, + g_cfg_opts_strs[num_opts], + g_cfg_opts_vals[num_opts]); + } #endif - num_opts++; - outstr = &(valbuf[0]); - opt_get_str = 0; - if(num_opts >= CFG_MAX_OPTS) { - fprintf(stderr, "CFG_MAX_OPTS oflow\n"); - my_exit(1); - } - } else { - if (type == CFGTYPE_INT) - { - val = strtoul(valbuf, 0, 0); - g_cfg_opts_vals[num_opts] = val; - } + num_opts++; + outstr = &(valbuf[0]); + opt_get_str = 0; + if(num_opts >= CFG_MAX_OPTS) { + fprintf(stderr, "CFG_MAX_OPTS oflow\n"); + my_exit(1); + } + } else { + if (type == CFGTYPE_INT) + { + val = strtoul(valbuf, 0, 0); + g_cfg_opts_vals[num_opts] = val; + } - if (type == CFGTYPE_STR) - { - strncpy(&(g_cfg_opts_strvals[num_opts][0]),&(valbuf[0]),CFG_OPT_MAXSTR); - } - outstr = &(g_cfg_opts_strs[num_opts][0]); - opt_get_str = 1; - } - outpos = 0; - outstr[0] = 0; - } - } + if (type == CFGTYPE_STR) + { + strncpy(&(g_cfg_opts_strvals[num_opts][0]),&(valbuf[0]),CFG_OPT_MAXSTR); + } + outstr = &(g_cfg_opts_strs[num_opts][0]); + opt_get_str = 1; + } + outpos = 0; + outstr[0] = 0; + } + } - if(menu_pos == highlight_pos) { - g_cfg_opt_buf[bufpos++] = '\b'; - } + if(menu_pos == highlight_pos) { + g_cfg_opt_buf[bufpos++] = '\b'; + } - g_cfg_opt_buf[bufpos] = 0; + g_cfg_opt_buf[bufpos] = 0; - // Figure out if we should get a checkmark - curval = -1; - defval = -1; - curstr = 0; + // Figure out if we should get a checkmark + curval = -1; + defval = -1; + curstr = 0; - switch(type) { + switch(type) { - case CFGTYPE_INT: - iptr = (int*)menuptr->ptr; // OG Added cast - curval = *iptr; - iptr = (int*)menuptr->defptr; // OG Added cast - defval = *iptr; - if(curval == defval) { - g_cfg_opt_buf[3] = 'D'; /* checkmark */ - g_cfg_opt_buf[4] = '\t'; - } - break; - case CFGTYPE_STR: - case CFGTYPE_FILE: - case CFGTYPE_DIR: - str_ptr = (char **)menuptr->ptr; - curstr = *str_ptr; - str_ptr = (char **)menuptr->defptr; - defstr = *str_ptr; - if(strcmp(curstr,defstr) == 0) { - g_cfg_opt_buf[3] = 'D'; /* checkmark */ - g_cfg_opt_buf[4] = '\t'; - } - break; + case CFGTYPE_INT: + iptr = (int*)menuptr->ptr; // OG Added cast + curval = *iptr; + iptr = (int*)menuptr->defptr; // OG Added cast + defval = *iptr; + if(curval == defval) { + g_cfg_opt_buf[3] = 'D'; /* checkmark */ + g_cfg_opt_buf[4] = '\t'; + } + break; + case CFGTYPE_STR: + case CFGTYPE_FILE: + case CFGTYPE_DIR: + str_ptr = (char **)menuptr->ptr; + curstr = *str_ptr; + str_ptr = (char **)menuptr->defptr; + defstr = *str_ptr; + if(strcmp(curstr,defstr) == 0) { + g_cfg_opt_buf[3] = 'D'; /* checkmark */ + g_cfg_opt_buf[4] = '\t'; + } + break; - // If it's a menu, give it a special menu indicator - case CFGTYPE_MENU: - g_cfg_opt_buf[1] = '\t'; - g_cfg_opt_buf[2] = 'M'; /* return-like symbol */ - g_cfg_opt_buf[3] = '\t'; - g_cfg_opt_buf[4] = ' '; - break; - } + // If it's a menu, give it a special menu indicator + case CFGTYPE_MENU: + g_cfg_opt_buf[1] = '\t'; + g_cfg_opt_buf[2] = 'M'; /* return-like symbol */ + g_cfg_opt_buf[3] = '\t'; + g_cfg_opt_buf[4] = ' '; + break; + } - // Decide what to display on the "right" side - str = 0; - opt_num = -1; + // Decide what to display on the "right" side + str = 0; + opt_num = -1; - switch(type) { + switch(type) { - case CFGTYPE_INT: - case CFGTYPE_FILE: - case CFGTYPE_DIR: - g_cfg_opt_buf[bufpos++] = ' '; - g_cfg_opt_buf[bufpos++] = '='; - g_cfg_opt_buf[bufpos++] = ' '; - g_cfg_opt_buf[bufpos] = 0; - for(i = 0; i < num_opts; i++) { - if(curval == g_cfg_opts_vals[i]) { - opt_num = i; - break; - } - } - break; + case CFGTYPE_INT: + case CFGTYPE_FILE: + case CFGTYPE_DIR: + g_cfg_opt_buf[bufpos++] = ' '; + g_cfg_opt_buf[bufpos++] = '='; + g_cfg_opt_buf[bufpos++] = ' '; + g_cfg_opt_buf[bufpos] = 0; + for(i = 0; i < num_opts; i++) { + if(curval == g_cfg_opts_vals[i]) { + opt_num = i; + break; + } + } + break; - case CFGTYPE_STR: - g_cfg_opt_buf[bufpos++] = ' '; - g_cfg_opt_buf[bufpos++] = '='; - g_cfg_opt_buf[bufpos++] = ' '; - g_cfg_opt_buf[bufpos] = 0; - for(i = 0; i < num_opts; i++) { - if(!strcmp(curstr,g_cfg_opts_strvals[i])) { - opt_num = i; - break; - } - } - break; - } + case CFGTYPE_STR: + g_cfg_opt_buf[bufpos++] = ' '; + g_cfg_opt_buf[bufpos++] = '='; + g_cfg_opt_buf[bufpos++] = ' '; + g_cfg_opt_buf[bufpos] = 0; + for(i = 0; i < num_opts; i++) { + if(!strcmp(curstr,g_cfg_opts_strvals[i])) { + opt_num = i; + break; + } + } + break; + } - if(change != 0) { - if(type == CFGTYPE_INT) { - if(num_opts > 0) { - opt_num += change; - if(opt_num >= num_opts) { - opt_num = 0; - } - if(opt_num < 0) { - opt_num = num_opts - 1; - } - curval = g_cfg_opts_vals[opt_num]; - } else { - curval += change; - /* HACK: min_val, max_val testing here */ - } - iptr = (int *)menuptr->ptr; - *iptr = curval; - } - if(type == CFGTYPE_STR) { - if(num_opts > 0) { - opt_num += change; - if(opt_num >= num_opts) { - opt_num = 0; - } - if(opt_num < 0) { - opt_num = num_opts - 1; - } - curstr = g_cfg_opts_strvals[opt_num]; - } else { - //curstr += change; - /* HACK: min_val, max_val testing here */ - } - str_ptr = (char **)menuptr->ptr; - *str_ptr = curstr; - } - g_config_gsplus_update_needed = 1; - } + if(change != 0) { + if(type == CFGTYPE_INT) { + if(num_opts > 0) { + opt_num += change; + if(opt_num >= num_opts) { + opt_num = 0; + } + if(opt_num < 0) { + opt_num = num_opts - 1; + } + curval = g_cfg_opts_vals[opt_num]; + } else { + curval += change; + /* HACK: min_val, max_val testing here */ + } + iptr = (int *)menuptr->ptr; + *iptr = curval; + } + if(type == CFGTYPE_STR) { + if(num_opts > 0) { + opt_num += change; + if(opt_num >= num_opts) { + opt_num = 0; + } + if(opt_num < 0) { + opt_num = num_opts - 1; + } + curstr = g_cfg_opts_strvals[opt_num]; + } else { + //curstr += change; + /* HACK: min_val, max_val testing here */ + } + str_ptr = (char **)menuptr->ptr; + *str_ptr = curstr; + } + g_config_gsplus_update_needed = 1; + } #if 0 - if(menu_pos == highlight_pos) { - printf("menu_pos %d opt_num %d\n", menu_pos, opt_num); - } + if(menu_pos == highlight_pos) { + printf("menu_pos %d opt_num %d\n", menu_pos, opt_num); + } #endif - if(opt_num >= 0) { - str = &(g_cfg_opts_strs[opt_num][0]); - } else { - switch(type) { - case CFGTYPE_INT: - str = &(g_cfg_opts_strs[0][0]); - snprintf(str, CFG_OPT_MAXSTR, "%d", curval); - break; - case CFGTYPE_STR: - str = &(g_cfg_opts_strs[0][0]); - //printf("curstr is: %s str is: %s\n", curstr,str); - snprintf(str, CFG_OPT_MAXSTR, "%s", curstr); - break; - case CFGTYPE_DISK: - str = &(g_cfg_opts_strs[0][0]), - cfg_get_disk_name(str, CFG_OPT_MAXSTR, type_ext, 1); - str = cfg_shorten_filename(str, 68); - break; - case CFGTYPE_FILE: - case CFGTYPE_DIR: - str = &(g_cfg_opts_strs[0][0]); - snprintf(str, CFG_OPT_MAXSTR, "%s", curstr); - str = cfg_shorten_filename(str, 68); - break; - default: - str = ""; - } - } + if(opt_num >= 0) { + str = &(g_cfg_opts_strs[opt_num][0]); + } else { + switch(type) { + case CFGTYPE_INT: + str = &(g_cfg_opts_strs[0][0]); + snprintf(str, CFG_OPT_MAXSTR, "%d", curval); + break; + case CFGTYPE_STR: + str = &(g_cfg_opts_strs[0][0]); + //printf("curstr is: %s str is: %s\n", curstr,str); + snprintf(str, CFG_OPT_MAXSTR, "%s", curstr); + break; + case CFGTYPE_DISK: + str = &(g_cfg_opts_strs[0][0]), + cfg_get_disk_name(str, CFG_OPT_MAXSTR, type_ext, 1); + str = cfg_shorten_filename(str, 68); + break; + case CFGTYPE_FILE: + case CFGTYPE_DIR: + str = &(g_cfg_opts_strs[0][0]); + snprintf(str, CFG_OPT_MAXSTR, "%s", curstr); + str = cfg_shorten_filename(str, 68); + break; + default: + str = ""; + } + } #if 0 - if(menu_pos == highlight_pos) { - printf("menu_pos %d buf_pos %d, str is %s, %02x, %02x, " - "%02x %02x\n", - menu_pos, bufpos, str, g_cfg_opt_buf[bufpos-1], - g_cfg_opt_buf[bufpos-2], - g_cfg_opt_buf[bufpos-3], - g_cfg_opt_buf[bufpos-4]); - } + if(menu_pos == highlight_pos) { + printf("menu_pos %d buf_pos %d, str is %s, %02x, %02x, " + "%02x %02x\n", + menu_pos, bufpos, str, g_cfg_opt_buf[bufpos-1], + g_cfg_opt_buf[bufpos-2], + g_cfg_opt_buf[bufpos-3], + g_cfg_opt_buf[bufpos-4]); + } #endif - g_cfg_opt_buf[bufpos] = 0; - strncpy(&(g_cfg_opt_buf[bufpos]), str, CFG_OPT_MAXSTR - bufpos - 1); - g_cfg_opt_buf[CFG_OPT_MAXSTR-1] = 0; + g_cfg_opt_buf[bufpos] = 0; + strncpy(&(g_cfg_opt_buf[bufpos]), str, CFG_OPT_MAXSTR - bufpos - 1); + g_cfg_opt_buf[CFG_OPT_MAXSTR-1] = 0; } -void -cfg_get_base_path(char *pathptr, const char *inptr, int go_up) -{ - const char *tmpptr; - char *slashptr; - char *outptr; - int add_dotdot, is_dotdot; - int len; - int c; +void cfg_get_base_path(char *pathptr, const char *inptr, int go_up) { + const char *tmpptr; + char *slashptr; + char *outptr; + int add_dotdot, is_dotdot; + int len; + int c; - /* Take full filename, copy it to pathptr, and truncate at last slash */ - /* inptr and pathptr can be the same */ - /* if go_up is set, then replace a blank dir with ".." */ - /* but first, see if path is currently just ../ over and over */ - /* if so, just tack .. onto the end and return */ - //printf("cfg_get_base start with %s\n", inptr); + /* Take full filename, copy it to pathptr, and truncate at last slash */ + /* inptr and pathptr can be the same */ + /* if go_up is set, then replace a blank dir with ".." */ + /* but first, see if path is currently just ../ over and over */ + /* if so, just tack .. onto the end and return */ + //printf("cfg_get_base start with %s\n", inptr); - g_cfg_file_match[0] = 0; - tmpptr = inptr; - is_dotdot = 1; - while(1) { - if(tmpptr[0] == 0) { - break; - } - if(tmpptr[0] == '.' && tmpptr[1] == '.' && tmpptr[2] == '/') { - tmpptr += 3; - } else { - is_dotdot = 0; - break; - } - } - slashptr = 0; - outptr = pathptr; - c = -1; - while(c != 0) { - c = *inptr++; - if(c == '/') { - if(*inptr != 0) { /* if not a trailing slash... */ - slashptr = outptr; - } - } - *outptr++ = c; - } - if(!go_up) { - /* if not go_up, copy chopped part to g_cfg_file_match*/ - /* copy from slashptr+1 to end */ - tmpptr = slashptr+1; - if(slashptr == 0) { - tmpptr = pathptr; - } - strncpy(&g_cfg_file_match[0], tmpptr, CFG_PATH_MAX); - /* remove trailing / from g_cfg_file_match */ - len = strlen(&g_cfg_file_match[0]); - if((len > 1) && (len < (CFG_PATH_MAX - 1)) && - g_cfg_file_match[len - 1] == '/') { - g_cfg_file_match[len - 1] = 0; - } - //printf("set g_cfg_file_match to %s\n", &g_cfg_file_match[0]); - } - if(!is_dotdot && (slashptr != 0)) { - slashptr[0] = '/'; - slashptr[1] = 0; - outptr = slashptr + 2; - } - add_dotdot = 0; - if(pathptr[0] == 0 || is_dotdot) { - /* path was blank, or consisted of just ../ pattern */ - if(go_up) { - add_dotdot = 1; - } - } else if(slashptr == 0) { - /* no slashes found, but path was not blank--make it blank */ - if(pathptr[0] == '/') { - pathptr[1] = 0; - } else { - pathptr[0] = 0; - } - } + g_cfg_file_match[0] = 0; + tmpptr = inptr; + is_dotdot = 1; + while(1) { + if(tmpptr[0] == 0) { + break; + } + if(tmpptr[0] == '.' && tmpptr[1] == '.' && tmpptr[2] == '/') { + tmpptr += 3; + } else { + is_dotdot = 0; + break; + } + } + slashptr = 0; + outptr = pathptr; + c = -1; + while(c != 0) { + c = *inptr++; + if(c == '/') { + if(*inptr != 0) { /* if not a trailing slash... */ + slashptr = outptr; + } + } + *outptr++ = c; + } + if(!go_up) { + /* if not go_up, copy chopped part to g_cfg_file_match*/ + /* copy from slashptr+1 to end */ + tmpptr = slashptr+1; + if(slashptr == 0) { + tmpptr = pathptr; + } + strncpy(&g_cfg_file_match[0], tmpptr, CFG_PATH_MAX); + /* remove trailing / from g_cfg_file_match */ + len = strlen(&g_cfg_file_match[0]); + if((len > 1) && (len < (CFG_PATH_MAX - 1)) && + g_cfg_file_match[len - 1] == '/') { + g_cfg_file_match[len - 1] = 0; + } + //printf("set g_cfg_file_match to %s\n", &g_cfg_file_match[0]); + } + if(!is_dotdot && (slashptr != 0)) { + slashptr[0] = '/'; + slashptr[1] = 0; + outptr = slashptr + 2; + } + add_dotdot = 0; + if(pathptr[0] == 0 || is_dotdot) { + /* path was blank, or consisted of just ../ pattern */ + if(go_up) { + add_dotdot = 1; + } + } else if(slashptr == 0) { + /* no slashes found, but path was not blank--make it blank */ + if(pathptr[0] == '/') { + pathptr[1] = 0; + } else { + pathptr[0] = 0; + } + } - if(add_dotdot) { - --outptr; - outptr[0] = '.'; - outptr[1] = '.'; - outptr[2] = '/'; - outptr[3] = 0; - } + if(add_dotdot) { + --outptr; + outptr[0] = '.'; + outptr[1] = '.'; + outptr[2] = '/'; + outptr[3] = 0; + } - //printf("cfg_get_base end with %s, is_dotdot:%d, add_dotdot:%d\n", - // pathptr, is_dotdot, add_dotdot); + //printf("cfg_get_base end with %s, is_dotdot:%d, add_dotdot:%d\n", + // pathptr, is_dotdot, add_dotdot); } -void -cfg_file_init() -{ - int slot, drive; - int i; +void cfg_file_init() { + int slot, drive; + int i; - if(g_cfg_slotdrive < 0xfff) { - cfg_get_disk_name(&g_cfg_tmp_path[0], CFG_PATH_MAX, - g_cfg_slotdrive, 0); + if(g_cfg_slotdrive < 0xfff) { + cfg_get_disk_name(&g_cfg_tmp_path[0], CFG_PATH_MAX, + g_cfg_slotdrive, 0); - slot = g_cfg_slotdrive >> 8; - drive = g_cfg_slotdrive & 1; - for(i = 0; i < 6; i++) { - if(g_cfg_tmp_path[0] != 0) { - break; - } - /* try to get a starting path from some mounted drive */ - drive = !drive; - if(i & 1) { - slot++; - if(slot >= 8) { - slot = 5; - } - } - cfg_get_disk_name(&g_cfg_tmp_path[0], CFG_PATH_MAX, - (slot << 8) + drive, 0); - } - } else { - // Just use g_cfg_file_def_name - strncpy(&g_cfg_tmp_path[0], g_cfg_file_def_name, CFG_PATH_MAX); - } + slot = g_cfg_slotdrive >> 8; + drive = g_cfg_slotdrive & 1; + for(i = 0; i < 6; i++) { + if(g_cfg_tmp_path[0] != 0) { + break; + } + /* try to get a starting path from some mounted drive */ + drive = !drive; + if(i & 1) { + slot++; + if(slot >= 8) { + slot = 5; + } + } + cfg_get_disk_name(&g_cfg_tmp_path[0], CFG_PATH_MAX, + (slot << 8) + drive, 0); + } + } else { + // Just use g_cfg_file_def_name + strncpy(&g_cfg_tmp_path[0], g_cfg_file_def_name, CFG_PATH_MAX); + } - cfg_get_base_path(&g_cfg_file_curpath[0], &g_cfg_tmp_path[0], 0); - g_cfg_dirlist.invalid = 1; + cfg_get_base_path(&g_cfg_file_curpath[0], &g_cfg_tmp_path[0], 0); + g_cfg_dirlist.invalid = 1; } -void -cfg_free_alldirents(Cfg_listhdr *listhdrptr) -{ - int i; +void cfg_free_alldirents(Cfg_listhdr *listhdrptr) { + int i; - if(listhdrptr->max > 0) { - // Free the old directory listing - for(i = 0; i < listhdrptr->last; i++) { - free(listhdrptr->direntptr[i].name); - } - free(listhdrptr->direntptr); - } + if(listhdrptr->max > 0) { + // Free the old directory listing + for(i = 0; i < listhdrptr->last; i++) { + free(listhdrptr->direntptr[i].name); + } + free(listhdrptr->direntptr); + } - listhdrptr->direntptr = 0; - listhdrptr->last = 0; - listhdrptr->max = 0; - listhdrptr->invalid = 0; + listhdrptr->direntptr = 0; + listhdrptr->last = 0; + listhdrptr->max = 0; + listhdrptr->invalid = 0; - listhdrptr->topent = 0; - listhdrptr->curent = 0; + listhdrptr->topent = 0; + listhdrptr->curent = 0; } -void -cfg_file_add_dirent(Cfg_listhdr *listhdrptr, const char *nameptr, int is_dir, - int size, int image_start, int part_num) -{ - Cfg_dirent *direntptr; - char *ptr; - int inc_amt; - int namelen; +void cfg_file_add_dirent(Cfg_listhdr *listhdrptr, const char *nameptr, int is_dir, + int size, int image_start, int part_num) { + Cfg_dirent *direntptr; + char *ptr; + int inc_amt; + int namelen; - namelen = strlen(nameptr); - if(listhdrptr->last >= listhdrptr->max) { - // realloc - inc_amt = MAX(64, listhdrptr->max); - inc_amt = MIN(inc_amt, 1024); - listhdrptr->max += inc_amt; - listhdrptr->direntptr = (Cfg_dirent*)realloc(listhdrptr->direntptr, - listhdrptr->max * sizeof(Cfg_dirent)); - } - ptr = (char*)malloc(namelen+1+is_dir); // OG Added cast - strncpy(ptr, nameptr, namelen+1); - if(is_dir) { - strcat(ptr, "/"); - } + namelen = strlen(nameptr); + if(listhdrptr->last >= listhdrptr->max) { + // realloc + inc_amt = MAX(64, listhdrptr->max); + inc_amt = MIN(inc_amt, 1024); + listhdrptr->max += inc_amt; + listhdrptr->direntptr = (Cfg_dirent*)realloc(listhdrptr->direntptr, + listhdrptr->max * sizeof(Cfg_dirent)); + } + ptr = (char*)malloc(namelen+1+is_dir); // OG Added cast + strncpy(ptr, nameptr, namelen+1); + if(is_dir) { + strcat(ptr, "/"); + } #if 0 - printf("...file entry %d is %s\n", g_cfg_dirlist.last, ptr); + printf("...file entry %d is %s\n", g_cfg_dirlist.last, ptr); #endif - direntptr = &(listhdrptr->direntptr[listhdrptr->last]); - direntptr->name = ptr; - direntptr->is_dir = is_dir; - direntptr->size = size; - direntptr->image_start = image_start; - direntptr->part_num = part_num; - listhdrptr->last++; + direntptr = &(listhdrptr->direntptr[listhdrptr->last]); + direntptr->name = ptr; + direntptr->is_dir = is_dir; + direntptr->size = size; + direntptr->image_start = image_start; + direntptr->part_num = part_num; + listhdrptr->last++; } -int -cfg_dirent_sortfn(const void *obj1, const void *obj2) -{ - const Cfg_dirent *direntptr1, *direntptr2; - int ret; +int cfg_dirent_sortfn(const void *obj1, const void *obj2) { + const Cfg_dirent *direntptr1, *direntptr2; + int ret; - /* Called by qsort to sort directory listings */ - direntptr1 = (const Cfg_dirent *)obj1; - direntptr2 = (const Cfg_dirent *)obj2; + /* Called by qsort to sort directory listings */ + direntptr1 = (const Cfg_dirent *)obj1; + direntptr2 = (const Cfg_dirent *)obj2; #if defined(MAC) || defined(_WIN32) - // OG - ret = 0; + // OG + ret = 0; // ret = strcasecmp(direntptr1->name, direntptr2->name); #else - ret = strcmp(direntptr1->name, direntptr2->name); + ret = strcmp(direntptr1->name, direntptr2->name); #endif - return ret; + return ret; } -int -cfg_str_match(const char *str1, const char *str2, int len) -{ - const byte *bptr1, *bptr2; - int c, c2; - int i; +int cfg_str_match(const char *str1, const char *str2, int len) { + const byte *bptr1, *bptr2; + int c, c2; + int i; - /* basically, work like strcmp, except if str1 ends first, return 0 */ + /* basically, work like strcmp, except if str1 ends first, return 0 */ - bptr1 = (const byte *)str1; - bptr2 = (const byte *)str2; - for(i = 0; i < len; i++) { - c = *bptr1++; - c2 = *bptr2++; - if(c == 0) { - if(i > 0) { - return 0; - } else { - return c - c2; - } - } - if(c != c2) { - return c - c2; - } - } + bptr1 = (const byte *)str1; + bptr2 = (const byte *)str2; + for(i = 0; i < len; i++) { + c = *bptr1++; + c2 = *bptr2++; + if(c == 0) { + if(i > 0) { + return 0; + } else { + return c - c2; + } + } + if(c != c2) { + return c - c2; + } + } - return 0; + return 0; } -void -cfg_file_readdir(const char *pathptr) -{ +void cfg_file_readdir(const char *pathptr) { - struct dirent *direntptr; - struct stat stat_buf; - DIR *dirptr; - mode_t fmt; - char *str; - const char *tmppathptr; - int size; - int ret; - int is_dir, is_gz; - int len; - int i; + struct dirent *direntptr; + struct stat stat_buf; + DIR *dirptr; + mode_t fmt; + char *str; + const char *tmppathptr; + int size; + int ret; + int is_dir, is_gz; + int len; + int i; - if(!strncmp(pathptr, &g_cfg_file_cachedpath[0], CFG_PATH_MAX) && - (g_cfg_dirlist.last > 0) && (g_cfg_dirlist.invalid==0)){ - return; - } - // No match, must read new directory + if(!strncmp(pathptr, &g_cfg_file_cachedpath[0], CFG_PATH_MAX) && + (g_cfg_dirlist.last > 0) && (g_cfg_dirlist.invalid==0)) { + return; + } + // No match, must read new directory - // Free all dirents that were cached previously - cfg_free_alldirents(&g_cfg_dirlist); + // Free all dirents that were cached previously + cfg_free_alldirents(&g_cfg_dirlist); - strncpy(&g_cfg_file_cachedpath[0], pathptr, CFG_PATH_MAX); - strncpy(&g_cfg_file_cachedreal[0], pathptr, CFG_PATH_MAX); + strncpy(&g_cfg_file_cachedpath[0], pathptr, CFG_PATH_MAX); + strncpy(&g_cfg_file_cachedreal[0], pathptr, CFG_PATH_MAX); - str = &g_cfg_file_cachedreal[0]; + str = &g_cfg_file_cachedreal[0]; - for(i = 0; i < 200; i++) { - len = strlen(str); - if(len <= 0) { - break; - } else if(len < CFG_PATH_MAX-2) { - if(str[len-1] != '/') { - // append / to make various routines work - str[len] = '/'; - str[len+1] = 0; - } - } - ret = cfg_stat(str, &stat_buf); - is_dir = 0; - if(ret == 0) { - fmt = stat_buf.st_mode & S_IFMT; - if(fmt == S_IFDIR) { - /* it's a directory */ - is_dir = 1; - } - } - if(is_dir) { - break; - } else { - // user is entering more path, use base for display - cfg_get_base_path(str, str, 0); - } - } - tmppathptr = str; - if(str[0] == 0) { - tmppathptr = "."; - } - cfg_file_add_dirent(&g_cfg_dirlist, "..", 1, 0, -1, -1); - dirptr = opendir(tmppathptr); - if(dirptr == 0) { - printf("Could not open %s as a directory\n", tmppathptr); - return; - } - while(1) { - direntptr = readdir(dirptr); - if(direntptr == 0) { - break; - } - if(!strcmp(".", direntptr->d_name)) { - continue; - } - if(!strcmp("..", direntptr->d_name)) { - continue; - } - /* Else, see if it is a directory or a file */ - snprintf(&g_cfg_tmp_path[0], CFG_PATH_MAX, "%s%s", - &g_cfg_file_cachedreal[0], direntptr->d_name); - ret = cfg_stat(&g_cfg_tmp_path[0], &stat_buf); - len = strlen(g_cfg_tmp_path); - is_dir = 0; - is_gz = 0; - if((len > 3) && (strcmp(&g_cfg_tmp_path[len - 3], ".gz") == 0)){ - is_gz = 1; - } - if(ret != 0) { - printf("stat %s ret %d, errno:%d\n", &g_cfg_tmp_path[0], - ret, errno); - stat_buf.st_size = 0; - continue; /* skip it */ - } else { - fmt = stat_buf.st_mode & S_IFMT; - size = stat_buf.st_size; - if(fmt == S_IFDIR) { - /* it's a directory */ - is_dir = 1; - } else if((fmt == S_IFREG) && (is_gz == 0)) { - if(g_cfg_slotdrive < 0xfff) { - if(size < 140*1024) { - continue; /* skip it */ - } - } else { - /* see if there are size limits */ - if((size < g_cfg_file_min_size) || - (size > g_cfg_file_max_size)) { - continue; /* skip it */ - } - } - } - } - if (g_cfg_file_dir_only && !is_dir) continue; + for(i = 0; i < 200; i++) { + len = strlen(str); + if(len <= 0) { + break; + } else if(len < CFG_PATH_MAX-2) { + if(str[len-1] != '/') { + // append / to make various routines work + str[len] = '/'; + str[len+1] = 0; + } + } + ret = cfg_stat(str, &stat_buf); + is_dir = 0; + if(ret == 0) { + fmt = stat_buf.st_mode & S_IFMT; + if(fmt == S_IFDIR) { + /* it's a directory */ + is_dir = 1; + } + } + if(is_dir) { + break; + } else { + // user is entering more path, use base for display + cfg_get_base_path(str, str, 0); + } + } + tmppathptr = str; + if(str[0] == 0) { + tmppathptr = "."; + } + cfg_file_add_dirent(&g_cfg_dirlist, "..", 1, 0, -1, -1); + dirptr = opendir(tmppathptr); + if(dirptr == 0) { + printf("Could not open %s as a directory\n", tmppathptr); + return; + } + while(1) { + direntptr = readdir(dirptr); + if(direntptr == 0) { + break; + } + if(!strcmp(".", direntptr->d_name)) { + continue; + } + if(!strcmp("..", direntptr->d_name)) { + continue; + } + /* Else, see if it is a directory or a file */ + snprintf(&g_cfg_tmp_path[0], CFG_PATH_MAX, "%s%s", + &g_cfg_file_cachedreal[0], direntptr->d_name); + ret = cfg_stat(&g_cfg_tmp_path[0], &stat_buf); + len = strlen(g_cfg_tmp_path); + is_dir = 0; + is_gz = 0; + if((len > 3) && (strcmp(&g_cfg_tmp_path[len - 3], ".gz") == 0)) { + is_gz = 1; + } + if(ret != 0) { + printf("stat %s ret %d, errno:%d\n", &g_cfg_tmp_path[0], + ret, errno); + stat_buf.st_size = 0; + continue; /* skip it */ + } else { + fmt = stat_buf.st_mode & S_IFMT; + size = stat_buf.st_size; + if(fmt == S_IFDIR) { + /* it's a directory */ + is_dir = 1; + } else if((fmt == S_IFREG) && (is_gz == 0)) { + if(g_cfg_slotdrive < 0xfff) { + if(size < 140*1024) { + continue; /* skip it */ + } + } else { + /* see if there are size limits */ + if((size < g_cfg_file_min_size) || + (size > g_cfg_file_max_size)) { + continue; /* skip it */ + } + } + } + } + if (g_cfg_file_dir_only && !is_dir) continue; - cfg_file_add_dirent(&g_cfg_dirlist, direntptr->d_name, is_dir, - stat_buf.st_size, -1, -1); - } - /* then sort the results (Mac's HFS+ is sorted, but other FS won't be)*/ - qsort(&(g_cfg_dirlist.direntptr[0]), g_cfg_dirlist.last, - sizeof(Cfg_dirent), cfg_dirent_sortfn); - g_cfg_dirlist.curent = g_cfg_dirlist.last - 1; - for(i = g_cfg_dirlist.last - 1; i >= 0; i--) { - ret = cfg_str_match(&g_cfg_file_match[0], - g_cfg_dirlist.direntptr[i].name, CFG_PATH_MAX); - if(ret <= 0) { - /* set cur ent to closest filename to the match name */ - g_cfg_dirlist.curent = i; - } - } + cfg_file_add_dirent(&g_cfg_dirlist, direntptr->d_name, is_dir, + stat_buf.st_size, -1, -1); + } + /* then sort the results (Mac's HFS+ is sorted, but other FS won't be)*/ + qsort(&(g_cfg_dirlist.direntptr[0]), g_cfg_dirlist.last, + sizeof(Cfg_dirent), cfg_dirent_sortfn); + g_cfg_dirlist.curent = g_cfg_dirlist.last - 1; + for(i = g_cfg_dirlist.last - 1; i >= 0; i--) { + ret = cfg_str_match(&g_cfg_file_match[0], + g_cfg_dirlist.direntptr[i].name, CFG_PATH_MAX); + if(ret <= 0) { + /* set cur ent to closest filename to the match name */ + g_cfg_dirlist.curent = i; + } + } } -void -cfg_inspect_maybe_insert_file(char *filename, int should_boot) -{ +void cfg_inspect_maybe_insert_file(char *filename, int should_boot) { /* -Take a look at a file. Based on its size, guess a slot/drive to insert it into. -Used for blind operations like dragging/dropping files. -Optionally boot from that slot. -*/ - int rc = 0; - int slot = 0; - rc = cfg_guess_image_size(filename); - switch (rc) - { - case 0: slot = 7; break; - case 1: slot = 6; break; - case 2: slot = 5; break; - case 3: slot = 7; break; - default: break; - } - if (slot > 0) - { - insert_disk(slot,0,filename,0,0,0,-1); - glogf("Inserted disk in slot %d, drive 1. Filename: %s", slot, filename); - if (should_boot) { - g_temp_boot_slot = slot; - glog("That slot has been set to boot."); - } - } - else - glogf("Unable to determine appropriate place to insert file %s.",filename); + Take a look at a file. Based on its size, guess a slot/drive to insert it into. + Used for blind operations like dragging/dropping files. + Optionally boot from that slot. + */ + int rc = 0; + int slot = 0; + rc = cfg_guess_image_size(filename); + switch (rc) + { + case 0: slot = 7; break; + case 1: slot = 6; break; + case 2: slot = 5; break; + case 3: slot = 7; break; + default: break; + } + if (slot > 0) + { + insert_disk(slot,0,filename,0,0,0,-1); + glogf("Inserted disk in slot %d, drive 1. Filename: %s", slot, filename); + if (should_boot) { + g_temp_boot_slot = slot; + glog("That slot has been set to boot."); + } + } + else + glogf("Unable to determine appropriate place to insert file %s.",filename); } -int -cfg_guess_image_size(char *filename) -{ +int cfg_guess_image_size(char *filename) { /* -Guess the image size. Return values: - -1 : invalid/unknown. Can't guess. - 0 : Less than 140k; might be ram disk image. - 1 : 140k, 5.25" image. - 2 : 800k, 3.5" image. - 3 : Something bigger. -*/ - struct stat stat_buf; - int rc = -1; - int len; - rc = stat(filename, &stat_buf); - if(rc < 0) - { - glogf("Can't get statistics on file %s; errno: %d", filename, errno); - rc = -1; - } else { - len = stat_buf.st_size; - if (len < 140 * 1024) { - /* Not enough for a 140k image */ - glogf("Found file %s, size %d; guessing small ProDOS image.", filename, len); - rc = 0; - } else if (len < 140 * 1024 + 256 + 1) { - /* Reasonable size for 140k image, maybe in 2mg format */ - glogf("Found file %s, size %d; guessing a 5-1/4\" image.", filename, len); - rc = 1; - } else if (len < 800 * 1024 + 256 + 1) { - /* Reasonable size for 800k image, maybe in 2mg format */ - glogf("Found file %s, size %d; guessing a 3-1/2\" image.", filename, len); - rc = 2; - } else { - /* Let's pretend it's an HDV image */ - glogf("Found file %s, size %d; guessing a hard drive image.", filename, len); - rc = 3; - } - } - return rc; + Guess the image size. Return values: + -1 : invalid/unknown. Can't guess. + 0 : Less than 140k; might be ram disk image. + 1 : 140k, 5.25" image. + 2 : 800k, 3.5" image. + 3 : Something bigger. + */ + struct stat stat_buf; + int rc = -1; + int len; + rc = stat(filename, &stat_buf); + if(rc < 0) + { + glogf("Can't get statistics on file %s; errno: %d", filename, errno); + rc = -1; + } else { + len = stat_buf.st_size; + if (len < 140 * 1024) { + /* Not enough for a 140k image */ + glogf("Found file %s, size %d; guessing small ProDOS image.", filename, len); + rc = 0; + } else if (len < 140 * 1024 + 256 + 1) { + /* Reasonable size for 140k image, maybe in 2mg format */ + glogf("Found file %s, size %d; guessing a 5-1/4\" image.", filename, len); + rc = 1; + } else if (len < 800 * 1024 + 256 + 1) { + /* Reasonable size for 800k image, maybe in 2mg format */ + glogf("Found file %s, size %d; guessing a 3-1/2\" image.", filename, len); + rc = 2; + } else { + /* Let's pretend it's an HDV image */ + glogf("Found file %s, size %d; guessing a hard drive image.", filename, len); + rc = 3; + } + } + return rc; } -char * -cfg_shorten_filename(const char *in_ptr, int maxlen) -{ - char *out_ptr; - int len; - int c; - int i; - /* Warning: uses a static string, not reentrant! */ - out_ptr = &(g_cfg_file_shortened[0]); - len = strlen(in_ptr); - maxlen = MIN(len, maxlen); - for(i = 0; i < maxlen; i++) { - c = in_ptr[i] & 0x7f; - if(c < 0x20) { - c = '*'; - } - out_ptr[i] = c; - } - out_ptr[maxlen] = 0; - if(len > maxlen) { - for(i = 0; i < (maxlen/2); i++) { - c = in_ptr[len-i-1] & 0x7f; - if(c < 0x20) { - c = '*'; - } - out_ptr[maxlen-i-1] = c; - } - out_ptr[(maxlen/2) - 1] = '.'; - out_ptr[maxlen/2] = '.'; - out_ptr[(maxlen/2) + 1] = '.'; - } - return out_ptr; +char *cfg_shorten_filename(const char *in_ptr, int maxlen) { + char *out_ptr; + int len; + int c; + int i; + /* Warning: uses a static string, not reentrant! */ + out_ptr = &(g_cfg_file_shortened[0]); + len = strlen(in_ptr); + maxlen = MIN(len, maxlen); + for(i = 0; i < maxlen; i++) { + c = in_ptr[i] & 0x7f; + if(c < 0x20) { + c = '*'; + } + out_ptr[i] = c; + } + out_ptr[maxlen] = 0; + if(len > maxlen) { + for(i = 0; i < (maxlen/2); i++) { + c = in_ptr[len-i-1] & 0x7f; + if(c < 0x20) { + c = '*'; + } + out_ptr[maxlen-i-1] = c; + } + out_ptr[(maxlen/2) - 1] = '.'; + out_ptr[maxlen/2] = '.'; + out_ptr[(maxlen/2) + 1] = '.'; + } + return out_ptr; } -void -cfg_fix_topent(Cfg_listhdr *listhdrptr) -{ - int num_to_show; - num_to_show = listhdrptr->num_to_show; - /* Force curent and topent to make sense */ - if(listhdrptr->curent >= listhdrptr->last) { - listhdrptr->curent = listhdrptr->last - 1; - } - if(listhdrptr->curent < 0) { - listhdrptr->curent = 0; - } - if(abs(listhdrptr->curent - listhdrptr->topent) >= num_to_show) { - listhdrptr->topent = listhdrptr->curent - (num_to_show/2); - } - if(listhdrptr->topent > listhdrptr->curent) { - listhdrptr->topent = listhdrptr->curent - (num_to_show/2); - } - if(listhdrptr->topent < 0) { - listhdrptr->topent = 0; - } +void cfg_fix_topent(Cfg_listhdr *listhdrptr) { + int num_to_show; + num_to_show = listhdrptr->num_to_show; + /* Force curent and topent to make sense */ + if(listhdrptr->curent >= listhdrptr->last) { + listhdrptr->curent = listhdrptr->last - 1; + } + if(listhdrptr->curent < 0) { + listhdrptr->curent = 0; + } + if(abs(listhdrptr->curent - listhdrptr->topent) >= num_to_show) { + listhdrptr->topent = listhdrptr->curent - (num_to_show/2); + } + if(listhdrptr->topent > listhdrptr->curent) { + listhdrptr->topent = listhdrptr->curent - (num_to_show/2); + } + if(listhdrptr->topent < 0) { + listhdrptr->topent = 0; + } } -void -cfg_file_draw() -{ - Cfg_listhdr *listhdrptr; - Cfg_dirent *direntptr; - char *str, *fmt; - int num_to_show; - int yoffset; - int x, y; - int i; - cfg_file_readdir(&g_cfg_file_curpath[0]); - for(y = 0; y < 21; y++) { - cfg_htab_vtab(0, y); - cfg_printf("\tZ\t"); - for(x = 1; x < 79; x++) { - cfg_htab_vtab(x, y); - cfg_putchar(' '); - } - cfg_htab_vtab(79, y); - cfg_printf("\t_\t"); - } - cfg_htab_vtab(1, 0); - cfg_putchar('\b'); - for(x = 1; x < 79; x++) { - cfg_putchar(' '); - } - if(g_cfg_slotdrive < 0xfff) { - cfg_htab_vtab(30, 0); - cfg_printf("\bSelect image for s%dd%d\b", - (g_cfg_slotdrive >> 8), (g_cfg_slotdrive & 0xff) + 1); - } else { - cfg_htab_vtab(5, 0); - cfg_printf("\bSelect file to use as %-40s\b", - cfg_shorten_filename(g_cfg_file_def_name, 40)); - } - cfg_htab_vtab(2, 1); - cfg_printf("Configuration file path: %-40s", - cfg_shorten_filename(&g_config_gsplus_name[0], 40)); - cfg_htab_vtab(2, 2); - cfg_printf("Current directory: %-50s", - cfg_shorten_filename(&g_cfg_cwd_str[0], 50)); - cfg_htab_vtab(2, 3); - str = ""; - if(g_cfg_file_pathfield) { - str = "\b \b"; - } - cfg_printf("Path: %s%s", - cfg_shorten_filename(&g_cfg_file_curpath[0], 68), str); - cfg_htab_vtab(0, 4); - cfg_printf(" \t"); - for(x = 1; x < 79; x++) { - cfg_putchar('\\'); - } - cfg_printf("\t "); +void cfg_file_draw() { + Cfg_listhdr *listhdrptr; + Cfg_dirent *direntptr; + char *str, *fmt; + int num_to_show; + int yoffset; + int x, y; + int i; + cfg_file_readdir(&g_cfg_file_curpath[0]); + for(y = 0; y < 21; y++) { + cfg_htab_vtab(0, y); + cfg_printf("\tZ\t"); + for(x = 1; x < 79; x++) { + cfg_htab_vtab(x, y); + cfg_putchar(' '); + } + cfg_htab_vtab(79, y); + cfg_printf("\t_\t"); + } + cfg_htab_vtab(1, 0); + cfg_putchar('\b'); + for(x = 1; x < 79; x++) { + cfg_putchar(' '); + } + if(g_cfg_slotdrive < 0xfff) { + cfg_htab_vtab(30, 0); + cfg_printf("\bSelect image for s%dd%d\b", + (g_cfg_slotdrive >> 8), (g_cfg_slotdrive & 0xff) + 1); + } else { + cfg_htab_vtab(5, 0); + cfg_printf("\bSelect file to use as %-40s\b", + cfg_shorten_filename(g_cfg_file_def_name, 40)); + } + cfg_htab_vtab(2, 1); + cfg_printf("Configuration file path: %-40s", + cfg_shorten_filename(&g_config_gsplus_name[0], 40)); + cfg_htab_vtab(2, 2); + cfg_printf("Current directory: %-50s", + cfg_shorten_filename(&g_cfg_cwd_str[0], 50)); + cfg_htab_vtab(2, 3); + str = ""; + if(g_cfg_file_pathfield) { + str = "\b \b"; + } + cfg_printf("Path: %s%s", + cfg_shorten_filename(&g_cfg_file_curpath[0], 68), str); + cfg_htab_vtab(0, 4); + cfg_printf(" \t"); + for(x = 1; x < 79; x++) { + cfg_putchar('\\'); + } + cfg_printf("\t "); - /* Force curent and topent to make sense */ - listhdrptr = &g_cfg_dirlist; - num_to_show = CFG_NUM_SHOWENTS; - yoffset = 5; - if(g_cfg_select_partition > 0) { - listhdrptr = &g_cfg_partitionlist; - num_to_show -= 2; - cfg_htab_vtab(2, yoffset); - cfg_printf("Select partition of %-50s\n", - cfg_shorten_filename(&g_cfg_file_path[0], 50), str); - yoffset += 2; - } - listhdrptr->num_to_show = num_to_show; - cfg_fix_topent(listhdrptr); - for(i = 0; i < num_to_show; i++) { - y = i + yoffset; - if(listhdrptr->last > (i + listhdrptr->topent)) { - direntptr = &(listhdrptr-> - direntptr[i + listhdrptr->topent]); - cfg_htab_vtab(2, y); - if(direntptr->is_dir) { - cfg_printf("\tXY\t "); - } else { - cfg_printf(" "); - } - if(direntptr->part_num >= 0) { - cfg_printf("%3d: ", direntptr->part_num); - } - str = cfg_shorten_filename(direntptr->name, 45); - fmt = "%-45s"; - if(i + listhdrptr->topent == listhdrptr->curent) { - if(g_cfg_file_pathfield == 0) { - fmt = "\b%-45s\b"; - } else { - fmt = "%-44s\b \b"; - } - } - cfg_printf(fmt, str); - if(!direntptr->is_dir) { - cfg_print_num(direntptr->size, 13); - } - } - } - cfg_htab_vtab(1, 5 + CFG_NUM_SHOWENTS); - cfg_putchar('\t'); - for(x = 1; x < 79; x++) { - cfg_putchar('L'); - } - cfg_putchar('\t'); + /* Force curent and topent to make sense */ + listhdrptr = &g_cfg_dirlist; + num_to_show = CFG_NUM_SHOWENTS; + yoffset = 5; + if(g_cfg_select_partition > 0) { + listhdrptr = &g_cfg_partitionlist; + num_to_show -= 2; + cfg_htab_vtab(2, yoffset); + cfg_printf("Select partition of %-50s\n", + cfg_shorten_filename(&g_cfg_file_path[0], 50), str); + yoffset += 2; + } + listhdrptr->num_to_show = num_to_show; + cfg_fix_topent(listhdrptr); + for(i = 0; i < num_to_show; i++) { + y = i + yoffset; + if(listhdrptr->last > (i + listhdrptr->topent)) { + direntptr = &(listhdrptr-> + direntptr[i + listhdrptr->topent]); + cfg_htab_vtab(2, y); + if(direntptr->is_dir) { + cfg_printf("\tXY\t "); + } else { + cfg_printf(" "); + } + if(direntptr->part_num >= 0) { + cfg_printf("%3d: ", direntptr->part_num); + } + str = cfg_shorten_filename(direntptr->name, 45); + fmt = "%-45s"; + if(i + listhdrptr->topent == listhdrptr->curent) { + if(g_cfg_file_pathfield == 0) { + fmt = "\b%-45s\b"; + } else { + fmt = "%-44s\b \b"; + } + } + cfg_printf(fmt, str); + if(!direntptr->is_dir) { + cfg_print_num(direntptr->size, 13); + } + } + } + cfg_htab_vtab(1, 5 + CFG_NUM_SHOWENTS); + cfg_putchar('\t'); + for(x = 1; x < 79; x++) { + cfg_putchar('L'); + } + cfg_putchar('\t'); } -void -cfg_partition_selected() -{ - char *str; - const char *part_str; - char *part_str2; - int pos; - int part_num; - pos = g_cfg_partitionlist.curent; - str = g_cfg_partitionlist.direntptr[pos].name; - part_num = -2; - part_str = 0; - if(str[0] == 0 || (str[0] >= '0' && str[0] <= '9')) { - part_num = g_cfg_partitionlist.direntptr[pos].part_num; - } else { - part_str = str; - } - part_str2 = 0; - if(part_str != 0) { - part_str2 = (char *)malloc(strlen(part_str)+1); - strcpy(part_str2, part_str); - } - insert_disk(g_cfg_slotdrive >> 8, g_cfg_slotdrive & 0xff, - &(g_cfg_file_path[0]), 0, 0, part_str2, part_num); - if(part_str2 != 0) { - free(part_str2); - } - g_cfg_slotdrive = -1; - g_cfg_select_partition = -1; +void cfg_partition_selected() { + char *str; + const char *part_str; + char *part_str2; + int pos; + int part_num; + pos = g_cfg_partitionlist.curent; + str = g_cfg_partitionlist.direntptr[pos].name; + part_num = -2; + part_str = 0; + if(str[0] == 0 || (str[0] >= '0' && str[0] <= '9')) { + part_num = g_cfg_partitionlist.direntptr[pos].part_num; + } else { + part_str = str; + } + part_str2 = 0; + if(part_str != 0) { + part_str2 = (char *)malloc(strlen(part_str)+1); + strcpy(part_str2, part_str); + } + insert_disk(g_cfg_slotdrive >> 8, g_cfg_slotdrive & 0xff, + &(g_cfg_file_path[0]), 0, 0, part_str2, part_num); + if(part_str2 != 0) { + free(part_str2); + } + g_cfg_slotdrive = -1; + g_cfg_select_partition = -1; } -void -cfg_file_update_ptr(char *str) -{ - char *newstr; - int len; - len = strlen(str) + 1; - newstr = (char*)malloc(len); - memcpy(newstr, str, len); - if(g_cfg_file_strptr) { - if(*g_cfg_file_strptr) { - free(*g_cfg_file_strptr); - } - } - *g_cfg_file_strptr = newstr; - if(g_cfg_file_strptr == &(g_cfg_rom_path)) { - glog("Updated ROM file"); - load_roms_init_memory(); - } - g_config_gsplus_update_needed = 1; +void cfg_file_update_ptr(char *str) { + char *newstr; + int len; + len = strlen(str) + 1; + newstr = (char*)malloc(len); + memcpy(newstr, str, len); + if(g_cfg_file_strptr) { + if(*g_cfg_file_strptr) { + free(*g_cfg_file_strptr); + } + } + *g_cfg_file_strptr = newstr; + if(g_cfg_file_strptr == &(g_cfg_rom_path)) { + glog("Updated ROM file"); + load_roms_init_memory(); + } + g_config_gsplus_update_needed = 1; } -void -cfg_file_selected(int select_dir) -{ - struct stat stat_buf; - char *str; - int fmt; - int ret; - if(g_cfg_select_partition > 0) { - cfg_partition_selected(); - return; - } - if(g_cfg_file_pathfield == 0) { - // in file section area of window - str = g_cfg_dirlist.direntptr[g_cfg_dirlist.curent].name; - if(!strcmp(str, "../")) { - /* go up one directory */ - cfg_get_base_path(&g_cfg_file_curpath[0], - &g_cfg_file_curpath[0], 1); - return; - } - snprintf(&g_cfg_file_path[0], CFG_PATH_MAX, "%s%s", - &g_cfg_file_cachedreal[0], str); - } else { - // just use cfg_file_curpath directly - strncpy(&g_cfg_file_path[0], &g_cfg_file_curpath[0], - CFG_PATH_MAX); - } - ret = cfg_stat(&g_cfg_file_path[0], &stat_buf); - fmt = stat_buf.st_mode & S_IFMT; - #if 0 - cfg_printf("Stat'ing %s, st_mode is: %08x\n", &g_cfg_file_path[0], - (int)stat_buf.st_mode); - #endif - if(ret != 0) { - glogf("stat %s returned %d, errno: %d", &g_cfg_file_path[0], ret, errno); - } else { - if(fmt == S_IFDIR && !select_dir) { - /* it's a directory */ - strncpy(&g_cfg_file_curpath[0], &g_cfg_file_path[0], - CFG_PATH_MAX); - } else { - /* select it */ - if(g_cfg_slotdrive < 0xfff) { - ret = cfg_maybe_insert_disk(g_cfg_slotdrive>>8, - g_cfg_slotdrive & 0xff, - &g_cfg_file_path[0]); - if(ret > 0) { - g_cfg_slotdrive = -1; - } - } else { - cfg_file_update_ptr(&g_cfg_file_path[0]); - g_cfg_slotdrive = -1; - } - } - } +void cfg_file_selected(int select_dir) { + struct stat stat_buf; + char *str; + int fmt; + int ret; + if(g_cfg_select_partition > 0) { + cfg_partition_selected(); + return; + } + if(g_cfg_file_pathfield == 0) { + // in file section area of window + str = g_cfg_dirlist.direntptr[g_cfg_dirlist.curent].name; + if(!strcmp(str, "../")) { + /* go up one directory */ + cfg_get_base_path(&g_cfg_file_curpath[0], + &g_cfg_file_curpath[0], 1); + return; + } + snprintf(&g_cfg_file_path[0], CFG_PATH_MAX, "%s%s", + &g_cfg_file_cachedreal[0], str); + } else { + // just use cfg_file_curpath directly + strncpy(&g_cfg_file_path[0], &g_cfg_file_curpath[0], + CFG_PATH_MAX); + } + ret = cfg_stat(&g_cfg_file_path[0], &stat_buf); + fmt = stat_buf.st_mode & S_IFMT; + #if 0 + cfg_printf("Stat'ing %s, st_mode is: %08x\n", &g_cfg_file_path[0], + (int)stat_buf.st_mode); + #endif + if(ret != 0) { + glogf("stat %s returned %d, errno: %d", &g_cfg_file_path[0], ret, errno); + } else { + if(fmt == S_IFDIR && !select_dir) { + /* it's a directory */ + strncpy(&g_cfg_file_curpath[0], &g_cfg_file_path[0], + CFG_PATH_MAX); + } else { + /* select it */ + if(g_cfg_slotdrive < 0xfff) { + ret = cfg_maybe_insert_disk(g_cfg_slotdrive>>8, + g_cfg_slotdrive & 0xff, + &g_cfg_file_path[0]); + if(ret > 0) { + g_cfg_slotdrive = -1; + } + } else { + cfg_file_update_ptr(&g_cfg_file_path[0]); + g_cfg_slotdrive = -1; + } + } + } } -void -cfg_file_handle_key(int key) -{ - Cfg_listhdr *listhdrptr; - int len; - if(g_cfg_file_pathfield) { - if(key >= 0x20 && key < 0x7f) { - len = strlen(&g_cfg_file_curpath[0]); - if(len < CFG_PATH_MAX-4) { - g_cfg_file_curpath[len] = key; - g_cfg_file_curpath[len+1] = 0; - } - return; - } - } - listhdrptr = &g_cfg_dirlist; - if(g_cfg_select_partition > 0) { - listhdrptr = &g_cfg_partitionlist; - } - if( (g_cfg_file_pathfield == 0) && - ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z')) ) { - /* jump to file starting with this letter */ - g_cfg_file_match[0] = key; - g_cfg_file_match[1] = 0; - g_cfg_dirlist.invalid = 1; /* re-read directory */ - } - switch(key) { - case 0x1b: - if(g_cfg_slotdrive < 0xfff) { - eject_disk_by_num(g_cfg_slotdrive >> 8, - g_cfg_slotdrive & 0xff); - } - g_cfg_slotdrive = -1; - g_cfg_select_partition = -1; - g_cfg_dirlist.invalid = 1; - break; - case 0x0a: /* down arrow */ - if(g_cfg_file_pathfield == 0) { - listhdrptr->curent++; - cfg_fix_topent(listhdrptr); - } - break; - case 0x0b: /* up arrow */ - if(g_cfg_file_pathfield == 0) { - listhdrptr->curent--; - cfg_fix_topent(listhdrptr); - } - break; - case 0x33: /* pg dn */ - if(g_cfg_file_pathfield == 0) { - listhdrptr->curent += CFG_PG_SCROLL_AMT; - cfg_fix_topent(listhdrptr); - } - break; - case 0x39: /* pg up */ - if(g_cfg_file_pathfield == 0) { - listhdrptr->curent -= CFG_PG_SCROLL_AMT; - cfg_fix_topent(listhdrptr); - } - break; - case 0x0d: /* return */ - cfg_file_selected(0); - break; - case 0x09: /* tab */ - g_cfg_file_pathfield = !g_cfg_file_pathfield; - break; - case 0x08: /* left arrow */ - case 0x7f: /* delete key */ - if(g_cfg_file_pathfield) { - len = strlen(&g_cfg_file_curpath[0]) - 1; - if(len >= 0) { - g_cfg_file_curpath[len] = 0; - } - } - break; - case 0x20: /* space -- selects file/directory */ - cfg_file_selected(g_cfg_file_dir_only); - break; - default: - glogf("Unhandled config key: 0x%02x", key); - } +void cfg_file_handle_key(int key) { + Cfg_listhdr *listhdrptr; + int len; + if(g_cfg_file_pathfield) { + if(key >= 0x20 && key < 0x7f) { + len = strlen(&g_cfg_file_curpath[0]); + if(len < CFG_PATH_MAX-4) { + g_cfg_file_curpath[len] = key; + g_cfg_file_curpath[len+1] = 0; + } + return; + } + } + listhdrptr = &g_cfg_dirlist; + if(g_cfg_select_partition > 0) { + listhdrptr = &g_cfg_partitionlist; + } + if( (g_cfg_file_pathfield == 0) && + ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z')) ) { + /* jump to file starting with this letter */ + g_cfg_file_match[0] = key; + g_cfg_file_match[1] = 0; + g_cfg_dirlist.invalid = 1; /* re-read directory */ + } + switch(key) { + case 0x1b: + if(g_cfg_slotdrive < 0xfff) { + eject_disk_by_num(g_cfg_slotdrive >> 8, + g_cfg_slotdrive & 0xff); + } + g_cfg_slotdrive = -1; + g_cfg_select_partition = -1; + g_cfg_dirlist.invalid = 1; + break; + case 0x0a: /* down arrow */ + if(g_cfg_file_pathfield == 0) { + listhdrptr->curent++; + cfg_fix_topent(listhdrptr); + } + break; + case 0x0b: /* up arrow */ + if(g_cfg_file_pathfield == 0) { + listhdrptr->curent--; + cfg_fix_topent(listhdrptr); + } + break; + case 0x33: /* pg dn */ + if(g_cfg_file_pathfield == 0) { + listhdrptr->curent += CFG_PG_SCROLL_AMT; + cfg_fix_topent(listhdrptr); + } + break; + case 0x39: /* pg up */ + if(g_cfg_file_pathfield == 0) { + listhdrptr->curent -= CFG_PG_SCROLL_AMT; + cfg_fix_topent(listhdrptr); + } + break; + case 0x0d: /* return */ + cfg_file_selected(0); + break; + case 0x09: /* tab */ + g_cfg_file_pathfield = !g_cfg_file_pathfield; + break; + case 0x08: /* left arrow */ + case 0x7f: /* delete key */ + if(g_cfg_file_pathfield) { + len = strlen(&g_cfg_file_curpath[0]) - 1; + if(len >= 0) { + g_cfg_file_curpath[len] = 0; + } + } + break; + case 0x20: /* space -- selects file/directory */ + cfg_file_selected(g_cfg_file_dir_only); + break; + default: + glogf("Unhandled config key: 0x%02x", key); + } } -void -config_control_panel() -{ - void (*fn_ptr)(); - const char *str; - Cfg_menu *menuptr; - void *ptr; - int print_eject_help; - int line; - int type; - int match_found; - int menu_line; - int menu_inc; - int max_line; - int key; - int i, j; - // First, save important text screen state - g_save_cur_a2_stat = g_cur_a2_stat; - for(i = 0; i < 0x400; i++) { - g_save_text_screen_bytes[i] = g_slow_memory_ptr[0x400+i]; - g_save_text_screen_bytes[0x400+i] =g_slow_memory_ptr[0x10400+i]; - } - g_cur_a2_stat = ALL_STAT_TEXT | ALL_STAT_VID80 | ALL_STAT_ANNUNC3 | - (0xf << BIT_ALL_STAT_TEXT_COLOR) | ALL_STAT_ALTCHARSET; - g_a2_new_all_stat[0] = g_cur_a2_stat; - g_new_a2_stat_cur_line = 0; - for(i = 0; i < 20; i++) { - // Toss any queued-up keypresses - if(adb_read_c000() & 0x80) { - (void)adb_access_c010(); - } - } - g_adb_repeat_vbl = 0; - g_cfg_vbl_count = 0; - // HACK: Force adb keyboard (and probably mouse) to "normal"... - g_full_refresh_needed = -1; - g_a2_screen_buffer_changed = -1; - cfg_home(); - j = 0; - menuptr = g_cfg_main_menu; - if(g_rom_version < 0) { - /* Must select ROM file */ - menuptr = g_cfg_rom_menu; - } - menu_line = 1; - menu_inc = 1; - g_cfg_slotdrive = -1; - g_cfg_select_partition = -1; - while(g_config_control_panel & !(halt_sim&HALT_WANTTOQUIT)) { - if(g_fatal_log > 0) { - x_show_alert(0, 0); - } - cfg_home(); - line = 1; - max_line = 1; - match_found = 0; - print_eject_help = 0; - cfg_printf("%s\n\n", menuptr[0].str); - while(line < 24) { - str = menuptr[line].str; - type = menuptr[line].cfgtype; - ptr = menuptr[line].ptr; - if(str == 0) { - break; - } - if((type & 0xf) == CFGTYPE_DISK) { - print_eject_help = 1; - } - cfg_parse_menu(menuptr, line, menu_line, 0); - if(line == menu_line) { - if(type != 0) { - match_found = 1; - } else if(menu_inc) { - menu_line++; - } else { - menu_line--; - } - } - if(line > max_line) { - max_line = line; - } - cfg_printf("%s\n", g_cfg_opt_buf); - line++; - } - if((menu_line < 1) && !match_found) { - menu_line = 1; - } - if((menu_line >= max_line) && !match_found) { - menu_line = max_line; - } - if(g_rom_version < 0) { - cfg_htab_vtab(0, 21); - cfg_printf("\bYOU MUST SELECT A VALID ROM FILE\b\n"); - } - cfg_htab_vtab(0, 23); - cfg_printf("Move: \tJ\t \tK\t Change: \tH\t \tU\t \tM\t"); - if(print_eject_help) { - cfg_printf(" Eject: "); - if(g_cfg_slotdrive >= 0) { - cfg_printf("\bESC\b"); - } else { - cfg_printf("E"); - } - } +void config_control_panel() { + void (*fn_ptr)(); + const char *str; + Cfg_menu *menuptr; + void *ptr; + int print_eject_help; + int line; + int type; + int match_found; + int menu_line; + int menu_inc; + int max_line; + int key; + int i, j; + // First, save important text screen state + g_save_cur_a2_stat = g_cur_a2_stat; + for(i = 0; i < 0x400; i++) { + g_save_text_screen_bytes[i] = g_slow_memory_ptr[0x400+i]; + g_save_text_screen_bytes[0x400+i] =g_slow_memory_ptr[0x10400+i]; + } + g_cur_a2_stat = ALL_STAT_TEXT | ALL_STAT_VID80 | ALL_STAT_ANNUNC3 | + (0xf << BIT_ALL_STAT_TEXT_COLOR) | ALL_STAT_ALTCHARSET; + g_a2_new_all_stat[0] = g_cur_a2_stat; + g_new_a2_stat_cur_line = 0; + for(i = 0; i < 20; i++) { + // Toss any queued-up keypresses + if(adb_read_c000() & 0x80) { + (void)adb_access_c010(); + } + } + g_adb_repeat_vbl = 0; + g_cfg_vbl_count = 0; + // HACK: Force adb keyboard (and probably mouse) to "normal"... + g_full_refresh_needed = -1; + g_a2_screen_buffer_changed = -1; + cfg_home(); + j = 0; + menuptr = g_cfg_main_menu; + if(g_rom_version < 0) { + /* Must select ROM file */ + menuptr = g_cfg_rom_menu; + } + menu_line = 1; + menu_inc = 1; + g_cfg_slotdrive = -1; + g_cfg_select_partition = -1; + while(g_config_control_panel & !(halt_sim&HALT_WANTTOQUIT)) { + if(g_fatal_log > 0) { + x_show_alert(0, 0); + } + cfg_home(); + line = 1; + max_line = 1; + match_found = 0; + print_eject_help = 0; + cfg_printf("%s\n\n", menuptr[0].str); + while(line < 24) { + str = menuptr[line].str; + type = menuptr[line].cfgtype; + ptr = menuptr[line].ptr; + if(str == 0) { + break; + } + if((type & 0xf) == CFGTYPE_DISK) { + print_eject_help = 1; + } + cfg_parse_menu(menuptr, line, menu_line, 0); + if(line == menu_line) { + if(type != 0) { + match_found = 1; + } else if(menu_inc) { + menu_line++; + } else { + menu_line--; + } + } + if(line > max_line) { + max_line = line; + } + cfg_printf("%s\n", g_cfg_opt_buf); + line++; + } + if((menu_line < 1) && !match_found) { + menu_line = 1; + } + if((menu_line >= max_line) && !match_found) { + menu_line = max_line; + } + if(g_rom_version < 0) { + cfg_htab_vtab(0, 21); + cfg_printf("\bYOU MUST SELECT A VALID ROM FILE\b\n"); + } + cfg_htab_vtab(0, 23); + cfg_printf("Move: \tJ\t \tK\t Change: \tH\t \tU\t \tM\t"); + if(print_eject_help) { + cfg_printf(" Eject: "); + if(g_cfg_slotdrive >= 0) { + cfg_printf("\bESC\b"); + } else { + cfg_printf("E"); + } + } #if 0 - cfg_htab_vtab(0, 22); - cfg_printf("menu_line: %d line: %d, vbl:%d, adb:%d key_dn:%d\n", - menu_line, line, g_cfg_vbl_count, g_adb_repeat_vbl, - g_key_down); + cfg_htab_vtab(0, 22); + cfg_printf("menu_line: %d line: %d, vbl:%d, adb:%d key_dn:%d\n", + menu_line, line, g_cfg_vbl_count, g_adb_repeat_vbl, + g_key_down); #endif - if(g_cfg_slotdrive >= 0) { - cfg_file_draw(); - } + if(g_cfg_slotdrive >= 0) { + cfg_file_draw(); + } #ifdef HAVE_TFE - /*HACK eh, at least I think it is. Display the available ethernet interfaces - when in the ethernet control panel. This is the only way one can customize a menu pane. - Kent did it with the directory browser, so why not.*/ - if(menuptr == g_cfg_ethernet_menu) - { - cfg_get_tfe_name(); - } + /*HACK eh, at least I think it is. Display the available ethernet interfaces + when in the ethernet control panel. This is the only way one can customize a menu pane. + Kent did it with the directory browser, so why not.*/ + if(menuptr == g_cfg_ethernet_menu) + { + cfg_get_tfe_name(); + } #endif #ifdef HAVE_SDL - /*If user enters the Virtual Imagewriter control panel, flag it so we can - automatically apply changes on exit.*/ - if(menuptr == g_cfg_imagewriter_menu) - { - g_cfg_triggeriwreset = 1; - } + /*If user enters the Virtual Imagewriter control panel, flag it so we can + automatically apply changes on exit.*/ + if(menuptr == g_cfg_imagewriter_menu) + { + g_cfg_triggeriwreset = 1; + } #endif - key = -1; - while(g_config_control_panel & !(halt_sim&HALT_WANTTOQUIT)) { - video_update(); - key = adb_read_c000(); - if(key & 0x80) { - key = key & 0x7f; - (void)adb_access_c010(); - break; - } else { - key = -1; - } - micro_sleep(1.0/60.0); - g_cfg_vbl_count++; - if(!match_found) { - break; - } - } - if((key >= 0) && (g_cfg_slotdrive < 0)) { - // Normal menu system - switch(key) { - case 0x0a: /* down arrow */ - menu_line++; - menu_inc = 1; - break; - case 0x0b: /* up arrow */ - menu_line--; - menu_inc = 0; - if(menu_line < 1) { - menu_line = 1; - } - break; - case 0x33: /* pg dn */ - menu_line += CFG_PG_SCROLL_AMT; - menu_inc = 1; - break; - case 0x39: /* pg up */ - menu_line -= CFG_PG_SCROLL_AMT; - menu_inc = 0; - if(menu_line < 1) { - menu_line = 1; - } - break; - case 0x15: /* right arrow */ - cfg_parse_menu(menuptr, menu_line,menu_line,1); - break; - case 0x08: /* left arrow */ - cfg_parse_menu(menuptr,menu_line,menu_line,-1); - break; - case 0x0d: - type = menuptr[menu_line].cfgtype; - ptr = menuptr[menu_line].ptr; - str = menuptr[menu_line].str; - switch(type & 0xf) { - case CFGTYPE_MENU: - menuptr = (Cfg_menu *)ptr; - menu_line = 1; - break; - case CFGTYPE_DISK: - g_cfg_slotdrive = type >> 4; - g_cfg_file_dir_only = 0; - cfg_file_init(); - break; - case CFGTYPE_FUNC: - fn_ptr = (void (*)())ptr; - (*fn_ptr)(); - adb_all_keys_up(); //Needed otherwise menu function will continue to repeat until we move selection up or down - break; - case CFGTYPE_FILE: - g_cfg_slotdrive = 0xfff; - g_cfg_file_def_name = str /* *((char **)ptr) */; // was ptr - g_cfg_file_strptr = (char **)ptr; - g_cfg_file_dir_only = 0; - cfg_file_init(); - break; - case CFGTYPE_DIR: - g_cfg_slotdrive = 0xfff; - g_cfg_file_def_name = str /* *((char **)ptr) */; // was ptr - g_cfg_file_strptr = (char **)ptr; - g_cfg_file_dir_only = 1; - cfg_file_init(); - break; - } - break; - case 0x1b: - // Jump to last menu entry - menu_line = max_line; - break; - case 'e': - case 'E': - type = menuptr[menu_line].cfgtype; - if((type & 0xf) == CFGTYPE_DISK) { - eject_disk_by_num(type >> 12, - (type >> 4) & 0xff); - } - break; - default: - glogf("Unhandled config key: 0x%02x", key); - } - } else if(key >= 0) { - cfg_file_handle_key(key); - } - } - for(i = 0; i < 0x400; i++) { - set_memory_c(0xe00400+i, g_save_text_screen_bytes[i], 0); - set_memory_c(0xe10400+i, g_save_text_screen_bytes[0x400+i], 0); - } - // And quit - if (g_cfg_triggeriwreset) - { - g_cfg_triggeriwreset = 0; - cfg_iwreset(); //Reset the virtual Imagewriter if the user was in the control panel. - } - g_config_control_panel = 0; - g_adb_repeat_vbl = g_vbl_count + 60; - g_cur_a2_stat = g_save_cur_a2_stat; - change_display_mode(g_cur_dcycs); - g_full_refresh_needed = -1; - g_a2_screen_buffer_changed = -1; + key = -1; + while(g_config_control_panel & !(halt_sim&HALT_WANTTOQUIT)) { + video_update(); + key = adb_read_c000(); + if(key & 0x80) { + key = key & 0x7f; + (void)adb_access_c010(); + break; + } else { + key = -1; + } + micro_sleep(1.0/60.0); + g_cfg_vbl_count++; + if(!match_found) { + break; + } + } + if((key >= 0) && (g_cfg_slotdrive < 0)) { + // Normal menu system + switch(key) { + case 0x0a: /* down arrow */ + menu_line++; + menu_inc = 1; + break; + case 0x0b: /* up arrow */ + menu_line--; + menu_inc = 0; + if(menu_line < 1) { + menu_line = 1; + } + break; + case 0x33: /* pg dn */ + menu_line += CFG_PG_SCROLL_AMT; + menu_inc = 1; + break; + case 0x39: /* pg up */ + menu_line -= CFG_PG_SCROLL_AMT; + menu_inc = 0; + if(menu_line < 1) { + menu_line = 1; + } + break; + case 0x15: /* right arrow */ + cfg_parse_menu(menuptr, menu_line,menu_line,1); + break; + case 0x08: /* left arrow */ + cfg_parse_menu(menuptr,menu_line,menu_line,-1); + break; + case 0x0d: + type = menuptr[menu_line].cfgtype; + ptr = menuptr[menu_line].ptr; + str = menuptr[menu_line].str; + switch(type & 0xf) { + case CFGTYPE_MENU: + menuptr = (Cfg_menu *)ptr; + menu_line = 1; + break; + case CFGTYPE_DISK: + g_cfg_slotdrive = type >> 4; + g_cfg_file_dir_only = 0; + cfg_file_init(); + break; + case CFGTYPE_FUNC: + fn_ptr = (void (*)())ptr; + (*fn_ptr)(); + adb_all_keys_up(); //Needed otherwise menu function will continue to repeat until we move selection up or down + break; + case CFGTYPE_FILE: + g_cfg_slotdrive = 0xfff; + g_cfg_file_def_name = str /* *((char **)ptr) */; // was ptr + g_cfg_file_strptr = (char **)ptr; + g_cfg_file_dir_only = 0; + cfg_file_init(); + break; + case CFGTYPE_DIR: + g_cfg_slotdrive = 0xfff; + g_cfg_file_def_name = str /* *((char **)ptr) */; // was ptr + g_cfg_file_strptr = (char **)ptr; + g_cfg_file_dir_only = 1; + cfg_file_init(); + break; + } + break; + case 0x1b: + // Jump to last menu entry + menu_line = max_line; + break; + case 'e': + case 'E': + type = menuptr[menu_line].cfgtype; + if((type & 0xf) == CFGTYPE_DISK) { + eject_disk_by_num(type >> 12, + (type >> 4) & 0xff); + } + break; + default: + glogf("Unhandled config key: 0x%02x", key); + } + } else if(key >= 0) { + cfg_file_handle_key(key); + } + } + for(i = 0; i < 0x400; i++) { + set_memory_c(0xe00400+i, g_save_text_screen_bytes[i], 0); + set_memory_c(0xe10400+i, g_save_text_screen_bytes[0x400+i], 0); + } + // And quit + if (g_cfg_triggeriwreset) + { + g_cfg_triggeriwreset = 0; + cfg_iwreset(); //Reset the virtual Imagewriter if the user was in the control panel. + } + g_config_control_panel = 0; + g_adb_repeat_vbl = g_vbl_count + 60; + g_cur_a2_stat = g_save_cur_a2_stat; + change_display_mode(g_cur_dcycs); + g_full_refresh_needed = -1; + g_a2_screen_buffer_changed = -1; } -void x_clk_setup_bram_version() -{ - if(g_rom_version < 3) { - g_bram_ptr = (&g_bram[0][0]); // ROM 01 - } else { - g_bram_ptr = (&g_bram[1][0]); // ROM 03 - } +void x_clk_setup_bram_version() { + if(g_rom_version < 3) { + g_bram_ptr = (&g_bram[0][0]); // ROM 01 + } else { + g_bram_ptr = (&g_bram[1][0]); // ROM 03 + } } diff --git a/src/debug.c b/src/debug.c index ab41edf..49c3021 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1,3 +1,10 @@ +/* + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ + #include #include #include @@ -25,7 +32,7 @@ extern const word32 disas_types[256]; extern Engine_reg engine; extern int g_config_control_panel; extern int g_num_breakpoints; -extern word32 g_breakpts[MAX_BREAK_POINTS]; +extern word32 g_breakpts[MAX_BREAK_POINTS]; int g_dbg_enable_port = 0; int debug_pause = 0; int g_dbg_step = 0; @@ -48,9 +55,9 @@ typedef enum { G_DBG_COMMAND_ADD_BRK = 8, G_DBG_COMMAND_DEL_BRK = 9, G_DBG_COMMAND_GET_BRK = 0xA, - G_DBG_COMMAND_PAUSE = 0xB, // deprecated - G_DBG_COMMAND_DEBUGGER = 0xC, // deprecated - G_DBG_COMMAND_QUIT = 0xD, // deprecated + G_DBG_COMMAND_PAUSE = 0xB, // deprecated + G_DBG_COMMAND_DEBUGGER = 0xC, // deprecated + G_DBG_COMMAND_QUIT = 0xD, // deprecated G_DBG_COMMAND_EMU_CMD = 0xE } G_DBG_COMMANDS; @@ -132,7 +139,7 @@ char buffer[BUFFER_SIZE]; char tmp_buffer_4k[4097]; // adding +1 in case someone forgets \0 char tmp_buffer2_4k[4097]; // adding +1 in case someone forgets \0 -struct sockaddr_in addr; +struct sockaddr_in addr; int timeout; // poll timeout in ms struct pollfd fds[200]; int nfds = 0, current_size = 0, i, j; @@ -141,20 +148,18 @@ int debugger_sd = -1; // this holds our socket file descriptor for the debugger, /* socket debug version */ -void -do_go_debug() -{ +void do_go_debug() { while (1) { if (g_dbg_step >= 0) { if (g_dbg_step == 1) { - g_dbg_step = -1; // we are taking a step, so chill on the next round + g_dbg_step = -1; // we are taking a step, so chill on the next round } g_config_control_panel = 0; clear_halt(); glog("calling run_prog()"); - run_prog(); // also calls debug_server_poll() + run_prog(); // also calls debug_server_poll() glog("left run_prog()"); step_count++; @@ -190,7 +195,7 @@ void debug_push_message(G_DBG_COMMANDS msg_type, char *msg_ptr, int msg_len) { glogf("debug_push_message() GOT: %d", (int) msg_type); dbg_cmd_queue[dbg_cmd_queue_len].command = msg_type; memcpy(dbg_cmd_queue[dbg_cmd_queue_len].cdata, msg_ptr, msg_len); // stripping that first char - dbg_cmd_queue[dbg_cmd_queue_len].cdata[msg_len+1] = '\0'; // terminator + dbg_cmd_queue[dbg_cmd_queue_len].cdata[msg_len+1] = '\0'; // terminator dbg_cmd_queue_len++; // INC POINTER } @@ -281,8 +286,8 @@ void api_push_memack() { void api_push_cpu() { Engine_reg *eptr; eptr = &engine; - int tmp_acc, tmp_x, tmp_y, tmp_psw; - int kpc, direct_page, dbank, stack; + int tmp_acc, tmp_x, tmp_y, tmp_psw; + int kpc, direct_page, dbank, stack; kpc = eptr->kpc; tmp_acc = eptr->acc; @@ -293,9 +298,9 @@ void api_push_cpu() { tmp_y = eptr->yreg; tmp_psw = eptr->psr; - int size = snprintf(tmp_buffer_4k, sizeof(tmp_buffer_4k),"{\"type\":\"cpu\",\"data\":{\"K\":\"%02X\",\"PC\":\"%04X\",\"A\":\"%04X\","\ - "\"X\":\"%04X\",\"Y\":\"%04X\",\"S\":\"%04X\",\"D\":\"%04X\",\"B\":\"%02X\",\"PSR\":\"%04X\"}}", - kpc>>16, kpc & 0xffff ,tmp_acc,tmp_x,tmp_y,stack,direct_page,dbank, tmp_psw & 0xFFF); + int size = snprintf(tmp_buffer_4k, sizeof(tmp_buffer_4k),"{\"type\":\"cpu\",\"data\":{\"K\":\"%02X\",\"PC\":\"%04X\",\"A\":\"%04X\"," \ + "\"X\":\"%04X\",\"Y\":\"%04X\",\"S\":\"%04X\",\"D\":\"%04X\",\"B\":\"%02X\",\"PSR\":\"%04X\"}}", + kpc>>16, kpc & 0xffff,tmp_acc,tmp_x,tmp_y,stack,direct_page,dbank, tmp_psw & 0xFFF); char *msg = (char*)malloc(sizeof(char) * size); strcpy(msg,tmp_buffer_4k); @@ -303,35 +308,35 @@ void api_push_cpu() { } void api_push_brk() { - int i; - // build our json array of breakpoints - tmp_buffer2_4k[0] = '\0'; // start with empty string - char *str_ptr = tmp_buffer2_4k; - for(i = 0; i < g_num_breakpoints; i++) { - //printf("{\"bp:%02x: %06x\n", i, g_breakpts[i]); - str_ptr += sprintf(str_ptr, "{\"trig\":\"addr\",\"match\":\"%06X\"}", g_breakpts[i]); - if (i < g_num_breakpoints-1) { - str_ptr += sprintf(str_ptr, ","); - } - } - str_ptr = tmp_buffer2_4k; // reset pointer to beginning of string + int i; + // build our json array of breakpoints + tmp_buffer2_4k[0] = '\0'; // start with empty string + char *str_ptr = tmp_buffer2_4k; + for(i = 0; i < g_num_breakpoints; i++) { + //printf("{\"bp:%02x: %06x\n", i, g_breakpts[i]); + str_ptr += sprintf(str_ptr, "{\"trig\":\"addr\",\"match\":\"%06X\"}", g_breakpts[i]); + if (i < g_num_breakpoints-1) { + str_ptr += sprintf(str_ptr, ","); + } + } + str_ptr = tmp_buffer2_4k; // reset pointer to beginning of string - int size = snprintf(tmp_buffer_4k, sizeof(tmp_buffer_4k),"{\"type\":\"brk\",\"data\":{\"breakpoints\":[%s]}}", str_ptr); + int size = snprintf(tmp_buffer_4k, sizeof(tmp_buffer_4k),"{\"type\":\"brk\",\"data\":{\"breakpoints\":[%s]}}", str_ptr); - char *msg = (char*)malloc(sizeof(char) * size); - strcpy(msg,tmp_buffer_4k); - push_api_msg(size, msg); + char *msg = (char*)malloc(sizeof(char) * size); + strcpy(msg,tmp_buffer_4k); + push_api_msg(size, msg); } void api_push_stack() { Engine_reg *eptr; eptr = &engine; - int kpc, stack; + int kpc, stack; kpc = eptr->kpc; stack = eptr->stack; - word32 stack_loc = (kpc & 0xff0000) + (stack & 0xff00); // e.g. 0x00000100 (default) or 0x00FF0100 (GS Boot) + word32 stack_loc = (kpc & 0xff0000) + (stack & 0xff00); // e.g. 0x00000100 (default) or 0x00FF0100 (GS Boot) // build our json array of 256 stack values ($nn00-nnFF) char *str_ptr = tmp_buffer2_4k; // (1024B) @@ -342,10 +347,10 @@ void api_push_stack() { str_ptr += sprintf(str_ptr, ","); } } - str_ptr = tmp_buffer2_4k; // reset pointer to beginning of string + str_ptr = tmp_buffer2_4k; // reset pointer to beginning of string int size = snprintf(tmp_buffer_4k, sizeof(tmp_buffer_4k),"{\"type\":\"stack\",\"data\":{\"loc\":\"%06X\",\"S\":\"%04X\",\"bytes\":[%s]}}", - stack_loc,stack,str_ptr); + stack_loc,stack,str_ptr); char *msg = (char*)malloc(sizeof(char) * size); strcpy(msg,tmp_buffer_4k); @@ -414,18 +419,18 @@ void api_push_disassembly_chain() { void api_push_dump(int bank, int start, int end) { char *post_str = "\"}}"; int pre_size = snprintf(tmp_buffer_4k, sizeof(tmp_buffer_4k), - "{\"type\":\"mem\",\"data\":{\"bank\":\"%02X\",\"start\":\"%04X\",\"end\":\"%04X\",\"b64data\":\"", - bank, start, end); + "{\"type\":\"mem\",\"data\":{\"bank\":\"%02X\",\"start\":\"%04X\",\"end\":\"%04X\",\"b64data\":\"", + bank, start, end); int len = end - start + 1; // +1 for inclusive if (len <= 0x10000) { int b64len = b64encode_len(len); // get pointer to length of full message with data and json wrapper - char *msg_json = (char*)malloc(sizeof(char) * (b64len + strlen(post_str) + pre_size + 1)); //? +1 - char *str_ptr = msg_json; // msg_json is our big malloced buffer + char *msg_json = (char*)malloc(sizeof(char) * (b64len + strlen(post_str) + pre_size + 1)); //? +1 + char *str_ptr = msg_json; // msg_json is our big malloced buffer - strcat(str_ptr, tmp_buffer_4k); // prefix string from above - str_ptr += strlen(tmp_buffer_4k); // move forward the ptr + strcat(str_ptr, tmp_buffer_4k); // prefix string from above + str_ptr += strlen(tmp_buffer_4k); // move forward the ptr // space to copy - can't figure out if i need to go through get_memory_c() or not char *memchunk = (char*)malloc(sizeof(char) * (len+1)); for (int i = start; i <= end; i++) { @@ -437,7 +442,7 @@ void api_push_dump(int bank, int start, int end) { b64encode(str_ptr, memchunk, len); str_ptr = strcat(str_ptr, post_str); // "\"}}" - push_api_msg(strlen(msg_json), msg_json); // don't free, de-queue will do that after writing to socket + push_api_msg(strlen(msg_json), msg_json); // don't free, de-queue will do that after writing to socket //free(msg_json); free(memchunk); } @@ -494,7 +499,7 @@ void event_did_step(int step_count) { api_push_stack(); api_push_cpu(); api_push_disassembly(); - if (step_count == 1 || g_dbg_step == -2) { // our first step? + if (step_count == 1 || g_dbg_step == -2) { // our first step? api_push_disassembly_start(); } api_write_socket(); @@ -523,7 +528,7 @@ void handle_set_bytes(int address, char *bytes_data) { while (sscanf(bytes_data, "%02x", &byte) == 1) { printf("$%02x <---- BYTE @ $%06X\n", byte, address ); bytes_data += 2; - address ++; + address++; set_byte_at_address(address, byte & 0xFF); } } @@ -536,15 +541,15 @@ void event_set_mem(char *str) { char *pch; pch = strtok (str," "); // split our memory sets on spaces while (pch != NULL) { - sscanf(pch, "%06X", &address); + sscanf(pch, "%06X", &address); - bytes_data = pch+6; - printf("BytesData %s\n", bytes_data); + bytes_data = pch+6; + printf("BytesData %s\n", bytes_data); - // for each token go try to handle it - handle_set_bytes(address, bytes_data); + // for each token go try to handle it + handle_set_bytes(address, bytes_data); - pch = strtok (NULL, " "); + pch = strtok (NULL, " "); } api_push_memack(); // send ack api_write_socket(); @@ -586,13 +591,13 @@ void event_emu_cmd(char *str) { pch = strtok (str," "); while (pch != NULL) { - cmd_char = pch[0]; - cmd_data = pch+1; - if (cmd_data[0] == '\0') { - cmd_data = NULL; - } - // for each token go try to handle it - handle_emu_cmd(cmd_char, cmd_data); + cmd_char = pch[0]; + cmd_data = pch+1; + if (cmd_data[0] == '\0') { + cmd_data = NULL; + } + // for each token go try to handle it + handle_emu_cmd(cmd_char, cmd_data); pch = strtok (NULL, " "); } @@ -694,13 +699,13 @@ void event_set_cpu(char *str) { char * pch; pch = strtok (str," "); while (pch != NULL) { - cmd_char = pch[0]; - cmd_data = pch+1; - if (cmd_data[0] == '\0') { - cmd_data = NULL; - } - // for each token go try to handle it - handle_cpu_cmd(cmd_char, cmd_data); + cmd_char = pch[0]; + cmd_data = pch+1; + if (cmd_data[0] == '\0') { + cmd_data = NULL; + } + // for each token go try to handle it + handle_cpu_cmd(cmd_char, cmd_data); pch = strtok (NULL, " "); } @@ -791,7 +796,7 @@ void debug_setup_socket() { /*************************************************************/ /* Initialize the pollfd structure */ /*************************************************************/ - memset(fds, 0 , sizeof(fds)); + memset(fds, 0, sizeof(fds)); /*************************************************************/ /* Set up the initial listening socket */ @@ -836,7 +841,7 @@ void api_write_socket() { // message_string is now built! we can send it. dbg_msg_queue_len = 0; // clear msg queue - writeStrToClient(debugger_sd, message_string); // ignores result + writeStrToClient(debugger_sd, message_string); // ignores result free(message_string); // assuming it was all written :P } @@ -865,10 +870,10 @@ void write_array_next() { int writeDataToClient(int sckt, const void *data, int datalen) { const char *pdata = (const char*) data; - while (datalen > 0){ + while (datalen > 0) { int numSent = send(sckt, pdata, datalen, 0); - if (numSent <= 0){ - if (numSent == 0){ + if (numSent <= 0) { + if (numSent == 0) { printf("The client was not written to: disconnected\n"); } else { perror("The client was not written to"); @@ -1051,10 +1056,10 @@ void debug_server_poll() { char *mesg_ptr = buffer; char *split_ptr = strchr(mesg_ptr, '\n'); - int mesg_len = len-1; // stripping that first char + int mesg_len = len-1; // stripping that first char if(split_ptr) { int index = split_ptr - buffer; - mesg_len = index - 1; // stripping that first char + mesg_len = index - 1; // stripping that first char } int debug_echo = FALSE; while (mesg_ptr < buffer + len - 1) { @@ -1095,13 +1100,13 @@ void debug_server_poll() { debug_push_message(G_DBG_COMMAND_GET_BRK, mesg_ptr+1, mesg_len); break; - case 'b': // DEPRECATED + case 'b': // DEPRECATED debug_push_message(G_DBG_COMMAND_PAUSE, mesg_ptr+1, mesg_len); break; - case 'c': // DEPRECATED + case 'c': // DEPRECATED debug_push_message(G_DBG_COMMAND_DEBUGGER, mesg_ptr+1, mesg_len); break; - case 'd': // DEPRECATED ???? + case 'd': // DEPRECATED ???? debug_push_message(G_DBG_COMMAND_QUIT, mesg_ptr+1, mesg_len); break; @@ -1119,11 +1124,11 @@ void debug_server_poll() { // @TODO probably send error response } - mesg_ptr += mesg_len + 2; // +1 for command char and +1 for '\n' + mesg_ptr += mesg_len + 2; // +1 for command char and +1 for '\n' split_ptr = strchr(mesg_ptr, '\n'); if(split_ptr) { int index = split_ptr - mesg_ptr; - mesg_len = index - 1; // stripping that first char + mesg_len = index - 1; // stripping that first char } } /*****************************************************/ @@ -1183,7 +1188,7 @@ void debug_server_poll() { /*************************************************************/ for (i = 0; i < nfds; i++) { if(fds[i].fd >= 0) - close(fds[i].fd); + close(fds[i].fd); } nfds = 0; } @@ -1196,12 +1201,12 @@ int do_dis_json(char *buf, word32 kpc, int accsize, int xsize, int op_provided, const char *out; - int args, type; - int opcode; - word32 val; - word32 oldkpc; - word32 dtype; - int signed_val; + int args, type; + int opcode; + word32 val; + word32 oldkpc; + word32 dtype; + int signed_val; oldkpc = kpc; if(op_provided) { @@ -1228,33 +1233,33 @@ int do_dis_json(char *buf, word32 kpc, int accsize, int xsize, int op_provided, val = -1; switch(args) { case 0: - val = 0; - break; + val = 0; + break; case 1: - if(op_provided) { - val = instr & 0xff; - } else { - val = get_memory_c(kpc, 0); - } - break; + if(op_provided) { + val = instr & 0xff; + } else { + val = get_memory_c(kpc, 0); + } + break; case 2: - if(op_provided) { - val = instr & 0xffff; - } else { - val = get_memory16_c(kpc, 0); - } - break; + if(op_provided) { + val = instr & 0xffff; + } else { + val = get_memory16_c(kpc, 0); + } + break; case 3: - if(op_provided) { - val = instr & 0xffffff; - } else { - val = get_memory24_c(kpc, 0); - } - break; + if(op_provided) { + val = instr & 0xffffff; + } else { + val = get_memory24_c(kpc, 0); + } + break; default: - fprintf(stderr, "args out of range: %d, opcode: %08x\n", - args, opcode); - break; + fprintf(stderr, "args out of range: %d, opcode: %08x\n", + args, opcode); + break; } kpc += args; @@ -1264,171 +1269,171 @@ int do_dis_json(char *buf, word32 kpc, int accsize, int xsize, int op_provided, switch(type) { case ABS: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%04x",out,val); - break; + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%04x",out,val); + break; case ABSX: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%04x,X",out,val); - break; + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%04x,X",out,val); + break; case ABSY: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%04x,Y",out,val); - break; + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%04x,Y",out,val); + break; case ABSLONG: - if(args != 3) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%06x",out,val); - break; + if(args != 3) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%06x",out,val); + break; case ABSIND: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s ($%04x)",out,val); - break; + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s ($%04x)",out,val); + break; case ABSXIND: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s ($%04x,X)",out,val); - break; + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s ($%04x,X)",out,val); + break; case IMPLY: - if(args != 0) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s",out); - break; + if(args != 0) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s",out); + break; case ACCUM: - if(args != 0) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s",out); - break; + if(args != 0) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s",out); + break; case IMMED: - if(args == 1) { - sprintf(buf_disasm,"%s #$%02x",out,val); - } else if(args == 2) { - sprintf(buf_disasm,"%s #$%04x",out,val); - } else { - printf("arg # mismatch for opcode %x\n", opcode); - } - break; + if(args == 1) { + sprintf(buf_disasm,"%s #$%02x",out,val); + } else if(args == 2) { + sprintf(buf_disasm,"%s #$%04x",out,val); + } else { + printf("arg # mismatch for opcode %x\n", opcode); + } + break; case JUST8: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%02x",out,val); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%02x",out,val); + break; case DLOC: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%02x",out,val); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%02x",out,val); + break; case DLOCX: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%02x,X",out,val); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%02x,X",out,val); + break; case DLOCY: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%02x,Y",out,val); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%02x,Y",out,val); + break; case LONG: - if(args != 3) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%06x",out,val); - break; + if(args != 3) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%06x",out,val); + break; case LONGX: - if(args != 3) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%06x,X",out,val); - break; + if(args != 3) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%06x,X",out,val); + break; case DLOCIND: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s ($%02x)",out,val); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s ($%02x)",out,val); + break; case DLOCINDY: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s ($%02x),Y",out,val); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s ($%02x),Y",out,val); + break; case DLOCXIND: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s ($%02x,X)",out,val); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s ($%02x,X)",out,val); + break; case DLOCBRAK: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s [$%02x]",out,val); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s [$%02x]",out,val); + break; case DLOCBRAKY: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s [$%02x],y",out,val); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s [$%02x],y",out,val); + break; case DISP8: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - signed_val = (signed char)val; - sprintf(buf_disasm,"%s $%04x",out, - (word32)(kpc+(signed_val)) & 0xffff); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + signed_val = (signed char)val; + sprintf(buf_disasm,"%s $%04x",out, + (word32)(kpc+(signed_val)) & 0xffff); + break; case DISP8S: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%02x,S",out,(word32)(byte)(val)); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%02x,S",out,(word32)(byte)(val)); + break; case DISP8SINDY: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s ($%02x,S),Y",out,(word32)(byte)(val)); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s ($%02x,S),Y",out,(word32)(byte)(val)); + break; case DISP16: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%04x", out, - (word32)(kpc+(signed)(word16)(val)) & 0xffff); - break; + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%04x", out, + (word32)(kpc+(signed)(word16)(val)) & 0xffff); + break; case MVPMVN: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s $%02x,$%02x",out,val&0xff,val>>8); - break; + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s $%02x,$%02x",out,val&0xff,val>>8); + break; case SEPVAL: case REPVAL: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buf_disasm,"%s #$%02x",out,val); - break; + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buf_disasm,"%s #$%02x",out,val); + break; default: - printf("argument type: %d unexpected\n", type); - break; + printf("argument type: %d unexpected\n", type); + break; } @@ -1437,26 +1442,26 @@ int do_dis_json(char *buf, word32 kpc, int accsize, int xsize, int op_provided, opcode = (operand >> 24) & 0xff; switch (args+1) { case 1: - snprintf(buf_instructions, sizeof(buf_instructions),"[\"%02X\"]", opcode); - break; + snprintf(buf_instructions, sizeof(buf_instructions),"[\"%02X\"]", opcode); + break; case 2: - snprintf(buf_instructions, sizeof(buf_instructions),"[\"%02X\",\"%02X\"]", opcode, instr & 0xff); - break; + snprintf(buf_instructions, sizeof(buf_instructions),"[\"%02X\",\"%02X\"]", opcode, instr & 0xff); + break; case 3: - snprintf(buf_instructions, sizeof(buf_instructions),"[\"%02X\",\"%02X\",\"%02X\"]", opcode, instr & 0xff, (instr & 0xff00) >> 8); - break; + snprintf(buf_instructions, sizeof(buf_instructions),"[\"%02X\",\"%02X\",\"%02X\"]", opcode, instr & 0xff, (instr & 0xff00) >> 8); + break; case 4: - snprintf(buf_instructions, sizeof(buf_instructions),"[\"%02X\",\"%02X\",\"%02X\",\"%02X\"]", opcode, instr & 0xff, (instr & 0xff00) >> 8, (instr & 0xff0000) >> 16); - break; + snprintf(buf_instructions, sizeof(buf_instructions),"[\"%02X\",\"%02X\",\"%02X\",\"%02X\"]", opcode, instr & 0xff, (instr & 0xff00) >> 8, (instr & 0xff0000) >> 16); + break; default: - break; + break; } // @TODO: FIX!!! NEEDS REAL BUFFER SIZE, note magic 1024 - snprintf(buf, 1024,"{\"type\":\"dis\",\"data\":{\"K\":\"%02X\",\"PC\":\"%04X\",\"bytes\":%s,"\ - "\"disasm\":\"%s\",\"chain\":\"%d\"}}", - oldkpc>>16, oldkpc & 0xffff ,buf_instructions, buf_disasm, chain); + snprintf(buf, 1024,"{\"type\":\"dis\",\"data\":{\"K\":\"%02X\",\"PC\":\"%04X\",\"bytes\":%s," \ + "\"disasm\":\"%s\",\"chain\":\"%d\"}}", + oldkpc>>16, oldkpc & 0xffff,buf_instructions, buf_disasm, chain); return(args+1); } @@ -1488,7 +1493,7 @@ int b64encode(char *encoded, const char *string, int len) { *p++ = '='; } else { *p++ = b64chars[((string[i] & 0x3) << 4) | - ((int) (string[i + 1] & 0xF0) >> 4)]; + ((int) (string[i + 1] & 0xF0) >> 4)]; *p++ = b64chars[((string[i + 1] & 0xF) << 2)]; } *p++ = '='; diff --git a/src/defc.h b/src/defc.h index 9f4cbbb..d6317d7 100644 --- a/src/defc.h +++ b/src/defc.h @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defcomm.h" @@ -12,8 +12,8 @@ #include extern "C" int outputInfo(const char* format,...); extern "C" int fOutputInfo(FILE*,const char* format,...); -#define printf outputInfo -#define fprintf fOutputInfo +#define printf outputInfo +#define fprintf fOutputInfo #endif #define STRUCT(a) typedef struct _ ## a a; struct _ ## a @@ -30,31 +30,31 @@ typedef unsigned long long word64; void U_STACK_TRACE(); /* 28MHz crystal, plus every 65th 1MHz cycle is stretched 140ns */ -#define CYCS_28_MHZ (28636360) -#define DCYCS_28_MHZ (1.0*CYCS_28_MHZ) -#define CYCS_3_5_MHZ (CYCS_28_MHZ/8) -#define DCYCS_1_MHZ ((DCYCS_28_MHZ/28.0)*(65.0*7/(65.0*7+1.0))) -#define CYCS_1_MHZ ((int)DCYCS_1_MHZ) +#define CYCS_28_MHZ (28636360) +#define DCYCS_28_MHZ (1.0*CYCS_28_MHZ) +#define CYCS_3_5_MHZ (CYCS_28_MHZ/8) +#define DCYCS_1_MHZ ((DCYCS_28_MHZ/28.0)*(65.0*7/(65.0*7+1.0))) +#define CYCS_1_MHZ ((int)DCYCS_1_MHZ) /* #define DCYCS_IN_16MS_RAW (DCYCS_1_MHZ / 60.0) */ -#define DCYCS_IN_16MS_RAW (262.0 * 65.0) +#define DCYCS_IN_16MS_RAW (262.0 * 65.0) /* Use precisely 17030 instead of forcing 60 Hz since this is the number of */ /* 1MHz cycles per screen */ -#define DCYCS_IN_16MS ((double)((int)DCYCS_IN_16MS_RAW)) -#define DRECIP_DCYCS_IN_16MS (1.0 / (DCYCS_IN_16MS)) +#define DCYCS_IN_16MS ((double)((int)DCYCS_IN_16MS_RAW)) +#define DRECIP_DCYCS_IN_16MS (1.0 / (DCYCS_IN_16MS)) #ifdef GSPLUS_LITTLE_ENDIAN // @todo: look at using for fastest platform implementations -# define BIGEND(a) ((((a) >> 24) & 0xff) + \ - (((a) >> 8) & 0xff00) + \ - (((a) << 8) & 0xff0000) + \ - (((a) << 24) & 0xff000000)) -# define GET_BE_WORD16(a) ((((a) >> 8) & 0xff) + (((a) << 8) & 0xff00)) -# define GET_BE_WORD32(a) (BIGEND(a)) +# define BIGEND(a) ((((a) >> 24) & 0xff) + \ + (((a) >> 8) & 0xff00) + \ + (((a) << 8) & 0xff0000) + \ + (((a) << 24) & 0xff000000)) +# define GET_BE_WORD16(a) ((((a) >> 8) & 0xff) + (((a) << 8) & 0xff00)) +# define GET_BE_WORD32(a) (BIGEND(a)) #else -# define BIGEND(a) (a) -# define GET_BE_WORD16(a) (a) -# define GET_BE_WORD32(a) (a) +# define BIGEND(a) (a) +# define GET_BE_WORD16(a) (a) +# define GET_BE_WORD32(a) (a) #endif #define MAXNUM_HEX_PER_LINE 32 @@ -63,7 +63,7 @@ void U_STACK_TRACE(); # include #endif -#if !defined(_WIN32) && !defined(UNDER_CE) // OG +#if !defined(_WIN32) && !defined(UNDER_CE) // OG # include # include # include @@ -77,7 +77,7 @@ void U_STACK_TRACE(); #include #include -#ifndef UNDER_CE // OG CE SPecific +#ifndef UNDER_CE // OG CE SPecific #include #include #include @@ -93,24 +93,24 @@ extern int open(const char* name,int,...); extern int read(int,char*,int); extern int close(int); extern int write( int fd, const void *buffer, unsigned int count ); -extern int lseek(int,int,int); +extern int lseek(int,int,int); struct stat { int st_size; }; extern int stat(const char* name, struct stat*); extern int fstat(int, struct stat*); -#define O_RDWR 1 -#define O_BINARY 2 -#define O_RDONLY 4 -#define O_WRONLY 8 -#define O_CREAT 16 -#define O_TRUNC 32 -#define EAGAIN 11 -#define EINTR 4 +#define O_RDWR 1 +#define O_BINARY 2 +#define O_RDONLY 4 +#define O_WRONLY 8 +#define O_CREAT 16 +#define O_TRUNC 32 +#define EAGAIN 11 +#define EINTR 4 #endif #ifdef HPUX -# include /* for GET_ITIMER */ +# include /* for GET_ITIMER */ #endif #ifdef SOLARIS @@ -119,232 +119,232 @@ extern int fstat(int, struct stat*); #ifndef O_BINARY /* work around some Windows junk */ -# define O_BINARY 0 +# define O_BINARY 0 #endif STRUCT(Pc_log) { - double dcycs; - word32 dbank_kpc; - word32 instr; - word32 psr_acc; - word32 xreg_yreg; - word32 stack_direct; - word32 pad; + double dcycs; + word32 dbank_kpc; + word32 instr; + word32 psr_acc; + word32 xreg_yreg; + word32 stack_direct; + word32 pad; }; STRUCT(Data_log) { - double dcycs; - word32 addr; - word32 val; - word32 size; + double dcycs; + word32 addr; + word32 val; + word32 size; }; STRUCT(Event) { - double dcycs; - int type; - Event *next; + double dcycs; + int type; + Event *next; }; STRUCT(Fplus) { - double plus_1; - double plus_2; - double plus_3; - double plus_x_minus_1; + double plus_1; + double plus_2; + double plus_3; + double plus_x_minus_1; }; STRUCT(Engine_reg) { - double fcycles; - word32 kpc; - word32 acc; + double fcycles; + word32 kpc; + word32 acc; - word32 xreg; - word32 yreg; + word32 xreg; + word32 yreg; - word32 stack; - word32 dbank; + word32 stack; + word32 dbank; - word32 direct; - word32 psr; - Fplus *fplus_ptr; + word32 direct; + word32 psr; + Fplus *fplus_ptr; }; STRUCT(Kimage) { - void *dev_handle; - void *dev_handle2; - byte *data_ptr; - int width_req; - int width_act; - int height; - int depth; - int mdepth; - int aux_info; + void *dev_handle; + void *dev_handle2; + byte *data_ptr; + int width_req; + int width_act; + int height; + int depth; + int mdepth; + int aux_info; }; typedef byte *Pg_info; STRUCT(Page_info) { - Pg_info rd_wr; + Pg_info rd_wr; }; STRUCT(Cfg_menu) { - const char *str; - void *ptr; - const char *name_str; - void *defptr; - int cfgtype; + const char *str; + void *ptr; + const char *name_str; + void *defptr; + int cfgtype; }; STRUCT(Cfg_dirent) { - char *name; - int is_dir; - int size; - int image_start; - int part_num; + char *name; + int is_dir; + int size; + int image_start; + int part_num; }; STRUCT(Cfg_listhdr) { - Cfg_dirent *direntptr; - int max; - int last; - int invalid; + Cfg_dirent *direntptr; + int max; + int last; + int invalid; - int curent; - int topent; + int curent; + int topent; - int num_to_show; + int num_to_show; }; STRUCT(Emustate_intlist) { - const char *str; - int *iptr; + const char *str; + int *iptr; }; STRUCT(Emustate_dbllist) { - const char *str; - double *dptr; + const char *str; + double *dptr; }; STRUCT(Emustate_word32list) { - const char *str; - word32 *wptr; + const char *str; + word32 *wptr; }; #ifdef __LP64__ -# define PTR2WORD(a) ((unsigned long)(a)) +# define PTR2WORD(a) ((unsigned long)(a)) #else -# define PTR2WORD(a) ((unsigned int)(a)) +# define PTR2WORD(a) ((unsigned int)(a)) #endif -#define ALTZP (g_c068_statereg & 0x80) +#define ALTZP (g_c068_statereg & 0x80) /* #define PAGE2 (g_c068_statereg & 0x40) */ -#define RAMRD (g_c068_statereg & 0x20) -#define RAMWRT (g_c068_statereg & 0x10) -#define RDROM (g_c068_statereg & 0x08) -#define LCBANK2 (g_c068_statereg & 0x04) -#define ROMB (g_c068_statereg & 0x02) -#define INTCX (g_c068_statereg & 0x01) +#define RAMRD (g_c068_statereg & 0x20) +#define RAMWRT (g_c068_statereg & 0x10) +#define RDROM (g_c068_statereg & 0x08) +#define LCBANK2 (g_c068_statereg & 0x04) +#define ROMB (g_c068_statereg & 0x02) +#define INTCX (g_c068_statereg & 0x01) -#define C041_EN_25SEC_INTS 0x10 -#define C041_EN_VBL_INTS 0x08 -#define C041_EN_SWITCH_INTS 0x04 -#define C041_EN_MOVE_INTS 0x02 -#define C041_EN_MOUSE 0x01 +#define C041_EN_25SEC_INTS 0x10 +#define C041_EN_VBL_INTS 0x08 +#define C041_EN_SWITCH_INTS 0x04 +#define C041_EN_MOVE_INTS 0x02 +#define C041_EN_MOUSE 0x01 /* WARNING: SCC1 and SCC0 interrupts must be in this order for scc.c */ /* This order matches the SCC hardware */ -#define IRQ_PENDING_SCC1_ZEROCNT 0x00001 -#define IRQ_PENDING_SCC1_TX 0x00002 -#define IRQ_PENDING_SCC1_RX 0x00004 -#define IRQ_PENDING_SCC0_ZEROCNT 0x00008 -#define IRQ_PENDING_SCC0_TX 0x00010 -#define IRQ_PENDING_SCC0_RX 0x00020 -#define IRQ_PENDING_C023_SCAN 0x00100 -#define IRQ_PENDING_C023_1SEC 0x00200 -#define IRQ_PENDING_C046_25SEC 0x00400 -#define IRQ_PENDING_C046_VBL 0x00800 -#define IRQ_PENDING_ADB_KBD_SRQ 0x01000 -#define IRQ_PENDING_ADB_DATA 0x02000 -#define IRQ_PENDING_ADB_MOUSE 0x04000 -#define IRQ_PENDING_DOC 0x08000 +#define IRQ_PENDING_SCC1_ZEROCNT 0x00001 +#define IRQ_PENDING_SCC1_TX 0x00002 +#define IRQ_PENDING_SCC1_RX 0x00004 +#define IRQ_PENDING_SCC0_ZEROCNT 0x00008 +#define IRQ_PENDING_SCC0_TX 0x00010 +#define IRQ_PENDING_SCC0_RX 0x00020 +#define IRQ_PENDING_C023_SCAN 0x00100 +#define IRQ_PENDING_C023_1SEC 0x00200 +#define IRQ_PENDING_C046_25SEC 0x00400 +#define IRQ_PENDING_C046_VBL 0x00800 +#define IRQ_PENDING_ADB_KBD_SRQ 0x01000 +#define IRQ_PENDING_ADB_DATA 0x02000 +#define IRQ_PENDING_ADB_MOUSE 0x04000 +#define IRQ_PENDING_DOC 0x08000 -#define EXTRU(val, pos, len) \ - ( ( (len) >= (pos) + 1) ? ((val) >> (31-(pos))) : \ - (((val) >> (31-(pos)) ) & ( (1<<(len) ) - 1) ) ) +#define EXTRU(val, pos, len) \ + ( ( (len) >= (pos) + 1) ? ((val) >> (31-(pos))) : \ + (((val) >> (31-(pos)) ) & ( (1<<(len) ) - 1) ) ) -#define DEP1(val, pos, old_val) \ - (((old_val) & ~(1 << (31 - (pos))) ) | \ - ( ((val) & 1) << (31 - (pos))) ) +#define DEP1(val, pos, old_val) \ + (((old_val) & ~(1 << (31 - (pos))) ) | \ + ( ((val) & 1) << (31 - (pos))) ) #define set_halt(val) \ - if(val) { set_halt_act(val); } + if(val) { set_halt_act(val); } #define clear_halt() \ - clr_halt_act() + clr_halt_act() #define GET_PAGE_INFO_RD(page) \ - (page_info_rd_wr[page].rd_wr) + (page_info_rd_wr[page].rd_wr) #define GET_PAGE_INFO_WR(page) \ - (page_info_rd_wr[0x10000 + PAGE_INFO_PAD_SIZE + (page)].rd_wr) + (page_info_rd_wr[0x10000 + PAGE_INFO_PAD_SIZE + (page)].rd_wr) #define SET_PAGE_INFO_RD(page,val) \ - ;page_info_rd_wr[page].rd_wr = (Pg_info)val; + ; page_info_rd_wr[page].rd_wr = (Pg_info)val; #define SET_PAGE_INFO_WR(page,val) \ - ;page_info_rd_wr[0x10000 + PAGE_INFO_PAD_SIZE + (page)].rd_wr = \ - (Pg_info)val; + ; page_info_rd_wr[0x10000 + PAGE_INFO_PAD_SIZE + (page)].rd_wr = \ + (Pg_info)val; -#define VERBOSE_DISK 0x001 -#define VERBOSE_IRQ 0x002 -#define VERBOSE_CLK 0x004 -#define VERBOSE_SHADOW 0x008 -#define VERBOSE_IWM 0x010 -#define VERBOSE_DOC 0x020 -#define VERBOSE_ADB 0x040 -#define VERBOSE_SCC 0x080 -#define VERBOSE_TEST 0x100 -#define VERBOSE_VIDEO 0x200 -#define VERBOSE_MAC 0x400 +#define VERBOSE_DISK 0x001 +#define VERBOSE_IRQ 0x002 +#define VERBOSE_CLK 0x004 +#define VERBOSE_SHADOW 0x008 +#define VERBOSE_IWM 0x010 +#define VERBOSE_DOC 0x020 +#define VERBOSE_ADB 0x040 +#define VERBOSE_SCC 0x080 +#define VERBOSE_TEST 0x100 +#define VERBOSE_VIDEO 0x200 +#define VERBOSE_MAC 0x400 #ifdef NO_VERB -# define DO_VERBOSE 0 +# define DO_VERBOSE 0 #else -# define DO_VERBOSE 1 +# define DO_VERBOSE 1 #endif -#define disk_printf if(DO_VERBOSE && (Verbose & VERBOSE_DISK)) printf -#define irq_printf if(DO_VERBOSE && (Verbose & VERBOSE_IRQ)) printf -#define clk_printf if(DO_VERBOSE && (Verbose & VERBOSE_CLK)) printf -#define shadow_printf if(DO_VERBOSE && (Verbose & VERBOSE_SHADOW)) printf -#define iwm_printf if(DO_VERBOSE && (Verbose & VERBOSE_IWM)) printf -#define doc_printf if(DO_VERBOSE && (Verbose & VERBOSE_DOC)) printf -#define adb_printf if(DO_VERBOSE && (Verbose & VERBOSE_ADB)) printf -#define scc_printf if(DO_VERBOSE && (Verbose & VERBOSE_SCC)) printf -#define test_printf if(DO_VERBOSE && (Verbose & VERBOSE_TEST)) printf -#define vid_printf if(DO_VERBOSE && (Verbose & VERBOSE_VIDEO)) printf -#define mac_printf if(DO_VERBOSE && (Verbose & VERBOSE_MAC)) printf +#define disk_printf if(DO_VERBOSE && (Verbose & VERBOSE_DISK)) printf +#define irq_printf if(DO_VERBOSE && (Verbose & VERBOSE_IRQ)) printf +#define clk_printf if(DO_VERBOSE && (Verbose & VERBOSE_CLK)) printf +#define shadow_printf if(DO_VERBOSE && (Verbose & VERBOSE_SHADOW)) printf +#define iwm_printf if(DO_VERBOSE && (Verbose & VERBOSE_IWM)) printf +#define doc_printf if(DO_VERBOSE && (Verbose & VERBOSE_DOC)) printf +#define adb_printf if(DO_VERBOSE && (Verbose & VERBOSE_ADB)) printf +#define scc_printf if(DO_VERBOSE && (Verbose & VERBOSE_SCC)) printf +#define test_printf if(DO_VERBOSE && (Verbose & VERBOSE_TEST)) printf +#define vid_printf if(DO_VERBOSE && (Verbose & VERBOSE_VIDEO)) printf +#define mac_printf if(DO_VERBOSE && (Verbose & VERBOSE_MAC)) printf -#define HALT_ON_SCAN_INT 0x001 -#define HALT_ON_IRQ 0x002 -#define HALT_ON_SHADOW_REG 0x004 -#define HALT_ON_C70D_WRITES 0x008 +#define HALT_ON_SCAN_INT 0x001 +#define HALT_ON_IRQ 0x002 +#define HALT_ON_SHADOW_REG 0x004 +#define HALT_ON_C70D_WRITES 0x008 -#define HALT_ON(a, msg) \ - if(Halt_on & a) { \ - halt_printf(msg); \ - } +#define HALT_ON(a, msg) \ + if(Halt_on & a) { \ + halt_printf(msg); \ + } #ifndef MIN -# define MIN(a,b) (((a) < (b)) ? (a) : (b)) +# define MIN(a,b) (((a) < (b)) ? (a) : (b)) #endif #ifndef MAX -# define MAX(a,b) (((a) < (b)) ? (b) : (a)) +# define MAX(a,b) (((a) < (b)) ? (b) : (a)) #endif -#define GET_ITIMER(dest) dest = get_itimer(); +#define GET_ITIMER(dest) dest = get_itimer(); #include "iwm.h" #include "protos.h" @@ -353,7 +353,7 @@ STRUCT(Emustate_word32list) { #define JOYSTICK_TYPE_MOUSE 1 #define JOYSTICK_TYPE_NATIVE_1 2 #define JOYSTICK_TYPE_NATIVE_2 3 -#define JOYSTICK_TYPE_NONE 4 // OG Added Joystick None +#define JOYSTICK_TYPE_NONE 4 // OG Added Joystick None #define NB_JOYSTICK_TYPE 5 // starting window x/y position if Undefined diff --git a/src/dis.c b/src/dis.c index 0dae8b2..077a191 100644 --- a/src/dis.c +++ b/src/dis.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include #include "defc.h" @@ -17,8 +17,8 @@ extern byte *g_memory_ptr; extern byte *g_slow_memory_ptr; extern int halt_sim; extern int enter_debug; -extern int g_dbg_step; //debug.c -extern int timeout; //debug.c +extern int g_dbg_step; //debug.c +extern int timeout; //debug.c extern int g_c068_statereg; extern word32 stop_run_at; extern int Verbose; @@ -27,22 +27,22 @@ extern int Halt_on; extern int g_fullscreen; extern int g_config_control_panel; -int g_num_breakpoints = 0; -word32 g_breakpts[MAX_BREAK_POINTS]; +int g_num_breakpoints = 0; +word32 g_breakpts[MAX_BREAK_POINTS]; extern int g_irq_pending; extern Engine_reg engine; -#define W_BUF_LEN 128 +#define W_BUF_LEN 128 char w_buff[W_BUF_LEN]; int g_stepping = 0; -word32 list_kpc; -int hex_line_len; -word32 a1,a2,a3; -word32 g_a4, g_a4bank; +word32 list_kpc; +int hex_line_len; +word32 a1,a2,a3; +word32 g_a4, g_a4bank; int a1bank, a2bank, a3bank; char *line_ptr; int mode,old_mode; @@ -51,1128 +51,1074 @@ int got_num; // OG replaced by HALT_WANTTOQUIT //int g_quit_sim_now = 0; -int -get_num() -{ - int tmp1; +int get_num() { + int tmp1; - a2 = 0; - got_num = 0; - while(1) { - if(mode == 0 && got_num != 0) { -/* - printf("In getnum, mode =0, setting a1,a3 = a2\n"); - printf("a2: %x\n", a2); -*/ - a3 = a2; - a3bank = a2bank; - a1 = a2; - a1bank = a2bank; - } - tmp1 = *line_ptr++ & 0x7f; - if(tmp1 >= '0' && tmp1 <= '9') { - a2 = (a2 << 4) + tmp1 - '0'; - got_num = 1; - continue; - } - if(tmp1 >= 'a' && tmp1 <= 'f') { - a2 = (a2 << 4) + tmp1 - 'a' + 10; - got_num = 1; - continue; - } - if(tmp1 == '/') { - a2bank = a2; - a2 = 0; - continue; - } - return tmp1; - } + a2 = 0; + got_num = 0; + while(1) { + if(mode == 0 && got_num != 0) { + a3 = a2; + a3bank = a2bank; + a1 = a2; + a1bank = a2bank; + } + tmp1 = *line_ptr++ & 0x7f; + if(tmp1 >= '0' && tmp1 <= '9') { + a2 = (a2 << 4) + tmp1 - '0'; + got_num = 1; + continue; + } + if(tmp1 >= 'a' && tmp1 <= 'f') { + a2 = (a2 << 4) + tmp1 - 'a' + 10; + got_num = 1; + continue; + } + if(tmp1 == '/') { + a2bank = a2; + a2 = 0; + continue; + } + return tmp1; + } } -void -debugger_help() -{ - printf("GSplus Debugger Help (courtesy Fredric Devernay)\n\n"); - printf("General command syntax:\n"); - printf(" [bank]/[address][command]\n\n"); - printf("Example: > e1/0010B Set a Breakpoint at the interrupt jump pt\n\n"); - printf("Enter all addresses using lower-case.\n"); - printf("As with the IIgs monitor, you can omit the bank number after\n"); - printf("having set it: 'e1/0010B' followed by '14B' will set breakpoints\n"); - printf("at e1/0010 and e1/0014\n\n"); - printf(" g Go\n"); - printf(" [bank]/[addr]g Go from [bank]/[address]\n"); - printf(" s Step one instruction\n"); - printf(" [bank]/[addr]s Step one instr at [bank]/[address]\n"); - printf(" [bank]/[addr]B Set breakpoint at [bank]/[address]\n"); - printf(" B Show all breakpoints\n"); - printf(" [bank]/[addr]D Delete breakpoint at [bank]/[address]\n"); - printf(" [bank]/[addr].[addr2] View memory\n"); - printf(" [bank]/[addr]L Disassemble memory\n"); - printf(" P Dump the trace to 'pc_log_out'\n"); - printf(" Z Dump SCC state\n"); - printf(" I Dump IWM state\n"); - printf("[drive].[track]I Dump IWM state\n"); - printf(" E Dump Ensoniq state\n"); - printf(" [osc]E Dump oscillator [osc] state\n"); - printf(" R Dump dtime array and events\n"); - printf(" T Show toolbox log\n"); - printf(" [bank]/[addr]T Dump tools using ptr [bank]/[addr]\n"); - printf(" as 'tool_set_info'\n"); - printf(" [mode]V XOR verbose with 1=DISK, 2=IRQ,\n"); - printf(" 4=CLK,8=SHADOW,10=IWM,20=DOC,\n"); - printf(" 40=ABD,80=SCC, 100=TEST, 200=VIDEO\n"); - printf(" [mode]H XOR halt_on with 1=SCAN_INT,\n"); - printf(" 2=IRQ, 4=SHADOW_REG, 8=C70D_WRITES\n"); - printf(" r Reset\n"); - printf(" [0/1]=m Changes m bit for l listings\n"); - printf(" [0/1]=x Changes x bit for l listings\n"); - printf(" [t]=z Stops at absolute time t (obsolete)\n"); - printf(" S show_bankptr_bank0 & smartport errs\n"); - printf(" P show_pmhz\n"); - printf(" A show_a2_line_stuff show_adb_log\n"); - printf(" Ctrl-e Dump registers\n"); - printf("[bank]/[addr1].[addr2]us[file] Save mem area to [file]\n"); - printf("[bank]/[addr1].[addr2]ul[file] Load mem area from [file]\n"); - printf(" v Show video information\n"); - printf(" q Quit Debugger (and GSplus)\n"); +void debugger_help() { + printf("GSplus Debugger Help (courtesy Fredric Devernay)\n\n"); + printf("General command syntax:\n"); + printf(" [bank]/[address][command]\n\n"); + printf("Example: > e1/0010B Set a Breakpoint at the interrupt jump pt\n\n"); + printf("Enter all addresses using lower-case.\n"); + printf("As with the IIgs monitor, you can omit the bank number after\n"); + printf("having set it: 'e1/0010B' followed by '14B' will set breakpoints\n"); + printf("at e1/0010 and e1/0014\n\n"); + printf(" g Go\n"); + printf(" [bank]/[addr]g Go from [bank]/[address]\n"); + printf(" s Step one instruction\n"); + printf(" [bank]/[addr]s Step one instr at [bank]/[address]\n"); + printf(" [bank]/[addr]B Set breakpoint at [bank]/[address]\n"); + printf(" B Show all breakpoints\n"); + printf(" [bank]/[addr]D Delete breakpoint at [bank]/[address]\n"); + printf(" [bank]/[addr].[addr2] View memory\n"); + printf(" [bank]/[addr]L Disassemble memory\n"); + printf(" P Dump the trace to 'pc_log_out'\n"); + printf(" Z Dump SCC state\n"); + printf(" I Dump IWM state\n"); + printf("[drive].[track]I Dump IWM state\n"); + printf(" E Dump Ensoniq state\n"); + printf(" [osc]E Dump oscillator [osc] state\n"); + printf(" R Dump dtime array and events\n"); + printf(" T Show toolbox log\n"); + printf(" [bank]/[addr]T Dump tools using ptr [bank]/[addr]\n"); + printf(" as 'tool_set_info'\n"); + printf(" [mode]V XOR verbose with 1=DISK, 2=IRQ,\n"); + printf(" 4=CLK,8=SHADOW,10=IWM,20=DOC,\n"); + printf(" 40=ABD,80=SCC, 100=TEST, 200=VIDEO\n"); + printf(" [mode]H XOR halt_on with 1=SCAN_INT,\n"); + printf(" 2=IRQ, 4=SHADOW_REG, 8=C70D_WRITES\n"); + printf(" r Reset\n"); + printf(" [0/1]=m Changes m bit for l listings\n"); + printf(" [0/1]=x Changes x bit for l listings\n"); + printf(" [t]=z Stops at absolute time t (obsolete)\n"); + printf(" S show_bankptr_bank0 & smartport errs\n"); + printf(" P show_pmhz\n"); + printf(" A show_a2_line_stuff show_adb_log\n"); + printf(" Ctrl-e Dump registers\n"); + printf("[bank]/[addr1].[addr2]us[file] Save mem area to [file]\n"); + printf("[bank]/[addr1].[addr2]ul[file] Load mem area from [file]\n"); + printf(" v Show video information\n"); + printf(" q Quit Debugger (and GSplus)\n"); } -void -do_debug_intfc() -{ - char linebuf[LINE_SIZE]; - int slot_drive; - int track; - int osc; - int done; - int ret_val; +void do_debug_intfc() { + char linebuf[LINE_SIZE]; + int slot_drive; + int track; + int osc; + int done; + int ret_val; - g_config_control_panel = 1; + g_config_control_panel = 1; - hex_line_len = 0x10; - a1 = 0; a2 = 0; a3 = 0; g_a4 = 0; - a1bank = 0; a2bank = 0; a3bank = 0; g_a4bank = 0; - list_kpc = engine.kpc; - g_stepping = 0; - mode = 0; old_mode = 0; - done = 0; - stop_run_at = -1; + hex_line_len = 0x10; + a1 = 0; a2 = 0; a3 = 0; g_a4 = 0; + a1bank = 0; a2bank = 0; a3bank = 0; g_a4bank = 0; + list_kpc = engine.kpc; + g_stepping = 0; + mode = 0; old_mode = 0; + done = 0; + stop_run_at = -1; - x_auto_repeat_on(0); - g_fullscreen = 0; - x_full_screen(0); + x_auto_repeat_on(0); + g_fullscreen = 0; + x_full_screen(0); - // OG use HALT_WANTTOQUIT instead of g_quit_sim_now - if (halt_sim&HALT_WANTTOQUIT) - { - printf("Exiting immediately\n"); - return; - } + // OG use HALT_WANTTOQUIT instead of g_quit_sim_now + if (halt_sim&HALT_WANTTOQUIT) + { + printf("Exiting immediately\n"); + return; + } - printf("Type 'h' for help\n"); + printf("Type 'h' for help\n"); - while(!done) { - printf("> "); fflush(stdout); - if(read_line(linebuf,LINE_SIZE-1) <= 0) { - done = 1; - continue; - } - line_ptr = linebuf; + while(!done) { + printf("> "); fflush(stdout); + if(read_line(linebuf,LINE_SIZE-1) <= 0) { + done = 1; + continue; + } + line_ptr = linebuf; /* - printf("input line: :%s:\n", linebuf); - printf("mode: %d\n", mode); -*/ - mode = 0; + printf("input line: :%s:\n", linebuf); + printf("mode: %d\n", mode); + */ + mode = 0; - while(*line_ptr != 0) { - ret_val = get_num(); + while(*line_ptr != 0) { + ret_val = get_num(); /* - printf("ret_val: %x, got_num= %d\n", ret_val, - got_num); -*/ - old_mode = mode; - mode = 0; - switch(ret_val) { - case 'h': - debugger_help(); - break; - case 'R': - show_dtime_array(); - show_all_events(); - break; - case 'I': - slot_drive = -1; - track = -1; - if(got_num) { - if(old_mode == '.') { - slot_drive = a1; - } - track = a2; - } - iwm_show_track(slot_drive, track); - iwm_show_stats(); - break; - case 'E': - osc = -1; - if(got_num) { - osc = a2; - } - doc_show_ensoniq_state(osc); - break; - case 'T': - if(got_num) { - show_toolset_tables(a2bank, a2); - } else { - show_toolbox_log(); - } - break; - case 'v': - if(got_num) { - dis_do_compare(); - } else { - video_show_debug_info(); - } - break; - case 'V': - printf("g_irq_pending: %05x\n", g_irq_pending); - printf("Setting Verbose ^= %04x\n", a1); - Verbose ^= a1; - printf("Verbose is now: %04x\n", Verbose); - break; - case 'H': - printf("Setting Halt_on ^= %04x\n", a1); - Halt_on ^= a1; - printf("Halt_on is now: %04x\n", Halt_on); - break; - case 'r': - do_reset(); - list_kpc = engine.kpc; - break; - case 'm': - if(old_mode == '=') { - if(!a1) { - engine.psr &= ~0x20; - } else { - engine.psr |= 0x20; - } - if(engine.psr & 0x100) { - engine.psr |= 0x30; - } - } else { - dis_do_memmove(); - } - break; - case 'p': - dis_do_pattern_search(); - break; - case 'x': - if(old_mode == '=') { - if(!a1) { - engine.psr &= ~0x10; - } else { - engine.psr |= 0x10; - } - if(engine.psr & 0x100) { - engine.psr |= 0x30; - } - } - break; - case 'z': - if(old_mode == '=') { - stop_run_at = a1; - printf("Calling add_event for t:%08x\n", - a1); - add_event_stop((double)a1); - printf("set stop_run_at = %x\n", a1); - } - break; - case 'l': case 'L': - do_debug_list(); - break; - case 'Z': - show_scc_log(); - show_scc_state(); - break; - case 'S': - show_bankptrs_bank0rdwr(); - smartport_error(); - break; - case 'C': - show_xcolor_array(); - break; - case 'P': - show_pc_log(); - break; - case 'M': - show_pmhz(); - break; - case 'A': - show_a2_line_stuff(); - show_adb_log(); - break; - case 's': - g_stepping = 1; - if(got_num) { - engine.kpc = (a2bank<<16) + (a2&0xffff); - } - mode = 's'; - list_kpc = engine.kpc; - break; - case 'B': - if(got_num) { - printf("got_num: %d, a2bank: %x, a2: %x\n", got_num, a2bank, a2); - set_bp((a2bank << 16) + a2); - } else { - show_bp(); - } - break; - case 'D': - if(got_num) { - printf("got_num: %d, a2bank: %x, a2: %x\n", got_num, a2bank, a2); - delete_bp((a2bank << 16) + a2); - } - break; - case 'g': - case 'G': - printf("Going..\n"); - g_stepping = 0; - if(got_num) { - engine.kpc = (a2bank<<16) + (a2&0xffff); - } - do_go(); - list_kpc = engine.kpc; - break; - case 'q': - case 'Q': - printf("Exiting debugger\n"); - return; - break; - case 'u': - printf("Unix commands\n"); - do_debug_unix(); - break; - case ':': case '.': - case '+': case '-': - case '=': case ',': - mode = ret_val; - printf("Setting mode = %x\n", mode); - break; - case ' ': case '\t': - if(!got_num) { - mode = old_mode; - break; - } - do_blank(); - break; - case '<': - g_a4 = a2; - g_a4bank = a2bank; - break; - case 0x05: /* ctrl-e */ - show_regs(); - break; - case '\n': - case '\r': - *line_ptr = 0; - if(old_mode == 's') { - do_blank(); - break; - } - if(line_ptr == &linebuf[1]) { - a2 = a1 | (hex_line_len - 1); - show_hex_mem(a1bank,a1,a2bank,a2, -1); - a1 = a2 + 1; - } else { - if(got_num == 1 || mode == 's') { - do_blank(); - } - } - break; - case 'w': - read_line(w_buff, W_BUF_LEN); - break; - default: - printf("\nUnrecognized command: %s\n",linebuf); - *line_ptr = 0; - break; - } - } + printf("ret_val: %x, got_num= %d\n", ret_val, + got_num); + */ + old_mode = mode; + mode = 0; + switch(ret_val) { + case 'h': + debugger_help(); + break; + case 'R': + show_dtime_array(); + show_all_events(); + break; + case 'I': + slot_drive = -1; + track = -1; + if(got_num) { + if(old_mode == '.') { + slot_drive = a1; + } + track = a2; + } + iwm_show_track(slot_drive, track); + iwm_show_stats(); + break; + case 'E': + osc = -1; + if(got_num) { + osc = a2; + } + doc_show_ensoniq_state(osc); + break; + case 'T': + if(got_num) { + show_toolset_tables(a2bank, a2); + } else { + show_toolbox_log(); + } + break; + case 'v': + if(got_num) { + dis_do_compare(); + } else { + video_show_debug_info(); + } + break; + case 'V': + printf("g_irq_pending: %05x\n", g_irq_pending); + printf("Setting Verbose ^= %04x\n", a1); + Verbose ^= a1; + printf("Verbose is now: %04x\n", Verbose); + break; + case 'H': + printf("Setting Halt_on ^= %04x\n", a1); + Halt_on ^= a1; + printf("Halt_on is now: %04x\n", Halt_on); + break; + case 'r': + do_reset(); + list_kpc = engine.kpc; + break; + case 'm': + if(old_mode == '=') { + if(!a1) { + engine.psr &= ~0x20; + } else { + engine.psr |= 0x20; + } + if(engine.psr & 0x100) { + engine.psr |= 0x30; + } + } else { + dis_do_memmove(); + } + break; + case 'p': + dis_do_pattern_search(); + break; + case 'x': + if(old_mode == '=') { + if(!a1) { + engine.psr &= ~0x10; + } else { + engine.psr |= 0x10; + } + if(engine.psr & 0x100) { + engine.psr |= 0x30; + } + } + break; + case 'z': + if(old_mode == '=') { + stop_run_at = a1; + printf("Calling add_event for t:%08x\n", + a1); + add_event_stop((double)a1); + printf("set stop_run_at = %x\n", a1); + } + break; + case 'l': case 'L': + do_debug_list(); + break; + case 'Z': + show_scc_log(); + show_scc_state(); + break; + case 'S': + show_bankptrs_bank0rdwr(); + smartport_error(); + break; + case 'C': + show_xcolor_array(); + break; + case 'P': + show_pc_log(); + break; + case 'M': + show_pmhz(); + break; + case 'A': + show_a2_line_stuff(); + show_adb_log(); + break; + case 's': + g_stepping = 1; + if(got_num) { + engine.kpc = (a2bank<<16) + (a2&0xffff); + } + mode = 's'; + list_kpc = engine.kpc; + break; + case 'B': + if(got_num) { + printf("got_num: %d, a2bank: %x, a2: %x\n", got_num, a2bank, a2); + set_bp((a2bank << 16) + a2); + } else { + show_bp(); + } + break; + case 'D': + if(got_num) { + printf("got_num: %d, a2bank: %x, a2: %x\n", got_num, a2bank, a2); + delete_bp((a2bank << 16) + a2); + } + break; + case 'g': + case 'G': + printf("Going..\n"); + g_stepping = 0; + if(got_num) { + engine.kpc = (a2bank<<16) + (a2&0xffff); + } + do_go(); + list_kpc = engine.kpc; + break; + case 'q': + case 'Q': + printf("Exiting debugger\n"); + return; + break; + case 'u': + printf("Unix commands\n"); + do_debug_unix(); + break; + case ':': case '.': + case '+': case '-': + case '=': case ',': + mode = ret_val; + printf("Setting mode = %x\n", mode); + break; + case ' ': case '\t': + if(!got_num) { + mode = old_mode; + break; + } + do_blank(); + break; + case '<': + g_a4 = a2; + g_a4bank = a2bank; + break; + case 0x05: /* ctrl-e */ + show_regs(); + break; + case '\n': + case '\r': + *line_ptr = 0; + if(old_mode == 's') { + do_blank(); + break; + } + if(line_ptr == &linebuf[1]) { + a2 = a1 | (hex_line_len - 1); + show_hex_mem(a1bank,a1,a2bank,a2, -1); + a1 = a2 + 1; + } else { + if(got_num == 1 || mode == 's') { + do_blank(); + } + } + break; + case 'w': + read_line(w_buff, W_BUF_LEN); + break; + default: + printf("\nUnrecognized command: %s\n",linebuf); + *line_ptr = 0; + break; + } + } - } - printf("Console closed.\n"); + } + printf("Console closed.\n"); } -word32 -dis_get_memory_ptr(word32 addr) -{ - word32 tmp1, tmp2, tmp3; +word32 dis_get_memory_ptr(word32 addr) { + word32 tmp1, tmp2, tmp3; - tmp1 = get_memory_c(addr, 0); - tmp2 = get_memory_c(addr + 1, 0); - tmp3 = get_memory_c(addr + 2, 0); + tmp1 = get_memory_c(addr, 0); + tmp2 = get_memory_c(addr + 1, 0); + tmp3 = get_memory_c(addr + 2, 0); - return (tmp3 << 16) + (tmp2 << 8) + tmp1; + return (tmp3 << 16) + (tmp2 << 8) + tmp1; } -void -show_one_toolset(FILE *toolfile, int toolnum, word32 addr) -{ - word32 rout_addr; - int num_routs; - int i; +void show_one_toolset(FILE *toolfile, int toolnum, word32 addr) { + word32 rout_addr; + int num_routs; + int i; - num_routs = dis_get_memory_ptr(addr); - fprintf(toolfile, "Tool 0x%02x, table: 0x%06x, num_routs:%03x\n", - toolnum, addr, num_routs); + num_routs = dis_get_memory_ptr(addr); + fprintf(toolfile, "Tool 0x%02x, table: 0x%06x, num_routs:%03x\n", + toolnum, addr, num_routs); - for(i = 1; i < num_routs; i++) { - rout_addr = dis_get_memory_ptr(addr + 4*i); - fprintf(toolfile, "%06x = %02x%02x\n", rout_addr, i, toolnum); - } + for(i = 1; i < num_routs; i++) { + rout_addr = dis_get_memory_ptr(addr + 4*i); + fprintf(toolfile, "%06x = %02x%02x\n", rout_addr, i, toolnum); + } } -void -show_toolset_tables(word32 a2bank, word32 addr) -{ - FILE *toolfile; - word32 tool_addr; - int num_tools; - int i; +void show_toolset_tables(word32 a2bank, word32 addr) { + FILE *toolfile; + word32 tool_addr; + int num_tools; + int i; - addr = (a2bank << 16) + (addr & 0xffff); + addr = (a2bank << 16) + (addr & 0xffff); - toolfile = fopen("tool_set_info", "w"); - if(toolfile == 0) { - fprintf(stderr, "fopen of tool_set_info failed: %d\n", errno); - exit(2); - } + toolfile = fopen("tool_set_info", "w"); + if(toolfile == 0) { + fprintf(stderr, "fopen of tool_set_info failed: %d\n", errno); + exit(2); + } - num_tools = dis_get_memory_ptr(addr); - fprintf(toolfile, "There are 0x%02x tools using ptr at %06x\n", - num_tools, addr); + num_tools = dis_get_memory_ptr(addr); + fprintf(toolfile, "There are 0x%02x tools using ptr at %06x\n", + num_tools, addr); - for(i = 1; i < num_tools; i++) { - tool_addr = dis_get_memory_ptr(addr + 4*i); - show_one_toolset(toolfile, i, tool_addr); - } + for(i = 1; i < num_tools; i++) { + tool_addr = dis_get_memory_ptr(addr + 4*i); + show_one_toolset(toolfile, i, tool_addr); + } - fclose(toolfile); + fclose(toolfile); } -void -set_bp(word32 addr) -{ - int count; +void set_bp(word32 addr) { + int count; - printf("About to set BP at %06x\n", addr); - count = g_num_breakpoints; - if(count >= MAX_BREAK_POINTS) { - printf("Too many (0x%02x) breakpoints set!\n", count); - return; - } + printf("About to set BP at %06x\n", addr); + count = g_num_breakpoints; + if(count >= MAX_BREAK_POINTS) { + printf("Too many (0x%02x) breakpoints set!\n", count); + return; + } - g_breakpts[count] = addr; - g_num_breakpoints = count + 1; - fixup_brks(); + g_breakpts[count] = addr; + g_num_breakpoints = count + 1; + fixup_brks(); } -void -show_bp() -{ - int i; +void show_bp() { + int i; - printf("Showing breakpoints set\n"); - for(i = 0; i < g_num_breakpoints; i++) { - printf("bp:%02x: %06x\n", i, g_breakpts[i]); - } + printf("Showing breakpoints set\n"); + for(i = 0; i < g_num_breakpoints; i++) { + printf("bp:%02x: %06x\n", i, g_breakpts[i]); + } } -void -delete_bp(word32 addr) -{ - int count; - int hit; - int i; +void delete_bp(word32 addr) { + int count; + int hit; + int i; - printf("About to delete BP at %06x\n", addr); - count = g_num_breakpoints; + printf("About to delete BP at %06x\n", addr); + count = g_num_breakpoints; - hit = -1; - for(i = 0; i < count; i++) { - if(g_breakpts[i] == addr) { - hit = i; - break; - } - } + hit = -1; + for(i = 0; i < count; i++) { + if(g_breakpts[i] == addr) { + hit = i; + break; + } + } - if(hit < 0) { - printf("Breakpoint not found!\n"); - } else { - printf("Deleting brkpoint #0x%02x\n", hit); - for(i = hit+1; i < count; i++) { - g_breakpts[i-1] = g_breakpts[i]; - } - g_num_breakpoints = count - 1; - setup_pageinfo(); - } + if(hit < 0) { + printf("Breakpoint not found!\n"); + } else { + printf("Deleting brkpoint #0x%02x\n", hit); + for(i = hit+1; i < count; i++) { + g_breakpts[i-1] = g_breakpts[i]; + } + g_num_breakpoints = count - 1; + setup_pageinfo(); + } - show_bp(); + show_bp(); } -void -do_blank() -{ - int tmp, i; +void do_blank() { + int tmp, i; - switch(old_mode) { - case 's': - tmp = a2; - if(tmp == 0) tmp = 1; - enter_debug = 0; - for(i = 0; i < tmp; i++) { - g_stepping = 1; - do_step(); - if(enter_debug || halt_sim != 0) { - if(halt_sim != HALT_EVENT) { - break; - } - } - } - list_kpc = engine.kpc; - /* video_update_through_line(262); */ - break; - case ':': - set_memory_c(((a3bank << 16) + a3), a2, 0); - a3++; - mode = old_mode; - break; - case '.': - case 0: - xam_mem(-1); - break; - case ',': - xam_mem(16); - break; - case '+': - printf("%x\n", a1 + a2); - break; - case '-': - printf("%x\n", a1 - a2); - break; - default: - printf("Unknown mode at space: %d\n", old_mode); - break; - } + switch(old_mode) { + case 's': + tmp = a2; + if(tmp == 0) tmp = 1; + enter_debug = 0; + for(i = 0; i < tmp; i++) { + g_stepping = 1; + do_step(); + if(enter_debug || halt_sim != 0) { + if(halt_sim != HALT_EVENT) { + break; + } + } + } + list_kpc = engine.kpc; + /* video_update_through_line(262); */ + break; + case ':': + set_memory_c(((a3bank << 16) + a3), a2, 0); + a3++; + mode = old_mode; + break; + case '.': + case 0: + xam_mem(-1); + break; + case ',': + xam_mem(16); + break; + case '+': + printf("%x\n", a1 + a2); + break; + case '-': + printf("%x\n", a1 - a2); + break; + default: + printf("Unknown mode at space: %d\n", old_mode); + break; + } } /* also called by do_step */ -void -do_go() -{ - g_config_control_panel = 0; - clear_halt(); - run_prog(); - show_regs(); - g_config_control_panel = 1; +void do_go() { + g_config_control_panel = 0; + clear_halt(); + run_prog(); + show_regs(); + g_config_control_panel = 1; } -void -do_step() -{ - int size; - int size_mem_imm, size_x_imm; +void do_step() { + int size; + int size_mem_imm, size_x_imm; - // run an instruction - do_go(); + // run an instruction + do_go(); - // check accumulator size - size_mem_imm = 2; - if(engine.psr & 0x20) { - size_mem_imm = 1; - } - // check xy size - size_x_imm = 2; - if(engine.psr & 0x10) { - size_x_imm = 1; - } - // then disassemble - size = do_dis(stdout, engine.kpc, size_mem_imm, size_x_imm, 0, 0); + // check accumulator size + size_mem_imm = 2; + if(engine.psr & 0x20) { + size_mem_imm = 1; + } + // check xy size + size_x_imm = 2; + if(engine.psr & 0x10) { + size_x_imm = 1; + } + // then disassemble + size = do_dis(stdout, engine.kpc, size_mem_imm, size_x_imm, 0, 0); } -void -xam_mem(int count) -{ - show_hex_mem(a1bank, a1, a2bank, a2, count); - a1 = a2 + 1; +void xam_mem(int count) { + show_hex_mem(a1bank, a1, a2bank, a2, count); + a1 = a2 + 1; } -void -show_hex_mem(int startbank, word32 start, int endbank, word32 end, int count) -{ - char ascii[MAXNUM_HEX_PER_LINE]; - word32 i; - int val, offset; +void show_hex_mem(int startbank, word32 start, int endbank, word32 end, int count) { + char ascii[MAXNUM_HEX_PER_LINE]; + word32 i; + int val, offset; - if(count < 0) { - count = 16 - (start & 0xf); - } + if(count < 0) { + count = 16 - (start & 0xf); + } - offset = 0; - ascii[0] = 0; - printf("Showing hex mem: bank: %x, start: %x, end: %x\n", - startbank, start, end); - for(i = start; i <= end; i++) { - if( (i==start) || (count == 16) ) { - printf("%04x:",i); - } - printf(" %02x", get_memory_c((startbank <<16) + i, 0)); - val = get_memory_c((startbank << 16) + i, 0) & 0x7f; - if(val < 32 || val >= 0x7f) { - val = '.'; - } - ascii[offset++] = val; - ascii[offset] = 0; - count--; - if(count <= 0) { - printf(" %s\n", ascii); - offset = 0; - ascii[0] = 0; - count = 16; - } - } - if(offset > 0) { - printf(" %s\n", ascii); - } + offset = 0; + ascii[0] = 0; + printf("Showing hex mem: bank: %x, start: %x, end: %x\n", + startbank, start, end); + for(i = start; i <= end; i++) { + if( (i==start) || (count == 16) ) { + printf("%04x:",i); + } + printf(" %02x", get_memory_c((startbank <<16) + i, 0)); + val = get_memory_c((startbank << 16) + i, 0) & 0x7f; + if(val < 32 || val >= 0x7f) { + val = '.'; + } + ascii[offset++] = val; + ascii[offset] = 0; + count--; + if(count <= 0) { + printf(" %s\n", ascii); + offset = 0; + ascii[0] = 0; + count = 16; + } + } + if(offset > 0) { + printf(" %s\n", ascii); + } } -int -read_line(char *buf, int len) -{ - int space_left; - int ret; +int read_line(char *buf, int len) { + int space_left; + int ret; #if !defined(_WIN32) - int flags, flags_save; + int flags, flags_save; - /* Unix */ - flags = fcntl(0, F_GETFL, 0); - flags_save = flags; - if(flags == -1) { - return 0; - } - ret = fcntl(0, F_SETFL, flags | O_NONBLOCK); - if(ret == -1) { - return 0; - } + /* Unix */ + flags = fcntl(0, F_GETFL, 0); + flags_save = flags; + if(flags == -1) { + return 0; + } + ret = fcntl(0, F_SETFL, flags | O_NONBLOCK); + if(ret == -1) { + return 0; + } #endif - space_left = len; + space_left = len; - buf[0] = 0; - ret = 0; - while(space_left > 0) { + buf[0] = 0; + ret = 0; + while(space_left > 0) { #ifdef _WIN32 - ret = win_nonblock_read_stdin(0, buf, 1); + ret = win_nonblock_read_stdin(0, buf, 1); #else - /* Unix */ - ret = read(0, buf, 1); + /* Unix */ + ret = read(0, buf, 1); #endif - if(ret <= 0) { - micro_sleep(15.0/60.0); - if(errno == EAGAIN) { - /* it would block, so no chars--do update */ - video_update(); - ret = 0; - continue; - } - printf("read ret %d, errno: %d\n", ret, errno); - if(errno == EAGAIN || errno == EINTR) { - ret = 0; - continue; - } - break; - } - space_left -= ret; - if(buf[ret-1] == '\r' || buf[ret-1] == '\n') { - break; - } - buf = &buf[ret]; - } + if(ret <= 0) { + micro_sleep(15.0/60.0); + if(errno == EAGAIN) { + /* it would block, so no chars--do update */ + video_update(); + ret = 0; + continue; + } + printf("read ret %d, errno: %d\n", ret, errno); + if(errno == EAGAIN || errno == EINTR) { + ret = 0; + continue; + } + break; + } + space_left -= ret; + if(buf[ret-1] == '\r' || buf[ret-1] == '\n') { + break; + } + buf = &buf[ret]; + } #if !defined(_WIN32) - (void)fcntl(0, F_SETFL, flags_save); + (void)fcntl(0, F_SETFL, flags_save); #endif - return (len-space_left); + return (len-space_left); } -void -do_debug_list() -{ - int i; - int size; - int size_mem_imm, size_x_imm; +void do_debug_list() { + int i; + int size; + int size_mem_imm, size_x_imm; - if(got_num) { - list_kpc = (a2bank << 16) + (a2 & 0xffff); - } - printf("%d=m %d=x %d=LCBANK\n", (engine.psr >> 5)&1, - (engine.psr >> 4) & 1, (g_c068_statereg & 0x4) >> 2); + if(got_num) { + list_kpc = (a2bank << 16) + (a2 & 0xffff); + } + printf("%d=m %d=x %d=LCBANK\n", (engine.psr >> 5)&1, + (engine.psr >> 4) & 1, (g_c068_statereg & 0x4) >> 2); - size_mem_imm = 2; - if(engine.psr & 0x20) { - size_mem_imm = 1; - } - size_x_imm = 2; - if(engine.psr & 0x10) { - size_x_imm = 1; - } - for(i=0;i<20;i++) { - size = do_dis(stdout, list_kpc, size_mem_imm, - size_x_imm, 0, 0); - list_kpc += size; - } + size_mem_imm = 2; + if(engine.psr & 0x20) { + size_mem_imm = 1; + } + size_x_imm = 2; + if(engine.psr & 0x10) { + size_x_imm = 1; + } + for(i=0; i<20; i++) { + size = do_dis(stdout, list_kpc, size_mem_imm, + size_x_imm, 0, 0); + list_kpc += size; + } } -void -dis_do_memmove() -{ - word32 val; +void dis_do_memmove() { + word32 val; - printf("Memory move from %02x/%04x.%04x to %02x/%04x\n", a1bank, a1, a2, g_a4bank, g_a4); - while(a1 <= (a2 & 0xffff)) { - val = get_memory_c((a1bank << 16) + a1, 0); - set_memory_c((g_a4bank << 16) + g_a4, val, 0); - a1++; - g_a4++; - } - a1 = a1 & 0xffff; - g_a4 = g_a4 & 0xffff; + printf("Memory move from %02x/%04x.%04x to %02x/%04x\n", a1bank, a1, a2, g_a4bank, g_a4); + while(a1 <= (a2 & 0xffff)) { + val = get_memory_c((a1bank << 16) + a1, 0); + set_memory_c((g_a4bank << 16) + g_a4, val, 0); + a1++; + g_a4++; + } + a1 = a1 & 0xffff; + g_a4 = g_a4 & 0xffff; } -void -dis_do_pattern_search() -{ - printf("Memory pattern search for %04x in %02x/%04x.%04x\n", g_a4, a1bank, a1, a2); +void dis_do_pattern_search() { + printf("Memory pattern search for %04x in %02x/%04x.%04x\n", g_a4, a1bank, a1, a2); } -void -dis_do_compare() -{ - word32 val1, val2; +void dis_do_compare() { + word32 val1, val2; - printf("Memory Compare from %02x/%04x.%04x with %02x/%04x\n", a1bank, a1, a2, g_a4bank, g_a4); - while(a1 <= (a2 & 0xffff)) { - val1 = get_memory_c((a1bank << 16) + a1, 0); - val2 = get_memory_c((g_a4bank << 16) + g_a4, 0); - if(val1 != val2) { - printf("%02x/%04x: %02x vs %02x\n", a1bank, a1, val1, val2); - } - a1++; - g_a4++; - } - a1 = a1 & 0xffff; - g_a4 = g_a4 & 0xffff; + printf("Memory Compare from %02x/%04x.%04x with %02x/%04x\n", a1bank, a1, a2, g_a4bank, g_a4); + while(a1 <= (a2 & 0xffff)) { + val1 = get_memory_c((a1bank << 16) + a1, 0); + val2 = get_memory_c((g_a4bank << 16) + g_a4, 0); + if(val1 != val2) { + printf("%02x/%04x: %02x vs %02x\n", a1bank, a1, val1, val2); + } + a1++; + g_a4++; + } + a1 = a1 & 0xffff; + g_a4 = g_a4 & 0xffff; } -void -do_debug_unix() -{ - char localbuf[LINE_SIZE]; - word32 offset, len; - int fd, ret; - int load, save; - int i; +void do_debug_unix() { + char localbuf[LINE_SIZE]; + word32 offset, len; + int fd, ret; + int load, save; + int i; - load = 0; save = 0; - switch(*line_ptr++) { - case 'l': case 'L': - printf("Loading.."); - load = 1; - break; - case 's': case 'S': - printf("Saving..."); - save = 1; - break; - default: - printf("Unknown unix command: %c\n", *(line_ptr-1)); - *line_ptr = 0; - return; - } - while(*line_ptr == ' ' || *line_ptr == '\t') { - line_ptr++; - } - i = 0; - while(i < LINE_SIZE) { - localbuf[i++] = *line_ptr++; - if(*line_ptr==' ' || *line_ptr=='\t' || *line_ptr == '\n') { - break; - } - } - localbuf[i] = 0; + load = 0; save = 0; + switch(*line_ptr++) { + case 'l': case 'L': + printf("Loading.."); + load = 1; + break; + case 's': case 'S': + printf("Saving..."); + save = 1; + break; + default: + printf("Unknown unix command: %c\n", *(line_ptr-1)); + *line_ptr = 0; + return; + } + while(*line_ptr == ' ' || *line_ptr == '\t') { + line_ptr++; + } + i = 0; + while(i < LINE_SIZE) { + localbuf[i++] = *line_ptr++; + if(*line_ptr==' ' || *line_ptr=='\t' || *line_ptr == '\n') { + break; + } + } + localbuf[i] = 0; - printf("About to open: %s,len: %d\n", localbuf, (int)strlen(localbuf)); - if(load) { - fd = open(localbuf,O_RDONLY | O_BINARY); - } else { - fd = open(localbuf,O_WRONLY | O_CREAT | O_BINARY, 0x1b6); - } - if(fd < 0) { - printf("Open %s failed: %d\n", localbuf, fd); - printf("errno: %d\n", errno); - return; - } - if(load) { - offset = a1 & 0xffff; - len = 0x20000 - offset; - } else { - if(old_mode == '.') { - len = a2 - a1 + 1; - } else { - len = 0x100; - } - } - if(load) { - if(a1bank >= 0xe0 && a1bank < 0xe2) { - ret = read(fd,(char*)&g_slow_memory_ptr[((a1bank & 1)<<16)+a1],len); - } else { - ret = read(fd,(char*)&g_memory_ptr[(a1bank << 16) + a1],len); - } - } else { - if(a1bank >= 0xe0 && a1bank < 0xe2) { - ret = write(fd,(char*)&g_slow_memory_ptr[((a1bank & 1)<<16)+a1],len); - } else { - ret = write(fd,(char*)&g_memory_ptr[(a1bank << 16) + a1],len); - } - } - printf("Read/write: addr %06x for %04x bytes, ret: %x bytes\n", - (a1bank << 16) + a1, len, ret); - if(ret < 0) { - printf("errno: %d\n", errno); - } - a1 = a1 + ret; + printf("About to open: %s,len: %d\n", localbuf, (int)strlen(localbuf)); + if(load) { + fd = open(localbuf,O_RDONLY | O_BINARY); + } else { + fd = open(localbuf,O_WRONLY | O_CREAT | O_BINARY, 0x1b6); + } + if(fd < 0) { + printf("Open %s failed: %d\n", localbuf, fd); + printf("errno: %d\n", errno); + return; + } + if(load) { + offset = a1 & 0xffff; + len = 0x20000 - offset; + } else { + if(old_mode == '.') { + len = a2 - a1 + 1; + } else { + len = 0x100; + } + } + if(load) { + if(a1bank >= 0xe0 && a1bank < 0xe2) { + ret = read(fd,(char*)&g_slow_memory_ptr[((a1bank & 1)<<16)+a1],len); + } else { + ret = read(fd,(char*)&g_memory_ptr[(a1bank << 16) + a1],len); + } + } else { + if(a1bank >= 0xe0 && a1bank < 0xe2) { + ret = write(fd,(char*)&g_slow_memory_ptr[((a1bank & 1)<<16)+a1],len); + } else { + ret = write(fd,(char*)&g_memory_ptr[(a1bank << 16) + a1],len); + } + } + printf("Read/write: addr %06x for %04x bytes, ret: %x bytes\n", + (a1bank << 16) + a1, len, ret); + if(ret < 0) { + printf("errno: %d\n", errno); + } + a1 = a1 + ret; } -void -do_debug_load() -{ - printf("Sorry, can't load now\n"); +void do_debug_load() { + printf("Sorry, can't load now\n"); } -int -do_dis(FILE *outfile, word32 kpc, int accsize, int xsize, - int op_provided, word32 instr) -{ - char buffer[150]; - const char *out; - int args, type; - int opcode; - word32 val; - word32 oldkpc; - word32 dtype; - int signed_val; +int do_dis(FILE *outfile, word32 kpc, int accsize, int xsize, + int op_provided, word32 instr) { + char buffer[150]; + const char *out; + int args, type; + int opcode; + word32 val; + word32 oldkpc; + word32 dtype; + int signed_val; - oldkpc = kpc; - if(op_provided) { - opcode = (instr >> 24) & 0xff; - } else { - opcode = (int)get_memory_c(kpc, 0) & 0xff; - } + oldkpc = kpc; + if(op_provided) { + opcode = (instr >> 24) & 0xff; + } else { + opcode = (int)get_memory_c(kpc, 0) & 0xff; + } - kpc++; + kpc++; - dtype = disas_types[opcode]; - out = disas_opcodes[opcode]; - type = dtype & 0xff; - args = dtype >> 8; + dtype = disas_types[opcode]; + out = disas_opcodes[opcode]; + type = dtype & 0xff; + args = dtype >> 8; - if(args > 3) { - if(args == 4) { - args = accsize; - } else if(args == 5) { - args = xsize; - } - } + if(args > 3) { + if(args == 4) { + args = accsize; + } else if(args == 5) { + args = xsize; + } + } - val = -1; - switch(args) { - case 0: - val = 0; - break; - case 1: - if(op_provided) { - val = instr & 0xff; - } else { - val = get_memory_c(kpc, 0); - } - break; - case 2: - if(op_provided) { - val = instr & 0xffff; - } else { - val = get_memory16_c(kpc, 0); - } - break; - case 3: - if(op_provided) { - val = instr & 0xffffff; - } else { - val = get_memory24_c(kpc, 0); - } - break; - default: - fprintf(stderr, "args out of rang: %d, opcode: %08x\n", - args, opcode); - break; - } - kpc += args; + val = -1; + switch(args) { + case 0: + val = 0; + break; + case 1: + if(op_provided) { + val = instr & 0xff; + } else { + val = get_memory_c(kpc, 0); + } + break; + case 2: + if(op_provided) { + val = instr & 0xffff; + } else { + val = get_memory16_c(kpc, 0); + } + break; + case 3: + if(op_provided) { + val = instr & 0xffffff; + } else { + val = get_memory24_c(kpc, 0); + } + break; + default: + fprintf(stderr, "args out of rang: %d, opcode: %08x\n", + args, opcode); + break; + } + kpc += args; - if(!op_provided) { - instr = (opcode << 24) | (val & 0xffffff); - } + if(!op_provided) { + instr = (opcode << 24) | (val & 0xffffff); + } - switch(type) { - case ABS: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%04x",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case ABSX: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%04x,X",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case ABSY: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%04x,Y",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case ABSLONG: - if(args != 3) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%06x",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case ABSIND: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t($%04x)",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case ABSXIND: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t($%04x,X)",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case IMPLY: - if(args != 0) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s",out); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case ACCUM: - if(args != 0) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s",out); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case IMMED: - if(args == 1) { - sprintf(buffer,"%s\t#$%02x",out,val); - } else if(args == 2) { - sprintf(buffer,"%s\t#$%04x",out,val); - } else { - printf("arg # mismatch for opcode %x\n", opcode); - } - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case JUST8: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%02x",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DLOC: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%02x",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DLOCX: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%02x,X",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DLOCY: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%02x,Y",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case LONG: - if(args != 3) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%06x",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case LONGX: - if(args != 3) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%06x,X",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DLOCIND: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t($%02x)",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DLOCINDY: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t($%02x),Y",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DLOCXIND: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t($%02x,X)",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DLOCBRAK: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t[$%02x]",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DLOCBRAKY: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t[$%02x],y",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DISP8: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - signed_val = (signed char)val; - sprintf(buffer,"%s\t$%04x",out, - (word32)(kpc+(signed_val)) & 0xffff); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DISP8S: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%02x,S",out,(word32)(byte)(val)); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DISP8SINDY: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t($%02x,S),Y",out,(word32)(byte)(val)); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case DISP16: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%04x", out, - (word32)(kpc+(signed)(word16)(val)) & 0xffff); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case MVPMVN: - if(args != 2) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t$%02x,$%02x",out,val&0xff,val>>8); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - case SEPVAL: - case REPVAL: - if(args != 1) { - printf("arg # mismatch for opcode %x\n", opcode); - } - sprintf(buffer,"%s\t#$%02x",out,val); - show_line(outfile, oldkpc,instr,args+1,buffer); - break; - default: - printf("argument type: %d unexpected\n", type); - break; - } + switch(type) { + case ABS: + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%04x",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case ABSX: + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%04x,X",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case ABSY: + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%04x,Y",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case ABSLONG: + if(args != 3) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%06x",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case ABSIND: + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t($%04x)",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case ABSXIND: + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t($%04x,X)",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case IMPLY: + if(args != 0) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s",out); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case ACCUM: + if(args != 0) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s",out); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case IMMED: + if(args == 1) { + sprintf(buffer,"%s\t#$%02x",out,val); + } else if(args == 2) { + sprintf(buffer,"%s\t#$%04x",out,val); + } else { + printf("arg # mismatch for opcode %x\n", opcode); + } + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case JUST8: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%02x",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DLOC: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%02x",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DLOCX: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%02x,X",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DLOCY: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%02x,Y",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case LONG: + if(args != 3) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%06x",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case LONGX: + if(args != 3) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%06x,X",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DLOCIND: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t($%02x)",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DLOCINDY: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t($%02x),Y",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DLOCXIND: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t($%02x,X)",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DLOCBRAK: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t[$%02x]",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DLOCBRAKY: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t[$%02x],y",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DISP8: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + signed_val = (signed char)val; + sprintf(buffer,"%s\t$%04x",out, + (word32)(kpc+(signed_val)) & 0xffff); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DISP8S: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%02x,S",out,(word32)(byte)(val)); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DISP8SINDY: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t($%02x,S),Y",out,(word32)(byte)(val)); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case DISP16: + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%04x", out, + (word32)(kpc+(signed)(word16)(val)) & 0xffff); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case MVPMVN: + if(args != 2) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t$%02x,$%02x",out,val&0xff,val>>8); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + case SEPVAL: + case REPVAL: + if(args != 1) { + printf("arg # mismatch for opcode %x\n", opcode); + } + sprintf(buffer,"%s\t#$%02x",out,val); + show_line(outfile, oldkpc,instr,args+1,buffer); + break; + default: + printf("argument type: %d unexpected\n", type); + break; + } - return(args+1); + return(args+1); } -void -show_line(FILE *outfile, word32 kaddr, word32 operand, int size, - char *string) -{ - int i; - int opcode; +void show_line(FILE *outfile, word32 kaddr, word32 operand, int size, + char *string) { + int i; + int opcode; - fprintf(outfile, "%02x/%04x: ", kaddr >> 16, kaddr & 0xffff); - opcode = (operand >> 24) & 0xff; - fprintf(outfile,"%02x ", opcode); + fprintf(outfile, "%02x/%04x: ", kaddr >> 16, kaddr & 0xffff); + opcode = (operand >> 24) & 0xff; + fprintf(outfile,"%02x ", opcode); - for(i=1;i> 8; - } - for(;i<5;i++) { - fprintf(outfile, " "); - } - fprintf(outfile,"%s\n", string); + for(i=1; i> 8; + } + for(; i<5; i++) { + fprintf(outfile, " "); + } + fprintf(outfile,"%s\n", string); } -void -halt_printf(const char *fmt, ...) -{ - va_list args; +void halt_printf(const char *fmt, ...) { + va_list args; - va_start(args, fmt); - vprintf(fmt, args); - va_end(args); + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); - set_halt(1); + set_halt(1); } -void -halt2_printf(const char *fmt, ...) -{ - va_list args; +void halt2_printf(const char *fmt, ...) { + va_list args; - va_start(args, fmt); - vprintf(fmt, args); - va_end(args); + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); - set_halt(2); + set_halt(2); } diff --git a/src/engine_c.c b/src/engine_c.c index 0d8ca08..9372be2 100644 --- a/src/engine_c.c +++ b/src/engine_c.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include "protos_engine_c.h" @@ -17,7 +17,7 @@ /* define FCYCS_PTR_FCYCLES_ROUND_SLOW to get accurate 1MHz write to slow mem*/ /* this might help joystick emulation in some Apple //gs games like */ /* Madness */ -# define FCYCS_PTR_FCYCLES_ROUND_SLOW FCYCLES_ROUND; *fcycs_ptr = fcycles; +# define FCYCS_PTR_FCYCLES_ROUND_SLOW FCYCLES_ROUND; *fcycs_ptr = fcycles; #endif #ifndef FCYCS_PTR_FCYCLES_ROUND_SLOW @@ -42,7 +42,7 @@ extern byte *g_memory_ptr; extern byte *g_rom_fc_ff_ptr; extern byte *g_rom_cards_ptr; extern byte *g_dummy_memory1_ptr; -extern int g_c068_statereg; +extern int g_c068_statereg; unsigned char ioslotsel = 0; unsigned char iostrobe = 0; @@ -69,53 +69,53 @@ int size_tab[] = { }; int bogus[] = { - 0, + 0, #include "op_routs.h" }; -#define FINISH(arg1, arg2) g_ret1 = arg1; g_ret2 = arg2; goto finish; -#define INC_KPC_1 kpc = (kpc & 0xff0000) + ((kpc + 1) & 0xffff); -#define INC_KPC_2 kpc = (kpc & 0xff0000) + ((kpc + 2) & 0xffff); -#define INC_KPC_3 kpc = (kpc & 0xff0000) + ((kpc + 3) & 0xffff); -#define INC_KPC_4 kpc = (kpc & 0xff0000) + ((kpc + 4) & 0xffff); +#define FINISH(arg1, arg2) g_ret1 = arg1; g_ret2 = arg2; goto finish; +#define INC_KPC_1 kpc = (kpc & 0xff0000) + ((kpc + 1) & 0xffff); +#define INC_KPC_2 kpc = (kpc & 0xff0000) + ((kpc + 2) & 0xffff); +#define INC_KPC_3 kpc = (kpc & 0xff0000) + ((kpc + 3) & 0xffff); +#define INC_KPC_4 kpc = (kpc & 0xff0000) + ((kpc + 4) & 0xffff); -#define CYCLES_PLUS_1 fcycles += fplus_1; -#define CYCLES_PLUS_2 fcycles += fplus_2; -#define CYCLES_PLUS_3 fcycles += fplus_3; -#define CYCLES_PLUS_4 fcycles += (fplus_1 + fplus_3); -#define CYCLES_PLUS_5 fcycles += (fplus_2 + fplus_3); -#define CYCLES_MINUS_1 fcycles -= fplus_1; -#define CYCLES_MINUS_2 fcycles -= fplus_2; +#define CYCLES_PLUS_1 fcycles += fplus_1; +#define CYCLES_PLUS_2 fcycles += fplus_2; +#define CYCLES_PLUS_3 fcycles += fplus_3; +#define CYCLES_PLUS_4 fcycles += (fplus_1 + fplus_3); +#define CYCLES_PLUS_5 fcycles += (fplus_2 + fplus_3); +#define CYCLES_MINUS_1 fcycles -= fplus_1; +#define CYCLES_MINUS_2 fcycles -= fplus_2; -#define CYCLES_FINISH fcycles = g_fcycles_stop + fplus_1; +#define CYCLES_FINISH fcycles = g_fcycles_stop + fplus_1; -#define FCYCLES_ROUND fcycles = (int)(fcycles + fplus_x_m1); +#define FCYCLES_ROUND fcycles = (int)(fcycles + fplus_x_m1); #ifdef LOG_PC -# define LOG_PC_MACRO() \ - tmp_pc_ptr = g_log_pc_ptr++; \ - tmp_pc_ptr->dbank_kpc = (dbank << 24) + kpc; \ - tmp_pc_ptr->instr = (opcode << 24) + arg_ptr[1] + \ - (arg_ptr[2] << 8) + (arg_ptr[3] << 16); \ - tmp_pc_ptr->psr_acc = ((psr & ~(0x82)) << 16) + acc + \ - (neg << 23) + ((!zero) << 17); \ - tmp_pc_ptr->xreg_yreg = (xreg << 16) + yreg; \ - tmp_pc_ptr->stack_direct = (stack << 16) + direct; \ - tmp_pc_ptr->dcycs = fcycles + g_last_vbl_dcycs - fplus_2; \ - if(g_log_pc_ptr >= g_log_pc_end_ptr) { \ - /*halt2_printf("log_pc oflow %f\n", tmp_pc_ptr->dcycs);*/ \ - g_log_pc_ptr = g_log_pc_start_ptr; \ - } +# define LOG_PC_MACRO() \ + tmp_pc_ptr = g_log_pc_ptr++; \ + tmp_pc_ptr->dbank_kpc = (dbank << 24) + kpc; \ + tmp_pc_ptr->instr = (opcode << 24) + arg_ptr[1] + \ + (arg_ptr[2] << 8) + (arg_ptr[3] << 16); \ + tmp_pc_ptr->psr_acc = ((psr & ~(0x82)) << 16) + acc + \ + (neg << 23) + ((!zero) << 17); \ + tmp_pc_ptr->xreg_yreg = (xreg << 16) + yreg; \ + tmp_pc_ptr->stack_direct = (stack << 16) + direct; \ + tmp_pc_ptr->dcycs = fcycles + g_last_vbl_dcycs - fplus_2; \ + if(g_log_pc_ptr >= g_log_pc_end_ptr) { \ + /*halt2_printf("log_pc oflow %f\n", tmp_pc_ptr->dcycs);*/ \ + g_log_pc_ptr = g_log_pc_start_ptr; \ + } -# define LOG_DATA_MACRO(in_addr, in_val, in_size) \ - g_log_data_ptr->dcycs = fcycles + g_last_vbl_dcycs; \ - g_log_data_ptr->addr = in_addr; \ - g_log_data_ptr->val = in_val; \ - g_log_data_ptr->size = in_size; \ - g_log_data_ptr++; \ - if(g_log_data_ptr >= g_log_data_end_ptr) { \ - g_log_data_ptr = g_log_data_start_ptr; \ - } +# define LOG_DATA_MACRO(in_addr, in_val, in_size) \ + g_log_data_ptr->dcycs = fcycles + g_last_vbl_dcycs; \ + g_log_data_ptr->addr = in_addr; \ + g_log_data_ptr->val = in_val; \ + g_log_data_ptr->size = in_size; \ + g_log_data_ptr++; \ + if(g_log_data_ptr >= g_log_data_end_ptr) { \ + g_log_data_ptr = g_log_data_start_ptr; \ + } #else # define LOG_PC_MACRO() @@ -124,953 +124,906 @@ int bogus[] = { #endif -#define GET_1BYTE_ARG arg = arg_ptr[1]; -#define GET_2BYTE_ARG arg = arg_ptr[1] + (arg_ptr[2] << 8); -#define GET_3BYTE_ARG arg = arg_ptr[1] + (arg_ptr[2] << 8) + (arg_ptr[3]<<16); +#define GET_1BYTE_ARG arg = arg_ptr[1]; +#define GET_2BYTE_ARG arg = arg_ptr[1] + (arg_ptr[2] << 8); +#define GET_3BYTE_ARG arg = arg_ptr[1] + (arg_ptr[2] << 8) + (arg_ptr[3]<<16); /* HACK HACK HACK */ -#define UPDATE_PSR(dummy, old_psr) \ - if(psr & 0x100) { \ - psr |= 0x30; \ - stack = 0x100 + (stack & 0xff); \ - } \ - if((old_psr ^ psr) & 0x10) { \ - if(psr & 0x10) { \ - xreg = xreg & 0xff; \ - yreg = yreg & 0xff; \ - } \ - } \ - if(((psr & 0x4) == 0) && g_irq_pending) { \ - FINISH(RET_IRQ, 0); \ - } \ - if((old_psr ^ psr) & 0x20) { \ - goto recalc_accsize; \ - } +#define UPDATE_PSR(dummy, old_psr) \ + if(psr & 0x100) { \ + psr |= 0x30; \ + stack = 0x100 + (stack & 0xff); \ + } \ + if((old_psr ^ psr) & 0x10) { \ + if(psr & 0x10) { \ + xreg = xreg & 0xff; \ + yreg = yreg & 0xff; \ + } \ + } \ + if(((psr & 0x4) == 0) && g_irq_pending) { \ + FINISH(RET_IRQ, 0); \ + } \ + if((old_psr ^ psr) & 0x20) { \ + goto recalc_accsize; \ + } extern Page_info page_info_rd_wr[]; extern word32 slow_mem_changed[]; -#define GET_MEMORY8(addr,dest) \ - addr_latch = (addr);\ - CYCLES_PLUS_1; \ - stat = GET_PAGE_INFO_RD(((addr) >> 8) & 0xffff); \ - wstat = PTR2WORD(stat) & 0xff; \ - ptr = stat - wstat + ((addr) & 0xff); \ - if(wstat & (1 << (31 - BANK_IO_BIT)) || iostrobe == 1) { \ - fcycles_tmp1 = fcycles; \ - dest = get_memory8_io_stub((addr), stat, \ - &fcycles_tmp1, fplus_x_m1); \ - fcycles = fcycles_tmp1; \ - } else { \ - dest = *ptr; \ - } +#define GET_MEMORY8(addr,dest) \ + addr_latch = (addr); \ + CYCLES_PLUS_1; \ + stat = GET_PAGE_INFO_RD(((addr) >> 8) & 0xffff); \ + wstat = PTR2WORD(stat) & 0xff; \ + ptr = stat - wstat + ((addr) & 0xff); \ + if(wstat & (1 << (31 - BANK_IO_BIT)) || iostrobe == 1) { \ + fcycles_tmp1 = fcycles; \ + dest = get_memory8_io_stub((addr), stat, \ + &fcycles_tmp1, fplus_x_m1); \ + fcycles = fcycles_tmp1; \ + } else { \ + dest = *ptr; \ + } -#define GET_MEMORY(addr,dest) GET_MEMORY8(addr, dest) +#define GET_MEMORY(addr,dest) GET_MEMORY8(addr, dest) -#define GET_MEMORY16(addr, dest, in_bank) \ - save_addr = addr; \ - stat = GET_PAGE_INFO_RD(((addr) >> 8) & 0xffff); \ - wstat = PTR2WORD(stat) & 0xff; \ - ptr = stat - wstat + ((addr) & 0xff); \ - if((wstat & (1 << (31 - BANK_IO_BIT))) || (((addr) & 0xff) == 0xff)) { \ - fcycles_tmp1 = fcycles; \ - dest = get_memory16_pieces_stub((addr), stat, \ - &fcycles_tmp1, fplus_ptr, in_bank); \ - fcycles = fcycles_tmp1; \ - } else { \ - CYCLES_PLUS_2; \ - dest = ptr[0] + (ptr[1] << 8); \ - } \ - addr_latch = save_addr; +#define GET_MEMORY16(addr, dest, in_bank) \ + save_addr = addr; \ + stat = GET_PAGE_INFO_RD(((addr) >> 8) & 0xffff); \ + wstat = PTR2WORD(stat) & 0xff; \ + ptr = stat - wstat + ((addr) & 0xff); \ + if((wstat & (1 << (31 - BANK_IO_BIT))) || (((addr) & 0xff) == 0xff)) { \ + fcycles_tmp1 = fcycles; \ + dest = get_memory16_pieces_stub((addr), stat, \ + &fcycles_tmp1, fplus_ptr, in_bank); \ + fcycles = fcycles_tmp1; \ + } else { \ + CYCLES_PLUS_2; \ + dest = ptr[0] + (ptr[1] << 8); \ + } \ + addr_latch = save_addr; -#define GET_MEMORY24(addr, dest, in_bank) \ - save_addr = addr; \ - stat = GET_PAGE_INFO_RD(((addr) >> 8) & 0xffff); \ - wstat = PTR2WORD(stat) & 0xff; \ - ptr = stat - wstat + ((addr) & 0xff); \ - if((wstat & (1 << (31 - BANK_IO_BIT))) || (((addr) & 0xfe) == 0xfe)) { \ - fcycles_tmp1 = fcycles; \ - dest = get_memory24_pieces_stub((addr), stat, \ - &fcycles_tmp1, fplus_ptr, in_bank); \ - fcycles = fcycles_tmp1; \ - } else { \ - CYCLES_PLUS_3; \ - dest = ptr[0] + (ptr[1] << 8) + (ptr[2] << 16); \ - } \ - addr_latch = save_addr; +#define GET_MEMORY24(addr, dest, in_bank) \ + save_addr = addr; \ + stat = GET_PAGE_INFO_RD(((addr) >> 8) & 0xffff); \ + wstat = PTR2WORD(stat) & 0xff; \ + ptr = stat - wstat + ((addr) & 0xff); \ + if((wstat & (1 << (31 - BANK_IO_BIT))) || (((addr) & 0xfe) == 0xfe)) { \ + fcycles_tmp1 = fcycles; \ + dest = get_memory24_pieces_stub((addr), stat, \ + &fcycles_tmp1, fplus_ptr, in_bank); \ + fcycles = fcycles_tmp1; \ + } else { \ + CYCLES_PLUS_3; \ + dest = ptr[0] + (ptr[1] << 8) + (ptr[2] << 16); \ + } \ + addr_latch = save_addr; -#define GET_MEMORY_DIRECT_PAGE16(addr, dest) \ - save_addr = addr; \ - if(psr & 0x100) { \ - if((direct & 0xff) == 0) { \ - save_addr = (save_addr & 0xff) + direct; \ - } \ - } \ - if((psr & 0x100) && (((addr) & 0xff) == 0xff)) { \ - GET_MEMORY8(save_addr, getmem_tmp); \ - save_addr = (save_addr + 1) & 0xffff; \ - if((direct & 0xff) == 0) { \ - save_addr = (save_addr & 0xff) + direct; \ - } \ - GET_MEMORY8(save_addr, dest); \ - dest = (dest << 8) + getmem_tmp; \ - } else { \ - GET_MEMORY16(save_addr, dest, 1); \ - } +#define GET_MEMORY_DIRECT_PAGE16(addr, dest) \ + save_addr = addr; \ + if(psr & 0x100) { \ + if((direct & 0xff) == 0) { \ + save_addr = (save_addr & 0xff) + direct; \ + } \ + } \ + if((psr & 0x100) && (((addr) & 0xff) == 0xff)) { \ + GET_MEMORY8(save_addr, getmem_tmp); \ + save_addr = (save_addr + 1) & 0xffff; \ + if((direct & 0xff) == 0) { \ + save_addr = (save_addr & 0xff) + direct; \ + } \ + GET_MEMORY8(save_addr, dest); \ + dest = (dest << 8) + getmem_tmp; \ + } else { \ + GET_MEMORY16(save_addr, dest, 1); \ + } -#define PUSH8(arg) \ - SET_MEMORY8(stack, arg); \ - stack--; \ - if(psr & 0x100) { \ - stack = 0x100 | (stack & 0xff); \ - } \ - stack = stack & 0xffff; +#define PUSH8(arg) \ + SET_MEMORY8(stack, arg); \ + stack--; \ + if(psr & 0x100) { \ + stack = 0x100 | (stack & 0xff); \ + } \ + stack = stack & 0xffff; -#define PUSH16(arg) \ - if((stack & 0xfe) == 0) { \ - /* stack will cross page! */ \ - PUSH8((arg) >> 8); \ - PUSH8(arg); \ - } else { \ - stack -= 2; \ - stack = stack & 0xffff; \ - SET_MEMORY16(stack + 1, arg, 1); \ - } +#define PUSH16(arg) \ + if((stack & 0xfe) == 0) { \ + /* stack will cross page! */ \ + PUSH8((arg) >> 8); \ + PUSH8(arg); \ + } else { \ + stack -= 2; \ + stack = stack & 0xffff; \ + SET_MEMORY16(stack + 1, arg, 1); \ + } -#define PUSH16_UNSAFE(arg) \ - save_addr = (stack - 1) & 0xffff; \ - stack -= 2; \ - if(psr & 0x100) { \ - stack = 0x100 | (stack & 0xff); \ - } \ - stack = stack & 0xffff; \ - SET_MEMORY16(save_addr, arg, 1); +#define PUSH16_UNSAFE(arg) \ + save_addr = (stack - 1) & 0xffff; \ + stack -= 2; \ + if(psr & 0x100) { \ + stack = 0x100 | (stack & 0xff); \ + } \ + stack = stack & 0xffff; \ + SET_MEMORY16(save_addr, arg, 1); -#define PUSH24_UNSAFE(arg) \ - save_addr = (stack - 2) & 0xffff; \ - stack -= 3; \ - if(psr & 0x100) { \ - stack = 0x100 | (stack & 0xff); \ - } \ - stack = stack & 0xffff; \ - SET_MEMORY24(save_addr, arg, 1); +#define PUSH24_UNSAFE(arg) \ + save_addr = (stack - 2) & 0xffff; \ + stack -= 3; \ + if(psr & 0x100) { \ + stack = 0x100 | (stack & 0xff); \ + } \ + stack = stack & 0xffff; \ + SET_MEMORY24(save_addr, arg, 1); -#define PULL8(dest) \ - stack++; \ - if(psr & 0x100) { \ - stack = 0x100 | (stack & 0xff); \ - } \ - stack = stack & 0xffff; \ - GET_MEMORY8(stack, dest); +#define PULL8(dest) \ + stack++; \ + if(psr & 0x100) { \ + stack = 0x100 | (stack & 0xff); \ + } \ + stack = stack & 0xffff; \ + GET_MEMORY8(stack, dest); -#define PULL16(dest) \ - if((stack & 0xfe) == 0xfe) { /* page cross */ \ - PULL8(dest); \ - PULL8(pull_tmp); \ - dest = (pull_tmp << 8) + dest; \ - } else { \ - GET_MEMORY16(stack + 1, dest, 1); \ - stack = (stack + 2) & 0xffff; \ - if(psr & 0x100) { \ - stack = 0x100 | (stack & 0xff); \ - } \ - } +#define PULL16(dest) \ + if((stack & 0xfe) == 0xfe) { /* page cross */ \ + PULL8(dest); \ + PULL8(pull_tmp); \ + dest = (pull_tmp << 8) + dest; \ + } else { \ + GET_MEMORY16(stack + 1, dest, 1); \ + stack = (stack + 2) & 0xffff; \ + if(psr & 0x100) { \ + stack = 0x100 | (stack & 0xff); \ + } \ + } -#define PULL16_UNSAFE(dest) \ - stack = (stack + 1) & 0xffff; \ - if(psr & 0x100) { \ - stack = 0x100 | (stack & 0xff); \ - } \ - GET_MEMORY16(stack, dest, 1); \ - stack = (stack + 1) & 0xffff; \ - if(psr & 0x100) { \ - stack = 0x100 | (stack & 0xff); \ - } +#define PULL16_UNSAFE(dest) \ + stack = (stack + 1) & 0xffff; \ + if(psr & 0x100) { \ + stack = 0x100 | (stack & 0xff); \ + } \ + GET_MEMORY16(stack, dest, 1); \ + stack = (stack + 1) & 0xffff; \ + if(psr & 0x100) { \ + stack = 0x100 | (stack & 0xff); \ + } -#define PULL24(dest) \ - if((stack & 0xfc) == 0xfc) { /* page cross */ \ - PULL8(dest); \ - PULL8(pull_tmp); \ - pull_tmp = (pull_tmp << 8) + dest; \ - PULL8(dest); \ - dest = (dest << 16) + pull_tmp; \ - } else { \ - GET_MEMORY24(stack + 1, dest, 1); \ - stack = (stack + 3) & 0xffff; \ - if(psr & 0x100) { \ - stack = 0x100 | (stack & 0xff); \ - } \ - } +#define PULL24(dest) \ + if((stack & 0xfc) == 0xfc) { /* page cross */ \ + PULL8(dest); \ + PULL8(pull_tmp); \ + pull_tmp = (pull_tmp << 8) + dest; \ + PULL8(dest); \ + dest = (dest << 16) + pull_tmp; \ + } else { \ + GET_MEMORY24(stack + 1, dest, 1); \ + stack = (stack + 3) & 0xffff; \ + if(psr & 0x100) { \ + stack = 0x100 | (stack & 0xff); \ + } \ + } -#define SET_MEMORY8(addr, val) \ - LOG_DATA_MACRO(addr, val, 8); \ - CYCLES_PLUS_1; \ - stat = GET_PAGE_INFO_WR(((addr) >> 8) & 0xffff); \ - wstat = PTR2WORD(stat) & 0xff; \ - ptr = stat - wstat + ((addr) & 0xff); \ - if(wstat) { \ - fcycles_tmp1 = fcycles; \ - set_memory8_io_stub((addr), val, stat, &fcycles_tmp1, \ - fplus_x_m1); \ - fcycles = fcycles_tmp1; \ - } else { \ - *ptr = val; \ - } +#define SET_MEMORY8(addr, val) \ + LOG_DATA_MACRO(addr, val, 8); \ + CYCLES_PLUS_1; \ + stat = GET_PAGE_INFO_WR(((addr) >> 8) & 0xffff); \ + wstat = PTR2WORD(stat) & 0xff; \ + ptr = stat - wstat + ((addr) & 0xff); \ + if(wstat) { \ + fcycles_tmp1 = fcycles; \ + set_memory8_io_stub((addr), val, stat, &fcycles_tmp1, \ + fplus_x_m1); \ + fcycles = fcycles_tmp1; \ + } else { \ + *ptr = val; \ + } -#define SET_MEMORY16(addr, val, in_bank) \ - LOG_DATA_MACRO(addr, val, 16); \ - stat = GET_PAGE_INFO_WR(((addr) >> 8) & 0xffff); \ - wstat = PTR2WORD(stat) & 0xff; \ - ptr = stat - wstat + ((addr) & 0xff); \ - if((wstat) || (((addr) & 0xff) == 0xff)) { \ - fcycles_tmp1 = fcycles; \ - set_memory16_pieces_stub((addr), (val), \ - &fcycles_tmp1, fplus_1, fplus_x_m1, in_bank); \ - fcycles = fcycles_tmp1; \ - } else { \ - CYCLES_PLUS_2; \ - ptr[0] = (val); \ - ptr[1] = (val) >> 8; \ - } +#define SET_MEMORY16(addr, val, in_bank) \ + LOG_DATA_MACRO(addr, val, 16); \ + stat = GET_PAGE_INFO_WR(((addr) >> 8) & 0xffff); \ + wstat = PTR2WORD(stat) & 0xff; \ + ptr = stat - wstat + ((addr) & 0xff); \ + if((wstat) || (((addr) & 0xff) == 0xff)) { \ + fcycles_tmp1 = fcycles; \ + set_memory16_pieces_stub((addr), (val), \ + &fcycles_tmp1, fplus_1, fplus_x_m1, in_bank); \ + fcycles = fcycles_tmp1; \ + } else { \ + CYCLES_PLUS_2; \ + ptr[0] = (val); \ + ptr[1] = (val) >> 8; \ + } -#define SET_MEMORY24(addr, val, in_bank) \ - LOG_DATA_MACRO(addr, val, 24); \ - stat = GET_PAGE_INFO_WR(((addr) >> 8) & 0xffff); \ - wstat = PTR2WORD(stat) & 0xff; \ - ptr = stat - wstat + ((addr) & 0xff); \ - if((wstat) || (((addr) & 0xfe) == 0xfe)) { \ - fcycles_tmp1 = fcycles; \ - set_memory24_pieces_stub((addr), (val), \ - &fcycles_tmp1, fplus_ptr, in_bank); \ - fcycles = fcycles_tmp1; \ - } else { \ - CYCLES_PLUS_3; \ - ptr[0] = (val); \ - ptr[1] = (val) >> 8; \ - ptr[2] = (val) >> 16; \ - } +#define SET_MEMORY24(addr, val, in_bank) \ + LOG_DATA_MACRO(addr, val, 24); \ + stat = GET_PAGE_INFO_WR(((addr) >> 8) & 0xffff); \ + wstat = PTR2WORD(stat) & 0xff; \ + ptr = stat - wstat + ((addr) & 0xff); \ + if((wstat) || (((addr) & 0xfe) == 0xfe)) { \ + fcycles_tmp1 = fcycles; \ + set_memory24_pieces_stub((addr), (val), \ + &fcycles_tmp1, fplus_ptr, in_bank); \ + fcycles = fcycles_tmp1; \ + } else { \ + CYCLES_PLUS_3; \ + ptr[0] = (val); \ + ptr[1] = (val) >> 8; \ + ptr[2] = (val) >> 16; \ + } -void -check_breakpoints(word32 addr) -{ - int count; - int i; +void check_breakpoints(word32 addr) { + int count; + int i; - count = g_num_breakpoints; - for(i = 0; i < count; i++) { - if((g_breakpts[i] & 0xffffff) == addr) { - g_dbg_step = -2; - halt2_printf("Hit breakpoint at %06x\n", addr); - } - } + count = g_num_breakpoints; + for(i = 0; i < count; i++) { + if((g_breakpts[i] & 0xffffff) == addr) { + g_dbg_step = -2; + halt2_printf("Hit breakpoint at %06x\n", addr); + } + } } -word32 -get_memory8_io_stub(word32 addr, byte *stat, double *fcycs_ptr, - double fplus_x_m1) -{ - double fcycles; - word32 wstat; - byte *ptr; - wstat = PTR2WORD(stat) & 0xff; +word32 get_memory8_io_stub(word32 addr, byte *stat, double *fcycs_ptr, + double fplus_x_m1) { + double fcycles; + word32 wstat; + byte *ptr; + wstat = PTR2WORD(stat) & 0xff; - if(wstat & BANK_BREAK) { - check_breakpoints(addr); - } - fcycles = *fcycs_ptr; - if(wstat & BANK_IO2_TMP || iostrobe == 1) { - FCYCLES_ROUND; - *fcycs_ptr = fcycles; - return get_memory_io((addr), fcycs_ptr); - } else { - ptr = stat - wstat + (addr & 0xff); - return *ptr; - } + if(wstat & BANK_BREAK) { + check_breakpoints(addr); + } + fcycles = *fcycs_ptr; + if(wstat & BANK_IO2_TMP || iostrobe == 1) { + FCYCLES_ROUND; + *fcycs_ptr = fcycles; + return get_memory_io((addr), fcycs_ptr); + } else { + ptr = stat - wstat + (addr & 0xff); + return *ptr; + } } -word32 -get_memory16_pieces_stub(word32 addr, byte *stat, double *fcycs_ptr, - Fplus *fplus_ptr, int in_bank) -{ - byte *ptr; - double fcycles, fcycles_tmp1; - double fplus_1; - double fplus_x_m1; - word32 addrp1; - word32 wstat; - word32 addr_latch; - word32 ret; - word32 tmp1; - fcycles = *fcycs_ptr; - fplus_1 = fplus_ptr->plus_1; - fplus_x_m1 = fplus_ptr->plus_x_minus_1; - GET_MEMORY8(addr, tmp1); - addrp1 = addr + 1; - if(in_bank) { - addrp1 = (addr & 0xff0000) + (addrp1 & 0xffff); - } - GET_MEMORY8(addrp1, ret); - *fcycs_ptr = fcycles; - return (ret << 8) + (tmp1); +word32 get_memory16_pieces_stub(word32 addr, byte *stat, double *fcycs_ptr, + Fplus *fplus_ptr, int in_bank) { + byte *ptr; + double fcycles, fcycles_tmp1; + double fplus_1; + double fplus_x_m1; + word32 addrp1; + word32 wstat; + word32 addr_latch; + word32 ret; + word32 tmp1; + fcycles = *fcycs_ptr; + fplus_1 = fplus_ptr->plus_1; + fplus_x_m1 = fplus_ptr->plus_x_minus_1; + GET_MEMORY8(addr, tmp1); + addrp1 = addr + 1; + if(in_bank) { + addrp1 = (addr & 0xff0000) + (addrp1 & 0xffff); + } + GET_MEMORY8(addrp1, ret); + *fcycs_ptr = fcycles; + return (ret << 8) + (tmp1); } -word32 -get_memory24_pieces_stub(word32 addr, byte *stat, double *fcycs_ptr, - Fplus *fplus_ptr, int in_bank) -{ - byte *ptr; - double fcycles, fcycles_tmp1; - double fplus_1; - double fplus_x_m1; - word32 addrp1, addrp2; - word32 wstat; - word32 addr_latch; - word32 ret; - word32 tmp1; - word32 tmp2; - fcycles = *fcycs_ptr; - fplus_1 = fplus_ptr->plus_1; - fplus_x_m1 = fplus_ptr->plus_x_minus_1; - GET_MEMORY8(addr, tmp1); - addrp1 = addr + 1; - if(in_bank) { - addrp1 = (addr & 0xff0000) + (addrp1 & 0xffff); - } - GET_MEMORY8(addrp1, tmp2); - addrp2 = addr + 2; - if(in_bank) { - addrp2 = (addr & 0xff0000) + (addrp2 & 0xffff); - } - GET_MEMORY8(addrp2, ret); - *fcycs_ptr = fcycles; - return (ret << 16) + (tmp2 << 8) + tmp1; +word32 get_memory24_pieces_stub(word32 addr, byte *stat, double *fcycs_ptr, + Fplus *fplus_ptr, int in_bank) { + byte *ptr; + double fcycles, fcycles_tmp1; + double fplus_1; + double fplus_x_m1; + word32 addrp1, addrp2; + word32 wstat; + word32 addr_latch; + word32 ret; + word32 tmp1; + word32 tmp2; + fcycles = *fcycs_ptr; + fplus_1 = fplus_ptr->plus_1; + fplus_x_m1 = fplus_ptr->plus_x_minus_1; + GET_MEMORY8(addr, tmp1); + addrp1 = addr + 1; + if(in_bank) { + addrp1 = (addr & 0xff0000) + (addrp1 & 0xffff); + } + GET_MEMORY8(addrp1, tmp2); + addrp2 = addr + 2; + if(in_bank) { + addrp2 = (addr & 0xff0000) + (addrp2 & 0xffff); + } + GET_MEMORY8(addrp2, ret); + *fcycs_ptr = fcycles; + return (ret << 16) + (tmp2 << 8) + tmp1; } -void -set_memory8_io_stub(word32 addr, word32 val, byte *stat, double *fcycs_ptr, - double fplus_x_m1) -{ - double fcycles; - word32 setmem_tmp1; - word32 tmp1, tmp2; - byte *ptr; - word32 wstat; +void set_memory8_io_stub(word32 addr, word32 val, byte *stat, double *fcycs_ptr, + double fplus_x_m1) { + double fcycles; + word32 setmem_tmp1; + word32 tmp1, tmp2; + byte *ptr; + word32 wstat; - wstat = PTR2WORD(stat) & 0xff; - if(wstat & (1 << (31 - BANK_BREAK_BIT))) { - check_breakpoints(addr); - } - ptr = stat - wstat + ((addr) & 0xff); \ - fcycles = *fcycs_ptr; - if(wstat & (1 << (31 - BANK_IO2_BIT))) { - FCYCLES_ROUND; - *fcycs_ptr = fcycles; - set_memory_io((addr), val, fcycs_ptr); - } else if(wstat & (1 << (31 - BANK_SHADOW_BIT))) { - FCYCS_PTR_FCYCLES_ROUND_SLOW; - tmp1 = (addr & 0xffff); - setmem_tmp1 = g_slow_memory_ptr[tmp1]; - *ptr = val; - if(setmem_tmp1 != ((val) & 0xff)) { - g_slow_memory_ptr[tmp1] = val; - slow_mem_changed[tmp1 >> CHANGE_SHIFT] |= - (1 << (31-((tmp1 >> SHIFT_PER_CHANGE) & 0x1f))); - } - } else if(wstat & (1 << (31 - BANK_SHADOW2_BIT))) { - FCYCS_PTR_FCYCLES_ROUND_SLOW; - tmp2 = (addr & 0xffff); - tmp1 = 0x10000 + tmp2; - setmem_tmp1 = g_slow_memory_ptr[tmp1]; - *ptr = val; - if(setmem_tmp1 != ((val) & 0xff)) { - g_slow_memory_ptr[tmp1] = val; - slow_mem_changed[tmp2 >>CHANGE_SHIFT] |= - (1 <<(31-((tmp2 >> SHIFT_PER_CHANGE) & 0x1f))); - } - } else { - /* breakpoint only */ - *ptr = val; - } + wstat = PTR2WORD(stat) & 0xff; + if(wstat & (1 << (31 - BANK_BREAK_BIT))) { + check_breakpoints(addr); + } + ptr = stat - wstat + ((addr) & 0xff); \ + fcycles = *fcycs_ptr; + if(wstat & (1 << (31 - BANK_IO2_BIT))) { + FCYCLES_ROUND; + *fcycs_ptr = fcycles; + set_memory_io((addr), val, fcycs_ptr); + } else if(wstat & (1 << (31 - BANK_SHADOW_BIT))) { + FCYCS_PTR_FCYCLES_ROUND_SLOW; + tmp1 = (addr & 0xffff); + setmem_tmp1 = g_slow_memory_ptr[tmp1]; + *ptr = val; + if(setmem_tmp1 != ((val) & 0xff)) { + g_slow_memory_ptr[tmp1] = val; + slow_mem_changed[tmp1 >> CHANGE_SHIFT] |= + (1 << (31-((tmp1 >> SHIFT_PER_CHANGE) & 0x1f))); + } + } else if(wstat & (1 << (31 - BANK_SHADOW2_BIT))) { + FCYCS_PTR_FCYCLES_ROUND_SLOW; + tmp2 = (addr & 0xffff); + tmp1 = 0x10000 + tmp2; + setmem_tmp1 = g_slow_memory_ptr[tmp1]; + *ptr = val; + if(setmem_tmp1 != ((val) & 0xff)) { + g_slow_memory_ptr[tmp1] = val; + slow_mem_changed[tmp2 >>CHANGE_SHIFT] |= + (1 <<(31-((tmp2 >> SHIFT_PER_CHANGE) & 0x1f))); + } + } else { + /* breakpoint only */ + *ptr = val; + } } -void -set_memory16_pieces_stub(word32 addr, word32 val, double *fcycs_ptr, - double fplus_1, double fplus_x_m1, int in_bank) -{ - byte *ptr; - byte *stat; - double fcycles, fcycles_tmp1; - word32 addrp1; - word32 wstat; - fcycles = *fcycs_ptr; - SET_MEMORY8(addr, val); - addrp1 = addr + 1; - if(in_bank) { - addrp1 = (addr & 0xff0000) + (addrp1 & 0xffff); - } - SET_MEMORY8(addrp1, val >> 8); +void set_memory16_pieces_stub(word32 addr, word32 val, double *fcycs_ptr, + double fplus_1, double fplus_x_m1, int in_bank) { + byte *ptr; + byte *stat; + double fcycles, fcycles_tmp1; + word32 addrp1; + word32 wstat; + fcycles = *fcycs_ptr; + SET_MEMORY8(addr, val); + addrp1 = addr + 1; + if(in_bank) { + addrp1 = (addr & 0xff0000) + (addrp1 & 0xffff); + } + SET_MEMORY8(addrp1, val >> 8); - *fcycs_ptr = fcycles; + *fcycs_ptr = fcycles; } -void -set_memory24_pieces_stub(word32 addr, word32 val, double *fcycs_ptr, - Fplus *fplus_ptr, int in_bank) -{ - byte *ptr; - byte *stat; - double fcycles, fcycles_tmp1; - double fplus_1; - double fplus_x_m1; - word32 addrp1, addrp2; - word32 wstat; +void set_memory24_pieces_stub(word32 addr, word32 val, double *fcycs_ptr, + Fplus *fplus_ptr, int in_bank) { + byte *ptr; + byte *stat; + double fcycles, fcycles_tmp1; + double fplus_1; + double fplus_x_m1; + word32 addrp1, addrp2; + word32 wstat; - fcycles = *fcycs_ptr; - fplus_1 = fplus_ptr->plus_1; - fplus_x_m1 = fplus_ptr->plus_x_minus_1; - SET_MEMORY8(addr, val); - addrp1 = addr + 1; - if(in_bank) { - addrp1 = (addr & 0xff0000) + (addrp1 & 0xffff); - } - SET_MEMORY8(addrp1, val >> 8); - addrp2 = addr + 2; - if(in_bank) { - addrp2 = (addr & 0xff0000) + (addrp2 & 0xffff); - } - SET_MEMORY8(addrp2, val >> 16); + fcycles = *fcycs_ptr; + fplus_1 = fplus_ptr->plus_1; + fplus_x_m1 = fplus_ptr->plus_x_minus_1; + SET_MEMORY8(addr, val); + addrp1 = addr + 1; + if(in_bank) { + addrp1 = (addr & 0xff0000) + (addrp1 & 0xffff); + } + SET_MEMORY8(addrp1, val >> 8); + addrp2 = addr + 2; + if(in_bank) { + addrp2 = (addr & 0xff0000) + (addrp2 & 0xffff); + } + SET_MEMORY8(addrp2, val >> 16); - *fcycs_ptr = fcycles; + *fcycs_ptr = fcycles; } -word32 -get_memory_c(word32 addr, int cycs) -{ - byte *stat; - byte *ptr; - double fcycles, fcycles_tmp1; - double fplus_1; - double fplus_x_m1; - word32 addr_latch; - word32 wstat; - word32 ret; +word32 get_memory_c(word32 addr, int cycs) { + byte *stat; + byte *ptr; + double fcycles, fcycles_tmp1; + double fplus_1; + double fplus_x_m1; + word32 addr_latch; + word32 wstat; + word32 ret; - fcycles = 0; - fplus_1 = 0; - fplus_x_m1 = 0; - GET_MEMORY8(addr, ret); - return ret; + fcycles = 0; + fplus_1 = 0; + fplus_x_m1 = 0; + GET_MEMORY8(addr, ret); + return ret; } -word32 -get_memory16_c(word32 addr, int cycs) -{ - double fcycs; +word32 get_memory16_c(word32 addr, int cycs) { + double fcycs; - fcycs = 0; - return get_memory_c(addr, (int)fcycs) + - (get_memory_c(addr+1, (int)fcycs) << 8); + fcycs = 0; + return get_memory_c(addr, (int)fcycs) + + (get_memory_c(addr+1, (int)fcycs) << 8); } -word32 -get_memory24_c(word32 addr, int cycs) -{ - double fcycs; +word32 get_memory24_c(word32 addr, int cycs) { + double fcycs; - fcycs = 0; - return get_memory_c(addr, (int)fcycs) + - (get_memory_c(addr+1, (int)fcycs) << 8) + - (get_memory_c(addr+2, (int)fcycs) << 16); + fcycs = 0; + return get_memory_c(addr, (int)fcycs) + + (get_memory_c(addr+1, (int)fcycs) << 8) + + (get_memory_c(addr+2, (int)fcycs) << 16); } -word32 -get_memory32_c(word32 addr, int cycs) -{ - double fcycs; +word32 get_memory32_c(word32 addr, int cycs) { + double fcycs; - fcycs = 0; - return get_memory_c(addr, (int)fcycs) + - (get_memory_c(addr+1, (int)fcycs) << 8) + - (get_memory_c(addr+2, (int)fcycs) << 16) + - (get_memory_c(addr+3, (int)fcycs) << 24); + fcycs = 0; + return get_memory_c(addr, (int)fcycs) + + (get_memory_c(addr+1, (int)fcycs) << 8) + + (get_memory_c(addr+2, (int)fcycs) << 16) + + (get_memory_c(addr+3, (int)fcycs) << 24); } -void -set_memory_c(word32 addr, word32 val, int cycs) -{ - byte *stat; - byte *ptr; - double fcycles, fcycles_tmp1; - double fplus_1; - double fplus_x_m1; - word32 wstat; - fcycles = g_cur_dcycs - g_last_vbl_dcycs; - fplus_1 = 0; - fplus_x_m1 = 0; - SET_MEMORY8(addr, val); +void set_memory_c(word32 addr, word32 val, int cycs) { + byte *stat; + byte *ptr; + double fcycles, fcycles_tmp1; + double fplus_1; + double fplus_x_m1; + word32 wstat; + fcycles = g_cur_dcycs - g_last_vbl_dcycs; + fplus_1 = 0; + fplus_x_m1 = 0; + SET_MEMORY8(addr, val); } -void -set_memory16_c(word32 addr, word32 val, int cycs) -{ - byte *stat; - byte *ptr; - double fcycles, fcycles_tmp1; - double fplus_1, fplus_2; - double fplus_x_m1; - word32 wstat; +void set_memory16_c(word32 addr, word32 val, int cycs) { + byte *stat; + byte *ptr; + double fcycles, fcycles_tmp1; + double fplus_1, fplus_2; + double fplus_x_m1; + word32 wstat; - fcycles = g_cur_dcycs - g_last_vbl_dcycs; - fplus_1 = 0; - fplus_2 = 0; - fplus_x_m1 = 0; - SET_MEMORY16(addr, val, 0); + fcycles = g_cur_dcycs - g_last_vbl_dcycs; + fplus_1 = 0; + fplus_2 = 0; + fplus_x_m1 = 0; + SET_MEMORY16(addr, val, 0); } -void -set_memory24_c(word32 addr, word32 val, int cycs) -{ - set_memory_c(addr, val, 0); - set_memory_c(addr + 1, val >> 8, 0); - set_memory_c(addr + 2, val >> 16, 0); +void set_memory24_c(word32 addr, word32 val, int cycs) { + set_memory_c(addr, val, 0); + set_memory_c(addr + 1, val >> 8, 0); + set_memory_c(addr + 2, val >> 16, 0); } -void -set_memory32_c(word32 addr, word32 val, int cycs) -{ - set_memory_c(addr, val, 0); - set_memory_c(addr + 1, val >> 8, 0); - set_memory_c(addr + 2, val >> 16, 0); - set_memory_c(addr + 3, val >> 24, 0); +void set_memory32_c(word32 addr, word32 val, int cycs) { + set_memory_c(addr, val, 0); + set_memory_c(addr + 1, val >> 8, 0); + set_memory_c(addr + 2, val >> 16, 0); + set_memory_c(addr + 3, val >> 24, 0); } -word32 -do_adc_sbc8(word32 in1, word32 in2, word32 psr, int sub) -{ - word32 sum, carry, overflow; - word32 zero; - int decimal; +word32 do_adc_sbc8(word32 in1, word32 in2, word32 psr, int sub) { + word32 sum, carry, overflow; + word32 zero; + int decimal; - overflow = 0; - decimal = psr & 8; - if(sub) { - in2 = (in2 ^ 0xff); - } - if(!decimal) { - sum = (in1 & 0xff) + in2 + (psr & 1); - overflow = ((sum ^ in2) >> 1) & 0x40; - } else { - /* decimal */ - sum = (in1 & 0xf) + (in2 & 0xf) + (psr & 1); - if(sub) { - if(sum < 0x10) { - sum = (sum - 0x6) & 0xf; - } - } else { - if(sum >= 0xa) { - sum = (sum - 0xa) | 0x10; - } - } + overflow = 0; + decimal = psr & 8; + if(sub) { + in2 = (in2 ^ 0xff); + } + if(!decimal) { + sum = (in1 & 0xff) + in2 + (psr & 1); + overflow = ((sum ^ in2) >> 1) & 0x40; + } else { + /* decimal */ + sum = (in1 & 0xf) + (in2 & 0xf) + (psr & 1); + if(sub) { + if(sum < 0x10) { + sum = (sum - 0x6) & 0xf; + } + } else { + if(sum >= 0xa) { + sum = (sum - 0xa) | 0x10; + } + } - sum = (in1 & 0xf0) + (in2 & 0xf0) + sum; - overflow = ((sum >> 2) ^ (sum >> 1)) & 0x40; - if(sub) { - if(sum < 0x100) { - sum = (sum + 0xa0) & 0xff; - } - } else { - if(sum >= 0xa0) { - sum += 0x60; - } - } - } + sum = (in1 & 0xf0) + (in2 & 0xf0) + sum; + overflow = ((sum >> 2) ^ (sum >> 1)) & 0x40; + if(sub) { + if(sum < 0x100) { + sum = (sum + 0xa0) & 0xff; + } + } else { + if(sum >= 0xa0) { + sum += 0x60; + } + } + } - zero = ((sum & 0xff) == 0); - carry = (sum >= 0x100); - if((in1 ^ in2) & 0x80) { - overflow = 0; - } + zero = ((sum & 0xff) == 0); + carry = (sum >= 0x100); + if((in1 ^ in2) & 0x80) { + overflow = 0; + } - psr = psr & (~0xc3); - psr = psr + (sum & 0x80) + overflow + (zero << 1) + carry; + psr = psr & (~0xc3); + psr = psr + (sum & 0x80) + overflow + (zero << 1) + carry; - return (psr << 16) + (sum & 0xff); + return (psr << 16) + (sum & 0xff); } -word32 -do_adc_sbc16(word32 in1, word32 in2, word32 psr, int sub) -{ - word32 sum, carry, overflow; - word32 tmp1, tmp2; - word32 zero; - int decimal; +word32 do_adc_sbc16(word32 in1, word32 in2, word32 psr, int sub) { + word32 sum, carry, overflow; + word32 tmp1, tmp2; + word32 zero; + int decimal; - overflow = 0; - decimal = psr & 8; - if(!decimal) { - if(sub) { - in2 = (in2 ^ 0xffff); - } - sum = in1 + in2 + (psr & 1); - overflow = ((sum ^ in2) >> 9) & 0x40; - } else { - /* decimal */ - if(sub) { - tmp1 = do_adc_sbc8(in1 & 0xff, in2 & 0xff, psr, sub); - psr = (tmp1 >> 16); - tmp2 = do_adc_sbc8((in1 >> 8) & 0xff, - (in2 >> 8) & 0xff, psr, sub); - in2 = (in2 ^ 0xfffff); - } else { - tmp1 = do_adc_sbc8(in1 & 0xff, in2 & 0xff, psr, sub); - psr = (tmp1 >> 16); - tmp2 = do_adc_sbc8((in1 >> 8) & 0xff, - (in2 >> 8) &0xff, psr, sub); - } - sum = ((tmp2 & 0xff) << 8) + (tmp1 & 0xff) + - (((tmp2 >> 16) & 1) << 16); - overflow = (tmp2 >> 16) & 0x40; - } + overflow = 0; + decimal = psr & 8; + if(!decimal) { + if(sub) { + in2 = (in2 ^ 0xffff); + } + sum = in1 + in2 + (psr & 1); + overflow = ((sum ^ in2) >> 9) & 0x40; + } else { + /* decimal */ + if(sub) { + tmp1 = do_adc_sbc8(in1 & 0xff, in2 & 0xff, psr, sub); + psr = (tmp1 >> 16); + tmp2 = do_adc_sbc8((in1 >> 8) & 0xff, + (in2 >> 8) & 0xff, psr, sub); + in2 = (in2 ^ 0xfffff); + } else { + tmp1 = do_adc_sbc8(in1 & 0xff, in2 & 0xff, psr, sub); + psr = (tmp1 >> 16); + tmp2 = do_adc_sbc8((in1 >> 8) & 0xff, + (in2 >> 8) &0xff, psr, sub); + } + sum = ((tmp2 & 0xff) << 8) + (tmp1 & 0xff) + + (((tmp2 >> 16) & 1) << 16); + overflow = (tmp2 >> 16) & 0x40; + } - zero = ((sum & 0xffff) == 0); - carry = (sum >= 0x10000); - if((in1 ^ in2) & 0x8000) { - overflow = 0; - } + zero = ((sum & 0xffff) == 0); + carry = (sum >= 0x10000); + if((in1 ^ in2) & 0x8000) { + overflow = 0; + } - psr = psr & (~0xc3); - psr = psr + ((sum & 0x8000) >> 8) + overflow + (zero << 1) + carry; + psr = psr & (~0xc3); + psr = psr + ((sum & 0x8000) >> 8) + overflow + (zero << 1) + carry; - return (psr << 16) + (sum & 0xffff); + return (psr << 16) + (sum & 0xffff); } -int g_ret1; -int g_ret2; +int g_ret1; +int g_ret2; -void -fixed_memory_ptrs_init() -{ - /* set g_slow_memory_ptr, g_rom_fc_ff_ptr, g_dummy_memory1_ptr, */ - /* and rom_cards_ptr */ +void fixed_memory_ptrs_init() { + /* set g_slow_memory_ptr, g_rom_fc_ff_ptr, g_dummy_memory1_ptr, */ + /* and rom_cards_ptr */ - // OG Filled allocated ptr parameter to free the memory - g_slow_memory_ptr = memalloc_align(128*1024, 0, (void**)&g_slow_memory_ptr_allocated); - g_dummy_memory1_ptr = memalloc_align(256, 1024, (void**)&g_dummy_memory1_ptr_allocated); - g_rom_fc_ff_ptr = memalloc_align(256*1024, 512, (void**)&g_rom_fc_ff_ptr_allocated); - g_rom_cards_ptr = memalloc_align(16*256, 256, (void**)&g_rom_cards_ptr_allocated); + // OG Filled allocated ptr parameter to free the memory + g_slow_memory_ptr = memalloc_align(128*1024, 0, (void**)&g_slow_memory_ptr_allocated); + g_dummy_memory1_ptr = memalloc_align(256, 1024, (void**)&g_dummy_memory1_ptr_allocated); + g_rom_fc_ff_ptr = memalloc_align(256*1024, 512, (void**)&g_rom_fc_ff_ptr_allocated); + g_rom_cards_ptr = memalloc_align(16*256, 256, (void**)&g_rom_cards_ptr_allocated); #if 0 - printf("g_memory_ptr: %08x, dummy_mem: %08x, slow_mem_ptr: %08x\n", - (word32)g_memory_ptr, (word32)g_dummy_memory1_ptr, - (word32)g_slow_memory_ptr); - printf("g_rom_fc_ff_ptr: %08x, g_rom_cards_ptr: %08x\n", - (word32)g_rom_fc_ff_ptr, (word32)g_rom_cards_ptr); - printf("page_info_rd = %08x, page_info_wr end = %08x\n", - (word32)&(page_info_rd_wr[0]), - (word32)&(page_info_rd_wr[PAGE_INFO_PAD_SIZE+0x1ffff].rd_wr)); + printf("g_memory_ptr: %08x, dummy_mem: %08x, slow_mem_ptr: %08x\n", + (word32)g_memory_ptr, (word32)g_dummy_memory1_ptr, + (word32)g_slow_memory_ptr); + printf("g_rom_fc_ff_ptr: %08x, g_rom_cards_ptr: %08x\n", + (word32)g_rom_fc_ff_ptr, (word32)g_rom_cards_ptr); + printf("page_info_rd = %08x, page_info_wr end = %08x\n", + (word32)&(page_info_rd_wr[0]), + (word32)&(page_info_rd_wr[PAGE_INFO_PAD_SIZE+0x1ffff].rd_wr)); #endif } -// OG added fixed_memory_ptrs_shut -void fixed_memory_ptrs_shut() -{ - - free(g_slow_memory_ptr_allocated); - free(g_dummy_memory1_ptr_allocated); - free(g_rom_fc_ff_ptr_allocated); - free(g_rom_cards_ptr_allocated); - g_slow_memory_ptr=g_slow_memory_ptr_allocated= NULL; - g_dummy_memory1_ptr = g_dummy_memory1_ptr_allocated = NULL; - g_rom_fc_ff_ptr = g_rom_fc_ff_ptr_allocated = NULL; - // g_rom_cards_ptr = g_rom_cards_ptr = NULL; // a mistake? - g_rom_cards_ptr = NULL; +// OG added fixed_memory_ptrs_shut +void fixed_memory_ptrs_shut() { + + free(g_slow_memory_ptr_allocated); + free(g_dummy_memory1_ptr_allocated); + free(g_rom_fc_ff_ptr_allocated); + free(g_rom_cards_ptr_allocated); + g_slow_memory_ptr=g_slow_memory_ptr_allocated= NULL; + g_dummy_memory1_ptr = g_dummy_memory1_ptr_allocated = NULL; + g_rom_fc_ff_ptr = g_rom_fc_ff_ptr_allocated = NULL; + // g_rom_cards_ptr = g_rom_cards_ptr = NULL; // a mistake? + g_rom_cards_ptr = NULL; } -word32 -get_itimer() -{ +word32 get_itimer() { #if defined(_WIN32) - LARGE_INTEGER count; - if (QueryPerformanceCounter(&count)) - return count.LowPart; - else - return 0; + LARGE_INTEGER count; + if (QueryPerformanceCounter(&count)) + return count.LowPart; + else + return 0; #elif defined(__i386) && defined(__GNUC__) - /* Here's my bad ia32 asm code to do rdtsc */ - /* Linux source uses: */ - /* asm volatile("rdtsc" : "=a"(ret) : : "edx"); */ - /* asm volatile("rdtsc" : "=%eax"(ret) : : "%edx"); */ + /* Here's my bad ia32 asm code to do rdtsc */ + /* Linux source uses: */ + /* asm volatile("rdtsc" : "=a"(ret) : : "edx"); */ + /* asm volatile("rdtsc" : "=%eax"(ret) : : "%edx"); */ - /* GCC bug report 2001-03/msg00786.html used: */ - /*register word64 dtmp; */ - /*asm volatile ("rdtsc" : "=A" (dtmp)); */ - /*return (word32)dtmp; */ + /* GCC bug report 2001-03/msg00786.html used: */ + /*register word64 dtmp; */ + /*asm volatile ("rdtsc" : "=A" (dtmp)); */ + /*return (word32)dtmp; */ - register word32 ret; + register word32 ret; - asm volatile ("rdtsc;movl %%eax,%0" : "=r"(ret) : : "%eax","%edx"); + asm volatile ("rdtsc;movl %%eax,%0" : "=r" (ret) : : "%eax","%edx"); - return ret; + return ret; #elif defined(__POWERPC__) && defined(__GNUC__) - register word32 ret; + register word32 ret; - asm volatile ("mftb %0" : "=r"(ret)); - return ret; + asm volatile ("mftb %0" : "=r" (ret)); + return ret; #else - return 0; + return 0; #endif } -void -set_halt_act(int val) -{ - if(val == 1 && g_ignore_halts && !g_user_halt_bad) { - g_code_red++; - } else { - halt_sim |= val; - g_fcycles_stop = (double)0.0; - } +void set_halt_act(int val) { + if(val == 1 && g_ignore_halts && !g_user_halt_bad) { + g_code_red++; + } else { + halt_sim |= val; + g_fcycles_stop = (double)0.0; + } } -void -clr_halt_act() -{ - halt_sim = 0; +void clr_halt_act() { + halt_sim = 0; } -word32 -get_remaining_operands(word32 addr, word32 opcode, word32 psr, Fplus *fplus_ptr) -{ - byte *stat; - byte *ptr; - double fcycles, fcycles_tmp1; - double fplus_1, fplus_2, fplus_3; - double fplus_x_m1; - word32 addr_latch; - word32 wstat; - word32 save_addr; - word32 arg; - word32 addrp1; - int size; +word32 get_remaining_operands(word32 addr, word32 opcode, word32 psr, Fplus *fplus_ptr) { + byte *stat; + byte *ptr; + double fcycles, fcycles_tmp1; + double fplus_1, fplus_2, fplus_3; + double fplus_x_m1; + word32 addr_latch; + word32 wstat; + word32 save_addr; + word32 arg; + word32 addrp1; + int size; - fcycles = 0; - fplus_1 = 0; - fplus_2 = 0; - fplus_3 = 0; - fplus_x_m1 = 0; + fcycles = 0; + fplus_1 = 0; + fplus_2 = 0; + fplus_3 = 0; + fplus_x_m1 = 0; - size = size_tab[opcode]; + size = size_tab[opcode]; - addrp1 = (addr & 0xff0000) + ((addr + 1) & 0xffff); - switch(size) { - case 0: - arg = 0; /* no args */ - break; - case 1: - GET_MEMORY8(addrp1, arg); - break; /* 1 arg, already done */ - case 2: - GET_MEMORY16(addrp1, arg, 1); - break; - case 3: - GET_MEMORY24(addrp1, arg, 1); - break; - case 4: - if(psr & 0x20) { - GET_MEMORY8(addrp1, arg); - } else { - GET_MEMORY16(addrp1, arg, 1); - } - break; - case 5: - if(psr & 0x10) { - GET_MEMORY8(addrp1, arg); - } else { - GET_MEMORY16(addrp1, arg, 1); - } - break; - default: - printf("Unknown size: %d\n", size); - arg = 0; - exit(-2); - } + addrp1 = (addr & 0xff0000) + ((addr + 1) & 0xffff); + switch(size) { + case 0: + arg = 0; /* no args */ + break; + case 1: + GET_MEMORY8(addrp1, arg); + break; /* 1 arg, already done */ + case 2: + GET_MEMORY16(addrp1, arg, 1); + break; + case 3: + GET_MEMORY24(addrp1, arg, 1); + break; + case 4: + if(psr & 0x20) { + GET_MEMORY8(addrp1, arg); + } else { + GET_MEMORY16(addrp1, arg, 1); + } + break; + case 5: + if(psr & 0x10) { + GET_MEMORY8(addrp1, arg); + } else { + GET_MEMORY16(addrp1, arg, 1); + } + break; + default: + printf("Unknown size: %d\n", size); + arg = 0; + exit(-2); + } - return arg; + return arg; } -#define FETCH_OPCODE \ - addr = kpc; \ - CYCLES_PLUS_2; \ - stat = GET_PAGE_INFO_RD(((addr) >> 8) & 0xffff); \ - wstat = PTR2WORD(stat) & 0xff; \ - ptr = stat - wstat + ((addr) & 0xff); \ - arg_ptr = ptr; \ - opcode = *ptr; \ - if((wstat & (1 << (31-BANK_IO_BIT))) || ((addr & 0xff) > 0xfc)) {\ - if(wstat & BANK_BREAK) { \ - check_breakpoints(addr); \ - } \ - if((addr & 0xfffff0) == 0x00c700) { \ - if(addr == 0xc700) { \ - FINISH(RET_C700, 0); \ - } else if(addr == 0xc70a) { \ - FINISH(RET_C70A, 0); \ - } else if(addr == 0xc70d) { \ - FINISH(RET_C70D, 0); \ - } \ - } \ - if(wstat & (1 << (31 - BANK_IO2_BIT)) || iostrobe == 1) { \ - FCYCLES_ROUND; \ - fcycles_tmp1 = fcycles; \ - opcode = get_memory_io((addr), &fcycles_tmp1); \ - fcycles = fcycles_tmp1; \ - } else { \ - opcode = *ptr; \ - } \ - arg = get_remaining_operands(addr, opcode, psr, fplus_ptr);\ - arg_ptr = (byte *)&tmp_bytes; \ - arg_ptr[1] = arg; \ - arg_ptr[2] = arg >> 8; \ - arg_ptr[3] = arg >> 16; \ - } +#define FETCH_OPCODE \ + addr = kpc; \ + CYCLES_PLUS_2; \ + stat = GET_PAGE_INFO_RD(((addr) >> 8) & 0xffff); \ + wstat = PTR2WORD(stat) & 0xff; \ + ptr = stat - wstat + ((addr) & 0xff); \ + arg_ptr = ptr; \ + opcode = *ptr; \ + if((wstat & (1 << (31-BANK_IO_BIT))) || ((addr & 0xff) > 0xfc)) { \ + if(wstat & BANK_BREAK) { \ + check_breakpoints(addr); \ + } \ + if((addr & 0xfffff0) == 0x00c700) { \ + if(addr == 0xc700) { \ + FINISH(RET_C700, 0); \ + } else if(addr == 0xc70a) { \ + FINISH(RET_C70A, 0); \ + } else if(addr == 0xc70d) { \ + FINISH(RET_C70D, 0); \ + } \ + } \ + if(wstat & (1 << (31 - BANK_IO2_BIT)) || iostrobe == 1) { \ + FCYCLES_ROUND; \ + fcycles_tmp1 = fcycles; \ + opcode = get_memory_io((addr), &fcycles_tmp1); \ + fcycles = fcycles_tmp1; \ + } else { \ + opcode = *ptr; \ + } \ + arg = get_remaining_operands(addr, opcode, psr, fplus_ptr); \ + arg_ptr = (byte *)&tmp_bytes; \ + arg_ptr[1] = arg; \ + arg_ptr[2] = arg >> 8; \ + arg_ptr[3] = arg >> 16; \ + } -int -enter_engine(Engine_reg *engine_ptr) -{ - register byte *ptr; - byte *arg_ptr; - Pc_log *tmp_pc_ptr; - byte *stat; - word32 wstat; - word32 arg; - register word32 kpc; - register word32 acc; - register word32 xreg; - register word32 yreg; - word32 stack; - word32 dbank; - register word32 direct; - register word32 psr; - register word32 zero; - register word32 neg; - word32 getmem_tmp; - word32 save_addr; - word32 pull_tmp; - word32 tmp_bytes; - double fcycles; - Fplus *fplus_ptr; - double fplus_1; - double fplus_2; - double fplus_3; - double fplus_x_m1; - double fcycles_tmp1; +int enter_engine(Engine_reg *engine_ptr) { + register byte *ptr; + byte *arg_ptr; + Pc_log *tmp_pc_ptr; + byte *stat; + word32 wstat; + word32 arg; + register word32 kpc; + register word32 acc; + register word32 xreg; + register word32 yreg; + word32 stack; + word32 dbank; + register word32 direct; + register word32 psr; + register word32 zero; + register word32 neg; + word32 getmem_tmp; + word32 save_addr; + word32 pull_tmp; + word32 tmp_bytes; + double fcycles; + Fplus *fplus_ptr; + double fplus_1; + double fplus_2; + double fplus_3; + double fplus_x_m1; + double fcycles_tmp1; - word32 opcode; - register word32 addr; - word32 addr_latch; - word32 tmp1, tmp2; + word32 opcode; + register word32 addr; + word32 addr_latch; + word32 tmp1, tmp2; - tmp_pc_ptr = 0; + tmp_pc_ptr = 0; - kpc = engine_ptr->kpc; - acc = engine_ptr->acc; - xreg = engine_ptr->xreg; - yreg = engine_ptr->yreg; - stack = engine_ptr->stack; - dbank = engine_ptr->dbank; - direct = engine_ptr->direct; - psr = engine_ptr->psr; - fcycles = engine_ptr->fcycles; - fplus_ptr = engine_ptr->fplus_ptr; - zero = !(psr & 2); - neg = (psr >> 7) & 1; + kpc = engine_ptr->kpc; + acc = engine_ptr->acc; + xreg = engine_ptr->xreg; + yreg = engine_ptr->yreg; + stack = engine_ptr->stack; + dbank = engine_ptr->dbank; + direct = engine_ptr->direct; + psr = engine_ptr->psr; + fcycles = engine_ptr->fcycles; + fplus_ptr = engine_ptr->fplus_ptr; + zero = !(psr & 2); + neg = (psr >> 7) & 1; - fplus_1 = fplus_ptr->plus_1; - fplus_2 = fplus_ptr->plus_2; - fplus_3 = fplus_ptr->plus_3; - fplus_x_m1 = fplus_ptr->plus_x_minus_1; + fplus_1 = fplus_ptr->plus_1; + fplus_2 = fplus_ptr->plus_2; + fplus_3 = fplus_ptr->plus_3; + fplus_x_m1 = fplus_ptr->plus_x_minus_1; - g_ret1 = 0; - g_ret2 = 0; + g_ret1 = 0; + g_ret2 = 0; recalc_accsize: - if(psr & 0x20) { - while(fcycles <= g_fcycles_stop) { + if(psr & 0x20) { + while(fcycles <= g_fcycles_stop) { #if 0 - if((neg & ~1) || (psr & (~0x1ff))) { - halt_printf("psr = %04x\n", psr); - } + if((neg & ~1) || (psr & (~0x1ff))) { + halt_printf("psr = %04x\n", psr); + } #endif - FETCH_OPCODE; + FETCH_OPCODE; - LOG_PC_MACRO(); + LOG_PC_MACRO(); - switch(opcode) { - default: - halt_printf("acc8 unk op: %02x\n", opcode); - arg = 9 + switch(opcode) { + default: + halt_printf("acc8 unk op: %02x\n", opcode); + arg = 9 #define ACC8 #include "defs_instr.h" - * 2; - break; + * 2; + break; #include "8inst_c.h" - break; - } - } - } else { - while(fcycles <= g_fcycles_stop) { - FETCH_OPCODE; - LOG_PC_MACRO(); + break; + } + } + } else { + while(fcycles <= g_fcycles_stop) { + FETCH_OPCODE; + LOG_PC_MACRO(); - switch(opcode) { - default: - halt_printf("acc16 unk op: %02x\n", opcode); - arg = 9 + switch(opcode) { + default: + halt_printf("acc16 unk op: %02x\n", opcode); + arg = 9 #undef ACC8 #include "defs_instr.h" - * 2; - break; + * 2; + break; #include "16inst_c.h" - break; - } - } - } + break; + } + } + } finish: - engine_ptr->kpc = kpc; - engine_ptr->acc = acc; - engine_ptr->xreg = xreg; - engine_ptr->yreg = yreg; - engine_ptr->stack = stack; - engine_ptr->dbank = dbank; - engine_ptr->direct = direct; - engine_ptr->fcycles = fcycles; + engine_ptr->kpc = kpc; + engine_ptr->acc = acc; + engine_ptr->xreg = xreg; + engine_ptr->yreg = yreg; + engine_ptr->stack = stack; + engine_ptr->dbank = dbank; + engine_ptr->direct = direct; + engine_ptr->fcycles = fcycles; - psr = psr & (~0x82); - psr |= (neg << 7); - psr |= ((!zero) << 1); + psr = psr & (~0x82); + psr |= (neg << 7); + psr |= ((!zero) << 1); - engine_ptr->psr = psr; + engine_ptr->psr = psr; - return (g_ret1 << 28) + g_ret2; + return (g_ret1 << 28) + g_ret2; } -int g_engine_c_mode = 1; +int g_engine_c_mode = 1; int defs_instr_start_8 = 0; int defs_instr_end_8 = 0; diff --git a/src/fbdriver.c b/src/fbdriver.c index 599f12e..b32abe1 100644 --- a/src/fbdriver.c +++ b/src/fbdriver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /* * fbdriver - Linux fullscreen framebuffer graphics driver @@ -27,11 +27,11 @@ extern word32 g_palette_8to1624[256]; extern word32 g_a2palette_8to1624[256]; extern word32 g_a2_screen_buffer_changed; extern word32 g_c025_val; -#define SHIFT_DOWN ( (g_c025_val & 0x01) ) -#define CTRL_DOWN ( (g_c025_val & 0x02) ) -#define CAPS_LOCK_DOWN ( (g_c025_val & 0x04) ) -#define OPTION_DOWN ( (g_c025_val & 0x40) ) -#define CMD_DOWN ( (g_c025_val & 0x80) ) +#define SHIFT_DOWN ( (g_c025_val & 0x01) ) +#define CTRL_DOWN ( (g_c025_val & 0x02) ) +#define CAPS_LOCK_DOWN ( (g_c025_val & 0x04) ) +#define OPTION_DOWN ( (g_c025_val & 0x40) ) +#define CMD_DOWN ( (g_c025_val & 0x80) ) extern int g_video_act_margin_left; extern int g_video_act_margin_right; extern int g_video_act_margin_top; @@ -53,491 +53,469 @@ extern int g_green_right_shift; extern int g_blue_right_shift; extern Kimage g_mainwin_kimage; -int keycode_to_a2code[128] = +int keycode_to_a2code[128] = { - -1, // KEY_RESERVED - 0x35, // KEY_ESC - 0x12, // KEY_1 - 0x13, // KEY_2 - 0x14, // KEY_3 - 0x15, // KEY_4 - 0x17, // KEY_5 - 0x16, // KEY_6 - 0x1A, // KEY_7 - 0x1C, // KEY_8 - 0x19, // KEY_9 - 0x1D, // KEY_0 - 0x1B, // KEY_MINUS - 0x18, // KEY_EQUAL - 0x3B, // KEY_BACKSPACE0 - 0x30, // KEY_TAB - 0x0C, // KEY_Q - 0x0D, // KEY_W - 0x0E, // KEY_E - 0x0F, // KEY_R - 0x11, // KEY_T - 0x10, // KEY_Y - 0x20, // KEY_U - 0x22, // KEY_I - 0x1F, // KEY_O - 0x23, // KEY_P - 0x21, // KEY_LEFTBRACE - 0x1E, // KEY_RIGHTBRACE - 0x24, // KEY_ENTER - 0x36, // KEY_LEFTCTRL - 0x00, // KEY_A - 0x01, // KEY_S - 0x02, // KEY_D - 0x03, // KEY_F - 0x05, // KEY_G - 0x04, // KEY_H - 0x26, // KEY_J - 0x28, // KEY_K - 0x25, // KEY_L - 0x29, // KEY_SEMICOLON - 0x27, // KEY_APOSTROPHE - 0x32, // KEY_GRAVE - 0x38, // KEY_LEFTSHIFT - 0x2A, // KEY_BACKSLASH - 0x06, // KEY_Z - 0x07, // KEY_X - 0x08, // KEY_C - 0x09, // KEY_V - 0x0B, // KEY_B - 0x2D, // KEY_N - 0x2E, // KEY_M - 0x2B, // KEY_COMMA - 0x2F, // KEY_DOT - 0x2C, // KEY_SLASH - 0x38, // KEY_RIGHTSHIFT - 0x43, // KEY_KPASTERISK - 0x37, // KEY_LEFTALT - 0x31, // KEY_SPACE - 0x39, // KEY_CAPSLOCK - 0x7A, // KEY_F1 - 0x78, // KEY_F2 - 0x63, // KEY_F3 - 0x76, // KEY_F4 - 0x60, // KEY_F5 - 0x61, // KEY_F6 - 0x62, // KEY_F7 - 0x64, // KEY_F8 - 0x65, // KEY_F9 - 0x6D, // KEY_F10 - 0x47, // KEY_NUMLOCK - 0x37, // KEY_SCROLLLOCK - 0x59, // KEY_KP7 - 0x5B, // KEY_KP8 - 0x5C, // KEY_KP9 - 0x4E, // KEY_KPMINUS - 0x56, // KEY_KP4 - 0x57, // KEY_KP5 - 0x58, // KEY_KP6 - 0x45, // KEY_KPPLUS - 0x53, // KEY_KP1 - 0x54, // KEY_KP2 - 0x55, // KEY_KP3 - 0x52, // KEY_KP0 - 0x41, // KEY_KPDOT - -1, - -1, // KEY_ZENKAKUHANKAKU - -1, // KEY_102ND - 0x67, // KEY_F11 - 0x6F, // KEY_F12 - -1, // KEY_RO - -1, // KEY_KATAKANA - -1, // KEY_HIRAGANA - -1, // KEY_HENKAN - -1, // KEY_KATAKANAHIRAGANA - -1, // KEY_MUHENKAN - -1, // KEY_KPJPCOMMA - 0x4C, // KEY_KPENTER - 0x36, // KEY_RIGHTCTRL - 0x4B, // KEY_KPSLASH - 0x7F, // KEY_SYSRQ - 0x37, // KEY_RIGHTALT - 0x6E, // KEY_LINEFEED - 0x73, // KEY_HOME - 0x3E, // KEY_UP - 0x74, // KEY_PAGEUP - 0x3B, // KEY_LEFT - 0x3C, // KEY_RIGHT - 0x77, // KEY_END - 0x3D, // KEY_DOWN - 0x79, // KEY_PAGEDOWN - 0x72, // KEY_INSERT - 0x33, // KEY_DELETE - -1, // KEY_MACRO - -1, // KEY_MUTE - -1, // KEY_VOLUMEDOWN - -1, // KEY_VOLUMEUP - 0x7F, // KEY_POWER /* SC System Power Down */ - 0x51, // KEY_KPEQUAL - 0x4E, // KEY_KPPLUSMINUS - -1, // KEY_PAUSE - -1, // KEY_SCALE /* AL Compiz Scale (Expose) */ - 0x2B, // KEY_KPCOMMA - -1, // KEY_HANGEUL - -1, // KEY_HANJA - -1, // KEY_YEN - 0x3A, // KEY_LEFTMETA - 0x3A, // KEY_RIGHTMETA - -1 // KEY_COMPOSE + -1, // KEY_RESERVED + 0x35, // KEY_ESC + 0x12, // KEY_1 + 0x13, // KEY_2 + 0x14, // KEY_3 + 0x15, // KEY_4 + 0x17, // KEY_5 + 0x16, // KEY_6 + 0x1A, // KEY_7 + 0x1C, // KEY_8 + 0x19, // KEY_9 + 0x1D, // KEY_0 + 0x1B, // KEY_MINUS + 0x18, // KEY_EQUAL + 0x3B, // KEY_BACKSPACE0 + 0x30, // KEY_TAB + 0x0C, // KEY_Q + 0x0D, // KEY_W + 0x0E, // KEY_E + 0x0F, // KEY_R + 0x11, // KEY_T + 0x10, // KEY_Y + 0x20, // KEY_U + 0x22, // KEY_I + 0x1F, // KEY_O + 0x23, // KEY_P + 0x21, // KEY_LEFTBRACE + 0x1E, // KEY_RIGHTBRACE + 0x24, // KEY_ENTER + 0x36, // KEY_LEFTCTRL + 0x00, // KEY_A + 0x01, // KEY_S + 0x02, // KEY_D + 0x03, // KEY_F + 0x05, // KEY_G + 0x04, // KEY_H + 0x26, // KEY_J + 0x28, // KEY_K + 0x25, // KEY_L + 0x29, // KEY_SEMICOLON + 0x27, // KEY_APOSTROPHE + 0x32, // KEY_GRAVE + 0x38, // KEY_LEFTSHIFT + 0x2A, // KEY_BACKSLASH + 0x06, // KEY_Z + 0x07, // KEY_X + 0x08, // KEY_C + 0x09, // KEY_V + 0x0B, // KEY_B + 0x2D, // KEY_N + 0x2E, // KEY_M + 0x2B, // KEY_COMMA + 0x2F, // KEY_DOT + 0x2C, // KEY_SLASH + 0x38, // KEY_RIGHTSHIFT + 0x43, // KEY_KPASTERISK + 0x37, // KEY_LEFTALT + 0x31, // KEY_SPACE + 0x39, // KEY_CAPSLOCK + 0x7A, // KEY_F1 + 0x78, // KEY_F2 + 0x63, // KEY_F3 + 0x76, // KEY_F4 + 0x60, // KEY_F5 + 0x61, // KEY_F6 + 0x62, // KEY_F7 + 0x64, // KEY_F8 + 0x65, // KEY_F9 + 0x6D, // KEY_F10 + 0x47, // KEY_NUMLOCK + 0x37, // KEY_SCROLLLOCK + 0x59, // KEY_KP7 + 0x5B, // KEY_KP8 + 0x5C, // KEY_KP9 + 0x4E, // KEY_KPMINUS + 0x56, // KEY_KP4 + 0x57, // KEY_KP5 + 0x58, // KEY_KP6 + 0x45, // KEY_KPPLUS + 0x53, // KEY_KP1 + 0x54, // KEY_KP2 + 0x55, // KEY_KP3 + 0x52, // KEY_KP0 + 0x41, // KEY_KPDOT + -1, + -1, // KEY_ZENKAKUHANKAKU + -1, // KEY_102ND + 0x67, // KEY_F11 + 0x6F, // KEY_F12 + -1, // KEY_RO + -1, // KEY_KATAKANA + -1, // KEY_HIRAGANA + -1, // KEY_HENKAN + -1, // KEY_KATAKANAHIRAGANA + -1, // KEY_MUHENKAN + -1, // KEY_KPJPCOMMA + 0x4C, // KEY_KPENTER + 0x36, // KEY_RIGHTCTRL + 0x4B, // KEY_KPSLASH + 0x7F, // KEY_SYSRQ + 0x37, // KEY_RIGHTALT + 0x6E, // KEY_LINEFEED + 0x73, // KEY_HOME + 0x3E, // KEY_UP + 0x74, // KEY_PAGEUP + 0x3B, // KEY_LEFT + 0x3C, // KEY_RIGHT + 0x77, // KEY_END + 0x3D, // KEY_DOWN + 0x79, // KEY_PAGEDOWN + 0x72, // KEY_INSERT + 0x33, // KEY_DELETE + -1, // KEY_MACRO + -1, // KEY_MUTE + -1, // KEY_VOLUMEDOWN + -1, // KEY_VOLUMEUP + 0x7F, // KEY_POWER /* SC System Power Down */ + 0x51, // KEY_KPEQUAL + 0x4E, // KEY_KPPLUSMINUS + -1, // KEY_PAUSE + -1, // KEY_SCALE /* AL Compiz Scale (Expose) */ + 0x2B, // KEY_KPCOMMA + -1, // KEY_HANGEUL + -1, // KEY_HANJA + -1, // KEY_YEN + 0x3A, // KEY_LEFTMETA + 0x3A, // KEY_RIGHTMETA + -1 // KEY_COMPOSE }; struct termios org_tio; struct fb_var_screeninfo orig_vinfo; struct fb_var_screeninfo vinfo; struct fb_fix_screeninfo finfo; -int pix_size, g_screen_mdepth, g_use_shmem = 1; -#define MOUSE_LBTN_DOWN 0x01 -#define MOUSE_MBTN_DOWN 0x02 -#define MOUSE_RBTN_DOWN 0x04 -#define MOUSE_LBTN_UP 0x00 -#define MOUSE_MBTN_UP 0x00 -#define MOUSE_RBTN_UP 0x00 -#define MOUSE_BTN_ACTIVE 0x07 -#define UPDATE_INPUT_MOUSE 0x10 +int pix_size, g_screen_mdepth, g_use_shmem = 1; +#define MOUSE_LBTN_DOWN 0x01 +#define MOUSE_MBTN_DOWN 0x02 +#define MOUSE_RBTN_DOWN 0x04 +#define MOUSE_LBTN_UP 0x00 +#define MOUSE_MBTN_UP 0x00 +#define MOUSE_RBTN_UP 0x00 +#define MOUSE_BTN_ACTIVE 0x07 +#define UPDATE_INPUT_MOUSE 0x10 #define MAX_EVDEV 8 char *fb_ptr, g_inputstate = 0; -int evfd[MAX_EVDEV], evdevs, termfd, fbfd = 0; +int evfd[MAX_EVDEV], evdevs, termfd, fbfd = 0; /* * Clean up */ -void xdriver_end(void) -{ - char c; - static char xexit = 0; - if (!xexit) +void xdriver_end(void) { + char c; + static char xexit = 0; + if (!xexit) + { + // cleanup + munmap(fb_ptr, finfo.smem_len); + ioctl(fbfd, FBIOPUT_VSCREENINFO, &orig_vinfo); + close(fbfd); + if (termfd > 0) { - // cleanup - munmap(fb_ptr, finfo.smem_len); - ioctl(fbfd, FBIOPUT_VSCREENINFO, &orig_vinfo); - close(fbfd); - if (termfd > 0) - { - // Flush input - while (read(termfd, &c, 1) == 1); - ioctl(termfd, KDSETMODE, KD_TEXT); - tcsetattr(termfd, TCSANOW, &org_tio); - close(termfd); - } - fclose(stdout); - fclose(stderr); - while (evdevs--) - close(evfd[evdevs]); - xexit = 1; + // Flush input + while (read(termfd, &c, 1) == 1); + ioctl(termfd, KDSETMODE, KD_TEXT); + tcsetattr(termfd, TCSANOW, &org_tio); + close(termfd); } + fclose(stdout); + fclose(stderr); + while (evdevs--) + close(evfd[evdevs]); + xexit = 1; + } } /* * Init framebuffer and input */ -void dev_video_init(void) -{ - int i; - char evdevname[20]; - struct termios termio; +void dev_video_init(void) { + int i; + char evdevname[20]; + struct termios termio; - // Set graphics mode on console - if ((termfd = open("/dev/tty", O_RDWR)) < 0) - { - fprintf(stderr, "Error opening tty device.\n"); - exit(-1); - } - // Save input settings. - tcgetattr(termfd, &termio); /* save current port settings */ - memcpy(&org_tio, &termio, sizeof(struct termios)); - ioctl(termfd, KDSETMODE, KD_GRAPHICS); - // Open the file for reading and writing - if ((fbfd = open("/dev/fb0", O_RDWR)) < 0) - { - fprintf(stderr, "Error opening framebuffer device.\n"); - ioctl(termfd, KDSETMODE, KD_TEXT); - exit(-1); - } - // Get variable screen information - if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) - { - fprintf(stderr, "Error reading variable screen information.\n"); - ioctl(termfd, KDSETMODE, KD_TEXT); - exit(-1); - } - // Store for reset(copy vinfo to vinfo_orig) - memcpy(&orig_vinfo, &vinfo, sizeof(struct fb_var_screeninfo)); - // Change variable info - //vinfo.bits_per_pixel = 8; - // Change resolution - vinfo.xres = 640; - vinfo.yres = 400; - if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &vinfo)) - { - fprintf(stderr, "Error setting variable screen information (640x400x8).\n"); - ioctl(termfd, KDSETMODE, KD_TEXT); - exit(-1); - } - // Get fixed screen information - if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) - { - fprintf(stderr, "Error reading fixed screen information.\n"); - ioctl(termfd, KDSETMODE, KD_TEXT); - exit(-1); - } - // map fb to user mem - fb_ptr = (char*)mmap(0, - finfo.smem_len, - PROT_READ|PROT_WRITE, - MAP_SHARED, - fbfd, - 0); - if ((int)fb_ptr == -1) - { - printf("Failed to mmap framebuffer.\n"); - ioctl(termfd, KDSETMODE, KD_TEXT); - exit (-1); - } - g_screen_depth = vinfo.bits_per_pixel; - g_screen_mdepth = g_screen_depth; - if (g_screen_depth > 8) - g_screen_mdepth = 16; - if (g_screen_depth > 16) - g_screen_mdepth = 32; - pix_size = g_screen_mdepth / 8; - if (vinfo.bits_per_pixel > 8) - { - g_red_mask = (1 << vinfo.red.length) - 1; - g_green_mask = (1 << vinfo.green.length) - 1; - g_blue_mask = (1 << vinfo.blue.length) - 1; - g_red_left_shift = vinfo.red.offset; - g_green_left_shift = vinfo.green.offset; - g_blue_left_shift = vinfo.blue.offset; - g_red_right_shift = 8 - vinfo.red.length; - g_green_right_shift = 8 - vinfo.green.length; - g_blue_right_shift = 8 - vinfo.blue.length; - } - video_get_kimages(); - if (g_screen_depth > 8) - video_get_kimage(&g_mainwin_kimage, 0, g_screen_depth, g_screen_mdepth); - for (i = 0; i < 256; i++) - { - video_update_color_raw(i, g_lores_colors[i & 0xf]); - g_a2palette_8to1624[i] = g_palette_8to1624[i]; - } - fclose(stdin); - freopen("gsport.log", "w+", stdout); - freopen("gsport.err", "w+", stderr); - termio.c_cflag = /*BAUDRATE | CRTSCTS |*/ CS8 | CLOCAL | CREAD; - termio.c_iflag = IGNPAR; - termio.c_oflag = 0; - termio.c_lflag = 0; /* set input mode (non-canonical, no echo,...) */ - termio.c_cc[VTIME] = 0; /* inter-character timer unused */ - termio.c_cc[VMIN] = 0; /* non-blocking read */ - tcsetattr(termfd, TCSANOW, &termio); - // Open input event devices - for (evdevs = 0; evdevs < MAX_EVDEV; evdevs++) - { - sprintf(evdevname, "/dev/input/event%c", evdevs + '0'); - if ((evfd[evdevs] = open(evdevname, O_RDONLY|O_NONBLOCK)) < 0) - break; - } - g_video_act_margin_left = 0; - g_video_act_margin_right = 1; - g_video_act_margin_top = 0; - g_video_act_margin_bottom = 0; + // Set graphics mode on console + if ((termfd = open("/dev/tty", O_RDWR)) < 0) + { + fprintf(stderr, "Error opening tty device.\n"); + exit(-1); + } + // Save input settings. + tcgetattr(termfd, &termio); /* save current port settings */ + memcpy(&org_tio, &termio, sizeof(struct termios)); + ioctl(termfd, KDSETMODE, KD_GRAPHICS); + // Open the file for reading and writing + if ((fbfd = open("/dev/fb0", O_RDWR)) < 0) + { + fprintf(stderr, "Error opening framebuffer device.\n"); + ioctl(termfd, KDSETMODE, KD_TEXT); + exit(-1); + } + // Get variable screen information + if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) + { + fprintf(stderr, "Error reading variable screen information.\n"); + ioctl(termfd, KDSETMODE, KD_TEXT); + exit(-1); + } + // Store for reset(copy vinfo to vinfo_orig) + memcpy(&orig_vinfo, &vinfo, sizeof(struct fb_var_screeninfo)); + // Change variable info + //vinfo.bits_per_pixel = 8; + // Change resolution + vinfo.xres = 640; + vinfo.yres = 400; + if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &vinfo)) + { + fprintf(stderr, "Error setting variable screen information (640x400x8).\n"); + ioctl(termfd, KDSETMODE, KD_TEXT); + exit(-1); + } + // Get fixed screen information + if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) + { + fprintf(stderr, "Error reading fixed screen information.\n"); + ioctl(termfd, KDSETMODE, KD_TEXT); + exit(-1); + } + // map fb to user mem + fb_ptr = (char*)mmap(0, + finfo.smem_len, + PROT_READ|PROT_WRITE, + MAP_SHARED, + fbfd, + 0); + if ((int)fb_ptr == -1) + { + printf("Failed to mmap framebuffer.\n"); + ioctl(termfd, KDSETMODE, KD_TEXT); + exit (-1); + } + g_screen_depth = vinfo.bits_per_pixel; + g_screen_mdepth = g_screen_depth; + if (g_screen_depth > 8) + g_screen_mdepth = 16; + if (g_screen_depth > 16) + g_screen_mdepth = 32; + pix_size = g_screen_mdepth / 8; + if (vinfo.bits_per_pixel > 8) + { + g_red_mask = (1 << vinfo.red.length) - 1; + g_green_mask = (1 << vinfo.green.length) - 1; + g_blue_mask = (1 << vinfo.blue.length) - 1; + g_red_left_shift = vinfo.red.offset; + g_green_left_shift = vinfo.green.offset; + g_blue_left_shift = vinfo.blue.offset; + g_red_right_shift = 8 - vinfo.red.length; + g_green_right_shift = 8 - vinfo.green.length; + g_blue_right_shift = 8 - vinfo.blue.length; + } + video_get_kimages(); + if (g_screen_depth > 8) + video_get_kimage(&g_mainwin_kimage, 0, g_screen_depth, g_screen_mdepth); + for (i = 0; i < 256; i++) + { + video_update_color_raw(i, g_lores_colors[i & 0xf]); + g_a2palette_8to1624[i] = g_palette_8to1624[i]; + } + fclose(stdin); + freopen("gsport.log", "w+", stdout); + freopen("gsport.err", "w+", stderr); + termio.c_cflag = /*BAUDRATE | CRTSCTS |*/ CS8 | CLOCAL | CREAD; + termio.c_iflag = IGNPAR; + termio.c_oflag = 0; + termio.c_lflag = 0; /* set input mode (non-canonical, no echo,...) */ + termio.c_cc[VTIME] = 0; /* inter-character timer unused */ + termio.c_cc[VMIN] = 0; /* non-blocking read */ + tcsetattr(termfd, TCSANOW, &termio); + // Open input event devices + for (evdevs = 0; evdevs < MAX_EVDEV; evdevs++) + { + sprintf(evdevname, "/dev/input/event%c", evdevs + '0'); + if ((evfd[evdevs] = open(evdevname, O_RDONLY|O_NONBLOCK)) < 0) + break; + } + g_video_act_margin_left = 0; + g_video_act_margin_right = 1; + g_video_act_margin_top = 0; + g_video_act_margin_bottom = 0; } /* * Colormap */ __u16 cmapred[256], cmapgreen[256], cmapblue[256]; int cmapstart, cmaplen, cmapdirty = 0; -void x_update_color(int col_num, int red, int green, int blue, word32 rgb) -{ - cmapred[col_num] = red | (red << 8); - cmapgreen[col_num] = green | (green << 8); - cmapblue[col_num] = blue | (blue << 8); - if (cmapdirty == 0) +void x_update_color(int col_num, int red, int green, int blue, word32 rgb) { + cmapred[col_num] = red | (red << 8); + cmapgreen[col_num] = green | (green << 8); + cmapblue[col_num] = blue | (blue << 8); + if (cmapdirty == 0) + { + cmapstart = col_num; + cmaplen = 1; + cmapdirty = 1; + } + else + { + if (col_num < cmapstart) { - cmapstart = col_num; - cmaplen = 1; - cmapdirty = 1; - } - else - { - if (col_num < cmapstart) - { - cmaplen += cmapstart - col_num; - cmapstart = col_num; - } - else if (col_num > cmapstart + cmaplen) - cmaplen = col_num - cmapstart + 1; + cmaplen += cmapstart - col_num; + cmapstart = col_num; } + else if (col_num > cmapstart + cmaplen) + cmaplen = col_num - cmapstart + 1; + } } -void x_update_physical_colormap(void) -{ - struct fb_cmap fbcol; - if (cmapdirty) - { - cmapdirty = 0; - fbcol.start = cmapstart; - fbcol.len = cmaplen; - fbcol.red = cmapred; - fbcol.green = cmapgreen; - fbcol.blue = cmapblue; - fbcol.transp = NULL; - ioctl(fbfd, FBIOPUTCMAP, &fbcol); - } +void x_update_physical_colormap(void) { + struct fb_cmap fbcol; + if (cmapdirty) + { + cmapdirty = 0; + fbcol.start = cmapstart; + fbcol.len = cmaplen; + fbcol.red = cmapred; + fbcol.green = cmapgreen; + fbcol.blue = cmapblue; + fbcol.transp = NULL; + ioctl(fbfd, FBIOPUTCMAP, &fbcol); + } } -void show_xcolor_array(void) -{ +void show_xcolor_array(void) { } /* * Screen update */ -void x_get_kimage(Kimage *kimage_ptr) -{ - kimage_ptr->data_ptr = (byte *)malloc(kimage_ptr->width_req * kimage_ptr->height * kimage_ptr->mdepth / 8); +void x_get_kimage(Kimage *kimage_ptr) { + kimage_ptr->data_ptr = (byte *)malloc(kimage_ptr->width_req * kimage_ptr->height * kimage_ptr->mdepth / 8); } -void x_release_kimage(Kimage* kimage_ptr) -{ - if (kimage_ptr->data_ptr) - if (kimage_ptr->width_req != 640 || kimage_ptr->height != 400) - free(kimage_ptr->data_ptr); - kimage_ptr->data_ptr = NULL; +void x_release_kimage(Kimage* kimage_ptr) { + if (kimage_ptr->data_ptr) + if (kimage_ptr->width_req != 640 || kimage_ptr->height != 400) + free(kimage_ptr->data_ptr); + kimage_ptr->data_ptr = NULL; } -void x_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, int width, int height) -{ - byte *src_ptr, *dst_ptr; +void x_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, int width, int height) { + byte *src_ptr, *dst_ptr; - // Copy sub-image to framebuffer - dst_ptr = (byte *)fb_ptr + desty * finfo.line_length + destx * pix_size; - src_ptr = kimage_ptr->data_ptr + (srcy * kimage_ptr->width_act + srcx) * pix_size; - width *= pix_size; - while (height--) - { - memcpy(dst_ptr, src_ptr, width); - dst_ptr += finfo.line_length; - src_ptr += kimage_ptr->width_act * pix_size; - } + // Copy sub-image to framebuffer + dst_ptr = (byte *)fb_ptr + desty * finfo.line_length + destx * pix_size; + src_ptr = kimage_ptr->data_ptr + (srcy * kimage_ptr->width_act + srcx) * pix_size; + width *= pix_size; + while (height--) + { + memcpy(dst_ptr, src_ptr, width); + dst_ptr += finfo.line_length; + src_ptr += kimage_ptr->width_act * pix_size; + } } -void x_push_done(void) -{ +void x_push_done(void) { } /* * NOP routines */ -void x_dialog_create_gsport_conf(const char *str) -{ - // Just write the config file already... - config_write_config_gsplus_file(); +void x_dialog_create_gsport_conf(const char *str) { + // Just write the config file already... + config_write_config_gsplus_file(); } -int x_show_alert(int is_fatal, const char *str) -{ - // Not implemented yet - adb_all_keys_up(); - clear_fatal_logs(); - return 0; +int x_show_alert(int is_fatal, const char *str) { + // Not implemented yet + adb_all_keys_up(); + clear_fatal_logs(); + return 0; } -void x_toggle_status_lines(void) -{ +void x_toggle_status_lines(void) { } -void x_redraw_status_lines(void) -{ +void x_redraw_status_lines(void) { } -void x_hide_pointer(int do_hide) -{ +void x_hide_pointer(int do_hide) { } -void x_auto_repeat_on(int must) -{ +void x_auto_repeat_on(int must) { } -void x_full_screen(int do_full) -{ +void x_full_screen(int do_full) { } -int x_calc_ratio(float x, float y) -{ - return 1; +int x_calc_ratio(float x, float y) { + return 1; } -void clipboard_paste(void) -{ +void clipboard_paste(void) { } -int clipboard_get_char(void) -{ - return 0; +int clipboard_get_char(void) { + return 0; } /* * Input handling */ -void check_input_events(void) -{ - struct input_event ev; - int i; - - for (i = 0; i < evdevs; i++) - // Check input events - while (read(evfd[i], &ev, sizeof(struct input_event)) == sizeof(struct input_event)) - { - if (ev.type == EV_REL) - { - if (ev.code == REL_X) - { - g_mouse_raw_x += ev.value; - if (g_mouse_raw_x < 0) - g_mouse_raw_x = 0; - if (g_mouse_raw_x > 639) - g_mouse_raw_x = 639; - } - else // REL_Y - { - g_mouse_raw_y += ev.value; - if (g_mouse_raw_y < 0) - g_mouse_raw_y = 0; - if (g_mouse_raw_y > 399) - g_mouse_raw_y = 399; - } - g_inputstate |= UPDATE_INPUT_MOUSE; - } - else if (ev.type == EV_KEY) - { - if (ev.code < 128) - { +void check_input_events(void) { + struct input_event ev; + int i; + + for (i = 0; i < evdevs; i++) + // Check input events + while (read(evfd[i], &ev, sizeof(struct input_event)) == sizeof(struct input_event)) + { + if (ev.type == EV_REL) + { + if (ev.code == REL_X) + { + g_mouse_raw_x += ev.value; + if (g_mouse_raw_x < 0) + g_mouse_raw_x = 0; + if (g_mouse_raw_x > 639) + g_mouse_raw_x = 639; + } + else // REL_Y + { + g_mouse_raw_y += ev.value; + if (g_mouse_raw_y < 0) + g_mouse_raw_y = 0; + if (g_mouse_raw_y > 399) + g_mouse_raw_y = 399; + } + g_inputstate |= UPDATE_INPUT_MOUSE; + } + else if (ev.type == EV_KEY) + { + if (ev.code < 128) + { #if 0 - if ((ev.code == KEY_F10) && SHIFT_DOWN) - { - //quitEmulator(); - iwm_shut(); - xdriver_end(); - my_exit(1); - } + if ((ev.code == KEY_F10) && SHIFT_DOWN) + { + //quitEmulator(); + iwm_shut(); + xdriver_end(); + my_exit(1); + } #endif - if (keycode_to_a2code[ev.code] >= 0) - adb_physical_key_update(keycode_to_a2code[ev.code], !ev.value); - } - else if (ev.code == BTN_LEFT) - { - g_inputstate = ev.value ? UPDATE_INPUT_MOUSE | MOUSE_LBTN_DOWN - : UPDATE_INPUT_MOUSE | MOUSE_LBTN_UP; - } - } - else if (ev.type == EV_SYN) - { - if (g_inputstate & UPDATE_INPUT_MOUSE) - update_mouse(g_mouse_raw_x, g_mouse_raw_y, g_inputstate & MOUSE_BTN_ACTIVE, MOUSE_BTN_ACTIVE); - g_inputstate &= ~UPDATE_INPUT_MOUSE; - } - } + if (keycode_to_a2code[ev.code] >= 0) + adb_physical_key_update(keycode_to_a2code[ev.code], !ev.value); + } + else if (ev.code == BTN_LEFT) + { + g_inputstate = ev.value ? UPDATE_INPUT_MOUSE | MOUSE_LBTN_DOWN + : UPDATE_INPUT_MOUSE | MOUSE_LBTN_UP; + } + } + else if (ev.type == EV_SYN) + { + if (g_inputstate & UPDATE_INPUT_MOUSE) + update_mouse(g_mouse_raw_x, g_mouse_raw_y, g_inputstate & MOUSE_BTN_ACTIVE, MOUSE_BTN_ACTIVE); + g_inputstate &= ~UPDATE_INPUT_MOUSE; + } + } } -static void sig_bye(int signo) -{ - xdriver_end(); - exit (-1); +static void sig_bye(int signo) { + xdriver_end(); + exit (-1); } /* * Application entrypoint */ -int main(int argc,char *argv[]) -{ - if (signal(SIGINT, sig_bye) == SIG_ERR) - exit(-1); - if (signal(SIGHUP, sig_bye) == SIG_ERR) - exit(-1); - gsplusmain(argc, argv); - xdriver_end(); - return 0; +int main(int argc,char *argv[]) { + if (signal(SIGINT, sig_bye) == SIG_ERR) + exit(-1); + if (signal(SIGHUP, sig_bye) == SIG_ERR) + exit(-1); + gsplusmain(argc, argv); + xdriver_end(); + return 0; } diff --git a/src/glog.c b/src/glog.c index e994703..55f7f01 100644 --- a/src/glog.c +++ b/src/glog.c @@ -1,3 +1,9 @@ +/* + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include #include #include @@ -5,37 +11,37 @@ #include "glog.h" int glog(const char *s) { - time_t timer; - char buffer[26]; - struct tm* tm_info; + time_t timer; + char buffer[26]; + struct tm* tm_info; - time(&timer); - tm_info = localtime(&timer); + time(&timer); + tm_info = localtime(&timer); - strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info); - printf("%s - %s\n", buffer, s); + strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info); + printf("%s - %s\n", buffer, s); - return 0; + return 0; } int glogf(const char *fmt, ...) { - time_t timer; - char buffer[26]; - struct tm* tm_info; + time_t timer; + char buffer[26]; + struct tm* tm_info; - time(&timer); - tm_info = localtime(&timer); + time(&timer); + tm_info = localtime(&timer); - strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info); + strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info); - printf("%s - ", buffer); + printf("%s - ", buffer); - va_list ap; - va_start(ap, fmt); - vprintf(fmt, ap); - va_end(ap); - fputc('\n', stdout); - return 0; + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + fputc('\n', stdout); + return 0; } diff --git a/src/headless_driver.c b/src/headless_driver.c index 3ec37ba..71b898c 100644 --- a/src/headless_driver.c +++ b/src/headless_driver.c @@ -1,21 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the - Free Software Foundation; either version 2 of the License, or (at your - option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ - + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include "glog.h" @@ -42,7 +30,7 @@ int clipboard_get_char() { return 0; } void clipboard_paste(void) { } -void dev_video_init() { +void dev_video_init() { g_screen_depth = 24; g_screen_mdepth = 32; @@ -100,4 +88,3 @@ void x_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, int width, int height) { } - diff --git a/src/host_fst.c b/src/host_fst.c index 16bd27a..8a4a8d4 100644 --- a/src/host_fst.c +++ b/src/host_fst.c @@ -1,5 +1,8 @@ /* - * host_fst.c + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) */ #define _BSD_SOURCE @@ -18,7 +21,7 @@ #include "gsos.h" #include "fst.h" -#if defined(__APPLE__) +#if defined(__APPLE__) #include #include #include @@ -30,7 +33,7 @@ #endif -#if defined(_WIN32) || defined(WIN_SDL) +#if defined(_WIN32) || defined(WIN_SDL) #include #include #endif @@ -69,33 +72,33 @@ extern Engine_reg engine; #define global_buffer 0x009a00 enum { - regular_file, - resource_file, - directory_file, + regular_file, + resource_file, + directory_file, }; enum { - translate_none, - translate_crlf, - translate_merlin, + translate_none, + translate_crlf, + translate_merlin, }; struct directory { - int displacement; - int num_entries; - char *entries[1]; + int displacement; + int num_entries; + char *entries[1]; }; struct fd_entry { - struct fd_entry *next; - char *path; - int cookie; - int type; - int access; - int fd; - int translate; - struct directory *dir; + struct fd_entry *next; + char *path; + int cookie; + int type; + int access; + int fd; + int translate; + struct directory *dir; }; static void free_directory(struct directory *dd); @@ -106,33 +109,33 @@ static struct directory *read_directory(const char *path, word16 *error); static word32 cookies[32] = {}; static int alloc_cookie() { - for (int i = 0; i < 32; ++i) { - word32 x = cookies[i]; + for (int i = 0; i < 32; ++i) { + word32 x = cookies[i]; - for (int j = 0; j < 32; ++j, x >>= 1) { - if (x & 0x01) continue; + for (int j = 0; j < 32; ++j, x >>= 1) { + if (x & 0x01) continue; - cookies[i] |= (1 << j); - return COOKIE_BASE + (i * 32 + j); - } - } - return -1; + cookies[i] |= (1 << j); + return COOKIE_BASE + (i * 32 + j); + } + } + return -1; } static int free_cookie(int cookie) { - if (cookie < COOKIE_BASE) return -1; - cookie -= COOKIE_BASE; - if (cookie >= 32 *32) return -1; + if (cookie < COOKIE_BASE) return -1; + cookie -= COOKIE_BASE; + if (cookie >= 32 *32) return -1; - int chunk = cookie / 32; - int offset = 1 << (cookie % 32); + int chunk = cookie / 32; + int offset = 1 << (cookie % 32); - word32 x = cookies[chunk]; + word32 x = cookies[chunk]; - if ((x & offset) == 0) return -1; - x &= ~offset; - cookies[chunk] = x; - return 0; + if ((x & offset) == 0) return -1; + x &= ~offset; + cookies[chunk] = x; + return 0; } static struct fd_entry *fd_head = NULL; @@ -154,447 +157,446 @@ int g_cfg_host_merlin = 0; static void *gc[16]; static void **gc_ptr = &gc[0]; static void *gc_malloc(size_t size) { - if (gc_ptr == &gc[16]) { - errno = ENOMEM; - return NULL; - } + if (gc_ptr == &gc[16]) { + errno = ENOMEM; + return NULL; + } - void *ptr = malloc(size); - if (ptr) { - *gc_ptr++ = ptr; - } - return ptr; + void *ptr = malloc(size); + if (ptr) { + *gc_ptr++ = ptr; + } + return ptr; } static void gc_free(void) { - while (gc_ptr > gc) free(*--gc_ptr); + while (gc_ptr > gc) free(*--gc_ptr); } static char *append_path(const char *a, const char *b) { - int aa = strlen(a); - int bb = strlen(b); + int aa = strlen(a); + int bb = strlen(b); - char *cp = gc_malloc(aa + bb + 2); - if (!cp) return NULL; - memcpy(cp, a, aa); - int len = aa; - if (len && cp[len-1] != '/' && b[0] != '/') cp[len++] = '/'; - memcpy(cp + len, b, bb); - len += bb; - cp[len] = 0; - while (len > 2 && cp[len - 1] == '/') cp[--len] = 0; - return cp; + char *cp = gc_malloc(aa + bb + 2); + if (!cp) return NULL; + memcpy(cp, a, aa); + int len = aa; + if (len && cp[len-1] != '/' && b[0] != '/') cp[len++] = '/'; + memcpy(cp + len, b, bb); + len += bb; + cp[len] = 0; + while (len > 2 && cp[len - 1] == '/') cp[--len] = 0; + return cp; } static char *append_string(const char *a, const char *b) { - int aa = strlen(a); - int bb = strlen(b); + int aa = strlen(a); + int bb = strlen(b); - char *cp = gc_malloc(aa + bb + 2); - if (!cp) return NULL; - memcpy(cp, a, aa); - int len = aa; - memcpy(cp + len, b, bb); - len += bb; - cp[len] = 0; - return cp; + char *cp = gc_malloc(aa + bb + 2); + if (!cp) return NULL; + memcpy(cp, a, aa); + int len = aa; + memcpy(cp + len, b, bb); + len += bb; + cp[len] = 0; + return cp; } static char *gc_strdup(const char *src) { - if (!src) return ""; - if (!*src) return ""; - int len = strlen(src) + 1; - char *cp = gc_malloc(len); - memcpy(cp, src, len); - return cp; + if (!src) return ""; + if (!*src) return ""; + int len = strlen(src) + 1; + char *cp = gc_malloc(len); + memcpy(cp, src, len); + return cp; } static word32 enoent(const char *path) { - /* - some op on path return ENOENT. check if it's - fileNotFound or pathNotFound - */ - char *p = (char *)path; - for(;;) { - struct stat st; - p = dirname(p); - if (p == NULL) break; - if (p[0] == '.' && p[1] == 0) break; - if (p[0] == '/' && p[1] == 0) break; - if (stat(p, &st) < 0) return pathNotFound; - } - return fileNotFound; + /* + some op on path return ENOENT. check if it's + fileNotFound or pathNotFound + */ + char *p = (char *)path; + for(;;) { + struct stat st; + p = dirname(p); + if (p == NULL) break; + if (p[0] == '.' && p[1] == 0) break; + if (p[0] == '/' && p[1] == 0) break; + if (stat(p, &st) < 0) return pathNotFound; + } + return fileNotFound; } static word32 map_errno() { - switch(errno) { - case 0: return 0; - case EBADF: - return invalidAccess; - case EDQUOT: - case EFBIG: - return volumeFull; - case ENOENT: - return fileNotFound; - case ENOTDIR: - return pathNotFound; - case ENOMEM: - return outOfMem; - case EEXIST: - return dupPathname; - default: - return drvrIOError; - } + switch(errno) { + case 0: return 0; + case EBADF: + return invalidAccess; + case EDQUOT: + case EFBIG: + return volumeFull; + case ENOENT: + return fileNotFound; + case ENOTDIR: + return pathNotFound; + case ENOMEM: + return outOfMem; + case EEXIST: + return dupPathname; + default: + return drvrIOError; + } } static word32 map_errno_path(const char *path) { - if (errno == ENOENT) return enoent(path); - return map_errno(); + if (errno == ENOENT) return enoent(path); + return map_errno(); } static struct fd_entry *find_fd(int cookie) { - struct fd_entry *head = fd_head; + struct fd_entry *head = fd_head; - while(head) { - if (head->cookie == cookie) return head; - head = head->next; - } - return NULL; + while(head) { + if (head->cookie == cookie) return head; + head = head->next; + } + return NULL; } -static void free_fd(struct fd_entry *e) { - if (!e) return; - if (e->cookie) free_cookie(e->cookie); - if (e->fd >= 0) close(e->fd); - if (e->dir) free_directory(e->dir); - free(e->path); - free(e); +static void free_fd(struct fd_entry *e) { + if (!e) return; + if (e->cookie) free_cookie(e->cookie); + if (e->fd >= 0) close(e->fd); + if (e->dir) free_directory(e->dir); + free(e->path); + free(e); } static struct fd_entry *alloc_fd() { - struct fd_entry *e = calloc(sizeof(struct fd_entry), 1); - e->fd = -1; - return e; + struct fd_entry *e = calloc(sizeof(struct fd_entry), 1); + e->fd = -1; + return e; } static word32 remove_fd(int cookie) { - word32 rv = invalidRefNum; + word32 rv = invalidRefNum; - struct fd_entry *prev = NULL; - struct fd_entry *head = fd_head; - while (head) { - if (head->cookie == cookie) { - if (prev) prev->next = head->next; - else fd_head = head->next; + struct fd_entry *prev = NULL; + struct fd_entry *head = fd_head; + while (head) { + if (head->cookie == cookie) { + if (prev) prev->next = head->next; + else fd_head = head->next; - free_fd(head); - rv = 0; - break; - } - prev = head; - head = head->next; - } - return rv; + free_fd(head); + rv = 0; + break; + } + prev = head; + head = head->next; + } + return rv; } static void cr_to_lf(byte *buffer, size_t size) { - size_t i; - for (i = 0; i < size; ++i) { - if (buffer[i] == '\r') buffer[i] = '\n'; - } + size_t i; + for (i = 0; i < size; ++i) { + if (buffer[i] == '\r') buffer[i] = '\n'; + } } static void lf_to_cr(byte *buffer, size_t size) { - size_t i; - for (i = 0; i < size; ++i) { - if (buffer[i] == '\n') buffer[i] = '\r'; - } + size_t i; + for (i = 0; i < size; ++i) { + if (buffer[i] == '\n') buffer[i] = '\r'; + } } static void merlin_to_text(byte *buffer, size_t size) { - size_t i; - for (i = 0; i < size; ++i) { - byte b = buffer[i]; - if (b == 0xa0) b = '\t'; - b &= 0x7f; - if (b == '\r') b = '\n'; - buffer[i] = b; - } + size_t i; + for (i = 0; i < size; ++i) { + byte b = buffer[i]; + if (b == 0xa0) b = '\t'; + b &= 0x7f; + if (b == '\r') b = '\n'; + buffer[i] = b; + } } static void text_to_merlin(byte *buffer, size_t size) { - size_t i; - for (i = 0; i < size; ++i) { - byte b = buffer[i]; - if (b == '\t') b = 0xa0; - if (b == '\n') b = '\r'; - if (b != ' ') b |= 0x80; - buffer[i] = b; - } + size_t i; + for (i = 0; i < size; ++i) { + byte b = buffer[i]; + if (b == '\t') b = 0xa0; + if (b == '\n') b = '\r'; + if (b != ' ') b |= 0x80; + buffer[i] = b; + } } static ssize_t safe_read(struct fd_entry *e, byte *buffer, size_t count) { - int fd = e->fd; - int tr = e->translate; + int fd = e->fd; + int tr = e->translate; - for (;;) { - ssize_t ok = read(fd, buffer, count); - if (ok >= 0) { - size_t i; - if (tr == translate_crlf) { - for (i = 0; i < ok; ++i) { - if (buffer[i] == '\n') buffer[i] = '\r'; - } - } - if (tr == translate_merlin) { - for (i = 0; i < ok; ++i) { - unsigned char c = buffer[i]; - if (c == '\t') c = 0xa0; - if (c == '\n') c = '\r'; - if (c != ' ') c |= 0x80; - buffer[i] = c; - } - } - return ok; - } - if (ok < 0 && errno == EINTR) continue; - return ok; - } + for (;;) { + ssize_t ok = read(fd, buffer, count); + if (ok >= 0) { + size_t i; + if (tr == translate_crlf) { + for (i = 0; i < ok; ++i) { + if (buffer[i] == '\n') buffer[i] = '\r'; + } + } + if (tr == translate_merlin) { + for (i = 0; i < ok; ++i) { + unsigned char c = buffer[i]; + if (c == '\t') c = 0xa0; + if (c == '\n') c = '\r'; + if (c != ' ') c |= 0x80; + buffer[i] = c; + } + } + return ok; + } + if (ok < 0 && errno == EINTR) continue; + return ok; + } } static ssize_t safe_write(struct fd_entry *e, byte *buffer, size_t count) { - int fd = e->fd; - int tr = e->translate; + int fd = e->fd; + int tr = e->translate; - if (tr == translate_crlf) { - size_t i; - for (i = 0; i < count; ++i) { - if (buffer[i] == '\r') buffer[i] = '\n'; - } - } - if (tr == translate_merlin) { - size_t i; - for (i = 0; i < count; ++i) { - unsigned char c = buffer[i]; - if (c == 0xa0) c = '\t'; - c &= 0x7f; - if (c == '\r') c = '\n'; - buffer[i] = c; - } - } + if (tr == translate_crlf) { + size_t i; + for (i = 0; i < count; ++i) { + if (buffer[i] == '\r') buffer[i] = '\n'; + } + } + if (tr == translate_merlin) { + size_t i; + for (i = 0; i < count; ++i) { + unsigned char c = buffer[i]; + if (c == 0xa0) c = '\t'; + c &= 0x7f; + if (c == '\r') c = '\n'; + buffer[i] = c; + } + } - for (;;) { - ssize_t ok = write(fd, buffer, count); - if (ok >= 0) return ok; - if (ok < 0 && errno == EINTR) continue; - return ok; - } + for (;;) { + ssize_t ok = write(fd, buffer, count); + if (ok >= 0) return ok; + if (ok < 0 && errno == EINTR) continue; + return ok; + } } struct file_info { - time_t create_date; - time_t modified_date; - word16 access; - word16 storage_type; - word16 file_type; - word32 aux_type; - word32 eof; - word32 blocks; - word32 resource_eof; - word32 resource_blocks; - mode_t st_mode; - int has_fi; - byte finder_info[32]; + time_t create_date; + time_t modified_date; + word16 access; + word16 storage_type; + word16 file_type; + word32 aux_type; + word32 eof; + word32 blocks; + word32 resource_eof; + word32 resource_blocks; + mode_t st_mode; + int has_fi; + byte finder_info[32]; }; -static int hex(byte c) -{ - if (c >= '0' && c <= '9') return c - '0'; - if (c >= 'a' && c <= 'f') return c + 10 - 'a'; - if (c >= 'A' && c <= 'F') return c + 10 - 'A'; - return 0; +static int hex(byte c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c + 10 - 'a'; + if (c >= 'A' && c <= 'F') return c + 10 - 'A'; + return 0; } static int finder_info_to_filetype(const byte *buffer, word16 *file_type, word32 *aux_type) { - if (!memcmp("pdos", buffer + 4, 4)) - { - if (buffer[0] == 'p') { - *file_type = buffer[1]; - *aux_type = (buffer[2] << 8) | buffer[3]; - return 0; - } - if (!memcmp("PSYS", buffer, 4)) { - *file_type = 0xff; - *aux_type = 0x0000; - return 0; - } - if (!memcmp("PS16", buffer, 4)) { - *file_type = 0xb3; - *aux_type = 0x0000; - return 0; - } + if (!memcmp("pdos", buffer + 4, 4)) + { + if (buffer[0] == 'p') { + *file_type = buffer[1]; + *aux_type = (buffer[2] << 8) | buffer[3]; + return 0; + } + if (!memcmp("PSYS", buffer, 4)) { + *file_type = 0xff; + *aux_type = 0x0000; + return 0; + } + if (!memcmp("PS16", buffer, 4)) { + *file_type = 0xb3; + *aux_type = 0x0000; + return 0; + } - // old mpw method for encoding. - if (!isxdigit(buffer[0]) && isxdigit(buffer[1]) && buffer[2] == ' ' && buffer[3] == ' ') - { - *file_type = (hex(buffer[0]) << 8) | hex(buffer[1]); - *aux_type = 0; - return 0; - } - } - if (!memcmp("TEXT", buffer, 4)) { - *file_type = 0x04; - *aux_type = 0x0000; - return 0; - } - if (!memcmp("BINA", buffer, 4)) { - *file_type = 0x00; - *aux_type = 0x0000; - return 0; - } - if (!memcmp("dImgdCpy", buffer, 8)) { - *file_type = 0xe0; - *aux_type = 0x0005; - return 0; - } + // old mpw method for encoding. + if (!isxdigit(buffer[0]) && isxdigit(buffer[1]) && buffer[2] == ' ' && buffer[3] == ' ') + { + *file_type = (hex(buffer[0]) << 8) | hex(buffer[1]); + *aux_type = 0; + return 0; + } + } + if (!memcmp("TEXT", buffer, 4)) { + *file_type = 0x04; + *aux_type = 0x0000; + return 0; + } + if (!memcmp("BINA", buffer, 4)) { + *file_type = 0x00; + *aux_type = 0x0000; + return 0; + } + if (!memcmp("dImgdCpy", buffer, 8)) { + *file_type = 0xe0; + *aux_type = 0x0005; + return 0; + } - if (!memcmp("MIDI", buffer, 4)) { - *file_type = 0xd7; - *aux_type = 0x0000; - return 0; - } + if (!memcmp("MIDI", buffer, 4)) { + *file_type = 0xd7; + *aux_type = 0x0000; + return 0; + } - if (!memcmp("AIFF", buffer, 4)) { - *file_type = 0xd8; - *aux_type = 0x0000; - return 0; - } + if (!memcmp("AIFF", buffer, 4)) { + *file_type = 0xd8; + *aux_type = 0x0000; + return 0; + } - if (!memcmp("AIFC", buffer, 4)) { - *file_type = 0xd8; - *aux_type = 0x0001; - return 0; - } + if (!memcmp("AIFC", buffer, 4)) { + *file_type = 0xd8; + *aux_type = 0x0001; + return 0; + } - return -1; + return -1; } static int file_type_to_finder_info(byte *buffer, word16 file_type, word32 aux_type) { - if (file_type > 0xff || aux_type > 0xffff) return -1; + if (file_type > 0xff || aux_type > 0xffff) return -1; - if (!file_type && aux_type == 0x0000) { - memcpy(buffer, "BINApdos", 8); - return 0; - } + if (!file_type && aux_type == 0x0000) { + memcpy(buffer, "BINApdos", 8); + return 0; + } - if (file_type == 0x04 && aux_type == 0x0000) { - memcpy(buffer, "TEXTpdos", 8); - return 0; - } + if (file_type == 0x04 && aux_type == 0x0000) { + memcpy(buffer, "TEXTpdos", 8); + return 0; + } - if (file_type == 0xff && aux_type == 0x0000) { - memcpy(buffer, "PSYSpdos", 8); - return 0; - } + if (file_type == 0xff && aux_type == 0x0000) { + memcpy(buffer, "PSYSpdos", 8); + return 0; + } - if (file_type == 0xb3 && aux_type == 0x0000) { - memcpy(buffer, "PS16pdos", 8); - return 0; - } + if (file_type == 0xb3 && aux_type == 0x0000) { + memcpy(buffer, "PS16pdos", 8); + return 0; + } - if (file_type == 0xd7 && aux_type == 0x0000) { - memcpy(buffer, "MIDIpdos", 8); - return 0; - } - if (file_type == 0xd8 && aux_type == 0x0000) { - memcpy(buffer, "AIFFpdos", 8); - return 0; - } - if (file_type == 0xd8 && aux_type == 0x0001) { - memcpy(buffer, "AIFCpdos", 8); - return 0; - } - if (file_type == 0xe0 && aux_type == 0x0005) { - memcpy(buffer, "dImgdCpy", 8); - return 0; - } + if (file_type == 0xd7 && aux_type == 0x0000) { + memcpy(buffer, "MIDIpdos", 8); + return 0; + } + if (file_type == 0xd8 && aux_type == 0x0000) { + memcpy(buffer, "AIFFpdos", 8); + return 0; + } + if (file_type == 0xd8 && aux_type == 0x0001) { + memcpy(buffer, "AIFCpdos", 8); + return 0; + } + if (file_type == 0xe0 && aux_type == 0x0005) { + memcpy(buffer, "dImgdCpy", 8); + return 0; + } - memcpy(buffer, "p pdos", 8); - buffer[1] = (file_type) & 0xff; - buffer[2] = (aux_type >> 8) & 0xff; - buffer[3] = (aux_type) & 0xff; - return 0; + memcpy(buffer, "p pdos", 8); + buffer[1] = (file_type) & 0xff; + buffer[2] = (aux_type >> 8) & 0xff; + buffer[3] = (aux_type) & 0xff; + return 0; } #if defined(__APPLE__) static void get_file_xinfo(const char *path, struct file_info *fi) { - ssize_t tmp; - tmp = getxattr(path, XATTR_RESOURCEFORK_NAME, NULL, 0, 0, 0); - if (tmp < 0) tmp = 0; - fi->resource_eof = tmp; - fi->resource_blocks = (tmp + 511) / 512; + ssize_t tmp; + tmp = getxattr(path, XATTR_RESOURCEFORK_NAME, NULL, 0, 0, 0); + if (tmp < 0) tmp = 0; + fi->resource_eof = tmp; + fi->resource_blocks = (tmp + 511) / 512; - tmp = getxattr(path, XATTR_FINDERINFO_NAME, fi->finder_info, 32, 0, 0); - if (tmp == 16 || tmp == 32){ - fi->has_fi = 1; + tmp = getxattr(path, XATTR_FINDERINFO_NAME, fi->finder_info, 32, 0, 0); + if (tmp == 16 || tmp == 32) { + fi->has_fi = 1; - finder_info_to_filetype(fi->finder_info, &fi->file_type, &fi->aux_type); - } + finder_info_to_filetype(fi->finder_info, &fi->file_type, &fi->aux_type); + } } #elif defined(__sun) static void get_file_xinfo(const char *path, struct file_info *fi) { - struct stat st; + struct stat st; - // can't stat an xattr directly? - int fd; - fd = attropen(path, XATTR_RESOURCEFORK_NAME, O_RDONLY); - if (fd >= 0) { - if (fstat(fd, &st) == 0) { - fi->resource_eof = st.st_size; - fi->resource_blocks = st.st_blocks; - } - close(fd); - } + // can't stat an xattr directly? + int fd; + fd = attropen(path, XATTR_RESOURCEFORK_NAME, O_RDONLY); + if (fd >= 0) { + if (fstat(fd, &st) == 0) { + fi->resource_eof = st.st_size; + fi->resource_blocks = st.st_blocks; + } + close(fd); + } - fd = attropen(path, XATTR_FINDERINFO_NAME, O_RDONLY); - if (fd >= 0) { - int tmp = read(fd, fi->finder_info, 32); - if (tmp == 16 || tmp == 32) { - fi->has_fi = 1; - finder_info_to_filetype(fi->finder_info, &fi->file_type, &fi->aux_type); - } - close(fd); - } + fd = attropen(path, XATTR_FINDERINFO_NAME, O_RDONLY); + if (fd >= 0) { + int tmp = read(fd, fi->finder_info, 32); + if (tmp == 16 || tmp == 32) { + fi->has_fi = 1; + finder_info_to_filetype(fi->finder_info, &fi->file_type, &fi->aux_type); + } + close(fd); + } } #elif defined(__linux__) static void get_file_xinfo(const char *path, struct file_info *fi) { - ssize_t tmp; - tmp = getxattr(path, "user.com.apple.ResourceFork", NULL, 0); - if (tmp < 0) tmp = 0; - fi->resource_eof = tmp; - fi->resource_blocks = (tmp + 511) / 512; + ssize_t tmp; + tmp = getxattr(path, "user.com.apple.ResourceFork", NULL, 0); + if (tmp < 0) tmp = 0; + fi->resource_eof = tmp; + fi->resource_blocks = (tmp + 511) / 512; - tmp = getxattr(path, "user.com.apple.FinderInfo", fi->finder_info, 32); - if (tmp == 16 || tmp == 32){ - fi->has_fi = 1; + tmp = getxattr(path, "user.com.apple.FinderInfo", fi->finder_info, 32); + if (tmp == 16 || tmp == 32) { + fi->has_fi = 1; - finder_info_to_filetype(fi->finder_info, &fi->file_type, &fi->aux_type); - } + finder_info_to_filetype(fi->finder_info, &fi->file_type, &fi->aux_type); + } } #else static void get_file_xinfo(const char *path, struct file_info *fi) { @@ -604,113 +606,113 @@ static void get_file_xinfo(const char *path, struct file_info *fi) { #undef _ #define _(a, b, c) { a, sizeof(a) - 1, b, c } struct ftype_entry { - char *ext; - unsigned length; - unsigned file_type; - unsigned aux_type; + char *ext; + unsigned length; + unsigned file_type; + unsigned aux_type; }; static struct ftype_entry suffixes[] = { - _("c", 0xb0, 0x0008), - _("cc", 0xb0, 0x0008), - _("h", 0xb0, 0x0008), - _("rez", 0xb0, 0x0015), - _("asm", 0xb0, 0x0003), - _("mac", 0xb0, 0x0003), - _("pas", 0xb0, 0x0005), - _("txt", 0x04, 0x0000), - _("text", 0x04, 0x0000), - _("s", 0x04, 0x0000), - { 0, 0, 0, 0} + _("c", 0xb0, 0x0008), + _("cc", 0xb0, 0x0008), + _("h", 0xb0, 0x0008), + _("rez", 0xb0, 0x0015), + _("asm", 0xb0, 0x0003), + _("mac", 0xb0, 0x0003), + _("pas", 0xb0, 0x0005), + _("txt", 0x04, 0x0000), + _("text", 0x04, 0x0000), + _("s", 0x04, 0x0000), + { 0, 0, 0, 0} }; static struct ftype_entry prefixes[] = { - _("m16.", 0xb0, 0x0003), - _("e16.", 0xb0, 0x0003), - { 0, 0, 0, 0} + _("m16.", 0xb0, 0x0003), + _("e16.", 0xb0, 0x0003), + { 0, 0, 0, 0} }; #undef _ static word32 get_file_info(const char *path, struct file_info *fi) { - struct stat st; - memset(fi, 0, sizeof(*fi)); + struct stat st; + memset(fi, 0, sizeof(*fi)); - int ok = stat(path, &st); - if (ok < 0) return map_errno(); + int ok = stat(path, &st); + if (ok < 0) return map_errno(); - fi->eof = st.st_size; - fi->blocks = st.st_blocks; + fi->eof = st.st_size; + fi->blocks = st.st_blocks; - fi->create_date = st.st_ctime; - fi->modified_date = st.st_mtime; + fi->create_date = st.st_ctime; + fi->modified_date = st.st_mtime; -#if defined(__APPLE__) - fi->create_date = st.st_birthtime; +#if defined(__APPLE__) + fi->create_date = st.st_birthtime; #endif - fi->st_mode = st.st_mode; + fi->st_mode = st.st_mode; - if (S_ISDIR(st.st_mode)) { - fi->storage_type = 0x0d; - fi->file_type = 0x0f; - if (st.st_ino == root_ino && st.st_dev == root_dev) - fi->storage_type = 0x0f; - } else if (S_ISREG(st.st_mode)) { - fi->file_type = 0x06; - if (st.st_size < 0x200) fi->storage_type = seedling; - else if (st.st_size < 0x20000) fi->storage_type = 0x0002; - else fi->storage_type = 0x0003; - } else { - fi->storage_type = st.st_mode & S_IFMT; - fi->file_type = 0; - } - // 0x01 = read enable - // 0x02 = write enable - // 0x04 = invisible - // 0x08 = reserved - // 0x10 = reserved - // 0x20 = backup needed - // 0x40 = rename enable - // 0x80 = destroy enable + if (S_ISDIR(st.st_mode)) { + fi->storage_type = 0x0d; + fi->file_type = 0x0f; + if (st.st_ino == root_ino && st.st_dev == root_dev) + fi->storage_type = 0x0f; + } else if (S_ISREG(st.st_mode)) { + fi->file_type = 0x06; + if (st.st_size < 0x200) fi->storage_type = seedling; + else if (st.st_size < 0x20000) fi->storage_type = 0x0002; + else fi->storage_type = 0x0003; + } else { + fi->storage_type = st.st_mode & S_IFMT; + fi->file_type = 0; + } + // 0x01 = read enable + // 0x02 = write enable + // 0x04 = invisible + // 0x08 = reserved + // 0x10 = reserved + // 0x20 = backup needed + // 0x40 = rename enable + // 0x80 = destroy enable - fi->access = 0xc3; // placeholder... + fi->access = 0xc3; // placeholder... - if (S_ISREG(st.st_mode)) { - get_file_xinfo(path, fi); + if (S_ISREG(st.st_mode)) { + get_file_xinfo(path, fi); - if (!fi->has_fi) { - /* guess the file type / auxtype based on extension */ - int n; - const char *dot = NULL; - const char *slash = NULL; + if (!fi->has_fi) { + /* guess the file type / auxtype based on extension */ + int n; + const char *dot = NULL; + const char *slash = NULL; - for(n = 0; ; ++n) { - char c = path[n]; - if (c == 0) break; - else if (c == '/') { slash = path + n + 1; dot = NULL; } - else if (c == '.') dot = path + n + 1; - } + for(n = 0;; ++n) { + char c = path[n]; + if (c == 0) break; + else if (c == '/') { slash = path + n + 1; dot = NULL; } + else if (c == '.') dot = path + n + 1; + } - if (dot && *dot) { - for (n = 0; n < sizeof(suffixes) / sizeof(suffixes[0]); ++n) { - if (!suffixes[n].ext) break; - if (!strcasecmp(dot, suffixes[n].ext)) { - fi->file_type = suffixes[n].file_type; - fi->aux_type = suffixes[n].aux_type; - break; - } - } - } - } - } + if (dot && *dot) { + for (n = 0; n < sizeof(suffixes) / sizeof(suffixes[0]); ++n) { + if (!suffixes[n].ext) break; + if (!strcasecmp(dot, suffixes[n].ext)) { + fi->file_type = suffixes[n].file_type; + fi->aux_type = suffixes[n].aux_type; + break; + } + } + } + } + } - // get file type/aux type + // get file type/aux type - if (fi->resource_eof) fi->storage_type = 0x0005; + if (fi->resource_eof) fi->storage_type = 0x0005; - return 0; + return 0; } @@ -719,99 +721,99 @@ static word32 get_file_info(const char *path, struct file_info *fi) { #if defined(__APPLE__) static word32 set_file_info(const char *path, struct file_info *fi) { - int ok; - struct attrlist list; - unsigned i = 0; - struct timespec dates[2]; + int ok; + struct attrlist list; + unsigned i = 0; + struct timespec dates[2]; - if (fi->has_fi && fi->storage_type != 0x0d) { - ok = setxattr(path, XATTR_FINDERINFO_NAME, fi->finder_info, 32, 0, 0); - if (ok < 0) return map_errno(); - } + if (fi->has_fi && fi->storage_type != 0x0d) { + ok = setxattr(path, XATTR_FINDERINFO_NAME, fi->finder_info, 32, 0, 0); + if (ok < 0) return map_errno(); + } - memset(&list, 0, sizeof(list)); - memset(dates, 0, sizeof(dates)); + memset(&list, 0, sizeof(list)); + memset(dates, 0, sizeof(dates)); - list.bitmapcount = ATTR_BIT_MAP_COUNT; - list.commonattr = 0; + list.bitmapcount = ATTR_BIT_MAP_COUNT; + list.commonattr = 0; - if (fi->create_date) - { - dates[i++].tv_sec = fi->create_date; - list.commonattr |= ATTR_CMN_CRTIME; - } + if (fi->create_date) + { + dates[i++].tv_sec = fi->create_date; + list.commonattr |= ATTR_CMN_CRTIME; + } - if (fi->modified_date) - { - dates[i++].tv_sec = fi->modified_date; - list.commonattr |= ATTR_CMN_MODTIME; - } + if (fi->modified_date) + { + dates[i++].tv_sec = fi->modified_date; + list.commonattr |= ATTR_CMN_MODTIME; + } - ok = 0; - if (i) ok = setattrlist(path, &list, dates, i * sizeof(struct timespec), 0); - return 0; + ok = 0; + if (i) ok = setattrlist(path, &list, dates, i * sizeof(struct timespec), 0); + return 0; } -#elif defined(__sun) +#elif defined(__sun) static word32 set_file_info(const char *path, struct file_info *fi) { - if (fi->has_fi && fi->storage_type != 0x0d) { - int fd = attropen(path, XATTR_FINDERINFO_NAME, O_WRONLY | O_CREAT, 0666); - if (fd < 0) return map_errno(); - write(fd, fi->finder_info, 32); - close(fd); - } + if (fi->has_fi && fi->storage_type != 0x0d) { + int fd = attropen(path, XATTR_FINDERINFO_NAME, O_WRONLY | O_CREAT, 0666); + if (fd < 0) return map_errno(); + write(fd, fi->finder_info, 32); + close(fd); + } - if (fi->modified_date) { - struct timeval times[2]; + if (fi->modified_date) { + struct timeval times[2]; - memset(times, 0, sizeof(times)); + memset(times, 0, sizeof(times)); - //times[0] = 0; // access - times[1].tv_sec = fi.modified_date; // modified - int ok = utimes(path, times); - if (ok < 0) return map_errno(); - } - return 0; + //times[0] = 0; // access + times[1].tv_sec = fi.modified_date; // modified + int ok = utimes(path, times); + if (ok < 0) return map_errno(); + } + return 0; } #elif defined(__linux__) static word32 set_file_info(const char *path, struct file_info *fi) { - if (fi->has_fi && fi->storage_type != 0x0d) { - int ok = setxattr(path, "user.apple.FinderInfo", fi->finder_info, 32, 0); - if (ok < 0) return map_errno(); - } + if (fi->has_fi && fi->storage_type != 0x0d) { + int ok = setxattr(path, "user.apple.FinderInfo", fi->finder_info, 32, 0); + if (ok < 0) return map_errno(); + } - if (fi->modified_date) { - struct timeval times[2]; + if (fi->modified_date) { + struct timeval times[2]; - memset(times, 0, sizeof(times)); + memset(times, 0, sizeof(times)); - //times[0] = 0; // access - times[1].tv_sec = fi->modified_date; // modified - int ok = utimes(path, times); - if (ok < 0) return map_errno(); - } - return 0; + //times[0] = 0; // access + times[1].tv_sec = fi->modified_date; // modified + int ok = utimes(path, times); + if (ok < 0) return map_errno(); + } + return 0; } #else static word32 set_file_info(const char *path, struct file_info *fi) { - if (fi->modified_date) { + if (fi->modified_date) { - struct timeval times[2]; + struct timeval times[2]; - memset(times, 0, sizeof(times)); + memset(times, 0, sizeof(times)); - times[0] = 0; // access - times[1].tv_sec = fi->modified_date; // modified + times[0] = 0; // access + times[1].tv_sec = fi->modified_date; // modified - int ok = utimes(path, times); - if (ok < 0) return map_errno(); - } - return 0; + int ok = utimes(path, times); + if (ok < 0) return map_errno(); + } + return 0; } #endif @@ -823,162 +825,162 @@ static word32 set_file_info(const char *path, struct file_info *fi) { */ static const char *check_path(const char *in, word32 *error) { - word32 tmp; - if (!error) error = &tmp; + word32 tmp; + if (!error) error = &tmp; - *error = 0; - if (!in) return ""; + *error = 0; + if (!in) return ""; - /* check for .. */ - const char *cp = in; - do { - while (*cp == '/') ++cp; - if (cp[0] == '.' && cp[1] == '.' && (cp[2] == '/' || cp[2] == 0)) - { - *error = badPathSyntax; - return NULL; - } - cp = strchr(cp, '/'); - } while(cp); + /* check for .. */ + const char *cp = in; + do { + while (*cp == '/') ++cp; + if (cp[0] == '.' && cp[1] == '.' && (cp[2] == '/' || cp[2] == 0)) + { + *error = badPathSyntax; + return NULL; + } + cp = strchr(cp, '/'); + } while(cp); - if (in[0] != '/') return in; + if (in[0] != '/') return in; - if (strncasecmp(in, "/Host", 5) == 0 && (in[5] == '/' || in[5] == 0)) { - in += 5; - while (*in == '/') ++in; - return in; - } - *error = volNotFound; - return NULL; + if (strncasecmp(in, "/Host", 5) == 0 && (in[5] == '/' || in[5] == 0)) { + in += 5; + while (*in == '/') ++in; + return in; + } + *error = volNotFound; + return NULL; } static char * get_gsstr(word32 ptr) { - if (!ptr) return NULL; - int length = get_memory16_c(ptr, 0); - ptr += 2; - char *str = gc_malloc(length + 1); - for (int i = 0; i < length; ++i) { - char c = get_memory_c(ptr+i, 0); - if (c == ':') c = '/'; - str[i] = c; - } - str[length] = 0; - return str; + if (!ptr) return NULL; + int length = get_memory16_c(ptr, 0); + ptr += 2; + char *str = gc_malloc(length + 1); + for (int i = 0; i < length; ++i) { + char c = get_memory_c(ptr+i, 0); + if (c == ':') c = '/'; + str[i] = c; + } + str[length] = 0; + return str; } static char * get_pstr(word32 ptr) { - if (!ptr) return NULL; - int length = get_memory16_c(ptr, 0); - ptr += 2; - char *str = gc_malloc(length + 1); - for (int i = 0; i < length; ++i) { - char c = get_memory_c(ptr+i, 0); - if (c == ':') c = '/'; - str[i] = c; - } - str[length] = 0; - return str; + if (!ptr) return NULL; + int length = get_memory16_c(ptr, 0); + ptr += 2; + char *str = gc_malloc(length + 1); + for (int i = 0; i < length; ++i) { + char c = get_memory_c(ptr+i, 0); + if (c == ':') c = '/'; + str[i] = c; + } + str[length] = 0; + return str; } static word32 set_gsstr(word32 ptr, const char *str) { - if (!ptr) return paramRangeErr; + if (!ptr) return paramRangeErr; - int l = str ? strlen(str) : 0; + int l = str ? strlen(str) : 0; - word32 cap = get_memory16_c(ptr, 0); - ptr += 2; + word32 cap = get_memory16_c(ptr, 0); + ptr += 2; - if (cap < 4) return paramRangeErr; + if (cap < 4) return paramRangeErr; - set_memory16_c(ptr, l, 0); - ptr += 2; + set_memory16_c(ptr, l, 0); + ptr += 2; - if (cap < l + 4) return buffTooSmall; + if (cap < l + 4) return buffTooSmall; - for (int i = 0; i < l; ++i) { - char c = *str++; - if (c == '/') c = ':'; - set_memory_c(ptr++, c, 0); - } - return 0; + for (int i = 0; i < l; ++i) { + char c = *str++; + if (c == '/') c = ':'; + set_memory_c(ptr++, c, 0); + } + return 0; } static word32 set_gsstr_truncate(word32 ptr, const char *str) { - if (!ptr) return paramRangeErr; + if (!ptr) return paramRangeErr; - int l = str ? strlen(str) : 0; + int l = str ? strlen(str) : 0; - word32 cap = get_memory16_c(ptr, 0); - ptr += 2; + word32 cap = get_memory16_c(ptr, 0); + ptr += 2; - if (cap < 4) return paramRangeErr; + if (cap < 4) return paramRangeErr; - set_memory16_c(ptr, l, 0); - ptr += 2; + set_memory16_c(ptr, l, 0); + ptr += 2; - // get dir entry copies data even - // if buffTooSmall... - int rv = 0; - if (cap < l + 4) { - l = cap - 4; - rv = buffTooSmall; - } + // get dir entry copies data even + // if buffTooSmall... + int rv = 0; + if (cap < l + 4) { + l = cap - 4; + rv = buffTooSmall; + } - for (int i = 0; i < l; ++i) { - char c = *str++; - if (c == '/') c = ':'; - set_memory_c(ptr++, c, 0); - } - return rv; + for (int i = 0; i < l; ++i) { + char c = *str++; + if (c == '/') c = ':'; + set_memory_c(ptr++, c, 0); + } + return rv; } static word32 set_pstr(word32 ptr, const char *str) { - if (!ptr) return paramRangeErr; + if (!ptr) return paramRangeErr; - int l = str ? strlen(str) : 0; + int l = str ? strlen(str) : 0; - if (l > 255) return buffTooSmall; - // / is the pascal separator. - set_memory_c(ptr++, l, 0); - for (int i = 0; i < l; ++i) { - set_memory_c(ptr++, *str++, 0); - } - return 0; + if (l > 255) return buffTooSmall; + // / is the pascal separator. + set_memory_c(ptr++, l, 0); + for (int i = 0; i < l; ++i) { + set_memory_c(ptr++, *str++, 0); + } + return 0; } static word32 set_option_list(word32 ptr, word16 fstID, const byte *data, int size) { - if (!ptr) return 0; + if (!ptr) return 0; - // totalSize - word32 cap = get_memory16_c(ptr, 0); - ptr += 2; + // totalSize + word32 cap = get_memory16_c(ptr, 0); + ptr += 2; - if (cap < 4) return paramRangeErr; + if (cap < 4) return paramRangeErr; - // reqSize - set_memory16_c(ptr, size + 2, 0); - ptr += 2; + // reqSize + set_memory16_c(ptr, size + 2, 0); + ptr += 2; - if (cap < size + 6) return buffTooSmall; + if (cap < size + 6) return buffTooSmall; - // fileSysID. - set_memory16_c(ptr, fstID, 0); - ptr += 2; + // fileSysID. + set_memory16_c(ptr, fstID, 0); + ptr += 2; - for (int i = 0; i < size; ++i) - set_memory_c(ptr++, *data++, 0); + for (int i = 0; i < size; ++i) + set_memory_c(ptr++, *data++, 0); - return 0; + return 0; } /* @@ -987,22 +989,22 @@ static word32 set_option_list(word32 ptr, word16 fstID, const byte *data, int si static void set_date_time_rec(word32 ptr, time_t time) { - if (time == 0) { - for (int i = 0; i < 8; ++i) set_memory_c(ptr++, 0, 0); - return; - } + if (time == 0) { + for (int i = 0; i < 8; ++i) set_memory_c(ptr++, 0, 0); + return; + } - struct tm *tm = localtime(&time); - if (tm->tm_sec == 60) tm->tm_sec = 59; /* leap second */ + struct tm *tm = localtime(&time); + if (tm->tm_sec == 60) tm->tm_sec = 59; /* leap second */ - set_memory_c(ptr++, tm->tm_sec, 0); - set_memory_c(ptr++, tm->tm_min, 0); - set_memory_c(ptr++, tm->tm_hour, 0); - set_memory_c(ptr++, tm->tm_year, 0); - set_memory_c(ptr++, tm->tm_mday - 1, 0); - set_memory_c(ptr++, tm->tm_mon, 0); - set_memory_c(ptr++, 0, 0); - set_memory_c(ptr++, tm->tm_wday + 1, 0); + set_memory_c(ptr++, tm->tm_sec, 0); + set_memory_c(ptr++, tm->tm_min, 0); + set_memory_c(ptr++, tm->tm_hour, 0); + set_memory_c(ptr++, tm->tm_year, 0); + set_memory_c(ptr++, tm->tm_mday - 1, 0); + set_memory_c(ptr++, tm->tm_mon, 0); + set_memory_c(ptr++, 0, 0); + set_memory_c(ptr++, tm->tm_wday + 1, 0); } /* @@ -1010,91 +1012,91 @@ static void set_date_time_rec(word32 ptr, time_t time) { */ static void set_date_time(word32 ptr, time_t time) { - if (time == 0) { - for (int i = 0; i < 4; ++i) set_memory_c(ptr++, 0, 0); - return; - } + if (time == 0) { + for (int i = 0; i < 4; ++i) set_memory_c(ptr++, 0, 0); + return; + } - struct tm *tm = localtime(&time); + struct tm *tm = localtime(&time); - word16 tmp = 0; - tmp |= (tm->tm_year % 100) << 9; - tmp |= tm->tm_mon << 5; - tmp |= tm->tm_mday; + word16 tmp = 0; + tmp |= (tm->tm_year % 100) << 9; + tmp |= tm->tm_mon << 5; + tmp |= tm->tm_mday; - set_memory16_c(ptr, tmp, 0); - ptr += 2; + set_memory16_c(ptr, tmp, 0); + ptr += 2; - tmp = 0; - tmp |= tm->tm_hour << 8; - tmp |= tm->tm_min; - set_memory16_c(ptr, tmp, 0); + tmp = 0; + tmp |= tm->tm_hour << 8; + tmp |= tm->tm_min; + set_memory16_c(ptr, tmp, 0); } static time_t get_date_time(word32 ptr) { - word16 a = get_memory16_c(ptr + 0, 0); - word16 b = get_memory16_c(ptr + 2, 0); - if (!a && !b) return 0; + word16 a = get_memory16_c(ptr + 0, 0); + word16 b = get_memory16_c(ptr + 2, 0); + if (!a && !b) return 0; - struct tm tm; - memset(&tm, 0, sizeof(tm)); + struct tm tm; + memset(&tm, 0, sizeof(tm)); - tm.tm_year = (a >> 9) & 0x7f; - tm.tm_mon = ((a >> 5) & 0x0f) - 1; - tm.tm_mday = (a >> 0) & 0x1f; + tm.tm_year = (a >> 9) & 0x7f; + tm.tm_mon = ((a >> 5) & 0x0f) - 1; + tm.tm_mday = (a >> 0) & 0x1f; - tm.tm_hour = (b >> 8) & 0x1f; - tm.tm_min = (b >> 0) & 0x3f; - tm.tm_sec = 0; + tm.tm_hour = (b >> 8) & 0x1f; + tm.tm_min = (b >> 0) & 0x3f; + tm.tm_sec = 0; - tm.tm_isdst = -1; + tm.tm_isdst = -1; - // 00 - 39 => 2000-2039 - // 40 - 99 => 1940-1999 - if (tm.tm_year < 40) tm.tm_year += 100; + // 00 - 39 => 2000-2039 + // 40 - 99 => 1940-1999 + if (tm.tm_year < 40) tm.tm_year += 100; - return mktime(&tm); + return mktime(&tm); } static time_t get_date_time_rec(word32 ptr) { - byte buffer[8]; - for (int i = 0; i < 8; ++i) buffer[i] = get_memory_c(ptr++, 0); + byte buffer[8]; + for (int i = 0; i < 8; ++i) buffer[i] = get_memory_c(ptr++, 0); - if (!memcmp(buffer, "\x00\x00\x00\x00\x00\x00\x00\x00", 8)) return 0; + if (!memcmp(buffer, "\x00\x00\x00\x00\x00\x00\x00\x00", 8)) return 0; - struct tm tm; - memset(&tm, 0, sizeof(tm)); + struct tm tm; + memset(&tm, 0, sizeof(tm)); - tm.tm_sec = buffer[0]; - tm.tm_min = buffer[1]; - tm.tm_hour = buffer[2]; - tm.tm_year = buffer[3]; - tm.tm_mday = buffer[4] + 1; - tm.tm_mon = buffer[5]; - tm.tm_isdst = -1; + tm.tm_sec = buffer[0]; + tm.tm_min = buffer[1]; + tm.tm_hour = buffer[2]; + tm.tm_year = buffer[3]; + tm.tm_mday = buffer[4] + 1; + tm.tm_mon = buffer[5]; + tm.tm_isdst = -1; - return mktime(&tm); + return mktime(&tm); } static char *get_path1(void) { - word32 direct = engine.direct; - word16 flags = get_memory16_c(direct + dp_path_flag, 0); - if (flags & (1 << 14)) - return get_gsstr( get_memory24_c(direct + dp_path1_ptr, 0)); - return NULL; + word32 direct = engine.direct; + word16 flags = get_memory16_c(direct + dp_path_flag, 0); + if (flags & (1 << 14)) + return get_gsstr( get_memory24_c(direct + dp_path1_ptr, 0)); + return NULL; } static char *get_path2(void) { - word32 direct = engine.direct; - word16 flags = get_memory16_c(direct + dp_path_flag, 0); - if (flags & (1 << 6)) - return get_gsstr( get_memory24_c(direct + dp_path2_ptr, 0)); - return NULL; + word32 direct = engine.direct; + word16 flags = get_memory16_c(direct + dp_path_flag, 0); + if (flags & (1 << 6)) + return get_gsstr( get_memory24_c(direct + dp_path2_ptr, 0)); + return NULL; } @@ -1104,471 +1106,471 @@ static char *get_path2(void) { static word32 fst_shutdown(void) { - // close any remaining files. - struct fd_entry *head = fd_head; - while (head) { - struct fd_entry *next = head->next; + // close any remaining files. + struct fd_entry *head = fd_head; + while (head) { + struct fd_entry *next = head->next; - free_fd(head); - head = next; - } - return 0; + free_fd(head); + head = next; + } + return 0; } static word32 fst_startup(void) { - // if restart, close any previous files. + // if restart, close any previous files. - struct stat st; + struct stat st; - if (!g_cfg_host_path) return invalidFSTop; - if (!*g_cfg_host_path) return invalidFSTop; - if (root) free(root); - root = strdup(g_cfg_host_path); + if (!g_cfg_host_path) return invalidFSTop; + if (!*g_cfg_host_path) return invalidFSTop; + if (root) free(root); + root = strdup(g_cfg_host_path); - read_only = g_cfg_host_read_only; + read_only = g_cfg_host_read_only; - fst_shutdown(); + fst_shutdown(); - memset(&cookies, 0, sizeof(cookies)); - if (stat(root, &st) < 0) { - fprintf(stderr, "%s does not exist\n", root); - return invalidFSTop; - } - if (!S_ISDIR(st.st_mode)) { - fprintf(stderr, "%s is not a directory\n", root); - return invalidFSTop; - } + memset(&cookies, 0, sizeof(cookies)); + if (stat(root, &st) < 0) { + fprintf(stderr, "%s does not exist\n", root); + return invalidFSTop; + } + if (!S_ISDIR(st.st_mode)) { + fprintf(stderr, "%s is not a directory\n", root); + return invalidFSTop; + } - root_ino = st.st_ino; - root_dev = st.st_dev; + root_ino = st.st_ino; + root_dev = st.st_dev; - return 0; + return 0; } static word32 fst_create(int class, const char *path) { - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - struct file_info fi; - memset(&fi, 0, sizeof(fi)); + struct file_info fi; + memset(&fi, 0, sizeof(fi)); - word16 pcount = 0; - if (class) { - pcount = get_memory16_c(pb, 0); - if (pcount >= 2) fi.access = get_memory16_c(pb + CreateRecGS_access, 0); - if (pcount >= 3) fi.file_type = get_memory16_c(pb + CreateRecGS_fileType, 0); - if (pcount >= 4) fi.aux_type = get_memory32_c(pb + CreateRecGS_auxType, 0); - if (pcount >= 5) fi.storage_type = get_memory16_c(pb + CreateRecGS_storageType, 0); - if (pcount >= 6) fi.eof = get_memory32_c(pb + CreateRecGS_eof, 0); - if (pcount >= 7) fi.resource_eof = get_memory32_c(pb + CreateRecGS_resourceEOF, 0); + word16 pcount = 0; + if (class) { + pcount = get_memory16_c(pb, 0); + if (pcount >= 2) fi.access = get_memory16_c(pb + CreateRecGS_access, 0); + if (pcount >= 3) fi.file_type = get_memory16_c(pb + CreateRecGS_fileType, 0); + if (pcount >= 4) fi.aux_type = get_memory32_c(pb + CreateRecGS_auxType, 0); + if (pcount >= 5) fi.storage_type = get_memory16_c(pb + CreateRecGS_storageType, 0); + if (pcount >= 6) fi.eof = get_memory32_c(pb + CreateRecGS_eof, 0); + if (pcount >= 7) fi.resource_eof = get_memory32_c(pb + CreateRecGS_resourceEOF, 0); - if (pcount >= 4) { - file_type_to_finder_info(fi.finder_info, fi.file_type, fi.aux_type); - fi.has_fi = 1; - } + if (pcount >= 4) { + file_type_to_finder_info(fi.finder_info, fi.file_type, fi.aux_type); + fi.has_fi = 1; + } - } else { + } else { - fi.access = get_memory16_c(pb + CreateRec_fAccess, 0); - fi.file_type = get_memory16_c(pb + CreateRec_fileType, 0); - fi.aux_type = get_memory32_c(pb + CreateRec_auxType, 0); - fi.storage_type = get_memory16_c(pb + CreateRec_storageType, 0); - fi.create_date = get_date_time(pb + CreateRec_createDate); + fi.access = get_memory16_c(pb + CreateRec_fAccess, 0); + fi.file_type = get_memory16_c(pb + CreateRec_fileType, 0); + fi.aux_type = get_memory32_c(pb + CreateRec_auxType, 0); + fi.storage_type = get_memory16_c(pb + CreateRec_storageType, 0); + fi.create_date = get_date_time(pb + CreateRec_createDate); - file_type_to_finder_info(fi.finder_info, fi.file_type, fi.aux_type); - fi.has_fi = 1; - } - int ok; + file_type_to_finder_info(fi.finder_info, fi.file_type, fi.aux_type); + fi.has_fi = 1; + } + int ok; - if (fi.storage_type == 0 && fi.file_type == 0x0f) - fi.storage_type = 0x0d; + if (fi.storage_type == 0 && fi.file_type == 0x0f) + fi.storage_type = 0x0d; - if (fi.storage_type == 0x0d) { - ok = mkdir(path, 0777); - if (ok < 0) { - return map_errno_path(path); - } + if (fi.storage_type == 0x0d) { + ok = mkdir(path, 0777); + if (ok < 0) { + return map_errno_path(path); + } - if (class) { - if (pcount >= 5) set_memory16_c(pb + CreateRecGS_storageType, fi.storage_type, 0); - } else { - set_memory16_c(pb + CreateRec_storageType, fi.storage_type, 0); - } + if (class) { + if (pcount >= 5) set_memory16_c(pb + CreateRecGS_storageType, fi.storage_type, 0); + } else { + set_memory16_c(pb + CreateRec_storageType, fi.storage_type, 0); + } - return 0; - } - if (fi.storage_type <= 3 || fi.storage_type == 0x05) { - // normal file. - // 0x05 is an extended/resource file but we don't do anything special. - ok = open(path, O_WRONLY | O_CREAT | O_EXCL, 0666); - if (ok < 0) return map_errno_path(path); - // set ftype, auxtype... - set_file_info(path, &fi); - close(ok); + return 0; + } + if (fi.storage_type <= 3 || fi.storage_type == 0x05) { + // normal file. + // 0x05 is an extended/resource file but we don't do anything special. + ok = open(path, O_WRONLY | O_CREAT | O_EXCL, 0666); + if (ok < 0) return map_errno_path(path); + // set ftype, auxtype... + set_file_info(path, &fi); + close(ok); - fi.storage_type = 1; + fi.storage_type = 1; - if (class) { - if (pcount >= 5) set_memory16_c(pb + CreateRecGS_storageType, fi.storage_type, 0); - } else { - set_memory16_c(pb + CreateRec_storageType, fi.storage_type, 0); - } + if (class) { + if (pcount >= 5) set_memory16_c(pb + CreateRecGS_storageType, fi.storage_type, 0); + } else { + set_memory16_c(pb + CreateRec_storageType, fi.storage_type, 0); + } - return 0; - } + return 0; + } - if (fi.storage_type == 0x8005) { - // convert an existing file to an extended file. - // this checks that the file exists and has a 0-sized resource. - word32 rv = get_file_info(path, &fi); - if (rv) return rv; - if (fi.storage_type == extendedFile) return resExistsErr; - if (fi.storage_type < seedling || fi.storage_type > tree) return resAddErr; - return 0; - } + if (fi.storage_type == 0x8005) { + // convert an existing file to an extended file. + // this checks that the file exists and has a 0-sized resource. + word32 rv = get_file_info(path, &fi); + if (rv) return rv; + if (fi.storage_type == extendedFile) return resExistsErr; + if (fi.storage_type < seedling || fi.storage_type > tree) return resAddErr; + return 0; + } - // other storage types? - return badStoreType; + // other storage types? + return badStoreType; } static word32 fst_destroy(int class, const char *path) { - struct stat st; + struct stat st; - if (!path) return badStoreType; + if (!path) return badStoreType; - if (stat(path, &st) < 0) { - return map_errno_path(path); - } + if (stat(path, &st) < 0) { + return map_errno_path(path); + } - // can't delete volume root. - if (st.st_ino == root_ino && st.st_dev == root_dev) { - return badStoreType; - } + // can't delete volume root. + if (st.st_ino == root_ino && st.st_dev == root_dev) { + return badStoreType; + } - int ok = S_ISDIR(st.st_mode) ? rmdir(path) : unlink(path); + int ok = S_ISDIR(st.st_mode) ? rmdir(path) : unlink(path); - if (ok < 0) return map_errno_path(path); - return 0; + if (ok < 0) return map_errno_path(path); + return 0; } static word32 fst_set_file_info(int class, const char *path) { - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - struct file_info fi; - memset(&fi, 0, sizeof(fi)); + struct file_info fi; + memset(&fi, 0, sizeof(fi)); - // load up existing file types / finder info. - get_file_xinfo(path, &fi); + // load up existing file types / finder info. + get_file_xinfo(path, &fi); - word32 option_list = 0; - if (class) { - word16 pcount = get_memory16_c(pb, 0); + word32 option_list = 0; + if (class) { + word16 pcount = get_memory16_c(pb, 0); - if (pcount >= 2) fi.access = get_memory16_c(pb + FileInfoRecGS_access, 0); - if (pcount >= 3) fi.file_type = get_memory16_c(pb + FileInfoRecGS_fileType, 0); - if (pcount >= 4) fi.aux_type = get_memory32_c(pb + FileInfoRecGS_auxType, 0); - // reserved. - //if (pcount >= 5) fi.storage_type = get_memory16_c(pb + FileInfoRecGS_storageType, 0); - if (pcount >= 6) fi.create_date = get_date_time_rec(pb + FileInfoRecGS_createDateTime); - if (pcount >= 7) fi.modified_date = get_date_time_rec(pb + FileInfoRecGS_modDateTime); - if (pcount >= 8) option_list = get_memory24_c(pb + FileInfoRecGS_optionList, 0); - // remainder reserved + if (pcount >= 2) fi.access = get_memory16_c(pb + FileInfoRecGS_access, 0); + if (pcount >= 3) fi.file_type = get_memory16_c(pb + FileInfoRecGS_fileType, 0); + if (pcount >= 4) fi.aux_type = get_memory32_c(pb + FileInfoRecGS_auxType, 0); + // reserved. + //if (pcount >= 5) fi.storage_type = get_memory16_c(pb + FileInfoRecGS_storageType, 0); + if (pcount >= 6) fi.create_date = get_date_time_rec(pb + FileInfoRecGS_createDateTime); + if (pcount >= 7) fi.modified_date = get_date_time_rec(pb + FileInfoRecGS_modDateTime); + if (pcount >= 8) option_list = get_memory24_c(pb + FileInfoRecGS_optionList, 0); + // remainder reserved - if (pcount >= 4) { - file_type_to_finder_info(fi.finder_info, fi.file_type, fi.aux_type); - fi.has_fi = 1; - } + if (pcount >= 4) { + file_type_to_finder_info(fi.finder_info, fi.file_type, fi.aux_type); + fi.has_fi = 1; + } - } else { - fi.access = get_memory16_c(pb + FileRec_fAccess, 0); - fi.file_type = get_memory16_c(pb + FileRec_fileType, 0); - fi.aux_type = get_memory32_c(pb + FileRec_auxType, 0); - // reserved. - //fi.storage_type = get_memory32_c(pb + FileRec_storageType, 0); - fi.create_date = get_date_time(pb + FileRec_createDate); - fi.modified_date = get_date_time(pb + FileRec_modDate); + } else { + fi.access = get_memory16_c(pb + FileRec_fAccess, 0); + fi.file_type = get_memory16_c(pb + FileRec_fileType, 0); + fi.aux_type = get_memory32_c(pb + FileRec_auxType, 0); + // reserved. + //fi.storage_type = get_memory32_c(pb + FileRec_storageType, 0); + fi.create_date = get_date_time(pb + FileRec_createDate); + fi.modified_date = get_date_time(pb + FileRec_modDate); - file_type_to_finder_info(fi.finder_info, fi.file_type, fi.aux_type); - fi.has_fi = 1; - } + file_type_to_finder_info(fi.finder_info, fi.file_type, fi.aux_type); + fi.has_fi = 1; + } - if (option_list) { - // total size, req size, fst id, data... - int total_size = get_memory16_c(option_list + 0, 0); - int req_size = get_memory16_c(option_list + 2, 0); - int fst_id = get_memory16_c(option_list + 4, 0); + if (option_list) { + // total size, req size, fst id, data... + int total_size = get_memory16_c(option_list + 0, 0); + int req_size = get_memory16_c(option_list + 2, 0); + int fst_id = get_memory16_c(option_list + 4, 0); - int size = req_size - 6; - if ((fst_id == proDOSFSID || fst_id == hfsFSID || fst_id == appleShareFSID) && size >= 32) { - fi.has_fi = 1; - for (int i = 0; i <32; ++i) - fi.finder_info[i] = get_memory_c(option_list + 6 + i, 0); - } - } + int size = req_size - 6; + if ((fst_id == proDOSFSID || fst_id == hfsFSID || fst_id == appleShareFSID) && size >= 32) { + fi.has_fi = 1; + for (int i = 0; i <32; ++i) + fi.finder_info[i] = get_memory_c(option_list + 6 + i, 0); + } + } - return set_file_info(path, &fi); + return set_file_info(path, &fi); } static word32 fst_get_file_info(int class, const char *path) { - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - struct file_info fi; - int rv = 0; + struct file_info fi; + int rv = 0; - rv = get_file_info(path, &fi); - if (rv) return rv; + rv = get_file_info(path, &fi); + if (rv) return rv; - if (class) { + if (class) { - word16 pcount = get_memory16_c(pb, 0); + word16 pcount = get_memory16_c(pb, 0); - if (pcount >= 2) set_memory16_c(pb + FileInfoRecGS_access, fi.access, 0); - if (pcount >= 3) set_memory16_c(pb + FileInfoRecGS_fileType, fi.file_type, 0); - if (pcount >= 4) set_memory32_c(pb + FileInfoRecGS_auxType, fi.aux_type, 0); - if (pcount >= 5) set_memory16_c(pb + FileInfoRecGS_storageType, fi.storage_type, 0); + if (pcount >= 2) set_memory16_c(pb + FileInfoRecGS_access, fi.access, 0); + if (pcount >= 3) set_memory16_c(pb + FileInfoRecGS_fileType, fi.file_type, 0); + if (pcount >= 4) set_memory32_c(pb + FileInfoRecGS_auxType, fi.aux_type, 0); + if (pcount >= 5) set_memory16_c(pb + FileInfoRecGS_storageType, fi.storage_type, 0); - if (pcount >= 6) set_date_time_rec(pb + FileInfoRecGS_createDateTime, fi.create_date); - if (pcount >= 7) set_date_time_rec(pb + FileInfoRecGS_modDateTime, fi.modified_date); + if (pcount >= 6) set_date_time_rec(pb + FileInfoRecGS_createDateTime, fi.create_date); + if (pcount >= 7) set_date_time_rec(pb + FileInfoRecGS_modDateTime, fi.modified_date); - if (pcount >= 8) { - word16 fst_id = hfsFSID; - //if (fi.storage_type == 0x0f) fst_id = mfsFSID; - word32 option_list = get_memory24_c(pb + FileInfoRecGS_optionList, 0); - rv = set_option_list(option_list, fst_id, fi.finder_info, fi.has_fi ? 32 : 0); - } - if (pcount >= 9) set_memory32_c(pb + FileInfoRecGS_eof, fi.eof, 0); - if (pcount >= 10) set_memory32_c(pb + FileInfoRecGS_blocksUsed, fi.blocks, 0); - if (pcount >= 11) set_memory32_c(pb + FileInfoRecGS_resourceEOF, fi.resource_eof, 0); - if (pcount >= 12) set_memory32_c(pb + FileInfoRecGS_resourceBlocks, fi.resource_blocks, 0); + if (pcount >= 8) { + word16 fst_id = hfsFSID; + //if (fi.storage_type == 0x0f) fst_id = mfsFSID; + word32 option_list = get_memory24_c(pb + FileInfoRecGS_optionList, 0); + rv = set_option_list(option_list, fst_id, fi.finder_info, fi.has_fi ? 32 : 0); + } + if (pcount >= 9) set_memory32_c(pb + FileInfoRecGS_eof, fi.eof, 0); + if (pcount >= 10) set_memory32_c(pb + FileInfoRecGS_blocksUsed, fi.blocks, 0); + if (pcount >= 11) set_memory32_c(pb + FileInfoRecGS_resourceEOF, fi.resource_eof, 0); + if (pcount >= 12) set_memory32_c(pb + FileInfoRecGS_resourceBlocks, fi.resource_blocks, 0); - } else { + } else { - set_memory16_c(pb + FileRec_fAccess, fi.access, 0); - set_memory16_c(pb + FileRec_fileType, fi.file_type, 0); - set_memory32_c(pb + FileRec_auxType, fi.aux_type, 0); - set_memory16_c(pb + FileRec_storageType, fi.storage_type, 0); + set_memory16_c(pb + FileRec_fAccess, fi.access, 0); + set_memory16_c(pb + FileRec_fileType, fi.file_type, 0); + set_memory32_c(pb + FileRec_auxType, fi.aux_type, 0); + set_memory16_c(pb + FileRec_storageType, fi.storage_type, 0); - set_date_time(pb + FileRec_createDate, fi.create_date); - set_date_time(pb + FileRec_modDate, fi.modified_date); + set_date_time(pb + FileRec_createDate, fi.create_date); + set_date_time(pb + FileRec_modDate, fi.modified_date); - set_memory32_c(pb + FileRec_blocksUsed, fi.blocks, 0); - } + set_memory32_c(pb + FileRec_blocksUsed, fi.blocks, 0); + } - return rv; + return rv; } static word32 fst_judge_name(int class, char *path) { - if (class == 0) return invalidClass; + if (class == 0) return invalidClass; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word16 pcount = get_memory16_c(pb, 0); - word16 name_type = get_memory16_c(pb + JudgeNameRecGS_nameType, 0); - word32 name = pcount >= 5 ? get_memory24_c(pb + JudgeNameRecGS_name, 0) : 0; + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word16 pcount = get_memory16_c(pb, 0); + word16 name_type = get_memory16_c(pb + JudgeNameRecGS_nameType, 0); + word32 name = pcount >= 5 ? get_memory24_c(pb + JudgeNameRecGS_name, 0) : 0; - // 255 max length. - if (pcount >= 4) set_memory16_c(pb + JudgeNameRecGS_maxLen, 255, 0); - if (pcount >= 6) set_memory16_c(pb + JudgeNameRecGS_nameFlags, 0, 0); + // 255 max length. + if (pcount >= 4) set_memory16_c(pb + JudgeNameRecGS_maxLen, 255, 0); + if (pcount >= 6) set_memory16_c(pb + JudgeNameRecGS_nameFlags, 0, 0); - word16 nameFlags = 0; - word16 rv = 0; + word16 nameFlags = 0; + word16 rv = 0; - if (name) { - word16 cap = get_memory16_c(name, 0); - if (cap < 4) return buffTooSmall; - word16 length = get_memory16_c(name + 2, 0); - if (length == 0) { - nameFlags |= 1 << 13; - rv = set_gsstr(name, "A"); - } else { - // if volume name, only allow "Host" ? - if (length > 255) nameFlags |= 1 << 14; - for (int i = 0; i < length; ++i) { - char c = get_memory_c(name + 4 + i, 0); - if (c == 0 || c == ':' || c == '/' || c == '\\') { - nameFlags |= 1 << 15; - set_memory_c(name + 4 + i, '.', 0); - } - } - } - } - if (pcount >= 6) set_memory16_c(pb + JudgeNameRecGS_nameFlags, nameFlags, 0); - return rv; + if (name) { + word16 cap = get_memory16_c(name, 0); + if (cap < 4) return buffTooSmall; + word16 length = get_memory16_c(name + 2, 0); + if (length == 0) { + nameFlags |= 1 << 13; + rv = set_gsstr(name, "A"); + } else { + // if volume name, only allow "Host" ? + if (length > 255) nameFlags |= 1 << 14; + for (int i = 0; i < length; ++i) { + char c = get_memory_c(name + 4 + i, 0); + if (c == 0 || c == ':' || c == '/' || c == '\\') { + nameFlags |= 1 << 15; + set_memory_c(name + 4 + i, '.', 0); + } + } + } + } + if (pcount >= 6) set_memory16_c(pb + JudgeNameRecGS_nameFlags, nameFlags, 0); + return rv; } static word32 fst_volume(int class) { - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word32 rv = 0; - if (class) { - word16 pcount = get_memory16_c(pb, 0); - if (pcount >= 2) rv = set_gsstr(get_memory24_c(pb + VolumeRecGS_volName, 0), ":Host"); - // finder bug -- if used blocks is 0, doesn't display header. - if (pcount >= 3) set_memory32_c(pb + VolumeRecGS_totalBlocks, 0x007fffff, 0); - if (pcount >= 4) set_memory32_c(pb + VolumeRecGS_freeBlocks, 0x007fffff-1, 0); - if (pcount >= 5) set_memory16_c(pb + VolumeRecGS_fileSysID, mfsFSID, 0); - if (pcount >= 6) set_memory16_c(pb + VolumeRecGS_blockSize, 512, 0); - // handled via gs/os - //if (pcount >= 7) set_memory16_c(pb + VolumeRecGS_characteristics, 0, 0); - //if (pcount >= 8) set_memory16_c(pb + VolumeRecGS_deviceID, 0, 0); - } else { + word32 rv = 0; + if (class) { + word16 pcount = get_memory16_c(pb, 0); + if (pcount >= 2) rv = set_gsstr(get_memory24_c(pb + VolumeRecGS_volName, 0), ":Host"); + // finder bug -- if used blocks is 0, doesn't display header. + if (pcount >= 3) set_memory32_c(pb + VolumeRecGS_totalBlocks, 0x007fffff, 0); + if (pcount >= 4) set_memory32_c(pb + VolumeRecGS_freeBlocks, 0x007fffff-1, 0); + if (pcount >= 5) set_memory16_c(pb + VolumeRecGS_fileSysID, mfsFSID, 0); + if (pcount >= 6) set_memory16_c(pb + VolumeRecGS_blockSize, 512, 0); + // handled via gs/os + //if (pcount >= 7) set_memory16_c(pb + VolumeRecGS_characteristics, 0, 0); + //if (pcount >= 8) set_memory16_c(pb + VolumeRecGS_deviceID, 0, 0); + } else { - // prodos 16 uses / sep. - rv = set_pstr(get_memory24_c(pb + VolumeRec_volName, 0), "/Host"); - set_memory32_c(pb + VolumeRec_totalBlocks, 0x007fffff, 0); - set_memory32_c(pb + VolumeRec_freeBlocks, 0x007fffff-1, 0); - set_memory16_c(pb + VolumeRec_fileSysID, mfsFSID, 0); - } + // prodos 16 uses / sep. + rv = set_pstr(get_memory24_c(pb + VolumeRec_volName, 0), "/Host"); + set_memory32_c(pb + VolumeRec_totalBlocks, 0x007fffff, 0); + set_memory32_c(pb + VolumeRec_freeBlocks, 0x007fffff-1, 0); + set_memory16_c(pb + VolumeRec_fileSysID, mfsFSID, 0); + } - return rv; + return rv; } static word32 fst_clear_backup(int class, const char *path) { - return invalidFSTop; + return invalidFSTop; } static int open_data_fork(const char *path, word16 *access, word16 *error) { - int fd = -1; - for (;;) { + int fd = -1; + for (;;) { - switch(*access) { - case readEnableAllowWrite: - case readWriteEnable: - fd = open(path, O_RDWR); - break; - case readEnable: - fd = open(path, O_RDONLY); - break; - case writeEnable: - fd = open(path, O_WRONLY); - break; - } + switch(*access) { + case readEnableAllowWrite: + case readWriteEnable: + fd = open(path, O_RDWR); + break; + case readEnable: + fd = open(path, O_RDONLY); + break; + case writeEnable: + fd = open(path, O_WRONLY); + break; + } - if (*access == readEnableAllowWrite) { - if (fd < 0) { - *access = readEnable; - continue; - } - *access = readWriteEnable; - } - break; - } - if (fd < 0) { - *error = map_errno_path(path); - } + if (*access == readEnableAllowWrite) { + if (fd < 0) { + *access = readEnable; + continue; + } + *access = readWriteEnable; + } + break; + } + if (fd < 0) { + *error = map_errno_path(path); + } - return fd; + return fd; } -#if defined(__APPLE__) +#if defined(__APPLE__) static int open_resource_fork(const char *path, word16 *access, word16 *error) { - // os x / hfs/apfs don't need to specifically create a resource fork. - // or do they? + // os x / hfs/apfs don't need to specifically create a resource fork. + // or do they? - char *rpath = append_path(path, _PATH_RSRCFORKSPEC); + char *rpath = append_path(path, _PATH_RSRCFORKSPEC); - int fd = -1; - for (;;) { + int fd = -1; + for (;;) { - switch(*access) { - case readEnableAllowWrite: - case readWriteEnable: - fd = open(rpath, O_RDWR | O_CREAT, 0666); - break; - case readEnable: - fd = open(rpath, O_RDONLY); - break; - case writeEnable: - fd = open(rpath, O_WRONLY | O_CREAT, 0666); - break; - } + switch(*access) { + case readEnableAllowWrite: + case readWriteEnable: + fd = open(rpath, O_RDWR | O_CREAT, 0666); + break; + case readEnable: + fd = open(rpath, O_RDONLY); + break; + case writeEnable: + fd = open(rpath, O_WRONLY | O_CREAT, 0666); + break; + } - if (*access == readEnableAllowWrite) { - if (fd < 0) { - *access = readEnable; - continue; - } - *access = readWriteEnable; - } - break; - } - if (fd < 0) { - *error = map_errno_path(path); - } + if (*access == readEnableAllowWrite) { + if (fd < 0) { + *access = readEnable; + continue; + } + *access = readWriteEnable; + } + break; + } + if (fd < 0) { + *error = map_errno_path(path); + } - return fd; + return fd; } #elif defined __sun static int open_resource_fork(const char *path, word16 *access, word16 *error) { - int tmp = open(path, O_RDONLY); - if (tmp < 0) { - *error = map_errno_path(path); - return -1; - } + int tmp = open(path, O_RDONLY); + if (tmp < 0) { + *error = map_errno_path(path); + return -1; + } - int fd = -1; - for(;;) { + int fd = -1; + for(;;) { - switch(*access) { - case readEnableAllowWrite: - case readWriteEnable: - fd = openat(tmp, XATTR_RESOURCEFORK_NAME, O_RDWR | O_CREAT | O_XATTR, 0666); - break; - case readEnable: - fd = openat(tmp, XATTR_RESOURCEFORK_NAME, O_RDONLY | O_XATTR); - break; - case writeEnable: - fd = openat(tmp, XATTR_RESOURCEFORK_NAME, O_WRONLY | O_CREAT | O_XATTR, 0666); - break; - } + switch(*access) { + case readEnableAllowWrite: + case readWriteEnable: + fd = openat(tmp, XATTR_RESOURCEFORK_NAME, O_RDWR | O_CREAT | O_XATTR, 0666); + break; + case readEnable: + fd = openat(tmp, XATTR_RESOURCEFORK_NAME, O_RDONLY | O_XATTR); + break; + case writeEnable: + fd = openat(tmp, XATTR_RESOURCEFORK_NAME, O_WRONLY | O_CREAT | O_XATTR, 0666); + break; + } - if (*access == readEnableAllowWrite) { - if (fd < 0) { - *access = readEnable; - continue; - } - *access = readWriteEnable; - } - break; - } + if (*access == readEnableAllowWrite) { + if (fd < 0) { + *access = readEnable; + continue; + } + *access = readWriteEnable; + } + break; + } - if (fd < 0) { - *error = map_errno_path(path); - close(tmp); - return -1; - } - close(tmp); + if (fd < 0) { + *error = map_errno_path(path); + close(tmp); + return -1; + } + close(tmp); - return fd; + return fd; } #elif defined __linux__ static int open_resource_fork(const char *path, word16 *access, word16 *error) { - *error = resForkNotFound; - return -1; + *error = resForkNotFound; + return -1; } #else static int open_resource_fork(const char *path, word16 *access, word16 *error) { - *error = resForkNotFound; - return -1; + *error = resForkNotFound; + return -1; } #endif @@ -1576,471 +1578,471 @@ static int open_resource_fork(const char *path, word16 *access, word16 *error) { static word32 fst_open(int class, const char *path) { - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - struct file_info fi; - word16 rv = 0; + struct file_info fi; + word16 rv = 0; - rv = get_file_info(path, &fi); - if (rv) return rv; + rv = get_file_info(path, &fi); + if (rv) return rv; - int fd = -1; - int type = regular_file; - struct directory *dd = NULL; + int fd = -1; + int type = regular_file; + struct directory *dd = NULL; - word16 pcount = 0; - word16 request_access = readEnableAllowWrite; - word16 access = 0; - word16 resource_number = 0; - if (class) { - pcount = get_memory16_c(pb, 0); - if (pcount >= 3) request_access = get_memory16_c(pb + OpenRecGS_requestAccess, 0); - if (pcount >= 4) resource_number = get_memory16_c(pb + OpenRecGS_resourceNumber, 0); - } + word16 pcount = 0; + word16 request_access = readEnableAllowWrite; + word16 access = 0; + word16 resource_number = 0; + if (class) { + pcount = get_memory16_c(pb, 0); + if (pcount >= 3) request_access = get_memory16_c(pb + OpenRecGS_requestAccess, 0); + if (pcount >= 4) resource_number = get_memory16_c(pb + OpenRecGS_resourceNumber, 0); + } - if (resource_number) { - if (resource_number > 1) return paramRangeErr; - type = resource_file; - } + if (resource_number) { + if (resource_number > 1) return paramRangeErr; + type = resource_file; + } - if (access > 3) return paramRangeErr; + if (access > 3) return paramRangeErr; - // special access checks for directories. - if (S_ISDIR(fi.st_mode)) { - if (resource_number) return resForkNotFound; - switch (request_access) { - case readEnableAllowWrite: - request_access = readEnable; - break; - case writeEnable: - case readWriteEnable: - return invalidAccess; - } - type = directory_file; - } + // special access checks for directories. + if (S_ISDIR(fi.st_mode)) { + if (resource_number) return resForkNotFound; + switch (request_access) { + case readEnableAllowWrite: + request_access = readEnable; + break; + case writeEnable: + case readWriteEnable: + return invalidAccess; + } + type = directory_file; + } - if (read_only) { - switch (request_access) { - case readEnableAllowWrite: - request_access = readEnable; - break; - case readWriteEnable: - case writeEnable: - return invalidAccess; - break; - } - } + if (read_only) { + switch (request_access) { + case readEnableAllowWrite: + request_access = readEnable; + break; + case readWriteEnable: + case writeEnable: + return invalidAccess; + break; + } + } - access = request_access; - switch(type) { - case regular_file: - fd = open_data_fork(path, &access, &rv); - break; - case resource_file: - fd = open_resource_fork(path, &access, &rv); - break; - case directory_file: - dd = read_directory(path, &rv); - break; - } + access = request_access; + switch(type) { + case regular_file: + fd = open_data_fork(path, &access, &rv); + break; + case resource_file: + fd = open_resource_fork(path, &access, &rv); + break; + case directory_file: + dd = read_directory(path, &rv); + break; + } - if (rv) return rv; + if (rv) return rv; - if (class) { - if (pcount >= 5) set_memory16_c(pb + OpenRecGS_access, access, 0); - if (pcount >= 6) set_memory16_c(pb + OpenRecGS_fileType, fi.file_type, 0); - if (pcount >= 7) set_memory32_c(pb + OpenRecGS_auxType, fi.aux_type, 0); - if (pcount >= 8) set_memory16_c(pb + OpenRecGS_storageType, fi. storage_type, 0); + if (class) { + if (pcount >= 5) set_memory16_c(pb + OpenRecGS_access, access, 0); + if (pcount >= 6) set_memory16_c(pb + OpenRecGS_fileType, fi.file_type, 0); + if (pcount >= 7) set_memory32_c(pb + OpenRecGS_auxType, fi.aux_type, 0); + if (pcount >= 8) set_memory16_c(pb + OpenRecGS_storageType, fi.storage_type, 0); - if (pcount >= 9) set_date_time_rec(pb + OpenRecGS_createDateTime, fi.create_date); - if (pcount >= 10) set_date_time_rec(pb + OpenRecGS_modDateTime, fi.modified_date); + if (pcount >= 9) set_date_time_rec(pb + OpenRecGS_createDateTime, fi.create_date); + if (pcount >= 10) set_date_time_rec(pb + OpenRecGS_modDateTime, fi.modified_date); - if (pcount >= 11) { - word16 fst_id = hfsFSID; - //if (fi.storage_type == 0x0f) fst_id = mfsFSID; + if (pcount >= 11) { + word16 fst_id = hfsFSID; + //if (fi.storage_type == 0x0f) fst_id = mfsFSID; - word32 option_list = get_memory24_c(pb + OpenRecGS_optionList, 0); - word32 tmp = set_option_list(option_list, fst_id, fi.finder_info, fi.has_fi ? 32 : 0); - if (!rv) rv = tmp; - } + word32 option_list = get_memory24_c(pb + OpenRecGS_optionList, 0); + word32 tmp = set_option_list(option_list, fst_id, fi.finder_info, fi.has_fi ? 32 : 0); + if (!rv) rv = tmp; + } - if (pcount >= 12) set_memory32_c(pb + OpenRecGS_eof, fi.eof, 0); - if (pcount >= 13) set_memory32_c(pb + OpenRecGS_blocksUsed, fi.blocks, 0); - if (pcount >= 14) set_memory32_c(pb + OpenRecGS_resourceEOF, fi.resource_eof, 0); - if (pcount >= 15) set_memory32_c(pb + OpenRecGS_resourceBlocks, fi.resource_blocks, 0); + if (pcount >= 12) set_memory32_c(pb + OpenRecGS_eof, fi.eof, 0); + if (pcount >= 13) set_memory32_c(pb + OpenRecGS_blocksUsed, fi.blocks, 0); + if (pcount >= 14) set_memory32_c(pb + OpenRecGS_resourceEOF, fi.resource_eof, 0); + if (pcount >= 15) set_memory32_c(pb + OpenRecGS_resourceBlocks, fi.resource_blocks, 0); - } - // prodos 16 doesn't return anything in the parameter block. + } + // prodos 16 doesn't return anything in the parameter block. - struct fd_entry *e = alloc_fd(); - if (!e) { - if (fd >=0) close(fd); - if (dd) free_directory(dd); - return outOfMem; - } - e->fd = fd; - e->dir = dd; + struct fd_entry *e = alloc_fd(); + if (!e) { + if (fd >=0) close(fd); + if (dd) free_directory(dd); + return outOfMem; + } + e->fd = fd; + e->dir = dd; - e->cookie = alloc_cookie(); - if (!e->cookie) { - free_fd(e); - return tooManyFilesOpen; - } + e->cookie = alloc_cookie(); + if (!e->cookie) { + free_fd(e); + return tooManyFilesOpen; + } - if (type == regular_file){ + if (type == regular_file) { - if (g_cfg_host_crlf) { - if (fi.file_type == 0x04 || fi.file_type == 0xb0) - e->translate = translate_crlf; - } + if (g_cfg_host_crlf) { + if (fi.file_type == 0x04 || fi.file_type == 0xb0) + e->translate = translate_crlf; + } - if (g_cfg_host_merlin && fi.file_type == 0x04) { - int n = strlen(path); - if (n >= 3 && path[n-1] == 'S' && path[n-2] == '.') - e->translate = translate_merlin; - } - } + if (g_cfg_host_merlin && fi.file_type == 0x04) { + int n = strlen(path); + if (n >= 3 && path[n-1] == 'S' && path[n-2] == '.') + e->translate = translate_merlin; + } + } - e->access = access; - e->path = strdup(path); - e->type = type; + e->access = access; + e->path = strdup(path); + e->type = type; - // insert it in the linked list. - e->next = fd_head; - fd_head = e; + // insert it in the linked list. + e->next = fd_head; + fd_head = e; - engine.xreg = e->cookie; - engine.yreg = access; // actual access, needed in fcr. + engine.xreg = e->cookie; + engine.yreg = access; // actual access, needed in fcr. - return rv; + return rv; } static word32 fst_read(int class) { - int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); + int cookie = engine.yreg; + struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + if (!e) return invalidRefNum; - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word32 data_buffer = 0; - word32 request_count = 0; - word32 transfer_count = 0; + word32 data_buffer = 0; + word32 request_count = 0; + word32 transfer_count = 0; - if (class) { - data_buffer = get_memory24_c(pb + IORecGS_dataBuffer, 0); - request_count = get_memory24_c(pb + IORecGS_requestCount, 0); - // pre-zero transfer count - set_memory32_c(pb + IORecGS_transferCount, 0, 0); - } else { - data_buffer = get_memory24_c(pb + FileIORec_dataBuffer, 0); - request_count = get_memory24_c(pb + FileIORec_requestCount, 0); - set_memory32_c(pb + FileIORec_transferCount, 0, 0); - } + if (class) { + data_buffer = get_memory24_c(pb + IORecGS_dataBuffer, 0); + request_count = get_memory24_c(pb + IORecGS_requestCount, 0); + // pre-zero transfer count + set_memory32_c(pb + IORecGS_transferCount, 0, 0); + } else { + data_buffer = get_memory24_c(pb + FileIORec_dataBuffer, 0); + request_count = get_memory24_c(pb + FileIORec_requestCount, 0); + set_memory32_c(pb + FileIORec_transferCount, 0, 0); + } - if (request_count == 0) return 0; + if (request_count == 0) return 0; - word16 newline_mask; - word32 rv = 0; - ssize_t ok; + word16 newline_mask; + word32 rv = 0; + ssize_t ok; - newline_mask = get_memory16_c(global_buffer, 0); - if (newline_mask) { - byte newline_table[256]; - for (int i = 0; i < 256; ++i) - newline_table[i] = get_memory_c(global_buffer + 2 + i, 0); + newline_mask = get_memory16_c(global_buffer, 0); + if (newline_mask) { + byte newline_table[256]; + for (int i = 0; i < 256; ++i) + newline_table[i] = get_memory_c(global_buffer + 2 + i, 0); - for (word32 i = 0 ; i < request_count; ++i) { - byte b; - ok = safe_read(e, &b, 1); - if (ok < 0) return map_errno(); - if (ok == 0) break; - transfer_count++; - set_memory_c(data_buffer++, b, 0); - if (newline_table[b & newline_mask]) break; - } - if (transfer_count == 0) rv = eofEncountered; - } - else { - byte *data = gc_malloc(request_count); - if (!data) return outOfMem; + for (word32 i = 0; i < request_count; ++i) { + byte b; + ok = safe_read(e, &b, 1); + if (ok < 0) return map_errno(); + if (ok == 0) break; + transfer_count++; + set_memory_c(data_buffer++, b, 0); + if (newline_table[b & newline_mask]) break; + } + if (transfer_count == 0) rv = eofEncountered; + } + else { + byte *data = gc_malloc(request_count); + if (!data) return outOfMem; - ok = safe_read(e, data, request_count); - if (ok < 0) rv = map_errno(); - if (ok == 0) rv = eofEncountered; - if (ok > 0) { - transfer_count = ok; - for (size_t i = 0; i < ok; ++i) { - set_memory_c(data_buffer + i, data[i], 0); - } - } - } + ok = safe_read(e, data, request_count); + if (ok < 0) rv = map_errno(); + if (ok == 0) rv = eofEncountered; + if (ok > 0) { + transfer_count = ok; + for (size_t i = 0; i < ok; ++i) { + set_memory_c(data_buffer + i, data[i], 0); + } + } + } - if (transfer_count) { - if (class) - set_memory32_c(pb + IORecGS_transferCount, transfer_count, 0); - else - set_memory32_c(pb + FileIORec_transferCount, transfer_count, 0); - } + if (transfer_count) { + if (class) + set_memory32_c(pb + IORecGS_transferCount, transfer_count, 0); + else + set_memory32_c(pb + FileIORec_transferCount, transfer_count, 0); + } - return rv; + return rv; } static word32 fst_write(int class) { - int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); + int cookie = engine.yreg; + struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + if (!e) return invalidRefNum; - if (!(e->access & writeEnable)) - return invalidAccess; + if (!(e->access & writeEnable)) + return invalidAccess; - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word32 data_buffer = 0; - word32 request_count = 0; + word32 data_buffer = 0; + word32 request_count = 0; - if (class) { - data_buffer = get_memory24_c(pb + IORecGS_dataBuffer, 0); - request_count = get_memory24_c(pb + IORecGS_requestCount, 0); - // pre-zero transfer count - set_memory32_c(pb + IORecGS_transferCount, 0, 0); - } else { - data_buffer = get_memory24_c(pb + FileIORec_dataBuffer, 0); - request_count = get_memory24_c(pb + FileIORec_requestCount, 0); - set_memory32_c(pb + FileIORec_transferCount, 0, 0); - } + if (class) { + data_buffer = get_memory24_c(pb + IORecGS_dataBuffer, 0); + request_count = get_memory24_c(pb + IORecGS_requestCount, 0); + // pre-zero transfer count + set_memory32_c(pb + IORecGS_transferCount, 0, 0); + } else { + data_buffer = get_memory24_c(pb + FileIORec_dataBuffer, 0); + request_count = get_memory24_c(pb + FileIORec_requestCount, 0); + set_memory32_c(pb + FileIORec_transferCount, 0, 0); + } - if (request_count == 0) return 0; - byte *data = gc_malloc(request_count); - if (!data) return outOfMem; + if (request_count == 0) return 0; + byte *data = gc_malloc(request_count); + if (!data) return outOfMem; - for (word32 i = 0; i < request_count; ++i) { - data[i] = get_memory_c(data_buffer + i,0); - } + for (word32 i = 0; i < request_count; ++i) { + data[i] = get_memory_c(data_buffer + i,0); + } - word32 rv = 0; - ssize_t ok = safe_write(e, data, request_count); - if (ok < 0) rv = map_errno(); - if (ok > 0) { - if (class) - set_memory32_c(pb + IORecGS_transferCount, ok, 0); - else - set_memory32_c(pb + FileIORec_transferCount, ok, 0); - } - return rv; + word32 rv = 0; + ssize_t ok = safe_write(e, data, request_count); + if (ok < 0) rv = map_errno(); + if (ok > 0) { + if (class) + set_memory32_c(pb + IORecGS_transferCount, ok, 0); + else + set_memory32_c(pb + FileIORec_transferCount, ok, 0); + } + return rv; } static word32 fst_close(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - return remove_fd(cookie); + return remove_fd(cookie); } static word32 fst_flush(int class) { - int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); + int cookie = engine.yreg; + struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + if (!e) return invalidRefNum; - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - int ok = fsync(e->fd); - if (ok < 0) return map_errno(); - return 0; + int ok = fsync(e->fd); + if (ok < 0) return map_errno(); + return 0; } static off_t get_offset(int fd, word16 base, word32 displacement) { - off_t pos = lseek(fd, 0, SEEK_CUR); - off_t eof = lseek(fd, 0, SEEK_END); - lseek(fd, pos, SEEK_SET); + off_t pos = lseek(fd, 0, SEEK_CUR); + off_t eof = lseek(fd, 0, SEEK_END); + lseek(fd, pos, SEEK_SET); - switch (base) { - case startPlus: - return displacement; - break; - case eofMinus: - return eof - displacement; - break; - case markPlus: - return pos + displacement; - break; - case markMinus: - return pos - displacement; - break; - default: - return -1; - } + switch (base) { + case startPlus: + return displacement; + break; + case eofMinus: + return eof - displacement; + break; + case markPlus: + return pos + displacement; + break; + case markMinus: + return pos - displacement; + break; + default: + return -1; + } } static word32 fst_set_mark(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + struct fd_entry *e = find_fd(cookie); + if (!e) return invalidRefNum; - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word16 base = 0; - word32 displacement = 0; - if (class) { - base = get_memory16_c(pb + SetPositionRecGS_base, 0); - displacement = get_memory32_c(pb + SetPositionRecGS_displacement, 0); - } else { - displacement = get_memory32_c(pb + MarkRec_position, 0); - } - if (base > markMinus) return paramRangeErr; + word16 base = 0; + word32 displacement = 0; + if (class) { + base = get_memory16_c(pb + SetPositionRecGS_base, 0); + displacement = get_memory32_c(pb + SetPositionRecGS_displacement, 0); + } else { + displacement = get_memory32_c(pb + MarkRec_position, 0); + } + if (base > markMinus) return paramRangeErr; - off_t offset = get_offset(e->fd, base, displacement); - if (offset < 0) return outOfRange; + off_t offset = get_offset(e->fd, base, displacement); + if (offset < 0) return outOfRange; - off_t ok = lseek(e->fd, offset, SEEK_SET); - if (ok < 0) return map_errno(); - return 0; + off_t ok = lseek(e->fd, offset, SEEK_SET); + if (ok < 0) return map_errno(); + return 0; } static word32 fst_set_eof(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + struct fd_entry *e = find_fd(cookie); + if (!e) return invalidRefNum; - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word16 base = 0; - word32 displacement = 0; - if (class) { - base = get_memory16_c(pb + SetPositionRecGS_base, 0); - displacement = get_memory32_c(pb + SetPositionRecGS_displacement, 0); - } else { - displacement = get_memory32_c(pb + EOFRec_eofPosition, 0); - } + word16 base = 0; + word32 displacement = 0; + if (class) { + base = get_memory16_c(pb + SetPositionRecGS_base, 0); + displacement = get_memory32_c(pb + SetPositionRecGS_displacement, 0); + } else { + displacement = get_memory32_c(pb + EOFRec_eofPosition, 0); + } - if (base > markMinus) return paramRangeErr; + if (base > markMinus) return paramRangeErr; - off_t offset = get_offset(e->fd, base, displacement); - if (offset < 0) return outOfRange; + off_t offset = get_offset(e->fd, base, displacement); + if (offset < 0) return outOfRange; - int ok = ftruncate(e->fd, offset); - if (ok < 0) return map_errno(); - return 0; + int ok = ftruncate(e->fd, offset); + if (ok < 0) return map_errno(); + return 0; } static word32 fst_get_mark(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + struct fd_entry *e = find_fd(cookie); + if (!e) return invalidRefNum; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - off_t pos = 0; + off_t pos = 0; - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - pos = lseek(e->fd, 0, SEEK_CUR); - if (pos < 0) return map_errno(); + pos = lseek(e->fd, 0, SEEK_CUR); + if (pos < 0) return map_errno(); - if (class) { - set_memory32_c(pb + PositionRecGS_position, pos, 0); - } else { - set_memory32_c(pb + MarkRec_position, pos, 0); - } + if (class) { + set_memory32_c(pb + PositionRecGS_position, pos, 0); + } else { + set_memory32_c(pb + MarkRec_position, pos, 0); + } - return 0; + return 0; } static word32 fst_get_eof(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + struct fd_entry *e = find_fd(cookie); + if (!e) return invalidRefNum; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - off_t eof = 0; - off_t pos = 0; + off_t eof = 0; + off_t pos = 0; - pos = lseek(e->fd, 0, SEEK_CUR); - eof = lseek(e->fd, 0, SEEK_END); - if (eof < 0) return map_errno(); - lseek(e->fd, pos, SEEK_SET); + pos = lseek(e->fd, 0, SEEK_CUR); + eof = lseek(e->fd, 0, SEEK_END); + if (eof < 0) return map_errno(); + lseek(e->fd, pos, SEEK_SET); - if (class) { - set_memory32_c(pb + PositionRecGS_position, eof, 0); - } else { - set_memory32_c(pb + MarkRec_position, eof, 0); - } + if (class) { + set_memory32_c(pb + PositionRecGS_position, eof, 0); + } else { + set_memory32_c(pb + MarkRec_position, eof, 0); + } - return 0; + return 0; } static void free_directory(struct directory *dd) { - if (!dd) return; - for (int i = 0; i < dd->num_entries; ++i) { - free(dd->entries[i]); - } - free(dd); + if (!dd) return; + for (int i = 0; i < dd->num_entries; ++i) { + free(dd->entries[i]); + } + free(dd); } static int qsort_callback(const void *a, const void *b) { - return strcmp(*(const char **)a, *(const char **)b); + return strcmp(*(const char **)a, *(const char **)b); } /* @@ -2048,682 +2050,682 @@ static int qsort_callback(const void *a, const void *b) { * */ static int filter_directory_entry(const char *name) { - if (!name[0]) return 1; - if (name[0] == '.') { - return 1; - /* - if (!strcmp(name, ".")) return 1; - if (!strcmp(name, "..")) return 1; - if (!strncmp(name, "._", 2)) return 1; // ._ resource fork - if (!strcmp(name, ".fseventsd")) return 1; - */ - } - return 0; + if (!name[0]) return 1; + if (name[0] == '.') { + return 1; + /* + if (!strcmp(name, ".")) return 1; + if (!strcmp(name, "..")) return 1; + if (!strncmp(name, "._", 2)) return 1; // ._ resource fork + if (!strcmp(name, ".fseventsd")) return 1; + */ + } + return 0; } static struct directory *read_directory(const char *path, word16 *error) { - DIR *dirp; - struct directory *dd; - int capacity = 100; - int size = sizeof(struct directory) + capacity * sizeof(char *); + DIR *dirp; + struct directory *dd; + int capacity = 100; + int size = sizeof(struct directory) + capacity * sizeof(char *); - dirp = opendir(path); - if (!dirp) { - *error = map_errno_path(path); - return NULL; - } + dirp = opendir(path); + if (!dirp) { + *error = map_errno_path(path); + return NULL; + } - dd = (struct directory *)malloc(size); - if (!dd) { - *error = outOfMem; - closedir(dirp); - return NULL; - } - memset(dd, 0, size); + dd = (struct directory *)malloc(size); + if (!dd) { + *error = outOfMem; + closedir(dirp); + return NULL; + } + memset(dd, 0, size); - for(;;) { - struct dirent *d = readdir(dirp); - if (!d) break; - if (filter_directory_entry(d->d_name)) continue; + for(;;) { + struct dirent *d = readdir(dirp); + if (!d) break; + if (filter_directory_entry(d->d_name)) continue; - if (dd->num_entries >= capacity) { - capacity += capacity; - size = sizeof(struct directory) + capacity * sizeof(char *); - struct directory * tmp = realloc(dd, size); - if (!tmp) { - *error = map_errno(); - free_directory(dd); - closedir(dirp); - return NULL; - } - dd = tmp; - } - dd->entries[dd->num_entries++] = strdup(d->d_name); - }; + if (dd->num_entries >= capacity) { + capacity += capacity; + size = sizeof(struct directory) + capacity * sizeof(char *); + struct directory * tmp = realloc(dd, size); + if (!tmp) { + *error = map_errno(); + free_directory(dd); + closedir(dirp); + return NULL; + } + dd = tmp; + } + dd->entries[dd->num_entries++] = strdup(d->d_name); + }; - closedir(dirp); + closedir(dirp); - // sort them.... - qsort(dd->entries, dd->num_entries, sizeof(char *), qsort_callback); + // sort them.... + qsort(dd->entries, dd->num_entries, sizeof(char *), qsort_callback); - return dd; + return dd; } static word32 fst_get_dir_entry(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + struct fd_entry *e = find_fd(cookie); + if (!e) return invalidRefNum; - if (e->type != directory_file) return badFileFormat; + if (e->type != directory_file) return badFileFormat; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word16 base = 0; - word16 pcount = 0; - word32 displacement = 0; - word32 name = 0; + word16 base = 0; + word16 pcount = 0; + word32 displacement = 0; + word32 name = 0; - if (class) { - pcount = get_memory16_c(pb, 0); - base = get_memory16_c(pb + DirEntryRecGS_base, 0); - displacement = get_memory16_c(pb + DirEntryRecGS_displacement, 0); - name = get_memory24_c(pb + DirEntryRecGS_name, 0); - } else { - base = get_memory16_c(pb + DirEntryRec_base, 0); - displacement = get_memory16_c(pb + DirEntryRec_displacement, 0); - name = get_memory24_c(pb + DirEntryRec_nameBuffer, 0); - } + if (class) { + pcount = get_memory16_c(pb, 0); + base = get_memory16_c(pb + DirEntryRecGS_base, 0); + displacement = get_memory16_c(pb + DirEntryRecGS_displacement, 0); + name = get_memory24_c(pb + DirEntryRecGS_name, 0); + } else { + base = get_memory16_c(pb + DirEntryRec_base, 0); + displacement = get_memory16_c(pb + DirEntryRec_displacement, 0); + name = get_memory24_c(pb + DirEntryRec_nameBuffer, 0); + } - if (base == 0 && displacement == 0) { - // count them up. - int count = e->dir->num_entries; - e->dir->displacement = 0; + if (base == 0 && displacement == 0) { + // count them up. + int count = e->dir->num_entries; + e->dir->displacement = 0; - if (class) { - if (pcount >= 6) set_memory16_c(pb + DirEntryRecGS_entryNum, count, 0); - } - else { - set_memory16_c(pb + DirEntryRec_entryNum, count, 0); - } + if (class) { + if (pcount >= 6) set_memory16_c(pb + DirEntryRecGS_entryNum, count, 0); + } + else { + set_memory16_c(pb + DirEntryRec_entryNum, count, 0); + } - return 0; - } + return 0; + } - int dir_displacement = e->dir->displacement; - switch (base) { - case 0: // displacement is absolute entry number. - break; - case 1: // displacement is added to the current displacement. - displacement = dir_displacement + displacement; - break; - case 2: // displacement is substracted from current displacement. - displacement = dir_displacement - displacement; - break; - default: - return paramRangeErr; - } - //if (displacement) --displacement; - --displacement; - if (displacement < 0) return endOfDir; - if (displacement >= e->dir->num_entries) return endOfDir; + int dir_displacement = e->dir->displacement; + switch (base) { + case 0: // displacement is absolute entry number. + break; + case 1: // displacement is added to the current displacement. + displacement = dir_displacement + displacement; + break; + case 2: // displacement is substracted from current displacement. + displacement = dir_displacement - displacement; + break; + default: + return paramRangeErr; + } + //if (displacement) --displacement; + --displacement; + if (displacement < 0) return endOfDir; + if (displacement >= e->dir->num_entries) return endOfDir; - word32 rv = 0; - const char *dname = e->dir->entries[displacement++]; - e->dir->displacement = displacement; - char *fullpath = append_path(e->path, dname); - struct file_info fi; - rv = get_file_info(fullpath, &fi); + word32 rv = 0; + const char *dname = e->dir->entries[displacement++]; + e->dir->displacement = displacement; + char *fullpath = append_path(e->path, dname); + struct file_info fi; + rv = get_file_info(fullpath, &fi); - if (dname) fprintf(stderr, " - %s", dname); + if (dname) fprintf(stderr, " - %s", dname); - // p16 and gs/os both use truncating c1 output string. - rv = set_gsstr_truncate(name, dname); + // p16 and gs/os both use truncating c1 output string. + rv = set_gsstr_truncate(name, dname); - if (class) { + if (class) { - if (pcount > 2) set_memory16_c(pb + DirEntryRecGS_flags, fi.storage_type == 0x05 ? 0x8000 : 0, 0); + if (pcount > 2) set_memory16_c(pb + DirEntryRecGS_flags, fi.storage_type == 0x05 ? 0x8000 : 0, 0); - if (pcount >= 6) set_memory16_c(pb + DirEntryRecGS_entryNum, displacement, 0); - if (pcount >= 7) set_memory16_c(pb + DirEntryRecGS_fileType, fi.file_type, 0); - if (pcount >= 8) set_memory32_c(pb + DirEntryRecGS_eof, fi.eof, 0); - if (pcount >= 9) set_memory32_c(pb + DirEntryRecGS_blockCount, fi.blocks, 0); + if (pcount >= 6) set_memory16_c(pb + DirEntryRecGS_entryNum, displacement, 0); + if (pcount >= 7) set_memory16_c(pb + DirEntryRecGS_fileType, fi.file_type, 0); + if (pcount >= 8) set_memory32_c(pb + DirEntryRecGS_eof, fi.eof, 0); + if (pcount >= 9) set_memory32_c(pb + DirEntryRecGS_blockCount, fi.blocks, 0); - if (pcount >= 10) set_date_time_rec(pb + DirEntryRecGS_createDateTime, fi.create_date); - if (pcount >= 11) set_date_time_rec(pb + DirEntryRecGS_modDateTime, fi.modified_date); + if (pcount >= 10) set_date_time_rec(pb + DirEntryRecGS_createDateTime, fi.create_date); + if (pcount >= 11) set_date_time_rec(pb + DirEntryRecGS_modDateTime, fi.modified_date); - if (pcount >= 12) set_memory16_c(pb + DirEntryRecGS_access, fi.access, 0); - if (pcount >= 13) set_memory32_c(pb + DirEntryRecGS_auxType, fi.aux_type, 0); - if (pcount >= 14) set_memory16_c(pb + DirEntryRecGS_fileSysID, mfsFSID, 0); + if (pcount >= 12) set_memory16_c(pb + DirEntryRecGS_access, fi.access, 0); + if (pcount >= 13) set_memory32_c(pb + DirEntryRecGS_auxType, fi.aux_type, 0); + if (pcount >= 14) set_memory16_c(pb + DirEntryRecGS_fileSysID, mfsFSID, 0); - if (pcount >= 15) { - word16 fst_id = hfsFSID; - //if (fi.storage_type == 0x0f) fst_id = mfsFSID; - word32 option_list = get_memory24_c(pb + DirEntryRecGS_optionList, 0); - word32 tmp = set_option_list(option_list, fst_id, fi.finder_info, fi.has_fi ? 32 : 0); - if (!rv) rv = tmp; - } + if (pcount >= 15) { + word16 fst_id = hfsFSID; + //if (fi.storage_type == 0x0f) fst_id = mfsFSID; + word32 option_list = get_memory24_c(pb + DirEntryRecGS_optionList, 0); + word32 tmp = set_option_list(option_list, fst_id, fi.finder_info, fi.has_fi ? 32 : 0); + if (!rv) rv = tmp; + } - if (pcount >= 16) set_memory32_c(pb + DirEntryRecGS_resourceEOF, fi.resource_eof, 0); - if (pcount >= 17) set_memory32_c(pb + DirEntryRecGS_resourceBlocks, fi.resource_blocks, 0); - } - else { - set_memory16_c(pb + DirEntryRec_flags, fi.storage_type == 0x05 ? 0x8000 : 0, 0); + if (pcount >= 16) set_memory32_c(pb + DirEntryRecGS_resourceEOF, fi.resource_eof, 0); + if (pcount >= 17) set_memory32_c(pb + DirEntryRecGS_resourceBlocks, fi.resource_blocks, 0); + } + else { + set_memory16_c(pb + DirEntryRec_flags, fi.storage_type == 0x05 ? 0x8000 : 0, 0); - set_memory16_c(pb + DirEntryRec_entryNum, displacement, 0); - set_memory16_c(pb + DirEntryRec_fileType, fi.file_type, 0); - set_memory32_c(pb + DirEntryRec_endOfFile, fi.eof, 0); - set_memory32_c(pb + DirEntryRec_blockCount, fi.blocks, 0); + set_memory16_c(pb + DirEntryRec_entryNum, displacement, 0); + set_memory16_c(pb + DirEntryRec_fileType, fi.file_type, 0); + set_memory32_c(pb + DirEntryRec_endOfFile, fi.eof, 0); + set_memory32_c(pb + DirEntryRec_blockCount, fi.blocks, 0); - set_date_time_rec(pb + DirEntryRec_createTime, fi.create_date); - set_date_time_rec(pb + DirEntryRec_modTime, fi.modified_date); + set_date_time_rec(pb + DirEntryRec_createTime, fi.create_date); + set_date_time_rec(pb + DirEntryRec_modTime, fi.modified_date); - set_memory16_c(pb + DirEntryRec_access, fi.access, 0); - set_memory32_c(pb + DirEntryRec_auxType, fi.aux_type, 0); - set_memory16_c(pb + DirEntryRec_fileSysID, mfsFSID, 0); + set_memory16_c(pb + DirEntryRec_access, fi.access, 0); + set_memory32_c(pb + DirEntryRec_auxType, fi.aux_type, 0); + set_memory16_c(pb + DirEntryRec_fileSysID, mfsFSID, 0); - } + } - return rv; + return rv; } static word32 fst_change_path(int class, const char *path1, const char *path2) { - /* make sure they're not trying to rename the volume... */ - struct stat st; - if (stat(path1, &st) < 0) return map_errno_path(path1); - if (st.st_dev == root_dev && st.st_ino == root_ino) - return invalidAccess; + /* make sure they're not trying to rename the volume... */ + struct stat st; + if (stat(path1, &st) < 0) return map_errno_path(path1); + if (st.st_dev == root_dev && st.st_ino == root_ino) + return invalidAccess; - // rename will delete any previous file. - if (rename(path1, path2) < 0) return map_errno_path(path2); - return 0; + // rename will delete any previous file. + if (rename(path1, path2) < 0) return map_errno_path(path2); + return 0; } static word32 fst_format(int class) { - return notBlockDev; + return notBlockDev; } static word32 fst_erase(int class) { - return notBlockDev; + return notBlockDev; } static const char *call_name(word16 call) { - static char* class1[] = { - // 0x00 - "", - "CreateGS", - "DestroyGS", - "", - "ChangePathGS", - "SetFileInfoGS", - "GetFileInfoGS", - "JudgeNameGS", - "VolumeGS", - "", - "", - "ClearBackupGS", - "", - "", - "", - "", + static char* class1[] = { + // 0x00 + "", + "CreateGS", + "DestroyGS", + "", + "ChangePathGS", + "SetFileInfoGS", + "GetFileInfoGS", + "JudgeNameGS", + "VolumeGS", + "", + "", + "ClearBackupGS", + "", + "", + "", + "", - // 0x10 - "OpenGS", - "", - "ReadGS", - "WriteGS", - "CloseGS", - "FlushGS", - "SetMarkGS", - "GetMarkGS", - "SetEOFGS", - "GetEOFGS", - "", - "", - "GetDirEntryGS", - "", - "", - "", + // 0x10 + "OpenGS", + "", + "ReadGS", + "WriteGS", + "CloseGS", + "FlushGS", + "SetMarkGS", + "GetMarkGS", + "SetEOFGS", + "GetEOFGS", + "", + "", + "GetDirEntryGS", + "", + "", + "", - // 0x20 - "", - "", - "", - "", - "FormatGS", - "EraseDiskGS", - }; + // 0x20 + "", + "", + "", + "", + "FormatGS", + "EraseDiskGS", + }; - static char* class0[] = { - // 0x00 - "", - "CREATE", - "DESTROY", - "", - "CHANGE_PATH", - "SET_FILE_INFO", - "GET_FILE_INFO", - "", - "VOLUME", - "", - "", - "CLEAR_BACKUP_BIT", - "", - "", - "", - "", + static char* class0[] = { + // 0x00 + "", + "CREATE", + "DESTROY", + "", + "CHANGE_PATH", + "SET_FILE_INFO", + "GET_FILE_INFO", + "", + "VOLUME", + "", + "", + "CLEAR_BACKUP_BIT", + "", + "", + "", + "", - // 0X10 - "OPEN", - "", - "READ", - "WRITE", - "CLOSE", - "FLUSH", - "SET_MARK", - "GET_MARK", - "SET_EOF", - "GET_EOF", - "", - "", - "GET_DIR_ENTRY", - "", - "", - "", + // 0X10 + "OPEN", + "", + "READ", + "WRITE", + "CLOSE", + "FLUSH", + "SET_MARK", + "GET_MARK", + "SET_EOF", + "GET_EOF", + "", + "", + "GET_DIR_ENTRY", + "", + "", + "", - // 0X20 - "", - "", - "", - "", - "FORMAT", - "ERASE_DISK", - }; + // 0X20 + "", + "", + "", + "", + "FORMAT", + "ERASE_DISK", + }; - if (call & 0x8000) { - static char *sys[] = { - "", - "fst_startup", - "fst_shutdown", - "fst_remove_vcr", - "fst_deferred_flush" - }; - call &= ~0x8000; + if (call & 0x8000) { + static char *sys[] = { + "", + "fst_startup", + "fst_shutdown", + "fst_remove_vcr", + "fst_deferred_flush" + }; + call &= ~0x8000; - if (call < sizeof(sys) / sizeof(sys[0])) - return sys[call]; + if (call < sizeof(sys) / sizeof(sys[0])) + return sys[call]; - return ""; - } + return ""; + } - int class = call >> 13; - call &= 0x1fff; - switch(class) { - case 0: - if (call < sizeof(class0) / sizeof(class0[0])) - return class0[call]; - break; - case 1: - if (call < sizeof(class1) / sizeof(class1[0])) - return class1[call]; - break; + int class = call >> 13; + call &= 0x1fff; + switch(class) { + case 0: + if (call < sizeof(class0) / sizeof(class0[0])) + return class0[call]; + break; + case 1: + if (call < sizeof(class1) / sizeof(class1[0])) + return class1[call]; + break; - } - return ""; + } + return ""; } static const char *error_name(word16 error) { - static char *errors[] = { - "", - "badSystemCall", - "", - "", - "invalidPcount", - "", - "", - "gsosActive", - "", - "", - "", - "", - "", - "", - "", - "", - // 0x10 - "devNotFound", - "invalidDevNum", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - // 0x20 - "drvrBadReq", - "drvrBadCode", - "drvrBadParm", - "drvrNotOpen", - "drvrPriorOpen", - "irqTableFull", - "drvrNoResrc", - "drvrIOError", - "drvrNoDevice", - "drvrBusy", - "", - "drvrWrtProt", - "drvrBadCount", - "drvrBadBlock", - "drvrDiskSwitch", - "drvrOffLine", - // 0x30 - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - // 0x40 - "badPathSyntax", - "", - "tooManyFilesOpen", - "invalidRefNum", - "pathNotFound", - "volNotFound", - "fileNotFound", - "dupPathname", - "volumeFull", - "volDirFull", - "badFileFormat", - "badStoreType", - "eofEncountered", - "outOfRange", - "invalidAccess", - "buffTooSmall", - // 0x50 - "fileBusy", - "dirError", - "unknownVol", - "paramRangeErr", - "outOfMem", - "", - "", - "dupVolume", - "notBlockDev", - "invalidLevel", - "damagedBitMap", - "badPathNames", - "notSystemFile", - "osUnsupported", - "", - "stackOverflow", - // 0x60 - "dataUnavail", - "endOfDir", - "invalidClass", - "resForkNotFound", - "invalidFSTID", - "invalidFSTop", - "fstCaution", - "devNameErr", - "defListFull", - "supListFull", - "fstError", - "", - "", - "", - "", - "", - //0x70 - "resExistsErr", - "resAddErr", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - //0x80 - "", - "", - "", - "", - "", - "", - "", - "", - "networkError" - }; + static char *errors[] = { + "", + "badSystemCall", + "", + "", + "invalidPcount", + "", + "", + "gsosActive", + "", + "", + "", + "", + "", + "", + "", + "", + // 0x10 + "devNotFound", + "invalidDevNum", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + // 0x20 + "drvrBadReq", + "drvrBadCode", + "drvrBadParm", + "drvrNotOpen", + "drvrPriorOpen", + "irqTableFull", + "drvrNoResrc", + "drvrIOError", + "drvrNoDevice", + "drvrBusy", + "", + "drvrWrtProt", + "drvrBadCount", + "drvrBadBlock", + "drvrDiskSwitch", + "drvrOffLine", + // 0x30 + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + // 0x40 + "badPathSyntax", + "", + "tooManyFilesOpen", + "invalidRefNum", + "pathNotFound", + "volNotFound", + "fileNotFound", + "dupPathname", + "volumeFull", + "volDirFull", + "badFileFormat", + "badStoreType", + "eofEncountered", + "outOfRange", + "invalidAccess", + "buffTooSmall", + // 0x50 + "fileBusy", + "dirError", + "unknownVol", + "paramRangeErr", + "outOfMem", + "", + "", + "dupVolume", + "notBlockDev", + "invalidLevel", + "damagedBitMap", + "badPathNames", + "notSystemFile", + "osUnsupported", + "", + "stackOverflow", + // 0x60 + "dataUnavail", + "endOfDir", + "invalidClass", + "resForkNotFound", + "invalidFSTID", + "invalidFSTop", + "fstCaution", + "devNameErr", + "defListFull", + "supListFull", + "fstError", + "", + "", + "", + "", + "", + //0x70 + "resExistsErr", + "resAddErr", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + //0x80 + "", + "", + "", + "", + "", + "", + "", + "", + "networkError" + }; - if (error < sizeof(errors) / sizeof(errors[0])) - return errors[error]; - return ""; + if (error < sizeof(errors) / sizeof(errors[0])) + return errors[error]; + return ""; } void host_fst(void) { - - /* - * input: - * c = set - * a = default error code - * x = gs/os callnum - * y = [varies] - * - * output: - * c = set/clear - * a = error code/0 - * x = varies - * y = varies - */ + + /* + * input: + * c = set + * a = default error code + * x = gs/os callnum + * y = [varies] + * + * output: + * c = set/clear + * a = error code/0 + * x = varies + * y = varies + */ - word32 acc = 0; - word16 call = engine.xreg; + word32 acc = 0; + word16 call = engine.xreg; - fprintf(stderr, "Host FST: %04x %s", call, call_name(call)); + fprintf(stderr, "Host FST: %04x %s", call, call_name(call)); - if (call & 0x8000) { - fputs("\n", stderr); - // system level. - switch(call) { - case 0x8001: - acc = fst_startup(); - break; - case 0x8002: - acc = fst_shutdown(); - break; - default: - acc = badSystemCall; - break; - } - } else { + if (call & 0x8000) { + fputs("\n", stderr); + // system level. + switch(call) { + case 0x8001: + acc = fst_startup(); + break; + case 0x8002: + acc = fst_shutdown(); + break; + default: + acc = badSystemCall; + break; + } + } else { - if (!root) { - acc = networkError; - engine.acc = acc; - SEC(); - fprintf(stderr, " %02x %s\n", acc, error_name(acc)); + if (!root) { + acc = networkError; + engine.acc = acc; + SEC(); + fprintf(stderr, " %02x %s\n", acc, error_name(acc)); - return; - } + return; + } - int class = call >> 13; - call &= 0x1fff; + int class = call >> 13; + call &= 0x1fff; - if (class > 1) { - acc = invalidClass; - engine.acc = acc; - SEC(); - fprintf(stderr, " %02x %s\n", acc, error_name(acc)); + if (class > 1) { + acc = invalidClass; + engine.acc = acc; + SEC(); + fprintf(stderr, " %02x %s\n", acc, error_name(acc)); - return; - } + return; + } - char *path1 = NULL; - char *path2 = NULL; - char *path3 = NULL; - char *path4 = NULL; - const char *cp; + char *path1 = NULL; + char *path2 = NULL; + char *path3 = NULL; + char *path4 = NULL; + const char *cp; - switch(call & 0xff) { - case 0x01: - case 0x02: - case 0x05: - case 0x06: - case 0x0b: - case 0x10: - path1 = get_path1(); - break; - case 0x04: - path1 = get_path1(); - path2 = get_path2(); - break; - } + switch(call & 0xff) { + case 0x01: + case 0x02: + case 0x05: + case 0x06: + case 0x0b: + case 0x10: + path1 = get_path1(); + break; + case 0x04: + path1 = get_path1(); + path2 = get_path2(); + break; + } - if (path1) fprintf(stderr, " - %s", path1); - if (path2) fprintf(stderr, " - %s", path2); + if (path1) fprintf(stderr, " - %s", path1); + if (path2) fprintf(stderr, " - %s", path2); - switch(call & 0xff) { - case 0x01: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); + switch(call & 0xff) { + case 0x01: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); - acc = fst_create(class, path3); - break; - case 0x02: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); + acc = fst_create(class, path3); + break; + case 0x02: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); - acc = fst_destroy(class, path3); - break; - case 0x04: + acc = fst_destroy(class, path3); + break; + case 0x04: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); - cp = check_path(path2, &acc); - if (acc) break; - path4 = append_path(root, cp); + cp = check_path(path2, &acc); + if (acc) break; + path4 = append_path(root, cp); - acc = fst_change_path(class, path3, path4); - break; - case 0x05: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); + acc = fst_change_path(class, path3, path4); + break; + case 0x05: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); - acc = fst_set_file_info(class, path3); - break; - case 0x06: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); - acc = fst_get_file_info(class, path3); - break; - case 0x07: - acc = fst_judge_name(class, path1); - break; - case 0x08: - acc = fst_volume(class); - break; - case 0x0b: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); - acc = fst_clear_backup(class, path3); - break; - case 0x10: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); - acc = fst_open(class, path3); - break; - case 0x012: - acc = fst_read(class); - break; - case 0x013: - acc = fst_write(class); - break; - case 0x14: - acc = fst_close(class); - break; - case 0x15: - acc = fst_flush(class); - break; - case 0x16: - acc = fst_set_mark(class); - break; - case 0x17: - acc = fst_get_mark(class); - break; - case 0x18: - acc = fst_set_eof(class); - break; - case 0x19: - acc = fst_get_eof(class); - break; - case 0x1c: - acc = fst_get_dir_entry(class); - break; - case 0x24: - acc = fst_format(class); - break; - case 0x25: - acc = fst_erase(class); - break; + acc = fst_set_file_info(class, path3); + break; + case 0x06: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); + acc = fst_get_file_info(class, path3); + break; + case 0x07: + acc = fst_judge_name(class, path1); + break; + case 0x08: + acc = fst_volume(class); + break; + case 0x0b: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); + acc = fst_clear_backup(class, path3); + break; + case 0x10: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); + acc = fst_open(class, path3); + break; + case 0x012: + acc = fst_read(class); + break; + case 0x013: + acc = fst_write(class); + break; + case 0x14: + acc = fst_close(class); + break; + case 0x15: + acc = fst_flush(class); + break; + case 0x16: + acc = fst_set_mark(class); + break; + case 0x17: + acc = fst_get_mark(class); + break; + case 0x18: + acc = fst_set_eof(class); + break; + case 0x19: + acc = fst_get_eof(class); + break; + case 0x1c: + acc = fst_get_dir_entry(class); + break; + case 0x24: + acc = fst_format(class); + break; + case 0x25: + acc = fst_erase(class); + break; - default: - acc = invalidFSTop; - break; - } - fputs("\n", stderr); - } + default: + acc = invalidFSTop; + break; + } + fputs("\n", stderr); + } - if (acc) fprintf(stderr, " %02x %s\n", acc, error_name(acc)); + if (acc) fprintf(stderr, " %02x %s\n", acc, error_name(acc)); - gc_free(); + gc_free(); - engine.acc = acc; - if (acc) SEC(); - else CLC(); -} \ No newline at end of file + engine.acc = acc; + if (acc) SEC(); + else CLC(); +} diff --git a/src/imagewriter.cpp b/src/imagewriter.cpp index 2e7de6d..a60119d 100644 --- a/src/imagewriter.cpp +++ b/src/imagewriter.cpp @@ -1,23 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Copyright (C) 2010 - 2011 by GSport contributors - - Based on the KEGS emulator written by and Copyright (C) 2003 Kent Dickey - - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the - Free Software Foundation; either version 2 of the License, or (at your - option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /* * Copyright (C) 2002-2004 The DOSBox Team diff --git a/src/iwm.c b/src/iwm.c index 041c5c2..cecce5f 100644 --- a/src/iwm.c +++ b/src/iwm.c @@ -1,77 +1,77 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include "glog.h" extern int Verbose; -extern word32 g_vbl_count; // OG change int to word32 +extern word32 g_vbl_count; // OG change int to word32 extern int g_c036_val_speed; const byte phys_to_dos_sec[] = { - 0x00, 0x07, 0x0e, 0x06, 0x0d, 0x05, 0x0c, 0x04, - 0x0b, 0x03, 0x0a, 0x02, 0x09, 0x01, 0x08, 0x0f + 0x00, 0x07, 0x0e, 0x06, 0x0d, 0x05, 0x0c, 0x04, + 0x0b, 0x03, 0x0a, 0x02, 0x09, 0x01, 0x08, 0x0f }; const byte phys_to_prodos_sec[] = { - 0x00, 0x08, 0x01, 0x09, 0x02, 0x0a, 0x03, 0x0b, - 0x04, 0x0c, 0x05, 0x0d, 0x06, 0x0e, 0x07, 0x0f + 0x00, 0x08, 0x01, 0x09, 0x02, 0x0a, 0x03, 0x0b, + 0x04, 0x0c, 0x05, 0x0d, 0x06, 0x0e, 0x07, 0x0f }; const byte to_disk_byte[] = { - 0x96, 0x97, 0x9a, 0x9b, 0x9d, 0x9e, 0x9f, 0xa6, - 0xa7, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb2, 0xb3, + 0x96, 0x97, 0x9a, 0x9b, 0x9d, 0x9e, 0x9f, 0xa6, + 0xa7, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb2, 0xb3, /* 0x10 */ - 0xb4, 0xb5, 0xb6, 0xb7, 0xb9, 0xba, 0xbb, 0xbc, - 0xbd, 0xbe, 0xbf, 0xcb, 0xcd, 0xce, 0xcf, 0xd3, + 0xb4, 0xb5, 0xb6, 0xb7, 0xb9, 0xba, 0xbb, 0xbc, + 0xbd, 0xbe, 0xbf, 0xcb, 0xcd, 0xce, 0xcf, 0xd3, /* 0x20 */ - 0xd6, 0xd7, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, - 0xdf, 0xe5, 0xe6, 0xe7, 0xe9, 0xea, 0xeb, 0xec, + 0xd6, 0xd7, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, + 0xdf, 0xe5, 0xe6, 0xe7, 0xe9, 0xea, 0xeb, 0xec, /* 0x30 */ - 0xed, 0xee, 0xef, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, - 0xf7, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff + 0xed, 0xee, 0xef, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, + 0xf7, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff }; -int g_track_bytes_35[] = { - 0x200*12, - 0x200*11, - 0x200*10, - 0x200*9, - 0x200*8 +int g_track_bytes_35[] = { + 0x200*12, + 0x200*11, + 0x200*10, + 0x200*9, + 0x200*8 }; -int g_track_nibs_35[] = { - 816*12, - 816*11, - 816*10, - 816*9, - 816*8 +int g_track_nibs_35[] = { + 816*12, + 816*11, + 816*10, + 816*9, + 816*8 }; -int g_fast_disk_emul = 1; -int g_slow_525_emul_wr = 0; -double g_dcycs_end_emul_wr = 0.0; -int g_fast_disk_unnib = 0; -int g_iwm_fake_fast = 0; +int g_fast_disk_emul = 1; +int g_slow_525_emul_wr = 0; +double g_dcycs_end_emul_wr = 0.0; +int g_fast_disk_unnib = 0; +int g_iwm_fake_fast = 0; -int from_disk_byte[256]; -int from_disk_byte_valid = 0; +int from_disk_byte[256]; +int from_disk_byte_valid = 0; -Iwm iwm; +Iwm iwm; extern int g_c031_disk35; -int g_iwm_motor_on = 0; +int g_iwm_motor_on = 0; -int g_check_nibblization = 0; +int g_check_nibblization = 0; /* prototypes for IWM special routs */ int iwm_read_data_35(Disk *dsk, int fast_disk_emul, double dcycs); @@ -79,240 +79,226 @@ int iwm_read_data_525(Disk *dsk, int fast_disk_emul, double dcycs); void iwm_write_data_35(Disk *dsk, word32 val, int fast_disk_emul, double dcycs); void iwm_write_data_525(Disk *dsk, word32 val, int fast_disk_emul,double dcycs); -void -iwm_init_drive(Disk *dsk, int smartport, int drive, int disk_525) -{ - dsk->dcycs_last_read = 0.0; - dsk->name_ptr = 0; - dsk->partition_name = 0; - dsk->partition_num = -1; - dsk->file = 0; - dsk->force_size = 0; - dsk->image_start = 0; - dsk->image_size = 0; - dsk->smartport = smartport; - dsk->disk_525 = disk_525; - dsk->drive = drive; - dsk->cur_qtr_track = 0; - dsk->image_type = 0; - dsk->vol_num = 254; - dsk->write_prot = 1; - dsk->write_through_to_unix = 0; - dsk->disk_dirty = 0; - dsk->just_ejected = 0; - dsk->last_phase = 0; - dsk->nib_pos = 0; - dsk->num_tracks = 0; - dsk->trks = 0; +void iwm_init_drive(Disk *dsk, int smartport, int drive, int disk_525) { + dsk->dcycs_last_read = 0.0; + dsk->name_ptr = 0; + dsk->partition_name = 0; + dsk->partition_num = -1; + dsk->file = 0; + dsk->force_size = 0; + dsk->image_start = 0; + dsk->image_size = 0; + dsk->smartport = smartport; + dsk->disk_525 = disk_525; + dsk->drive = drive; + dsk->cur_qtr_track = 0; + dsk->image_type = 0; + dsk->vol_num = 254; + dsk->write_prot = 1; + dsk->write_through_to_unix = 0; + dsk->disk_dirty = 0; + dsk->just_ejected = 0; + dsk->last_phase = 0; + dsk->nib_pos = 0; + dsk->num_tracks = 0; + dsk->trks = 0; } -void -disk_set_num_tracks(Disk *dsk, int num_tracks) -{ - int i; +void disk_set_num_tracks(Disk *dsk, int num_tracks) { + int i; - if(dsk->trks != 0) { - /* This should not be necessary! */ - free(dsk->trks); - halt_printf("Needed to free dsk->trks: %p\n", dsk->trks); - } - dsk->num_tracks = num_tracks; - dsk->trks = (Trk *)malloc(num_tracks * sizeof(Trk)); + if(dsk->trks != 0) { + /* This should not be necessary! */ + free(dsk->trks); + halt_printf("Needed to free dsk->trks: %p\n", dsk->trks); + } + dsk->num_tracks = num_tracks; + dsk->trks = (Trk *)malloc(num_tracks * sizeof(Trk)); - for(i = 0; i < num_tracks; i++) { - dsk->trks[i].dsk = dsk; - dsk->trks[i].nib_area = 0; - dsk->trks[i].track_dirty = 0; - dsk->trks[i].overflow_size = 0; - dsk->trks[i].track_len = 0; - dsk->trks[i].unix_pos = -1; - dsk->trks[i].unix_len = -1; - } + for(i = 0; i < num_tracks; i++) { + dsk->trks[i].dsk = dsk; + dsk->trks[i].nib_area = 0; + dsk->trks[i].track_dirty = 0; + dsk->trks[i].overflow_size = 0; + dsk->trks[i].track_len = 0; + dsk->trks[i].unix_pos = -1; + dsk->trks[i].unix_len = -1; + } } -void -iwm_init() -{ - int val; - int i; +void iwm_init() { + int val; + int i; - for(i = 0; i < 2; i++) { - iwm_init_drive(&(iwm.drive525[i]), 0, i, 1); - iwm_init_drive(&(iwm.drive35[i]), 0, i, 0); - } + for(i = 0; i < 2; i++) { + iwm_init_drive(&(iwm.drive525[i]), 0, i, 1); + iwm_init_drive(&(iwm.drive35[i]), 0, i, 0); + } - for(i = 0; i < MAX_C7_DISKS; i++) { - iwm_init_drive(&(iwm.smartport[i]), 1, i, 0); - } + for(i = 0; i < MAX_C7_DISKS; i++) { + iwm_init_drive(&(iwm.smartport[i]), 1, i, 0); + } - if(from_disk_byte_valid == 0) { - for(i = 0; i < 256; i++) { - from_disk_byte[i] = -1; - } - for(i = 0; i < 64; i++) { - val = to_disk_byte[i]; - from_disk_byte[val] = i; - } - from_disk_byte_valid = 1; - } else { - halt_printf("iwm_init called twice!\n"); - } + if(from_disk_byte_valid == 0) { + for(i = 0; i < 256; i++) { + from_disk_byte[i] = -1; + } + for(i = 0; i < 64; i++) { + val = to_disk_byte[i]; + from_disk_byte[val] = i; + } + from_disk_byte_valid = 1; + } else { + halt_printf("iwm_init called twice!\n"); + } - iwm_reset(); + iwm_reset(); } // OG Added shut function to IWM // Free the memory, and more important free the open handle onto the disk -void -iwm_shut() -{ - int i; - for(i = 0; i < 2; i++) { - eject_disk(&iwm.drive525[i]); - eject_disk(&iwm.drive35[i]); - } +void iwm_shut() { + int i; + for(i = 0; i < 2; i++) { + eject_disk(&iwm.drive525[i]); + eject_disk(&iwm.drive35[i]); + } - for(i = 0; i < MAX_C7_DISKS; i++) { - eject_disk(&iwm.smartport[i]); - } + for(i = 0; i < MAX_C7_DISKS; i++) { + eject_disk(&iwm.smartport[i]); + } - from_disk_byte_valid = 0; + from_disk_byte_valid = 0; } -void -iwm_reset() -{ - iwm.q6 = 0; - iwm.q7 = 0; - iwm.motor_on = 0; - iwm.motor_on35 = 0; - iwm.motor_off = 0; - iwm.motor_off_vbl_count = 0; - iwm.step_direction35 = 0; - iwm.head35 = 0; - iwm.drive_select = 0; - iwm.iwm_mode = 0; - iwm.enable2 = 0; - iwm.reset = 0; - iwm.iwm_phase[0] = 0; - iwm.iwm_phase[1] = 0; - iwm.iwm_phase[2] = 0; - iwm.iwm_phase[3] = 0; - iwm.previous_write_val = 0; - iwm.previous_write_bits = 0; +void iwm_reset() { + iwm.q6 = 0; + iwm.q7 = 0; + iwm.motor_on = 0; + iwm.motor_on35 = 0; + iwm.motor_off = 0; + iwm.motor_off_vbl_count = 0; + iwm.step_direction35 = 0; + iwm.head35 = 0; + iwm.drive_select = 0; + iwm.iwm_mode = 0; + iwm.enable2 = 0; + iwm.reset = 0; + iwm.iwm_phase[0] = 0; + iwm.iwm_phase[1] = 0; + iwm.iwm_phase[2] = 0; + iwm.iwm_phase[3] = 0; + iwm.previous_write_val = 0; + iwm.previous_write_bits = 0; - g_iwm_motor_on = 0; - g_c031_disk35 = 0; + g_iwm_motor_on = 0; + g_c031_disk35 = 0; } -void -draw_iwm_status(int line, char *buf) -{ - char *flag[2][2]; - int apple35_sel; +void draw_iwm_status(int line, char *buf) { + char *flag[2][2]; + int apple35_sel; - flag[0][0] = " "; - flag[0][1] = " "; - flag[1][0] = " "; - flag[1][1] = " "; + flag[0][0] = " "; + flag[0][1] = " "; + flag[1][0] = " "; + flag[1][1] = " "; - apple35_sel = (g_c031_disk35 >> 6) & 1; - if(g_iwm_motor_on) { - flag[apple35_sel][iwm.drive_select] = "*"; - } + apple35_sel = (g_c031_disk35 >> 6) & 1; + if(g_iwm_motor_on) { + flag[apple35_sel][iwm.drive_select] = "*"; + } - #ifdef ACTIVEGS // OG Pass monitoring info - { - extern void ki_loading(int _motorOn,int _slot,int _drive, int _curtrack); - int curtrack=0; - if (apple35_sel) - curtrack = iwm.drive35[iwm.drive_select].cur_qtr_track ; - else - curtrack = iwm.drive525[iwm.drive_select].cur_qtr_track >> 2 ; + #ifdef ACTIVEGS // OG Pass monitoring info + { + extern void ki_loading(int _motorOn,int _slot,int _drive, int _curtrack); + int curtrack=0; + if (apple35_sel) + curtrack = iwm.drive35[iwm.drive_select].cur_qtr_track; + else + curtrack = iwm.drive525[iwm.drive_select].cur_qtr_track >> 2; - ki_loading(g_iwm_motor_on,apple35_sel?5:6,iwm.drive_select+1,curtrack); - } - #endif + ki_loading(g_iwm_motor_on,apple35_sel ? 5 : 6,iwm.drive_select+1,curtrack); + } + #endif - sprintf(buf, "s6d1:%2d%s s6d2:%2d%s s5d1:%2d/%d%s " - "s5d2:%2d/%d%s fast_disk_emul:%d,%d c036:%02x", - iwm.drive525[0].cur_qtr_track >> 2, flag[0][0], - iwm.drive525[1].cur_qtr_track >> 2, flag[0][1], - iwm.drive35[0].cur_qtr_track >> 1, - iwm.drive35[0].cur_qtr_track & 1, flag[1][0], - iwm.drive35[1].cur_qtr_track >> 1, - iwm.drive35[1].cur_qtr_track & 1, flag[1][1], - g_fast_disk_emul, g_slow_525_emul_wr, g_c036_val_speed); + sprintf(buf, "s6d1:%2d%s s6d2:%2d%s s5d1:%2d/%d%s " + "s5d2:%2d/%d%s fast_disk_emul:%d,%d c036:%02x", + iwm.drive525[0].cur_qtr_track >> 2, flag[0][0], + iwm.drive525[1].cur_qtr_track >> 2, flag[0][1], + iwm.drive35[0].cur_qtr_track >> 1, + iwm.drive35[0].cur_qtr_track & 1, flag[1][0], + iwm.drive35[1].cur_qtr_track >> 1, + iwm.drive35[1].cur_qtr_track & 1, flag[1][1], + g_fast_disk_emul, g_slow_525_emul_wr, g_c036_val_speed); - video_update_status_line(line, buf); + video_update_status_line(line, buf); } -void -iwm_flush_disk_to_unix(Disk *dsk) -{ - byte buffer[0x4000]; - int num_dirty; - int j; - int ret; - int unix_pos; - int unix_len; +void iwm_flush_disk_to_unix(Disk *dsk) { + byte buffer[0x4000]; + int num_dirty; + int j; + int ret; + int unix_pos; + int unix_len; - if(dsk->disk_dirty == 0 || dsk->write_through_to_unix == 0) { - return; - } + if(dsk->disk_dirty == 0 || dsk->write_through_to_unix == 0) { + return; + } - glogf("Writing disk %s to Unix", dsk->name_ptr); - dsk->disk_dirty = 0; - num_dirty = 0; + glogf("Writing disk %s to Unix", dsk->name_ptr); + dsk->disk_dirty = 0; + num_dirty = 0; - /* Dirty data! */ - for(j = 0; j < dsk->num_tracks; j++) { + /* Dirty data! */ + for(j = 0; j < dsk->num_tracks; j++) { - ret = disk_track_to_unix(dsk, j, &(buffer[0])); + ret = disk_track_to_unix(dsk, j, &(buffer[0])); - if(ret != 1 && ret != 0) { - glogf("iwm_flush_disk_to_unix ret: %d, cannot write image to unix", ret); - halt_printf("Adjusting image not to write through!\n"); - dsk->write_through_to_unix = 0; - break; - } + if(ret != 1 && ret != 0) { + glogf("iwm_flush_disk_to_unix ret: %d, cannot write image to unix", ret); + halt_printf("Adjusting image not to write through!\n"); + dsk->write_through_to_unix = 0; + break; + } - if(ret != 1) { - /* not at an even track, or not dirty */ - continue; - } - if((j & 3) != 0 && dsk->disk_525) { - halt_printf("Valid data on a non-whole trk: %03x\n", j); - continue; - } + if(ret != 1) { + /* not at an even track, or not dirty */ + continue; + } + if((j & 3) != 0 && dsk->disk_525) { + halt_printf("Valid data on a non-whole trk: %03x\n", j); + continue; + } - num_dirty++; + num_dirty++; - /* Write it out */ - unix_pos = dsk->trks[j].unix_pos; - unix_len = dsk->trks[j].unix_len; - if(unix_pos < 0 || unix_len < 0x1000) { - halt_printf("Disk:%s trk:%d, unix_pos:%08x, len:%08x\n", - dsk->name_ptr, j, unix_pos, unix_len); - break; - } + /* Write it out */ + unix_pos = dsk->trks[j].unix_pos; + unix_len = dsk->trks[j].unix_len; + if(unix_pos < 0 || unix_len < 0x1000) { + halt_printf("Disk:%s trk:%d, unix_pos:%08x, len:%08x\n", + dsk->name_ptr, j, unix_pos, unix_len); + break; + } - ret = fseek(dsk->file, unix_pos, SEEK_SET); - if(ret != 0) { - halt_printf("fseek 525: errno: %d\n", errno); - } + ret = fseek(dsk->file, unix_pos, SEEK_SET); + if(ret != 0) { + halt_printf("fseek 525: errno: %d\n", errno); + } - ret = fwrite(&(buffer[0]), 1, unix_len, dsk->file); - if(ret != unix_len) { - glogf("fwrite: %08x, errno:%d, qtrk: %02x, disk: %s", ret, errno, j, dsk->name_ptr); - } - } + ret = fwrite(&(buffer[0]), 1, unix_len, dsk->file); + if(ret != unix_len) { + glogf("fwrite: %08x, errno:%d, qtrk: %02x, disk: %s", ret, errno, j, dsk->name_ptr); + } + } - if(num_dirty == 0) { - halt_printf("Drive %s was dirty, but no track was dirty!", dsk->name_ptr); - } + if(num_dirty == 0) { + halt_printf("Drive %s was dirty, but no track was dirty!", dsk->name_ptr); + } } @@ -323,693 +309,663 @@ extern byte* g_bram_ptr; extern byte g_temp_boot_slot; extern byte g_orig_boot_slot; extern int g_config_gsplus_update_needed; -void -iwm_vbl_update(int doit_3_persec) -{ - Disk *dsk; - int motor_on; - int i; +void iwm_vbl_update(int doit_3_persec) { + Disk *dsk; + int motor_on; + int i; - if(iwm.motor_on && iwm.motor_off) { - if((word32)iwm.motor_off_vbl_count <= g_vbl_count) { - glogf("Disk timer expired, drive off: %08x", g_vbl_count); - iwm.motor_on = 0; - iwm.motor_off = 0; - if (g_temp_boot_slot != 254) { - // Drive is off, now's a good time to turn off the temp boot slot if it was on. - g_temp_boot_slot = 254; - g_bram_ptr[40] = g_orig_boot_slot; - clk_calculate_bram_checksum(); - g_config_gsplus_update_needed = 1; - } - } - } + if(iwm.motor_on && iwm.motor_off) { + if((word32)iwm.motor_off_vbl_count <= g_vbl_count) { + glogf("Disk timer expired, drive off: %08x", g_vbl_count); + iwm.motor_on = 0; + iwm.motor_off = 0; + if (g_temp_boot_slot != 254) { + // Drive is off, now's a good time to turn off the temp boot slot if it was on. + g_temp_boot_slot = 254; + g_bram_ptr[40] = g_orig_boot_slot; + clk_calculate_bram_checksum(); + g_config_gsplus_update_needed = 1; + } + } + } - if(!doit_3_persec) { - return; - } + if(!doit_3_persec) { + return; + } - motor_on = iwm.motor_on; - if(g_c031_disk35 & 0x40) { - motor_on = iwm.motor_on35; - if (g_temp_boot_slot != 254) { - // Now's a good time to turn off the temp boot slot if it was on. - g_temp_boot_slot = 254; - g_bram_ptr[40] = g_orig_boot_slot; - clk_calculate_bram_checksum(); - g_config_gsplus_update_needed = 1; - } + motor_on = iwm.motor_on; + if(g_c031_disk35 & 0x40) { + motor_on = iwm.motor_on35; + if (g_temp_boot_slot != 254) { + // Now's a good time to turn off the temp boot slot if it was on. + g_temp_boot_slot = 254; + g_bram_ptr[40] = g_orig_boot_slot; + clk_calculate_bram_checksum(); + g_config_gsplus_update_needed = 1; + } - } + } - if(motor_on == 0 || iwm.motor_off) { - /* Disk not spinning, see if any dirty tracks to flush */ - /* out to Unix */ - for(i = 0; i < 2; i++) { - dsk = &(iwm.drive525[i]); - iwm_flush_disk_to_unix(dsk); - } - for(i = 0; i < 2; i++) { - dsk = &(iwm.drive35[i]); - iwm_flush_disk_to_unix(dsk); - } - } + if(motor_on == 0 || iwm.motor_off) { + /* Disk not spinning, see if any dirty tracks to flush */ + /* out to Unix */ + for(i = 0; i < 2; i++) { + dsk = &(iwm.drive525[i]); + iwm_flush_disk_to_unix(dsk); + } + for(i = 0; i < 2; i++) { + dsk = &(iwm.drive35[i]); + iwm_flush_disk_to_unix(dsk); + } + } } -void -iwm_show_stats() -{ - glogf("IWM stats: q7,q6: %d, %d, reset,enable2: %d,%d, mode: %02x", - iwm.q7, iwm.q6, iwm.reset, iwm.enable2, iwm.iwm_mode); - glogf("motor: %d,%d, motor35:%d drive: %d, c031:%02x phs: %d %d %d %d", - iwm.motor_on, iwm.motor_off, g_iwm_motor_on, - iwm.drive_select, g_c031_disk35, - iwm.iwm_phase[0], iwm.iwm_phase[1], iwm.iwm_phase[2], - iwm.iwm_phase[3]); - glogf("iwm.drive525[0].file: %p, [1].file: %p", - iwm.drive525[0].file, iwm.drive525[1].file); - glogf("iwm.drive525[0].last_phase: %d, [1].last_phase: %d", - iwm.drive525[0].last_phase, iwm.drive525[1].last_phase); +void iwm_show_stats() { + glogf("IWM stats: q7,q6: %d, %d, reset,enable2: %d,%d, mode: %02x", + iwm.q7, iwm.q6, iwm.reset, iwm.enable2, iwm.iwm_mode); + glogf("motor: %d,%d, motor35:%d drive: %d, c031:%02x phs: %d %d %d %d", + iwm.motor_on, iwm.motor_off, g_iwm_motor_on, + iwm.drive_select, g_c031_disk35, + iwm.iwm_phase[0], iwm.iwm_phase[1], iwm.iwm_phase[2], + iwm.iwm_phase[3]); + glogf("iwm.drive525[0].file: %p, [1].file: %p", + iwm.drive525[0].file, iwm.drive525[1].file); + glogf("iwm.drive525[0].last_phase: %d, [1].last_phase: %d", + iwm.drive525[0].last_phase, iwm.drive525[1].last_phase); } -void -iwm_touch_switches(int loc, double dcycs) -{ - Disk *dsk; - int phase; - int on; - int drive; +void iwm_touch_switches(int loc, double dcycs) { + Disk *dsk; + int phase; + int on; + int drive; - if(iwm.reset) { - iwm_printf("IWM under reset: %d, enable2: %d\n", iwm.reset, - iwm.enable2); - } + if(iwm.reset) { + iwm_printf("IWM under reset: %d, enable2: %d\n", iwm.reset, + iwm.enable2); + } - on = loc & 1; - drive = iwm.drive_select; - phase = loc >> 1; - if(g_c031_disk35 & 0x40) { - dsk = &(iwm.drive35[drive]); - } else { - dsk = &(iwm.drive525[drive]); - } + on = loc & 1; + drive = iwm.drive_select; + phase = loc >> 1; + if(g_c031_disk35 & 0x40) { + dsk = &(iwm.drive35[drive]); + } else { + dsk = &(iwm.drive525[drive]); + } - if(loc < 8) { - /* phase adjustments. See if motor is on */ + if(loc < 8) { + /* phase adjustments. See if motor is on */ - iwm.iwm_phase[phase] = on; - iwm_printf("Iwm phase %d=%d, all phases: %d %d %d %d (%f)\n", - phase, on, iwm.iwm_phase[0], iwm.iwm_phase[1], - iwm.iwm_phase[2], iwm.iwm_phase[3], dcycs); + iwm.iwm_phase[phase] = on; + iwm_printf("Iwm phase %d=%d, all phases: %d %d %d %d (%f)\n", + phase, on, iwm.iwm_phase[0], iwm.iwm_phase[1], + iwm.iwm_phase[2], iwm.iwm_phase[3], dcycs); - if(iwm.motor_on) { - if(g_c031_disk35 & 0x40) { - if(phase == 3 && on) { - iwm_do_action35(dcycs); - } - } else if(on) { - /* Move apple525 head */ - iwm525_phase_change(drive, phase); - } - } - /* See if enable or reset is asserted */ - if(iwm.iwm_phase[0] && iwm.iwm_phase[2]) { - iwm.reset = 1; - iwm_printf("IWM reset active\n"); - } else { - iwm.reset = 0; - } - if(iwm.iwm_phase[1] && iwm.iwm_phase[3]) { - iwm.enable2 = 1; - iwm_printf("IWM ENABLE2 active\n"); - } else { - iwm.enable2 = 0; - } - } else { - /* loc >= 8 */ - switch(loc) { - case 0x8: - iwm_printf("Turning IWM motor off!\n"); - if(iwm.iwm_mode & 0x04) { - /* Turn off immediately */ - iwm.motor_off = 0; - iwm.motor_on = 0; - } else { - /* 1 second delay */ - if(iwm.motor_on && !iwm.motor_off) { - iwm.motor_off = 1; - iwm.motor_off_vbl_count = g_vbl_count - + 60; - } - } + if(iwm.motor_on) { + if(g_c031_disk35 & 0x40) { + if(phase == 3 && on) { + iwm_do_action35(dcycs); + } + } else if(on) { + /* Move apple525 head */ + iwm525_phase_change(drive, phase); + } + } + /* See if enable or reset is asserted */ + if(iwm.iwm_phase[0] && iwm.iwm_phase[2]) { + iwm.reset = 1; + iwm_printf("IWM reset active\n"); + } else { + iwm.reset = 0; + } + if(iwm.iwm_phase[1] && iwm.iwm_phase[3]) { + iwm.enable2 = 1; + iwm_printf("IWM ENABLE2 active\n"); + } else { + iwm.enable2 = 0; + } + } else { + /* loc >= 8 */ + switch(loc) { + case 0x8: + iwm_printf("Turning IWM motor off!\n"); + if(iwm.iwm_mode & 0x04) { + /* Turn off immediately */ + iwm.motor_off = 0; + iwm.motor_on = 0; + } else { + /* 1 second delay */ + if(iwm.motor_on && !iwm.motor_off) { + iwm.motor_off = 1; + iwm.motor_off_vbl_count = g_vbl_count + + 60; + } + } - if(g_iwm_motor_on || g_slow_525_emul_wr) { - /* recalc current speed */ - set_halt(HALT_EVENT); - } + if(g_iwm_motor_on || g_slow_525_emul_wr) { + /* recalc current speed */ + set_halt(HALT_EVENT); + } - g_iwm_motor_on = 0; - g_slow_525_emul_wr = 0; - break; - case 0x9: - iwm_printf("Turning IWM motor on!\n"); - iwm.motor_on = 1; - iwm.motor_off = 0; + g_iwm_motor_on = 0; + g_slow_525_emul_wr = 0; + break; + case 0x9: + iwm_printf("Turning IWM motor on!\n"); + iwm.motor_on = 1; + iwm.motor_off = 0; - if(g_iwm_motor_on == 0) { - /* recalc current speed */ - set_halt(HALT_EVENT); - } - g_iwm_motor_on = 1; + if(g_iwm_motor_on == 0) { + /* recalc current speed */ + set_halt(HALT_EVENT); + } + g_iwm_motor_on = 1; - break; - case 0xa: - case 0xb: - iwm.drive_select = on; - break; - case 0xc: - case 0xd: - iwm.q6 = on; - break; - case 0xe: - case 0xf: - iwm.q7 = on; - break; - default: - printf("iwm_touch_switches: loc: %02x unknown!\n", loc); - exit(2); - } - } + break; + case 0xa: + case 0xb: + iwm.drive_select = on; + break; + case 0xc: + case 0xd: + iwm.q6 = on; + break; + case 0xe: + case 0xf: + iwm.q7 = on; + break; + default: + printf("iwm_touch_switches: loc: %02x unknown!\n", loc); + exit(2); + } + } - if(!iwm.q7) { - iwm.previous_write_bits = 0; - } + if(!iwm.q7) { + iwm.previous_write_bits = 0; + } - if((dcycs > g_dcycs_end_emul_wr) && g_slow_525_emul_wr) { - set_halt(HALT_EVENT); - g_slow_525_emul_wr = 0; - } + if((dcycs > g_dcycs_end_emul_wr) && g_slow_525_emul_wr) { + set_halt(HALT_EVENT); + g_slow_525_emul_wr = 0; + } } -void -iwm_move_to_track(Disk *dsk, int new_track) -{ - int disk_525; - int dr; +void iwm_move_to_track(Disk *dsk, int new_track) { + int disk_525; + int dr; - disk_525 = dsk->disk_525; + disk_525 = dsk->disk_525; - if(new_track < 0) { - new_track = 0; - } - if(new_track >= dsk->num_tracks) { - if(disk_525) { - new_track = dsk->num_tracks - 4; - } else { - new_track = dsk->num_tracks - 2 + iwm.head35; - } + if(new_track < 0) { + new_track = 0; + } + if(new_track >= dsk->num_tracks) { + if(disk_525) { + new_track = dsk->num_tracks - 4; + } else { + new_track = dsk->num_tracks - 2 + iwm.head35; + } - if(new_track <= 0) { - new_track = 0; - } - } + if(new_track <= 0) { + new_track = 0; + } + } - if(dsk->cur_qtr_track != new_track) { - dr = dsk->drive + 1; - if(disk_525) { - iwm_printf("s6d%d Track: %d.%02d\n", dr, - new_track >> 2, 25* (new_track & 3)); - } else { - iwm_printf("s5d%d Track: %d Side: %d\n", dr, - new_track >> 1, new_track & 1); - } + if(dsk->cur_qtr_track != new_track) { + dr = dsk->drive + 1; + if(disk_525) { + iwm_printf("s6d%d Track: %d.%02d\n", dr, + new_track >> 2, 25* (new_track & 3)); + } else { + iwm_printf("s5d%d Track: %d Side: %d\n", dr, + new_track >> 1, new_track & 1); + } - dsk->cur_qtr_track = new_track; - } + dsk->cur_qtr_track = new_track; + } } -void -iwm525_phase_change(int drive, int phase) -{ - Disk *dsk; - int qtr_track; - int delta; +void iwm525_phase_change(int drive, int phase) { + Disk *dsk; + int qtr_track; + int delta; - dsk = &(iwm.drive525[drive]); + dsk = &(iwm.drive525[drive]); - qtr_track = dsk->cur_qtr_track; - int half_track = qtr_track >> 1; + qtr_track = dsk->cur_qtr_track; + int half_track = qtr_track >> 1; - delta = 0; - if (iwm.iwm_phase[(half_track + 1) & 3]) - delta += 2; - if (iwm.iwm_phase[(half_track + 3) & 3]) - delta -= 2; + delta = 0; + if (iwm.iwm_phase[(half_track + 1) & 3]) + delta += 2; + if (iwm.iwm_phase[(half_track + 3) & 3]) + delta -= 2; - qtr_track += delta; - if(qtr_track < 0) { + qtr_track += delta; + if(qtr_track < 0) { #if 1 - printf("💾 "); + printf("💾 "); #else - printf("GRIND..."); + printf("GRIND..."); #endif - qtr_track = 0; - } - if(qtr_track > 4*34) { - glogf("Disk arm moved past track 34, moving it back"); - qtr_track = 4*34; - } + qtr_track = 0; + } + if(qtr_track > 4*34) { + glogf("Disk arm moved past track 34, moving it back"); + qtr_track = 4*34; + } - iwm_move_to_track(dsk, qtr_track); + iwm_move_to_track(dsk, qtr_track); - iwm_printf("Moving drive to qtr track: %04x (trk:%d.%02d), %d, %d, " - "%d %d %d %d\n", qtr_track, qtr_track>>2, 25*(qtr_track & 3), - phase, delta, iwm.iwm_phase[0], - iwm.iwm_phase[1], iwm.iwm_phase[2], iwm.iwm_phase[3]); + iwm_printf("Moving drive to qtr track: %04x (trk:%d.%02d), %d, %d, " + "%d %d %d %d\n", qtr_track, qtr_track>>2, 25*(qtr_track & 3), + phase, delta, iwm.iwm_phase[0], + iwm.iwm_phase[1], iwm.iwm_phase[2], iwm.iwm_phase[3]); } -int -iwm_read_status35(double dcycs) -{ - Disk *dsk; - int drive; - int state; - int tmp; +int iwm_read_status35(double dcycs) { + Disk *dsk; + int drive; + int state; + int tmp; - drive = iwm.drive_select; - dsk = &(iwm.drive35[drive]); + drive = iwm.drive_select; + dsk = &(iwm.drive35[drive]); - if(iwm.motor_on) { - /* Read status */ - state = (iwm.iwm_phase[1] << 3) + (iwm.iwm_phase[0] << 2) + - ((g_c031_disk35 >> 6) & 2) + iwm.iwm_phase[2]; + if(iwm.motor_on) { + /* Read status */ + state = (iwm.iwm_phase[1] << 3) + (iwm.iwm_phase[0] << 2) + + ((g_c031_disk35 >> 6) & 2) + iwm.iwm_phase[2]; - iwm_printf("Iwm status read state: %02x\n", state); + iwm_printf("Iwm status read state: %02x\n", state); - switch(state) { - case 0x00: /* step direction */ - return iwm.step_direction35; - break; - case 0x01: /* lower head activate */ - /* also return instantaneous data from head */ - iwm.head35 = 0; - iwm_move_to_track(dsk, (dsk->cur_qtr_track & (-2))); - return (((int)dcycs) & 1); - break; - case 0x02: /* disk in place */ - /* 1 = no disk, 0 = disk */ - iwm_printf("read disk in place, num_tracks: %d\n", - dsk->num_tracks); - tmp = (dsk->num_tracks <= 0); - return tmp; - break; - case 0x03: /* upper head activate */ - /* also return instantaneous data from head */ - iwm.head35 = 1; - iwm_move_to_track(dsk, (dsk->cur_qtr_track | 1)); - return (((int)dcycs) & 1); - break; - case 0x04: /* disk is stepping? */ - /* 1 = not stepping, 0 = stepping */ - return 1; - break; - case 0x05: /* Unknown function of ROM 03? */ - /* 1 = or $20 into 0xe1/f24+drive, 0 = don't */ - return 1; - break; - case 0x06: /* disk is locked */ - /* 0 = locked, 1 = unlocked */ - return (!dsk->write_prot); - break; - case 0x08: /* motor on */ - /* 0 = on, 1 = off */ - return !iwm.motor_on35; - break; - case 0x09: /* number of sides */ - /* 1 = 2 sides, 0 = 1 side */ - return 1; - break; - case 0x0a: /* at track 0 */ - /* 1 = not at track 0, 0 = there */ - tmp = (dsk->cur_qtr_track != 0); - iwm_printf("Read at track0_35: %d\n", tmp); - return tmp; - break; - case 0x0b: /* disk ready??? */ - /* 0 = ready, 1 = not ready? */ - tmp = !iwm.motor_on35; - iwm_printf("Read disk ready, ret: %d\n", tmp); - return tmp; - break; - case 0x0c: /* disk switched?? */ - /* 0 = not switched, 1 = switched? */ - tmp = (dsk->just_ejected != 0); - iwm_printf("Read disk switched: %d\n", tmp); - return tmp; - break; - case 0x0d: /* false read when ejecting disk */ - return 1; - case 0x0e: /* tachometer */ - halt_printf("Reading tachometer!\n"); - return (((int)dcycs) & 1); - break; - case 0x0f: /* drive installed? */ - /* 0 = drive exists, 1 = no drive */ - if(drive) { - /* pretend no drive 1 */ - return 1; - } - return 0; - break; - default: - halt_printf("Read 3.5 status, state: %02x\n", state); - return 1; - } - } else { - iwm_printf("Read 3.5 status with drive off!\n"); - return 1; - } + switch(state) { + case 0x00: /* step direction */ + return iwm.step_direction35; + break; + case 0x01: /* lower head activate */ + /* also return instantaneous data from head */ + iwm.head35 = 0; + iwm_move_to_track(dsk, (dsk->cur_qtr_track & (-2))); + return (((int)dcycs) & 1); + break; + case 0x02: /* disk in place */ + /* 1 = no disk, 0 = disk */ + iwm_printf("read disk in place, num_tracks: %d\n", + dsk->num_tracks); + tmp = (dsk->num_tracks <= 0); + return tmp; + break; + case 0x03: /* upper head activate */ + /* also return instantaneous data from head */ + iwm.head35 = 1; + iwm_move_to_track(dsk, (dsk->cur_qtr_track | 1)); + return (((int)dcycs) & 1); + break; + case 0x04: /* disk is stepping? */ + /* 1 = not stepping, 0 = stepping */ + return 1; + break; + case 0x05: /* Unknown function of ROM 03? */ + /* 1 = or $20 into 0xe1/f24+drive, 0 = don't */ + return 1; + break; + case 0x06: /* disk is locked */ + /* 0 = locked, 1 = unlocked */ + return (!dsk->write_prot); + break; + case 0x08: /* motor on */ + /* 0 = on, 1 = off */ + return !iwm.motor_on35; + break; + case 0x09: /* number of sides */ + /* 1 = 2 sides, 0 = 1 side */ + return 1; + break; + case 0x0a: /* at track 0 */ + /* 1 = not at track 0, 0 = there */ + tmp = (dsk->cur_qtr_track != 0); + iwm_printf("Read at track0_35: %d\n", tmp); + return tmp; + break; + case 0x0b: /* disk ready??? */ + /* 0 = ready, 1 = not ready? */ + tmp = !iwm.motor_on35; + iwm_printf("Read disk ready, ret: %d\n", tmp); + return tmp; + break; + case 0x0c: /* disk switched?? */ + /* 0 = not switched, 1 = switched? */ + tmp = (dsk->just_ejected != 0); + iwm_printf("Read disk switched: %d\n", tmp); + return tmp; + break; + case 0x0d: /* false read when ejecting disk */ + return 1; + case 0x0e: /* tachometer */ + halt_printf("Reading tachometer!\n"); + return (((int)dcycs) & 1); + break; + case 0x0f: /* drive installed? */ + /* 0 = drive exists, 1 = no drive */ + if(drive) { + /* pretend no drive 1 */ + return 1; + } + return 0; + break; + default: + halt_printf("Read 3.5 status, state: %02x\n", state); + return 1; + } + } else { + iwm_printf("Read 3.5 status with drive off!\n"); + return 1; + } } -void -iwm_do_action35(double dcycs) -{ - Disk *dsk; - int drive; - int state; +void iwm_do_action35(double dcycs) { + Disk *dsk; + int drive; + int state; - drive = iwm.drive_select; - dsk = &(iwm.drive35[drive]); + drive = iwm.drive_select; + dsk = &(iwm.drive35[drive]); - if(iwm.motor_on) { - /* Perform action */ - state = (iwm.iwm_phase[1] << 3) + (iwm.iwm_phase[0] << 2) + - ((g_c031_disk35 >> 6) & 2) + iwm.iwm_phase[2]; - switch(state) { - case 0x00: /* Set step direction inward */ - /* towards higher tracks */ - iwm.step_direction35 = 0; - iwm_printf("Iwm set step dir35 = 0\n"); - break; - case 0x01: /* Set step direction outward */ - /* towards lower tracks */ - iwm.step_direction35 = 1; - iwm_printf("Iwm set step dir35 = 1\n"); - break; - case 0x03: /* reset disk-switched flag? */ - iwm_printf("Iwm reset disk switch\n"); - dsk->just_ejected = 0; - /* set_halt(1); */ - break; - case 0x04: /* step disk */ - if(iwm.step_direction35) { - iwm_move_to_track(dsk, dsk->cur_qtr_track - 2); - } else { - iwm_move_to_track(dsk, dsk->cur_qtr_track + 2); - } - break; - case 0x08: /* turn motor on */ - iwm_printf("Iwm set motor_on35 = 1\n"); - iwm.motor_on35 = 1; - break; - case 0x09: /* turn motor off */ - iwm_printf("Iwm set motor_on35 = 0\n"); - iwm.motor_on35 = 0; - break; - case 0x0d: /* eject disk */ - eject_disk(dsk); - #ifdef ACTIVEGS // OG : pass eject info to the Control (ActiveX specific) - { - extern void ejectDisk(int slot,int disk); - ejectDisk(dsk->disk_525?6:5,dsk->drive+1); - } - #endif - break; - case 0x02: - case 0x07: - case 0x0b: /* hacks to allow AE 1.6MB driver to not crash me */ - break; - default: - halt_printf("Do 3.5 action, state: %02x\n", state); - return; - } - } else { - halt_printf("Set 3.5 status with drive off!\n"); - return; - } + if(iwm.motor_on) { + /* Perform action */ + state = (iwm.iwm_phase[1] << 3) + (iwm.iwm_phase[0] << 2) + + ((g_c031_disk35 >> 6) & 2) + iwm.iwm_phase[2]; + switch(state) { + case 0x00: /* Set step direction inward */ + /* towards higher tracks */ + iwm.step_direction35 = 0; + iwm_printf("Iwm set step dir35 = 0\n"); + break; + case 0x01: /* Set step direction outward */ + /* towards lower tracks */ + iwm.step_direction35 = 1; + iwm_printf("Iwm set step dir35 = 1\n"); + break; + case 0x03: /* reset disk-switched flag? */ + iwm_printf("Iwm reset disk switch\n"); + dsk->just_ejected = 0; + /* set_halt(1); */ + break; + case 0x04: /* step disk */ + if(iwm.step_direction35) { + iwm_move_to_track(dsk, dsk->cur_qtr_track - 2); + } else { + iwm_move_to_track(dsk, dsk->cur_qtr_track + 2); + } + break; + case 0x08: /* turn motor on */ + iwm_printf("Iwm set motor_on35 = 1\n"); + iwm.motor_on35 = 1; + break; + case 0x09: /* turn motor off */ + iwm_printf("Iwm set motor_on35 = 0\n"); + iwm.motor_on35 = 0; + break; + case 0x0d: /* eject disk */ + eject_disk(dsk); + #ifdef ACTIVEGS // OG : pass eject info to the Control (ActiveX specific) + { + extern void ejectDisk(int slot,int disk); + ejectDisk(dsk->disk_525 ? 6 : 5,dsk->drive+1); + } + #endif + break; + case 0x02: + case 0x07: + case 0x0b: /* hacks to allow AE 1.6MB driver to not crash me */ + break; + default: + halt_printf("Do 3.5 action, state: %02x\n", state); + return; + } + } else { + halt_printf("Set 3.5 status with drive off!\n"); + return; + } } -int -iwm_read_c0ec(double dcycs) -{ - Disk *dsk; - int drive; +int iwm_read_c0ec(double dcycs) { + Disk *dsk; + int drive; - iwm.q6 = 0; + iwm.q6 = 0; - if(iwm.q7 == 0 && iwm.enable2 == 0 && iwm.motor_on) { - drive = iwm.drive_select; - if(g_c031_disk35 & 0x40) { - dsk = &(iwm.drive35[drive]); - return iwm_read_data_35(dsk, g_fast_disk_emul, dcycs); - } else { - dsk = &(iwm.drive525[drive]); - return iwm_read_data_525(dsk, g_fast_disk_emul, dcycs); - } + if(iwm.q7 == 0 && iwm.enable2 == 0 && iwm.motor_on) { + drive = iwm.drive_select; + if(g_c031_disk35 & 0x40) { + dsk = &(iwm.drive35[drive]); + return iwm_read_data_35(dsk, g_fast_disk_emul, dcycs); + } else { + dsk = &(iwm.drive525[drive]); + return iwm_read_data_525(dsk, g_fast_disk_emul, dcycs); + } - } + } - return read_iwm(0xc, dcycs); + return read_iwm(0xc, dcycs); } -int -read_iwm(int loc, double dcycs) -{ - Disk *dsk; - word32 status; - double diff_dcycs; - double dcmp; - int on; - int state; - int drive; - int val; +int read_iwm(int loc, double dcycs) { + Disk *dsk; + word32 status; + double diff_dcycs; + double dcmp; + int on; + int state; + int drive; + int val; - loc = loc & 0xf; - on = loc & 1; + loc = loc & 0xf; + on = loc & 1; - if(loc == 0xc) { - iwm.q6 = 0; - } else { - iwm_touch_switches(loc, dcycs); - } + if(loc == 0xc) { + iwm.q6 = 0; + } else { + iwm_touch_switches(loc, dcycs); + } - state = (iwm.q7 << 1) + iwm.q6; - drive = iwm.drive_select; - if(g_c031_disk35 & 0x40) { - dsk = &(iwm.drive35[drive]); - } else { - dsk = &(iwm.drive525[drive]); - } + state = (iwm.q7 << 1) + iwm.q6; + drive = iwm.drive_select; + if(g_c031_disk35 & 0x40) { + dsk = &(iwm.drive35[drive]); + } else { + dsk = &(iwm.drive525[drive]); + } - if(on) { - /* odd address, return 0 */ - return 0; - } else { - /* even address */ - switch(state) { - case 0x00: /* q7 = 0, q6 = 0 */ - if(iwm.enable2) { - return iwm_read_enable2(dcycs); - } else { - if(iwm.motor_on) { - return iwm_read_data(dsk, - g_fast_disk_emul, dcycs); - } else { - iwm_printf("read iwm st 0, m off!\n"); + if(on) { + /* odd address, return 0 */ + return 0; + } else { + /* even address */ + switch(state) { + case 0x00: /* q7 = 0, q6 = 0 */ + if(iwm.enable2) { + return iwm_read_enable2(dcycs); + } else { + if(iwm.motor_on) { + return iwm_read_data(dsk, + g_fast_disk_emul, dcycs); + } else { + iwm_printf("read iwm st 0, m off!\n"); /* HACK!!!! */ - return 0xff; - //return (((int)dcycs) & 0x7f) + 0x80; - } - } - break; - case 0x01: /* q7 = 0, q6 = 1 */ - /* read IWM status reg */ - if(iwm.enable2) { - iwm_printf("Read status under enable2: 1\n"); - status = 1; - } else { - if(g_c031_disk35 & 0x40) { - status = iwm_read_status35(dcycs); - } else { - status = dsk->write_prot; - } - } + return 0xff; + //return (((int)dcycs) & 0x7f) + 0x80; + } + } + break; + case 0x01: /* q7 = 0, q6 = 1 */ + /* read IWM status reg */ + if(iwm.enable2) { + iwm_printf("Read status under enable2: 1\n"); + status = 1; + } else { + if(g_c031_disk35 & 0x40) { + status = iwm_read_status35(dcycs); + } else { + status = dsk->write_prot; + } + } - val = (status << 7) + (iwm.motor_on << 5) + - iwm.iwm_mode; - iwm_printf("Read status: %02x\n", val); + val = (status << 7) + (iwm.motor_on << 5) + + iwm.iwm_mode; + iwm_printf("Read status: %02x\n", val); - return val; - break; - case 0x02: /* q7 = 1, q6 = 0 */ - /* read handshake register */ - if(iwm.enable2) { - return iwm_read_enable2_handshake(dcycs); - } else { - status = 0xc0; - diff_dcycs = dcycs - dsk->dcycs_last_read; - dcmp = 16.0; - if(dsk->disk_525 == 0) { - dcmp = 32.0; - } - if(diff_dcycs > dcmp) { - iwm_printf("Write underrun!\n"); - iwm_printf("cur: %f, dc_last: %f\n", - dcycs, dsk->dcycs_last_read); - status = status & 0xbf; - } - return status; - } - break; - case 0x03: /* q7 = 1, q6 = 1 */ - halt_printf("read iwm state 3!\n"); - return 0; - break; - } + return val; + break; + case 0x02: /* q7 = 1, q6 = 0 */ + /* read handshake register */ + if(iwm.enable2) { + return iwm_read_enable2_handshake(dcycs); + } else { + status = 0xc0; + diff_dcycs = dcycs - dsk->dcycs_last_read; + dcmp = 16.0; + if(dsk->disk_525 == 0) { + dcmp = 32.0; + } + if(diff_dcycs > dcmp) { + iwm_printf("Write underrun!\n"); + iwm_printf("cur: %f, dc_last: %f\n", + dcycs, dsk->dcycs_last_read); + status = status & 0xbf; + } + return status; + } + break; + case 0x03: /* q7 = 1, q6 = 1 */ + halt_printf("read iwm state 3!\n"); + return 0; + break; + } - } - halt_printf("Got to end of read_iwm, loc: %02x!\n", loc); + } + halt_printf("Got to end of read_iwm, loc: %02x!\n", loc); - return 0; + return 0; } -void -write_iwm(int loc, int val, double dcycs) -{ - Disk *dsk; - int on; - int state; - int drive; - int fast_writes; +void write_iwm(int loc, int val, double dcycs) { + Disk *dsk; + int on; + int state; + int drive; + int fast_writes; - loc = loc & 0xf; - on = loc & 1; + loc = loc & 0xf; + on = loc & 1; - iwm_touch_switches(loc, dcycs); + iwm_touch_switches(loc, dcycs); - state = (iwm.q7 << 1) + iwm.q6; - drive = iwm.drive_select; - fast_writes = g_fast_disk_emul; - if(g_c031_disk35 & 0x40) { - dsk = &(iwm.drive35[drive]); - } else { - dsk = &(iwm.drive525[drive]); - fast_writes = !g_slow_525_emul_wr && fast_writes; - } + state = (iwm.q7 << 1) + iwm.q6; + drive = iwm.drive_select; + fast_writes = g_fast_disk_emul; + if(g_c031_disk35 & 0x40) { + dsk = &(iwm.drive35[drive]); + } else { + dsk = &(iwm.drive525[drive]); + fast_writes = !g_slow_525_emul_wr && fast_writes; + } - if(on) { - /* odd address, write something */ - if(state == 0x03) { - /* q7, q6 = 1,1 */ - if(iwm.motor_on) { - if(iwm.enable2) { - iwm_write_enable2(val, dcycs); - } else { - iwm_write_data(dsk, val, - fast_writes, dcycs); - } - } else { - /* write mode register */ - val = val & 0x1f; - iwm.iwm_mode = val; - if(val != 0 && val != 0x0f && val != 0x07 && - val != 0x04 && val != 0x0b) { - halt_printf("set iwm_mode:%02x!\n",val); - } - } - } else { - if(iwm.enable2) { - iwm_write_enable2(val, dcycs); - } else { + if(on) { + /* odd address, write something */ + if(state == 0x03) { + /* q7, q6 = 1,1 */ + if(iwm.motor_on) { + if(iwm.enable2) { + iwm_write_enable2(val, dcycs); + } else { + iwm_write_data(dsk, val, + fast_writes, dcycs); + } + } else { + /* write mode register */ + val = val & 0x1f; + iwm.iwm_mode = val; + if(val != 0 && val != 0x0f && val != 0x07 && + val != 0x04 && val != 0x0b) { + halt_printf("set iwm_mode:%02x!\n",val); + } + } + } else { + if(iwm.enable2) { + iwm_write_enable2(val, dcycs); + } else { #if 0 // Flobynoid writes to 0xc0e9 causing these messages... - printf("Write iwm1, st: %02x, loc: %x: %02x\n", - state, loc, val); + printf("Write iwm1, st: %02x, loc: %x: %02x\n", + state, loc, val); #endif - } - } - return; - } else { - /* even address */ - if(iwm.enable2) { - iwm_write_enable2(val, dcycs); - } else { - iwm_printf("Write iwm2, st: %02x, loc: %x: %02x\n", - state, loc, val); - } - return; - } + } + } + return; + } else { + /* even address */ + if(iwm.enable2) { + iwm_write_enable2(val, dcycs); + } else { + iwm_printf("Write iwm2, st: %02x, loc: %x: %02x\n", + state, loc, val); + } + return; + } - return; + return; } -int -iwm_read_enable2(double dcycs) -{ - iwm_printf("Read under enable2!\n"); - return 0xff; +int iwm_read_enable2(double dcycs) { + iwm_printf("Read under enable2!\n"); + return 0xff; } int g_cnt_enable2_handshake = 0; -int -iwm_read_enable2_handshake(double dcycs) -{ - int val; +int iwm_read_enable2_handshake(double dcycs) { + int val; - iwm_printf("Read handshake under enable2!\n"); + iwm_printf("Read handshake under enable2!\n"); - val = 0xc0; - g_cnt_enable2_handshake++; - if(g_cnt_enable2_handshake > 3) { - g_cnt_enable2_handshake = 0; - val = 0x80; - } + val = 0xc0; + g_cnt_enable2_handshake++; + if(g_cnt_enable2_handshake > 3) { + g_cnt_enable2_handshake = 0; + val = 0x80; + } - return val; + return val; } -void -iwm_write_enable2(int val, double dcycs) -{ - iwm_printf("Write under enable2: %02x!\n", val); +void iwm_write_enable2(int val, double dcycs) { + iwm_printf("Write under enable2: %02x!\n", val); - return; + return; } -int -iwm_read_data(Disk *dsk, int fast_disk_emul, double dcycs) -{ - if(dsk->disk_525) { - return iwm_read_data_525(dsk, fast_disk_emul, dcycs); - } else { - return iwm_read_data_35(dsk, fast_disk_emul, dcycs); - } +int iwm_read_data(Disk *dsk, int fast_disk_emul, double dcycs) { + if(dsk->disk_525) { + return iwm_read_data_525(dsk, fast_disk_emul, dcycs); + } else { + return iwm_read_data_35(dsk, fast_disk_emul, dcycs); + } } -void -iwm_write_data(Disk *dsk, word32 val, int fast_disk_emul, double dcycs) -{ - if(dsk->disk_525) { - iwm_write_data_525(dsk, val, fast_disk_emul, dcycs); - } else { - iwm_write_data_35(dsk, val, fast_disk_emul, dcycs); - } +void iwm_write_data(Disk *dsk, word32 val, int fast_disk_emul, double dcycs) { + if(dsk->disk_525) { + iwm_write_data_525(dsk, val, fast_disk_emul, dcycs); + } else { + iwm_write_data_35(dsk, val, fast_disk_emul, dcycs); + } } #undef IWM_READ_ROUT @@ -1017,10 +973,10 @@ iwm_write_data(Disk *dsk, word32 val, int fast_disk_emul, double dcycs) #undef IWM_CYC_MULT #undef IWM_DISK_525 -#define IWM_READ_ROUT iwm_read_data_35 -#define IWM_WRITE_ROUT iwm_write_data_35 -#define IWM_CYC_MULT 1 -#define IWM_DISK_525 0 +#define IWM_READ_ROUT iwm_read_data_35 +#define IWM_WRITE_ROUT iwm_write_data_35 +#define IWM_CYC_MULT 1 +#define IWM_DISK_525 0 #include "iwm_35_525.h" @@ -1029,10 +985,10 @@ iwm_write_data(Disk *dsk, word32 val, int fast_disk_emul, double dcycs) #undef IWM_CYC_MULT #undef IWM_DISK_525 -#define IWM_READ_ROUT iwm_read_data_525 -#define IWM_WRITE_ROUT iwm_write_data_525 -#define IWM_CYC_MULT 2 -#define IWM_DISK_525 1 +#define IWM_READ_ROUT iwm_read_data_525 +#define IWM_WRITE_ROUT iwm_write_data_525 +#define IWM_CYC_MULT 2 +#define IWM_DISK_525 1 #include "iwm_35_525.h" #undef IWM_READ_ROUT @@ -1045,584 +1001,576 @@ iwm_write_data(Disk *dsk, word32 val, int fast_disk_emul, double dcycs) /* c600 */ -void -sector_to_partial_nib(byte *in, byte *nib_ptr) -{ - byte *aux_buf; - byte *nib_out; - int val; - int val2; - int x; - int i; +void sector_to_partial_nib(byte *in, byte *nib_ptr) { + byte *aux_buf; + byte *nib_out; + int val; + int val2; + int x; + int i; - /* Convert 256(+1) data bytes to 342+1 disk nibbles */ + /* Convert 256(+1) data bytes to 342+1 disk nibbles */ - aux_buf = nib_ptr; - nib_out = nib_ptr + 0x56; + aux_buf = nib_ptr; + nib_out = nib_ptr + 0x56; - for(i = 0; i < 0x56; i++) { - aux_buf[i] = 0; - } + for(i = 0; i < 0x56; i++) { + aux_buf[i] = 0; + } - x = 0x55; - for(i = 0x101; i >= 0; i--) { - val = in[i]; - if(i >= 0x100) { - val = 0; - } - val2 = (aux_buf[x] << 1) + (val & 1); - val = val >> 1; - val2 = (val2 << 1) + (val & 1); - val = val >> 1; - nib_out[i] = val; - aux_buf[x] = val2; - x--; - if(x < 0) { - x = 0x55; - } - } + x = 0x55; + for(i = 0x101; i >= 0; i--) { + val = in[i]; + if(i >= 0x100) { + val = 0; + } + val2 = (aux_buf[x] << 1) + (val & 1); + val = val >> 1; + val2 = (val2 << 1) + (val & 1); + val = val >> 1; + nib_out[i] = val; + aux_buf[x] = val2; + x--; + if(x < 0) { + x = 0x55; + } + } } -int -disk_unnib_4x4(Disk *dsk) -{ - int val1; - int val2; +int disk_unnib_4x4(Disk *dsk) { + int val1; + int val2; - val1 = iwm_read_data(dsk, 1, 0); - val2 = iwm_read_data(dsk, 1, 0); + val1 = iwm_read_data(dsk, 1, 0); + val2 = iwm_read_data(dsk, 1, 0); - return ((val1 << 1) + 1) & val2; + return ((val1 << 1) + 1) & val2; } -int -iwm_denib_track525(Disk *dsk, Trk *trk, int qtr_track, byte *outbuf) -{ - byte aux_buf[0x80]; - byte *buf; - int sector_done[16]; - int num_sectors_done; - int track_len; - int vol, track, phys_sec, log_sec, cksum; - int val; - int val2; - int prev_val; - int x; - int my_nib_cnt; - int save_qtr_track; - int save_nib_pos; - int tmp_nib_pos; - int status; - int i; +int iwm_denib_track525(Disk *dsk, Trk *trk, int qtr_track, byte *outbuf) { + byte aux_buf[0x80]; + byte *buf; + int sector_done[16]; + int num_sectors_done; + int track_len; + int vol, track, phys_sec, log_sec, cksum; + int val; + int val2; + int prev_val; + int x; + int my_nib_cnt; + int save_qtr_track; + int save_nib_pos; + int tmp_nib_pos; + int status; + int i; - save_qtr_track = dsk->cur_qtr_track; - save_nib_pos = dsk->nib_pos; + save_qtr_track = dsk->cur_qtr_track; + save_nib_pos = dsk->nib_pos; - iwm_move_to_track(dsk, qtr_track); + iwm_move_to_track(dsk, qtr_track); - dsk->nib_pos = 0; - g_fast_disk_unnib = 1; + dsk->nib_pos = 0; + g_fast_disk_unnib = 1; - track_len = trk->track_len; + track_len = trk->track_len; - for(i = 0; i < 16; i++) { - sector_done[i] = 0; - } + for(i = 0; i < 16; i++) { + sector_done[i] = 0; + } - num_sectors_done = 0; + num_sectors_done = 0; - val = 0; - status = -1; - my_nib_cnt = 0; - while(my_nib_cnt++ < 2*track_len) { - /* look for start of a sector */ - if(val != 0xd5) { - val = iwm_read_data(dsk, 1, 0); - continue; - } + val = 0; + status = -1; + my_nib_cnt = 0; + while(my_nib_cnt++ < 2*track_len) { + /* look for start of a sector */ + if(val != 0xd5) { + val = iwm_read_data(dsk, 1, 0); + continue; + } - val = iwm_read_data(dsk, 1, 0); - if(val != 0xaa) { - continue; - } + val = iwm_read_data(dsk, 1, 0); + if(val != 0xaa) { + continue; + } - val = iwm_read_data(dsk, 1, 0); - if(val != 0x96) { - continue; - } + val = iwm_read_data(dsk, 1, 0); + if(val != 0x96) { + continue; + } - /* It's a sector start */ - vol = disk_unnib_4x4(dsk); - track = disk_unnib_4x4(dsk); - phys_sec = disk_unnib_4x4(dsk); - if(phys_sec < 0 || phys_sec > 15) { - printf("Track %02x, read sec as %02x\n", qtr_track>>2, - phys_sec); - break; - } - if(dsk->image_type == DSK_TYPE_DOS33) { - log_sec = phys_to_dos_sec[phys_sec]; - } else { - log_sec = phys_to_prodos_sec[phys_sec]; - } - cksum = disk_unnib_4x4(dsk); - if((vol ^ track ^ phys_sec ^ cksum) != 0) { - /* not correct format */ - printf("Track %02x not DOS 3.3 since hdr cksum, %02x " - "%02x %02x %02x\n", - qtr_track>>2, vol, track, phys_sec, cksum); - break; - } + /* It's a sector start */ + vol = disk_unnib_4x4(dsk); + track = disk_unnib_4x4(dsk); + phys_sec = disk_unnib_4x4(dsk); + if(phys_sec < 0 || phys_sec > 15) { + printf("Track %02x, read sec as %02x\n", qtr_track>>2, + phys_sec); + break; + } + if(dsk->image_type == DSK_TYPE_DOS33) { + log_sec = phys_to_dos_sec[phys_sec]; + } else { + log_sec = phys_to_prodos_sec[phys_sec]; + } + cksum = disk_unnib_4x4(dsk); + if((vol ^ track ^ phys_sec ^ cksum) != 0) { + /* not correct format */ + printf("Track %02x not DOS 3.3 since hdr cksum, %02x " + "%02x %02x %02x\n", + qtr_track>>2, vol, track, phys_sec, cksum); + break; + } - /* see what sector it is */ - if(track != (qtr_track>>2) || (phys_sec < 0)||(phys_sec > 15)) { - printf("Track %02x bad since track: %02x, sec: %02x\n", - qtr_track>>2, track, phys_sec); - break; - } + /* see what sector it is */ + if(track != (qtr_track>>2) || (phys_sec < 0)||(phys_sec > 15)) { + printf("Track %02x bad since track: %02x, sec: %02x\n", + qtr_track>>2, track, phys_sec); + break; + } - if(sector_done[phys_sec]) { - printf("Already done sector %02x on track %02x!\n", - phys_sec, qtr_track>>2); - break; - } + if(sector_done[phys_sec]) { + printf("Already done sector %02x on track %02x!\n", + phys_sec, qtr_track>>2); + break; + } - /* So far so good, let's do it! */ - val = 0; - i = 0; - while(i < NIBS_FROM_ADDR_TO_DATA) { - i++; - if(val != 0xd5) { - val = iwm_read_data(dsk, 1, 0); - continue; - } + /* So far so good, let's do it! */ + val = 0; + i = 0; + while(i < NIBS_FROM_ADDR_TO_DATA) { + i++; + if(val != 0xd5) { + val = iwm_read_data(dsk, 1, 0); + continue; + } - val = iwm_read_data(dsk, 1, 0); - if(val != 0xaa) { - continue; - } + val = iwm_read_data(dsk, 1, 0); + if(val != 0xaa) { + continue; + } - val = iwm_read_data(dsk, 1, 0); - if(val != 0xad) { - continue; - } + val = iwm_read_data(dsk, 1, 0); + if(val != 0xad) { + continue; + } - /* got it, just break */ - break; - } + /* got it, just break */ + break; + } - if(i >= NIBS_FROM_ADDR_TO_DATA) { - printf("No data header, track %02x, sec %02x\n", - qtr_track>>2, phys_sec); - printf("nib_pos: %08x\n", dsk->nib_pos); - break; - } + if(i >= NIBS_FROM_ADDR_TO_DATA) { + printf("No data header, track %02x, sec %02x\n", + qtr_track>>2, phys_sec); + printf("nib_pos: %08x\n", dsk->nib_pos); + break; + } - buf = outbuf + 0x100*log_sec; + buf = outbuf + 0x100*log_sec; - /* Data start! */ - prev_val = 0; - for(i = 0x55; i >= 0; i--) { - val = iwm_read_data(dsk, 1, 0); - val2 = from_disk_byte[val]; - if(val2 < 0) { - printf("Bad data area1, val:%02x,val2:%02x\n", - val, val2); - printf(" i:%03x,n_pos:%04x\n", i, dsk->nib_pos); - break; - } - prev_val = val2 ^ prev_val; - aux_buf[i] = prev_val; - } + /* Data start! */ + prev_val = 0; + for(i = 0x55; i >= 0; i--) { + val = iwm_read_data(dsk, 1, 0); + val2 = from_disk_byte[val]; + if(val2 < 0) { + printf("Bad data area1, val:%02x,val2:%02x\n", + val, val2); + printf(" i:%03x,n_pos:%04x\n", i, dsk->nib_pos); + break; + } + prev_val = val2 ^ prev_val; + aux_buf[i] = prev_val; + } - /* rest of data area */ - for(i = 0; i < 0x100; i++) { - val = iwm_read_data(dsk, 1, 0); - val2 = from_disk_byte[val]; - if(val2 < 0) { - printf("Bad data area2, read: %02x\n", val); - printf(" nib_pos: %04x\n", dsk->nib_pos); - break; - } - prev_val = val2 ^ prev_val; - buf[i] = prev_val; - } + /* rest of data area */ + for(i = 0; i < 0x100; i++) { + val = iwm_read_data(dsk, 1, 0); + val2 = from_disk_byte[val]; + if(val2 < 0) { + printf("Bad data area2, read: %02x\n", val); + printf(" nib_pos: %04x\n", dsk->nib_pos); + break; + } + prev_val = val2 ^ prev_val; + buf[i] = prev_val; + } - /* checksum */ - val = iwm_read_data(dsk, 1, 0); - val2 = from_disk_byte[val]; - if(val2 < 0) { - printf("Bad data area3, read: %02x\n", val); - printf(" nib_pos: %04x\n", dsk->nib_pos); - break; - } - if(val2 != prev_val) { - printf("Bad data cksum, got %02x, wanted: %02x\n", - val2, prev_val); - printf(" nib_pos: %04x\n", dsk->nib_pos); - break; - } + /* checksum */ + val = iwm_read_data(dsk, 1, 0); + val2 = from_disk_byte[val]; + if(val2 < 0) { + printf("Bad data area3, read: %02x\n", val); + printf(" nib_pos: %04x\n", dsk->nib_pos); + break; + } + if(val2 != prev_val) { + printf("Bad data cksum, got %02x, wanted: %02x\n", + val2, prev_val); + printf(" nib_pos: %04x\n", dsk->nib_pos); + break; + } - /* Got this far, data is good, merge aux_buf into buf */ - x = 0x55; - for(i = 0; i < 0x100; i++) { - val = aux_buf[x]; - val2 = (buf[i] << 1) + (val & 1); - val = val >> 1; - val2 = (val2 << 1) + (val & 1); - buf[i] = val2; - val = val >> 1; - aux_buf[x] = val; - x--; - if(x < 0) { - x = 0x55; - } - } - sector_done[phys_sec] = 1; - num_sectors_done++; - if(num_sectors_done >= 16) { - status = 0; - break; - } - } + /* Got this far, data is good, merge aux_buf into buf */ + x = 0x55; + for(i = 0; i < 0x100; i++) { + val = aux_buf[x]; + val2 = (buf[i] << 1) + (val & 1); + val = val >> 1; + val2 = (val2 << 1) + (val & 1); + buf[i] = val2; + val = val >> 1; + aux_buf[x] = val; + x--; + if(x < 0) { + x = 0x55; + } + } + sector_done[phys_sec] = 1; + num_sectors_done++; + if(num_sectors_done >= 16) { + status = 0; + break; + } + } - tmp_nib_pos = dsk->nib_pos; - iwm_move_to_track(dsk, save_qtr_track); - dsk->nib_pos = save_nib_pos; - g_fast_disk_unnib = 0; + tmp_nib_pos = dsk->nib_pos; + iwm_move_to_track(dsk, save_qtr_track); + dsk->nib_pos = save_nib_pos; + g_fast_disk_unnib = 0; - if(status == 0) { - return 1; - } + if(status == 0) { + return 1; + } - printf("Nibblization not done, %02x sectors found on track %02x\n", - num_sectors_done, qtr_track>>2); - printf("my_nib_cnt: %04x, nib_pos: %04x, trk_len: %04x\n", my_nib_cnt, - tmp_nib_pos, track_len); - for(i = 0; i < 16; i++) { - printf("sector_done[%d] = %d\n", i, sector_done[i]); - } - return -1; + printf("Nibblization not done, %02x sectors found on track %02x\n", + num_sectors_done, qtr_track>>2); + printf("my_nib_cnt: %04x, nib_pos: %04x, trk_len: %04x\n", my_nib_cnt, + tmp_nib_pos, track_len); + for(i = 0; i < 16; i++) { + printf("sector_done[%d] = %d\n", i, sector_done[i]); + } + return -1; } -int -iwm_denib_track35(Disk *dsk, Trk *trk, int qtr_track, byte *outbuf) -{ - word32 buf_c00[0x100]; - word32 buf_d00[0x100]; - word32 buf_e00[0x100]; - byte *buf; - word32 tmp_5c, tmp_5d, tmp_5e; - word32 tmp_66, tmp_67; - int sector_done[16]; - int num_sectors_done; - int track_len; - int phys_track, phys_sec, phys_side, phys_capacity, cksum; - int tmp; - int track, side; - int num_sectors; - int val; - int val2; - int x, y; - int carry; - int my_nib_cnt; - int save_qtr_track; - int save_nib_pos; - int status; - int i; +int iwm_denib_track35(Disk *dsk, Trk *trk, int qtr_track, byte *outbuf) { + word32 buf_c00[0x100]; + word32 buf_d00[0x100]; + word32 buf_e00[0x100]; + byte *buf; + word32 tmp_5c, tmp_5d, tmp_5e; + word32 tmp_66, tmp_67; + int sector_done[16]; + int num_sectors_done; + int track_len; + int phys_track, phys_sec, phys_side, phys_capacity, cksum; + int tmp; + int track, side; + int num_sectors; + int val; + int val2; + int x, y; + int carry; + int my_nib_cnt; + int save_qtr_track; + int save_nib_pos; + int status; + int i; - save_qtr_track = dsk->cur_qtr_track; - save_nib_pos = dsk->nib_pos; + save_qtr_track = dsk->cur_qtr_track; + save_nib_pos = dsk->nib_pos; - iwm_move_to_track(dsk, qtr_track); + iwm_move_to_track(dsk, qtr_track); - dsk->nib_pos = 0; - g_fast_disk_unnib = 1; + dsk->nib_pos = 0; + g_fast_disk_unnib = 1; - track_len = trk->track_len; + track_len = trk->track_len; - num_sectors = g_track_bytes_35[qtr_track >> 5] >> 9; + num_sectors = g_track_bytes_35[qtr_track >> 5] >> 9; - for(i = 0; i < num_sectors; i++) { - sector_done[i] = 0; - } + for(i = 0; i < num_sectors; i++) { + sector_done[i] = 0; + } - num_sectors_done = 0; + num_sectors_done = 0; - val = 0; - status = -1; - my_nib_cnt = 0; + val = 0; + status = -1; + my_nib_cnt = 0; - track = qtr_track >> 1; - side = qtr_track & 1; + track = qtr_track >> 1; + side = qtr_track & 1; - while(my_nib_cnt++ < 2*track_len) { - /* look for start of a sector */ - if(val != 0xd5) { - val = iwm_read_data(dsk, 1, 0); - continue; - } + while(my_nib_cnt++ < 2*track_len) { + /* look for start of a sector */ + if(val != 0xd5) { + val = iwm_read_data(dsk, 1, 0); + continue; + } - val = iwm_read_data(dsk, 1, 0); - if(val != 0xaa) { - continue; - } + val = iwm_read_data(dsk, 1, 0); + if(val != 0xaa) { + continue; + } - val = iwm_read_data(dsk, 1, 0); - if(val != 0x96) { - continue; - } + val = iwm_read_data(dsk, 1, 0); + if(val != 0x96) { + continue; + } - /* It's a sector start */ - val = iwm_read_data(dsk, 1, 0); - phys_track = from_disk_byte[val]; - if(phys_track != (track & 0x3f)) { - printf("Track %02x.%d, read track %02x, %02x\n", - track, side, phys_track, val); - break; - } + /* It's a sector start */ + val = iwm_read_data(dsk, 1, 0); + phys_track = from_disk_byte[val]; + if(phys_track != (track & 0x3f)) { + printf("Track %02x.%d, read track %02x, %02x\n", + track, side, phys_track, val); + break; + } - phys_sec = from_disk_byte[iwm_read_data(dsk, 1, 0)]; - if(phys_sec < 0 || phys_sec >= num_sectors) { - printf("Track %02x.%d, read sector %02x??\n", - track, side, phys_sec); - break; - } - phys_side = from_disk_byte[iwm_read_data(dsk, 1, 0)]; + phys_sec = from_disk_byte[iwm_read_data(dsk, 1, 0)]; + if(phys_sec < 0 || phys_sec >= num_sectors) { + printf("Track %02x.%d, read sector %02x??\n", + track, side, phys_sec); + break; + } + phys_side = from_disk_byte[iwm_read_data(dsk, 1, 0)]; - if(phys_side != ((side << 5) + (track >> 6))) { - printf("Track %02x.%d, read side %02x??\n", - track, side, phys_side); - break; - } - phys_capacity = from_disk_byte[iwm_read_data(dsk, 1, 0)]; - if(phys_capacity != 0x24 && phys_capacity != 0x22) { - printf("Track %02x.%x capacity: %02x != 0x24/22\n", - track, side, phys_capacity); - } - cksum = from_disk_byte[iwm_read_data(dsk, 1, 0)]; + if(phys_side != ((side << 5) + (track >> 6))) { + printf("Track %02x.%d, read side %02x??\n", + track, side, phys_side); + break; + } + phys_capacity = from_disk_byte[iwm_read_data(dsk, 1, 0)]; + if(phys_capacity != 0x24 && phys_capacity != 0x22) { + printf("Track %02x.%x capacity: %02x != 0x24/22\n", + track, side, phys_capacity); + } + cksum = from_disk_byte[iwm_read_data(dsk, 1, 0)]; - tmp = phys_track ^ phys_sec ^ phys_side ^ phys_capacity; - if(cksum != tmp) { - printf("Track %02x.%d, sector %02x, cksum: %02x.%02x\n", - track, side, phys_sec, cksum, tmp); - break; - } + tmp = phys_track ^ phys_sec ^ phys_side ^ phys_capacity; + if(cksum != tmp) { + printf("Track %02x.%d, sector %02x, cksum: %02x.%02x\n", + track, side, phys_sec, cksum, tmp); + break; + } - if(sector_done[phys_sec]) { - printf("Already done sector %02x on track %02x.%x!\n", - phys_sec, track, side); - break; - } + if(sector_done[phys_sec]) { + printf("Already done sector %02x on track %02x.%x!\n", + phys_sec, track, side); + break; + } - /* So far so good, let's do it! */ - val = 0; - for(i = 0; i < 38; i++) { - val = iwm_read_data(dsk, 1, 0); - if(val == 0xd5) { - break; - } - } - if(val != 0xd5) { - printf("No data header, track %02x.%x, sec %02x\n", - track, side, phys_sec); - break; - } + /* So far so good, let's do it! */ + val = 0; + for(i = 0; i < 38; i++) { + val = iwm_read_data(dsk, 1, 0); + if(val == 0xd5) { + break; + } + } + if(val != 0xd5) { + printf("No data header, track %02x.%x, sec %02x\n", + track, side, phys_sec); + break; + } - val = iwm_read_data(dsk, 1, 0); - if(val != 0xaa) { - printf("Bad data hdr1,val:%02x trk %02x.%x, sec %02x\n", - val, track, side, phys_sec); - printf("nib_pos: %08x\n", dsk->nib_pos); - break; - } + val = iwm_read_data(dsk, 1, 0); + if(val != 0xaa) { + printf("Bad data hdr1,val:%02x trk %02x.%x, sec %02x\n", + val, track, side, phys_sec); + printf("nib_pos: %08x\n", dsk->nib_pos); + break; + } - val = iwm_read_data(dsk, 1, 0); - if(val != 0xad) { - printf("Bad data hdr2,val:%02x trk %02x.%x, sec %02x\n", - val, track, side, phys_sec); - break; - } + val = iwm_read_data(dsk, 1, 0); + if(val != 0xad) { + printf("Bad data hdr2,val:%02x trk %02x.%x, sec %02x\n", + val, track, side, phys_sec); + break; + } - buf = outbuf + (phys_sec << 9); + buf = outbuf + (phys_sec << 9); - /* check sector again */ - val = from_disk_byte[iwm_read_data(dsk, 1, 0)]; - if(val != phys_sec) { - printf("Bad data hdr3,val:%02x trk %02x.%x, sec %02x\n", - val, track, side, phys_sec); - break; - } + /* check sector again */ + val = from_disk_byte[iwm_read_data(dsk, 1, 0)]; + if(val != phys_sec) { + printf("Bad data hdr3,val:%02x trk %02x.%x, sec %02x\n", + val, track, side, phys_sec); + break; + } - /* Data start! */ - tmp_5c = 0; - tmp_5d = 0; - tmp_5e = 0; - y = 0xaf; - carry = 0; + /* Data start! */ + tmp_5c = 0; + tmp_5d = 0; + tmp_5e = 0; + y = 0xaf; + carry = 0; - while(y > 0) { + while(y > 0) { /* 626f */ - val = iwm_read_data(dsk, 1, 0); - val2 = from_disk_byte[val]; - if(val2 < 0) { - printf("Bad data area1b, read: %02x\n", val); - printf(" i:%03x,n_pos:%04x\n", i, dsk->nib_pos); - break; - } - tmp_66 = val2; + val = iwm_read_data(dsk, 1, 0); + val2 = from_disk_byte[val]; + if(val2 < 0) { + printf("Bad data area1b, read: %02x\n", val); + printf(" i:%03x,n_pos:%04x\n", i, dsk->nib_pos); + break; + } + tmp_66 = val2; - tmp_5c = tmp_5c << 1; - carry = (tmp_5c >> 8); - tmp_5c = (tmp_5c + carry) & 0xff; + tmp_5c = tmp_5c << 1; + carry = (tmp_5c >> 8); + tmp_5c = (tmp_5c + carry) & 0xff; - val = iwm_read_data(dsk, 1, 0); - val2 = from_disk_byte[val]; - if(val2 < 0) { - printf("Bad data area2, read: %02x\n", val); - break; - } + val = iwm_read_data(dsk, 1, 0); + val2 = from_disk_byte[val]; + if(val2 < 0) { + printf("Bad data area2, read: %02x\n", val); + break; + } - val2 = val2 + ((tmp_66 << 2) & 0xc0); + val2 = val2 + ((tmp_66 << 2) & 0xc0); - val2 = val2 ^ tmp_5c; - buf_c00[y] = val2; + val2 = val2 ^ tmp_5c; + buf_c00[y] = val2; - tmp_5e = val2 + tmp_5e + carry; - carry = (tmp_5e >> 8); - tmp_5e = tmp_5e & 0xff; + tmp_5e = val2 + tmp_5e + carry; + carry = (tmp_5e >> 8); + tmp_5e = tmp_5e & 0xff; /* 62b8 */ - val = iwm_read_data(dsk, 1, 0); - val2 = from_disk_byte[val]; - val2 = val2 + ((tmp_66 << 4) & 0xc0); - val2 = val2 ^ tmp_5e; - buf_d00[y] = val2; - tmp_5d = val2 + tmp_5d + carry; + val = iwm_read_data(dsk, 1, 0); + val2 = from_disk_byte[val]; + val2 = val2 + ((tmp_66 << 4) & 0xc0); + val2 = val2 ^ tmp_5e; + buf_d00[y] = val2; + tmp_5d = val2 + tmp_5d + carry; - carry = (tmp_5d >> 8); - tmp_5d = tmp_5d & 0xff; + carry = (tmp_5d >> 8); + tmp_5d = tmp_5d & 0xff; - y--; - if(y <= 0) { - break; - } + y--; + if(y <= 0) { + break; + } /* 6274 */ - val = iwm_read_data(dsk, 1, 0); - val2 = from_disk_byte[val]; - val2 = val2 + ((tmp_66 << 6) & 0xc0); - val2 = val2 ^ tmp_5d; - buf_e00[y+1] = val2; + val = iwm_read_data(dsk, 1, 0); + val2 = from_disk_byte[val]; + val2 = val2 + ((tmp_66 << 6) & 0xc0); + val2 = val2 ^ tmp_5d; + buf_e00[y+1] = val2; - tmp_5c = val2 + tmp_5c + carry; - carry = (tmp_5c >> 8); - tmp_5c = tmp_5c & 0xff; - } + tmp_5c = val2 + tmp_5c + carry; + carry = (tmp_5c >> 8); + tmp_5c = tmp_5c & 0xff; + } /* 62d0 */ - val = iwm_read_data(dsk, 1, 0); - val2 = from_disk_byte[val]; + val = iwm_read_data(dsk, 1, 0); + val2 = from_disk_byte[val]; - tmp_66 = (val2 << 6) & 0xc0; - tmp_67 = (val2 << 4) & 0xc0; - val2 = (val2 << 2) & 0xc0; + tmp_66 = (val2 << 6) & 0xc0; + tmp_67 = (val2 << 4) & 0xc0; + val2 = (val2 << 2) & 0xc0; - val = iwm_read_data(dsk, 1, 0); - val2 = from_disk_byte[val] + val2; - if(tmp_5e != (word32)val2) { - printf("Checksum 5e bad: %02x vs %02x\n", tmp_5e, val2); - printf("val:%02x trk %02x.%x, sec %02x\n", - val, track, side, phys_sec); - break; - } + val = iwm_read_data(dsk, 1, 0); + val2 = from_disk_byte[val] + val2; + if(tmp_5e != (word32)val2) { + printf("Checksum 5e bad: %02x vs %02x\n", tmp_5e, val2); + printf("val:%02x trk %02x.%x, sec %02x\n", + val, track, side, phys_sec); + break; + } - val = iwm_read_data(dsk, 1, 0); - val2 = from_disk_byte[val] + tmp_67; - if(tmp_5d != (word32)val2) { - printf("Checksum 5d bad: %02x vs %02x\n", tmp_5e, val2); - printf("val:%02x trk %02x.%x, sec %02x\n", - val, track, side, phys_sec); - break; - } + val = iwm_read_data(dsk, 1, 0); + val2 = from_disk_byte[val] + tmp_67; + if(tmp_5d != (word32)val2) { + printf("Checksum 5d bad: %02x vs %02x\n", tmp_5e, val2); + printf("val:%02x trk %02x.%x, sec %02x\n", + val, track, side, phys_sec); + break; + } - val = iwm_read_data(dsk, 1, 0); - val2 = from_disk_byte[val] + tmp_66; - if(tmp_5c != (word32)val2) { - printf("Checksum 5c bad: %02x vs %02x\n", tmp_5e, val2); - printf("val:%02x trk %02x.%x, sec %02x\n", - val, track, side, phys_sec); - break; - } + val = iwm_read_data(dsk, 1, 0); + val2 = from_disk_byte[val] + tmp_66; + if(tmp_5c != (word32)val2) { + printf("Checksum 5c bad: %02x vs %02x\n", tmp_5e, val2); + printf("val:%02x trk %02x.%x, sec %02x\n", + val, track, side, phys_sec); + break; + } - /* Whew, got it!...check for DE AA */ - val = iwm_read_data(dsk, 1, 0); - if(val != 0xde) { - printf("Bad data epi1,val:%02x trk %02x.%x, sec %02x\n", - val, track, side, phys_sec); - printf("nib_pos: %08x\n", dsk->nib_pos); - break; - } + /* Whew, got it!...check for DE AA */ + val = iwm_read_data(dsk, 1, 0); + if(val != 0xde) { + printf("Bad data epi1,val:%02x trk %02x.%x, sec %02x\n", + val, track, side, phys_sec); + printf("nib_pos: %08x\n", dsk->nib_pos); + break; + } - val = iwm_read_data(dsk, 1, 0); - if(val != 0xaa) { - printf("Bad data epi2,val:%02x trk %02x.%x, sec %02x\n", - val, track, side, phys_sec); - break; - } + val = iwm_read_data(dsk, 1, 0); + if(val != 0xaa) { + printf("Bad data epi2,val:%02x trk %02x.%x, sec %02x\n", + val, track, side, phys_sec); + break; + } - /* Now, convert buf_c/d/e to output */ + /* Now, convert buf_c/d/e to output */ /* 6459 */ - y = 0; - for(x = 0xab; x >= 0; x--) { - *buf++ = buf_c00[x]; - y++; - if(y >= 0x200) { - break; - } + y = 0; + for(x = 0xab; x >= 0; x--) { + *buf++ = buf_c00[x]; + y++; + if(y >= 0x200) { + break; + } - *buf++ = buf_d00[x]; - y++; - if(y >= 0x200) { - break; - } + *buf++ = buf_d00[x]; + y++; + if(y >= 0x200) { + break; + } - *buf++ = buf_e00[x]; - y++; - if(y >= 0x200) { - break; - } - } + *buf++ = buf_e00[x]; + y++; + if(y >= 0x200) { + break; + } + } - sector_done[phys_sec] = 1; - num_sectors_done++; - if(num_sectors_done >= num_sectors) { - status = 0; - break; - } - val = 0; - } + sector_done[phys_sec] = 1; + num_sectors_done++; + if(num_sectors_done >= num_sectors) { + status = 0; + break; + } + val = 0; + } - if(status < 0) { - printf("dsk->nib_pos: %04x, status: %d\n", dsk->nib_pos, - status); - for(i = 0; i < num_sectors; i++) { - printf("sector done[%d] = %d\n", i, sector_done[i]); - } - } + if(status < 0) { + printf("dsk->nib_pos: %04x, status: %d\n", dsk->nib_pos, + status); + for(i = 0; i < num_sectors; i++) { + printf("sector done[%d] = %d\n", i, sector_done[i]); + } + } - iwm_move_to_track(dsk, save_qtr_track); - dsk->nib_pos = save_nib_pos; - g_fast_disk_unnib = 0; + iwm_move_to_track(dsk, save_qtr_track); + dsk->nib_pos = save_nib_pos; + g_fast_disk_unnib = 0; - if(status == 0) { - return 1; - } + if(status == 0) { + return 1; + } - printf("Nibblization not done, %02x sectors found on track %02x\n", - num_sectors_done, qtr_track>>2); - return -1; + printf("Nibblization not done, %02x sectors found on track %02x\n", + num_sectors_done, qtr_track>>2); + return -1; @@ -1631,751 +1579,727 @@ iwm_denib_track35(Disk *dsk, Trk *trk, int qtr_track, byte *outbuf) /* ret = 1 -> dirty data written out */ /* ret = 0 -> not dirty, no error */ /* ret < 0 -> error */ -int -disk_track_to_unix(Disk *dsk, int qtr_track, byte *outbuf) -{ - int i; - Trk *trk; - int disk_525; +int disk_track_to_unix(Disk *dsk, int qtr_track, byte *outbuf) { + int i; + Trk *trk; + int disk_525; - disk_525 = dsk->disk_525; + disk_525 = dsk->disk_525; - trk = &(dsk->trks[qtr_track]); + trk = &(dsk->trks[qtr_track]); - if(trk->track_len == 0 || trk->track_dirty == 0) { - return 0; - } + if(trk->track_len == 0 || trk->track_dirty == 0) { + return 0; + } - trk->track_dirty = 0; + trk->track_dirty = 0; - if((qtr_track & 3) && disk_525) { - halt_printf("You wrote to phase %02x! Can't wr bk to unix!\n", - qtr_track); - dsk->write_through_to_unix = 0; - return -1; - } + if((qtr_track & 3) && disk_525) { + halt_printf("You wrote to phase %02x! Can't wr bk to unix!\n", + qtr_track); + dsk->write_through_to_unix = 0; + return -1; + } - if(disk_525) - { - // OG - // Add support for .nib file - if (dsk->image_type!=DSK_TYPE_NIB) - return iwm_denib_track525(dsk, trk, qtr_track, outbuf); - else - { - int len = trk->track_len; - byte* trk_ptr = trk->nib_area+1; - byte* nib_ptr = outbuf; - for(i = 0; i < len; i += 2) - { - *nib_ptr++ = *trk_ptr; - trk_ptr+=2; - } - return 1; - } - } else { - return iwm_denib_track35(dsk, trk, qtr_track, outbuf); - } + if(disk_525) + { + // OG + // Add support for .nib file + if (dsk->image_type!=DSK_TYPE_NIB) + return iwm_denib_track525(dsk, trk, qtr_track, outbuf); + else + { + int len = trk->track_len; + byte* trk_ptr = trk->nib_area+1; + byte* nib_ptr = outbuf; + for(i = 0; i < len; i += 2) + { + *nib_ptr++ = *trk_ptr; + trk_ptr+=2; + } + return 1; + } + } else { + return iwm_denib_track35(dsk, trk, qtr_track, outbuf); + } } -void -show_hex_data(byte *buf, int count) -{ - int i; +void show_hex_data(byte *buf, int count) { + int i; - for(i = 0; i < count; i += 16) { - printf("%04x: %02x %02x %02x %02x %02x %02x %02x %02x " - "%02x %02x %02x %02x %02x %02x %02x %02x\n", i, - buf[i+0], buf[i+1], buf[i+2], buf[i+3], - buf[i+4], buf[i+5], buf[i+6], buf[i+7], - buf[i+8], buf[i+9], buf[i+10], buf[i+11], - buf[i+12], buf[i+13], buf[i+14], buf[i+15]); - } + for(i = 0; i < count; i += 16) { + printf("%04x: %02x %02x %02x %02x %02x %02x %02x %02x " + "%02x %02x %02x %02x %02x %02x %02x %02x\n", i, + buf[i+0], buf[i+1], buf[i+2], buf[i+3], + buf[i+4], buf[i+5], buf[i+6], buf[i+7], + buf[i+8], buf[i+9], buf[i+10], buf[i+11], + buf[i+12], buf[i+13], buf[i+14], buf[i+15]); + } } -void -disk_check_nibblization(Disk *dsk, int qtr_track, byte *buf, int size) -{ - byte buffer[0x3000]; - Trk *trk; - int ret, ret2; - int i; +void disk_check_nibblization(Disk *dsk, int qtr_track, byte *buf, int size) { + byte buffer[0x3000]; + Trk *trk; + int ret, ret2; + int i; - if(size > 0x3000) { - printf("size %08x is > 0x3000, disk_check_nibblization\n",size); - exit(3); - } + if(size > 0x3000) { + printf("size %08x is > 0x3000, disk_check_nibblization\n",size); + exit(3); + } - for(i = 0; i < size; i++) { - buffer[i] = 0; - } + for(i = 0; i < size; i++) { + buffer[i] = 0; + } - trk = &(dsk->trks[qtr_track]); + trk = &(dsk->trks[qtr_track]); - if(dsk->disk_525) { - ret = iwm_denib_track525(dsk, trk, qtr_track, &(buffer[0])); - } else { - ret = iwm_denib_track35(dsk, trk, qtr_track, &(buffer[0])); - } + if(dsk->disk_525) { + ret = iwm_denib_track525(dsk, trk, qtr_track, &(buffer[0])); + } else { + ret = iwm_denib_track35(dsk, trk, qtr_track, &(buffer[0])); + } - ret2 = -1; - for(i = 0; i < size; i++) { - if(buffer[i] != buf[i]) { - printf("buffer[%04x]: %02x != %02x\n", i, buffer[i], - buf[i]); - ret2 = i; - break; - } - } + ret2 = -1; + for(i = 0; i < size; i++) { + if(buffer[i] != buf[i]) { + printf("buffer[%04x]: %02x != %02x\n", i, buffer[i], + buf[i]); + ret2 = i; + break; + } + } - if(ret != 1 || ret2 >= 0) { - printf("disk_check_nib ret:%d, ret2:%d for q_track %03x\n", - ret, ret2, qtr_track); - show_hex_data(buf, 0x1000); - show_hex_data(buffer, 0x1000); - iwm_show_a_track(&(dsk->trks[qtr_track])); + if(ret != 1 || ret2 >= 0) { + printf("disk_check_nib ret:%d, ret2:%d for q_track %03x\n", + ret, ret2, qtr_track); + show_hex_data(buf, 0x1000); + show_hex_data(buffer, 0x1000); + iwm_show_a_track(&(dsk->trks[qtr_track])); - exit(2); - } + exit(2); + } } -#define TRACK_BUF_LEN 0x2000 +#define TRACK_BUF_LEN 0x2000 -void -disk_unix_to_nib(Disk *dsk, int qtr_track, int unix_pos, int unix_len, - int nib_len) -{ - byte track_buf[TRACK_BUF_LEN]; - Trk *trk; - int must_clear_track; - int ret; - int len; - int i; +void disk_unix_to_nib(Disk *dsk, int qtr_track, int unix_pos, int unix_len, + int nib_len) { + byte track_buf[TRACK_BUF_LEN]; + Trk *trk; + int must_clear_track; + int ret; + int len; + int i; - /* Read track from dsk int track_buf */ + /* Read track from dsk int track_buf */ - must_clear_track = 0; + must_clear_track = 0; - if(unix_len > TRACK_BUF_LEN) { - printf("diks_unix_to_nib: requested len of image %s = %05x\n", - dsk->name_ptr, unix_len); - } + if(unix_len > TRACK_BUF_LEN) { + printf("diks_unix_to_nib: requested len of image %s = %05x\n", + dsk->name_ptr, unix_len); + } - if(unix_pos >= 0) { - ret = fseek(dsk->file, unix_pos, SEEK_SET); - if(ret != 0) { - printf("fseek of disk %s len 0x%x errno: %d\n", - dsk->name_ptr, unix_pos, errno); - must_clear_track = 1; - } + if(unix_pos >= 0) { + ret = fseek(dsk->file, unix_pos, SEEK_SET); + if(ret != 0) { + printf("fseek of disk %s len 0x%x errno: %d\n", + dsk->name_ptr, unix_pos, errno); + must_clear_track = 1; + } - len = fread(track_buf, 1, unix_len, dsk->file); - if(len != unix_len) { - printf("read of disk %s q_trk %d ret: %d, errno: %d\n", - dsk->name_ptr, qtr_track, ret, errno); - must_clear_track = 1; - } - } + len = fread(track_buf, 1, unix_len, dsk->file); + if(len != unix_len) { + printf("read of disk %s q_trk %d ret: %d, errno: %d\n", + dsk->name_ptr, qtr_track, ret, errno); + must_clear_track = 1; + } + } - if(must_clear_track) { - for(i = 0; i < TRACK_BUF_LEN; i++) { - track_buf[i] = 0; - } - } + if(must_clear_track) { + for(i = 0; i < TRACK_BUF_LEN; i++) { + track_buf[i] = 0; + } + } #if 0 - printf("Q_track %02x dumped out\n", qtr_track); + printf("Q_track %02x dumped out\n", qtr_track); - for(i = 0; i < 4096; i += 32) { - printf("%04x: %02x%02x%02x%02x%02x%02x%02x%02x " - "%02x%02x%02x%02x%02x%02x%02x%02x " - "%02x%02x%02x%02x%02x%02x%02x%02x " - "%02x%02x%02x%02x%02x%02x%02x%02x\n", i, - track_buf[i+0], track_buf[i+1], track_buf[i+2], - track_buf[i+3], track_buf[i+4], track_buf[i+5], - track_buf[i+6], track_buf[i+7], track_buf[i+8], - track_buf[i+9], track_buf[i+10], track_buf[i+11], - track_buf[i+12], track_buf[i+13], track_buf[i+14], - track_buf[i+15], track_buf[i+16], track_buf[i+17], - track_buf[i+18], track_buf[i+19], track_buf[i+20], - track_buf[i+21], track_buf[i+22], track_buf[i+23], - track_buf[i+24], track_buf[i+25], track_buf[i+26], - track_buf[i+27], track_buf[i+28], track_buf[i+29], - track_buf[i+30], track_buf[i+31]); - } + for(i = 0; i < 4096; i += 32) { + printf("%04x: %02x%02x%02x%02x%02x%02x%02x%02x " + "%02x%02x%02x%02x%02x%02x%02x%02x " + "%02x%02x%02x%02x%02x%02x%02x%02x " + "%02x%02x%02x%02x%02x%02x%02x%02x\n", i, + track_buf[i+0], track_buf[i+1], track_buf[i+2], + track_buf[i+3], track_buf[i+4], track_buf[i+5], + track_buf[i+6], track_buf[i+7], track_buf[i+8], + track_buf[i+9], track_buf[i+10], track_buf[i+11], + track_buf[i+12], track_buf[i+13], track_buf[i+14], + track_buf[i+15], track_buf[i+16], track_buf[i+17], + track_buf[i+18], track_buf[i+19], track_buf[i+20], + track_buf[i+21], track_buf[i+22], track_buf[i+23], + track_buf[i+24], track_buf[i+25], track_buf[i+26], + track_buf[i+27], track_buf[i+28], track_buf[i+29], + track_buf[i+30], track_buf[i+31]); + } #endif - dsk->nib_pos = 0; /* for consistency */ + dsk->nib_pos = 0; /* for consistency */ - trk = &(dsk->trks[qtr_track]); - trk->track_dirty = 0; - trk->overflow_size = 0; - trk->track_len = 2*nib_len; - trk->unix_pos = unix_pos; - trk->unix_len = unix_len; - trk->dsk = dsk; - trk->nib_area = (byte *)malloc(trk->track_len); + trk = &(dsk->trks[qtr_track]); + trk->track_dirty = 0; + trk->overflow_size = 0; + trk->track_len = 2*nib_len; + trk->unix_pos = unix_pos; + trk->unix_len = unix_len; + trk->dsk = dsk; + trk->nib_area = (byte *)malloc(trk->track_len); - /* create nibblized image */ + /* create nibblized image */ - if(dsk->disk_525 && dsk->image_type == DSK_TYPE_NIB) { - iwm_nibblize_track_nib525(dsk, trk, track_buf, qtr_track); - } else if(dsk->disk_525) { - iwm_nibblize_track_525(dsk, trk, track_buf, qtr_track); - } else { - iwm_nibblize_track_35(dsk, trk, track_buf, qtr_track); - } + if(dsk->disk_525 && dsk->image_type == DSK_TYPE_NIB) { + iwm_nibblize_track_nib525(dsk, trk, track_buf, qtr_track); + } else if(dsk->disk_525) { + iwm_nibblize_track_525(dsk, trk, track_buf, qtr_track); + } else { + iwm_nibblize_track_35(dsk, trk, track_buf, qtr_track); + } } -void -iwm_nibblize_track_nib525(Disk *dsk, Trk *trk, byte *track_buf, int qtr_track) -{ - byte *nib_ptr; - byte *trk_ptr; - int len; - int i; +void iwm_nibblize_track_nib525(Disk *dsk, Trk *trk, byte *track_buf, int qtr_track) { + byte *nib_ptr; + byte *trk_ptr; + int len; + int i; - len = trk->track_len; - trk_ptr = track_buf; - nib_ptr = &(trk->nib_area[0]); - for(i = 0; i < len; i += 2) { - nib_ptr[i] = 8; - nib_ptr[i+1] = *trk_ptr++;; - } + len = trk->track_len; + trk_ptr = track_buf; + nib_ptr = &(trk->nib_area[0]); + for(i = 0; i < len; i += 2) { + nib_ptr[i] = 8; + nib_ptr[i+1] = *trk_ptr++;; + } - iwm_printf("Nibblized q_track %02x\n", qtr_track); + iwm_printf("Nibblized q_track %02x\n", qtr_track); } -void -iwm_nibblize_track_525(Disk *dsk, Trk *trk, byte *track_buf, int qtr_track) -{ - byte partial_nib_buf[0x300]; - word32 *word_ptr; - word32 val; - word32 last_val; - int phys_sec; - int log_sec; - int num_sync; - int i; +void iwm_nibblize_track_525(Disk *dsk, Trk *trk, byte *track_buf, int qtr_track) { + byte partial_nib_buf[0x300]; + word32 *word_ptr; + word32 val; + word32 last_val; + int phys_sec; + int log_sec; + int num_sync; + int i; - word_ptr = (word32 *)&(trk->nib_area[0]); + word_ptr = (word32 *)&(trk->nib_area[0]); #if defined(GSPLUS_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) // OSX needs to calculate endianness mid-compilation, can't be passed on compile command - val = 0xff08ff08; + val = 0xff08ff08; #else - val = 0x08ff08ff; + val = 0x08ff08ff; #endif - for(i = 0; i < trk->track_len; i += 4) { - *word_ptr++ = val; - } + for(i = 0; i < trk->track_len; i += 4) { + *word_ptr++ = val; + } - for(phys_sec = 0; phys_sec < 16; phys_sec++) { - if(dsk->image_type == DSK_TYPE_DOS33) { - log_sec = phys_to_dos_sec[phys_sec]; - } else { - log_sec = phys_to_prodos_sec[phys_sec]; - } + for(phys_sec = 0; phys_sec < 16; phys_sec++) { + if(dsk->image_type == DSK_TYPE_DOS33) { + log_sec = phys_to_dos_sec[phys_sec]; + } else { + log_sec = phys_to_prodos_sec[phys_sec]; + } - /* Create sync headers */ - if(phys_sec == 0) { - num_sync = 70; - } else { - num_sync = 14; - } + /* Create sync headers */ + if(phys_sec == 0) { + num_sync = 70; + } else { + num_sync = 14; + } - for(i = 0; i < num_sync; i++) { - disk_nib_out(dsk, 0xff, 10); - } - disk_nib_out(dsk, 0xd5, 10); /* prolog */ - disk_nib_out(dsk, 0xaa, 8); /* prolog */ - disk_nib_out(dsk, 0x96, 8); /* prolog */ - disk_4x4_nib_out(dsk, dsk->vol_num); - disk_4x4_nib_out(dsk, qtr_track >> 2); - disk_4x4_nib_out(dsk, phys_sec); - disk_4x4_nib_out(dsk, dsk->vol_num ^ (qtr_track>>2) ^ phys_sec); - disk_nib_out(dsk, 0xde, 8); /* epi */ - disk_nib_out(dsk, 0xaa, 8); /* epi */ - disk_nib_out(dsk, 0xeb, 8); /* epi */ + for(i = 0; i < num_sync; i++) { + disk_nib_out(dsk, 0xff, 10); + } + disk_nib_out(dsk, 0xd5, 10); /* prolog */ + disk_nib_out(dsk, 0xaa, 8); /* prolog */ + disk_nib_out(dsk, 0x96, 8); /* prolog */ + disk_4x4_nib_out(dsk, dsk->vol_num); + disk_4x4_nib_out(dsk, qtr_track >> 2); + disk_4x4_nib_out(dsk, phys_sec); + disk_4x4_nib_out(dsk, dsk->vol_num ^ (qtr_track>>2) ^ phys_sec); + disk_nib_out(dsk, 0xde, 8); /* epi */ + disk_nib_out(dsk, 0xaa, 8); /* epi */ + disk_nib_out(dsk, 0xeb, 8); /* epi */ - /* Inter sync */ - disk_nib_out(dsk, 0xff, 8); - for(i = 0; i < 5; i++) { - disk_nib_out(dsk, 0xff, 10); - } - disk_nib_out(dsk, 0xd5, 10); /* data prolog */ - disk_nib_out(dsk, 0xaa, 8); /* data prolog */ - disk_nib_out(dsk, 0xad, 8); /* data prolog */ + /* Inter sync */ + disk_nib_out(dsk, 0xff, 8); + for(i = 0; i < 5; i++) { + disk_nib_out(dsk, 0xff, 10); + } + disk_nib_out(dsk, 0xd5, 10); /* data prolog */ + disk_nib_out(dsk, 0xaa, 8); /* data prolog */ + disk_nib_out(dsk, 0xad, 8); /* data prolog */ - sector_to_partial_nib( &(track_buf[log_sec*256]), - &(partial_nib_buf[0])); + sector_to_partial_nib( &(track_buf[log_sec*256]), + &(partial_nib_buf[0])); - last_val = 0; - for(i = 0; i < 0x156; i++) { - val = partial_nib_buf[i]; - disk_nib_out(dsk, to_disk_byte[last_val ^ val], 8); - last_val = val; - } - disk_nib_out(dsk, to_disk_byte[last_val], 8); + last_val = 0; + for(i = 0; i < 0x156; i++) { + val = partial_nib_buf[i]; + disk_nib_out(dsk, to_disk_byte[last_val ^ val], 8); + last_val = val; + } + disk_nib_out(dsk, to_disk_byte[last_val], 8); - /* data epilog */ - disk_nib_out(dsk, 0xde, 8); /* epi */ - disk_nib_out(dsk, 0xaa, 8); /* epi */ - disk_nib_out(dsk, 0xeb, 8); /* epi */ - disk_nib_out(dsk, 0xff, 8); - for(i = 0; i < 6; i++) { - disk_nib_out(dsk, 0xff, 10); - } - } + /* data epilog */ + disk_nib_out(dsk, 0xde, 8); /* epi */ + disk_nib_out(dsk, 0xaa, 8); /* epi */ + disk_nib_out(dsk, 0xeb, 8); /* epi */ + disk_nib_out(dsk, 0xff, 8); + for(i = 0; i < 6; i++) { + disk_nib_out(dsk, 0xff, 10); + } + } - /* finish nibblization */ - disk_nib_end_track(dsk); + /* finish nibblization */ + disk_nib_end_track(dsk); - iwm_printf("Nibblized q_track %02x\n", qtr_track); + iwm_printf("Nibblized q_track %02x\n", qtr_track); - if(g_check_nibblization) { - disk_check_nibblization(dsk, qtr_track, &(track_buf[0]),0x1000); - } + if(g_check_nibblization) { + disk_check_nibblization(dsk, qtr_track, &(track_buf[0]),0x1000); + } } -void -iwm_nibblize_track_35(Disk *dsk, Trk *trk, byte *track_buf, int qtr_track) -{ - int phys_to_log_sec[16]; - word32 buf_c00[0x100]; - word32 buf_d00[0x100]; - word32 buf_e00[0x100]; - byte *buf; - word32 *word_ptr; - word32 val; - int num_sectors; - int unix_len; - int log_sec; - int phys_sec; - int track; - int side; - int interleave; - int num_sync; - word32 phys_track, phys_side, capacity, cksum; - word32 tmp_5c, tmp_5d, tmp_5e, tmp_5f; - word32 tmp_63, tmp_64, tmp_65; - word32 acc_hi; - int carry; - int x, y; - int i; +void iwm_nibblize_track_35(Disk *dsk, Trk *trk, byte *track_buf, int qtr_track) { + int phys_to_log_sec[16]; + word32 buf_c00[0x100]; + word32 buf_d00[0x100]; + word32 buf_e00[0x100]; + byte *buf; + word32 *word_ptr; + word32 val; + int num_sectors; + int unix_len; + int log_sec; + int phys_sec; + int track; + int side; + int interleave; + int num_sync; + word32 phys_track, phys_side, capacity, cksum; + word32 tmp_5c, tmp_5d, tmp_5e, tmp_5f; + word32 tmp_63, tmp_64, tmp_65; + word32 acc_hi; + int carry; + int x, y; + int i; - word_ptr = (word32 *)&(trk->nib_area[0]); + word_ptr = (word32 *)&(trk->nib_area[0]); #if defined(GSPLUS_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) - val = 0xff08ff08; + val = 0xff08ff08; #else - val = 0x08ff08ff; + val = 0x08ff08ff; #endif - if(trk->track_len & 3) { - halt_printf("track_len: %08x is not a multiple of 4\n", - trk->track_len); - } + if(trk->track_len & 3) { + halt_printf("track_len: %08x is not a multiple of 4\n", + trk->track_len); + } - for(i = 0; i < trk->track_len; i += 4) { - *word_ptr++ = val; - } + for(i = 0; i < trk->track_len; i += 4) { + *word_ptr++ = val; + } - unix_len = trk->unix_len; + unix_len = trk->unix_len; - num_sectors = (unix_len >> 9); + num_sectors = (unix_len >> 9); - for(i = 0; i < num_sectors; i++) { - phys_to_log_sec[i] = -1; - } + for(i = 0; i < num_sectors; i++) { + phys_to_log_sec[i] = -1; + } - phys_sec = 0; - interleave = 2; - for(log_sec = 0; log_sec < num_sectors; log_sec++) { - while(phys_to_log_sec[phys_sec] >= 0) { - phys_sec++; - if(phys_sec >= num_sectors) { - phys_sec = 0; - } - } - phys_to_log_sec[phys_sec] = log_sec; - phys_sec += interleave; - if(phys_sec >= num_sectors) { - phys_sec -= num_sectors; - } - } + phys_sec = 0; + interleave = 2; + for(log_sec = 0; log_sec < num_sectors; log_sec++) { + while(phys_to_log_sec[phys_sec] >= 0) { + phys_sec++; + if(phys_sec >= num_sectors) { + phys_sec = 0; + } + } + phys_to_log_sec[phys_sec] = log_sec; + phys_sec += interleave; + if(phys_sec >= num_sectors) { + phys_sec -= num_sectors; + } + } - track = qtr_track >> 1; - side = qtr_track & 1; - for(phys_sec = 0; phys_sec < num_sectors; phys_sec++) { + track = qtr_track >> 1; + side = qtr_track & 1; + for(phys_sec = 0; phys_sec < num_sectors; phys_sec++) { - log_sec = phys_to_log_sec[phys_sec]; - if(log_sec < 0) { - printf("Track: %02x.%x phys_sec: %02x = %d!\n", - track, side, phys_sec, log_sec); - exit(2); - } + log_sec = phys_to_log_sec[phys_sec]; + if(log_sec < 0) { + printf("Track: %02x.%x phys_sec: %02x = %d!\n", + track, side, phys_sec, log_sec); + exit(2); + } - /* Create sync headers */ - if(phys_sec == 0) { - num_sync = 400; - } else { - num_sync = 54; - } + /* Create sync headers */ + if(phys_sec == 0) { + num_sync = 400; + } else { + num_sync = 54; + } - for(i = 0; i < num_sync; i++) { - disk_nib_out(dsk, 0xff, 10); - } + for(i = 0; i < num_sync; i++) { + disk_nib_out(dsk, 0xff, 10); + } - disk_nib_out(dsk, 0xd5, 10); /* prolog */ - disk_nib_out(dsk, 0xaa, 8); /* prolog */ - disk_nib_out(dsk, 0x96, 8); /* prolog */ + disk_nib_out(dsk, 0xd5, 10); /* prolog */ + disk_nib_out(dsk, 0xaa, 8); /* prolog */ + disk_nib_out(dsk, 0x96, 8); /* prolog */ - phys_track = track & 0x3f; - phys_side = (side << 5) + (track >> 6); - capacity = 0x22; - disk_nib_out(dsk, to_disk_byte[phys_track], 8); /* trk */ - disk_nib_out(dsk, to_disk_byte[log_sec], 8); /* sec */ - disk_nib_out(dsk, to_disk_byte[phys_side], 8); /* sides+trk */ - disk_nib_out(dsk, to_disk_byte[capacity], 8); /* capacity*/ + phys_track = track & 0x3f; + phys_side = (side << 5) + (track >> 6); + capacity = 0x22; + disk_nib_out(dsk, to_disk_byte[phys_track], 8); /* trk */ + disk_nib_out(dsk, to_disk_byte[log_sec], 8); /* sec */ + disk_nib_out(dsk, to_disk_byte[phys_side], 8); /* sides+trk */ + disk_nib_out(dsk, to_disk_byte[capacity], 8); /* capacity*/ - cksum = (phys_track ^ log_sec ^ phys_side ^ capacity) & 0x3f; - disk_nib_out(dsk, to_disk_byte[cksum], 8); /* cksum*/ + cksum = (phys_track ^ log_sec ^ phys_side ^ capacity) & 0x3f; + disk_nib_out(dsk, to_disk_byte[cksum], 8); /* cksum*/ - disk_nib_out(dsk, 0xde, 8); /* epi */ - disk_nib_out(dsk, 0xaa, 8); /* epi */ + disk_nib_out(dsk, 0xde, 8); /* epi */ + disk_nib_out(dsk, 0xaa, 8); /* epi */ - /* Inter sync */ - for(i = 0; i < 5; i++) { - disk_nib_out(dsk, 0xff, 10); - } - disk_nib_out(dsk, 0xd5, 10); /* data prolog */ - disk_nib_out(dsk, 0xaa, 8); /* data prolog */ - disk_nib_out(dsk, 0xad, 8); /* data prolog */ - disk_nib_out(dsk, to_disk_byte[log_sec], 8); /* sec again */ + /* Inter sync */ + for(i = 0; i < 5; i++) { + disk_nib_out(dsk, 0xff, 10); + } + disk_nib_out(dsk, 0xd5, 10); /* data prolog */ + disk_nib_out(dsk, 0xaa, 8); /* data prolog */ + disk_nib_out(dsk, 0xad, 8); /* data prolog */ + disk_nib_out(dsk, to_disk_byte[log_sec], 8); /* sec again */ - /* do nibblizing! */ - buf = track_buf + (log_sec << 9); + /* do nibblizing! */ + buf = track_buf + (log_sec << 9); /* 6320 */ - tmp_5e = 0; - tmp_5d = 0; - tmp_5c = 0; - y = 0; - x = 0xaf; - buf_c00[0] = 0; - buf_d00[0] = 0; - buf_e00[0] = 0; - buf_e00[1] = 0; - for(y = 0x4; y > 0; y--) { - buf_c00[x] = 0; - buf_d00[x] = 0; - buf_e00[x] = 0; - x--; - } + tmp_5e = 0; + tmp_5d = 0; + tmp_5c = 0; + y = 0; + x = 0xaf; + buf_c00[0] = 0; + buf_d00[0] = 0; + buf_e00[0] = 0; + buf_e00[1] = 0; + for(y = 0x4; y > 0; y--) { + buf_c00[x] = 0; + buf_d00[x] = 0; + buf_e00[x] = 0; + x--; + } - while(x >= 0) { + while(x >= 0) { /* 6338 */ - tmp_5c = tmp_5c << 1; - carry = (tmp_5c >> 8); - tmp_5c = (tmp_5c + carry) & 0xff; + tmp_5c = tmp_5c << 1; + carry = (tmp_5c >> 8); + tmp_5c = (tmp_5c + carry) & 0xff; - val = buf[y]; - tmp_5e = val + tmp_5e + carry; - carry = (tmp_5e >> 8); - tmp_5e = tmp_5e & 0xff; + val = buf[y]; + tmp_5e = val + tmp_5e + carry; + carry = (tmp_5e >> 8); + tmp_5e = tmp_5e & 0xff; - val = val ^ tmp_5c; - buf_c00[x] = val; - y++; + val = val ^ tmp_5c; + buf_c00[x] = val; + y++; /* 634c */ - val = buf[y]; - tmp_5d = tmp_5d + val + carry; - carry = (tmp_5d >> 8); - tmp_5d = tmp_5d & 0xff; - val = val ^ tmp_5e; - buf_d00[x] = val; - y++; - x--; - if(x <= 0) { - break; - } + val = buf[y]; + tmp_5d = tmp_5d + val + carry; + carry = (tmp_5d >> 8); + tmp_5d = tmp_5d & 0xff; + val = val ^ tmp_5e; + buf_d00[x] = val; + y++; + x--; + if(x <= 0) { + break; + } /* 632a */ - val = buf[y]; - tmp_5c = tmp_5c + val + carry; - carry = (tmp_5c >> 8); - tmp_5c = tmp_5c & 0xff; + val = buf[y]; + tmp_5c = tmp_5c + val + carry; + carry = (tmp_5c >> 8); + tmp_5c = tmp_5c & 0xff; - val = val ^ tmp_5d; - buf_e00[x+1] = val; - y++; - } + val = val ^ tmp_5d; + buf_e00[x+1] = val; + y++; + } /* 635f */ - val = ((tmp_5c >> 2) ^ tmp_5d) & 0x3f; + val = ((tmp_5c >> 2) ^ tmp_5d) & 0x3f; /* 6367 */ - val = (val ^ tmp_5d) >> 2; + val = (val ^ tmp_5d) >> 2; /* 636b */ - val = (val ^ tmp_5e) & 0x3f; + val = (val ^ tmp_5e) & 0x3f; /* 636f */ - val = (val ^ tmp_5e) >> 2; + val = (val ^ tmp_5e) >> 2; /* 6373 */ - tmp_5f = val; + tmp_5f = val; /* 6375 */ - tmp_63 = 0; - tmp_64 = 0; - tmp_65 = 0; - acc_hi = 0; + tmp_63 = 0; + tmp_64 = 0; + tmp_65 = 0; + acc_hi = 0; - y = 0xae; - while(y >= 0) { + y = 0xae; + while(y >= 0) { /* 63e4 */ - /* write out acc_hi */ - val = to_disk_byte[acc_hi & 0x3f]; - disk_nib_out(dsk, val, 8); + /* write out acc_hi */ + val = to_disk_byte[acc_hi & 0x3f]; + disk_nib_out(dsk, val, 8); /* 63f2 */ - val = to_disk_byte[tmp_63 & 0x3f]; - tmp_63 = buf_c00[y]; - acc_hi = tmp_63 >> 6; - disk_nib_out(dsk, val, 8); + val = to_disk_byte[tmp_63 & 0x3f]; + tmp_63 = buf_c00[y]; + acc_hi = tmp_63 >> 6; + disk_nib_out(dsk, val, 8); /* 640b */ - val = to_disk_byte[tmp_64 & 0x3f]; - tmp_64 = buf_d00[y]; - acc_hi = (acc_hi << 2) + (tmp_64 >> 6); - disk_nib_out(dsk, val, 8); - y--; - if(y < 0) { - break; - } + val = to_disk_byte[tmp_64 & 0x3f]; + tmp_64 = buf_d00[y]; + acc_hi = (acc_hi << 2) + (tmp_64 >> 6); + disk_nib_out(dsk, val, 8); + y--; + if(y < 0) { + break; + } /* 63cb */ - val = to_disk_byte[tmp_65 & 0x3f]; - tmp_65 = buf_e00[y+1]; - acc_hi = (acc_hi << 2) + (tmp_65 >> 6); - disk_nib_out(dsk, val, 8); - } + val = to_disk_byte[tmp_65 & 0x3f]; + tmp_65 = buf_e00[y+1]; + acc_hi = (acc_hi << 2) + (tmp_65 >> 6); + disk_nib_out(dsk, val, 8); + } /* 6429 */ - val = to_disk_byte[tmp_5f & 0x3f]; - disk_nib_out(dsk, val, 8); + val = to_disk_byte[tmp_5f & 0x3f]; + disk_nib_out(dsk, val, 8); - val = to_disk_byte[tmp_5e & 0x3f]; - disk_nib_out(dsk, val, 8); + val = to_disk_byte[tmp_5e & 0x3f]; + disk_nib_out(dsk, val, 8); - val = to_disk_byte[tmp_5d & 0x3f]; - disk_nib_out(dsk, val, 8); + val = to_disk_byte[tmp_5d & 0x3f]; + disk_nib_out(dsk, val, 8); - val = to_disk_byte[tmp_5c & 0x3f]; - disk_nib_out(dsk, val, 8); + val = to_disk_byte[tmp_5c & 0x3f]; + disk_nib_out(dsk, val, 8); /* 6440 */ - /* data epilog */ - disk_nib_out(dsk, 0xde, 8); /* epi */ - disk_nib_out(dsk, 0xaa, 8); /* epi */ - disk_nib_out(dsk, 0xff, 8); - } + /* data epilog */ + disk_nib_out(dsk, 0xde, 8); /* epi */ + disk_nib_out(dsk, 0xaa, 8); /* epi */ + disk_nib_out(dsk, 0xff, 8); + } - disk_nib_end_track(dsk); + disk_nib_end_track(dsk); - if(g_check_nibblization) { - disk_check_nibblization(dsk, qtr_track, &(track_buf[0]), - unix_len); - } + if(g_check_nibblization) { + disk_check_nibblization(dsk, qtr_track, &(track_buf[0]), + unix_len); + } } -void -disk_4x4_nib_out(Disk *dsk, word32 val) -{ - disk_nib_out(dsk, 0xaa | (val >> 1), 8); - disk_nib_out(dsk, 0xaa | val, 8); +void disk_4x4_nib_out(Disk *dsk, word32 val) { + disk_nib_out(dsk, 0xaa | (val >> 1), 8); + disk_nib_out(dsk, 0xaa | val, 8); } -void -disk_nib_out(Disk *dsk, byte val, int size) -{ - Trk *trk; - int pos; - int old_size; - int track_len; - int overflow_size; - int qtr_track; +void disk_nib_out(Disk *dsk, byte val, int size) { + Trk *trk; + int pos; + int old_size; + int track_len; + int overflow_size; + int qtr_track; - qtr_track = dsk->cur_qtr_track; + qtr_track = dsk->cur_qtr_track; - track_len = 0; - trk = 0; - if(dsk->trks != 0) { - trk = &(dsk->trks[qtr_track]); - track_len = trk->track_len; - } + track_len = 0; + trk = 0; + if(dsk->trks != 0) { + trk = &(dsk->trks[qtr_track]); + track_len = trk->track_len; + } - if(track_len <= 10) { - printf("Writing to an invalid qtr track: %02x!\n", qtr_track); - printf("name: %s, track_len: %08x, val: %08x, size: %d\n", - dsk->name_ptr, track_len, val, size); - exit(1); - return; - } + if(track_len <= 10) { + printf("Writing to an invalid qtr track: %02x!\n", qtr_track); + printf("name: %s, track_len: %08x, val: %08x, size: %d\n", + dsk->name_ptr, track_len, val, size); + exit(1); + return; + } - trk->track_dirty = 1; - dsk->disk_dirty = 1; + trk->track_dirty = 1; + dsk->disk_dirty = 1; - pos = trk->dsk->nib_pos; - overflow_size = trk->overflow_size; - if(pos >= track_len) { - pos = 0; - } + pos = trk->dsk->nib_pos; + overflow_size = trk->overflow_size; + if(pos >= track_len) { + pos = 0; + } - old_size = trk->nib_area[pos]; + old_size = trk->nib_area[pos]; - while(size >= (10 + old_size)) { - size = size - old_size; - pos += 2; - if(pos >= track_len) { - pos = 0; - } - old_size = trk->nib_area[pos]; - } + while(size >= (10 + old_size)) { + size = size - old_size; + pos += 2; + if(pos >= track_len) { + pos = 0; + } + old_size = trk->nib_area[pos]; + } - if(size > 10) { - size = 10; - } + if(size > 10) { + size = 10; + } - if((val & 0x80) == 0) { - val |= 0x80; - } + if((val & 0x80) == 0) { + val |= 0x80; + } - trk->nib_area[pos++] = size; - trk->nib_area[pos++] = val; - if(pos >= track_len) { - pos = 0; - } + trk->nib_area[pos++] = size; + trk->nib_area[pos++] = val; + if(pos >= track_len) { + pos = 0; + } - overflow_size += (size - old_size); - if((overflow_size > 8) && (size > 8)) { - overflow_size -= trk->nib_area[pos]; - trk->nib_area[pos++] = 0; - trk->nib_area[pos++] = 0; - if(pos >= track_len) { - pos = 0; - } - } else if(overflow_size < -64) { - halt_printf("overflow_sz:%03x, pos:%02x\n",overflow_size,pos); - } + overflow_size += (size - old_size); + if((overflow_size > 8) && (size > 8)) { + overflow_size -= trk->nib_area[pos]; + trk->nib_area[pos++] = 0; + trk->nib_area[pos++] = 0; + if(pos >= track_len) { + pos = 0; + } + } else if(overflow_size < -64) { + halt_printf("overflow_sz:%03x, pos:%02x\n",overflow_size,pos); + } - trk->dsk->nib_pos = pos; - trk->overflow_size = overflow_size; + trk->dsk->nib_pos = pos; + trk->overflow_size = overflow_size; - if((val & 0x80) == 0 || size < 8) { - halt_printf("disk_nib_out, wrote %02x, size: %d\n", val, size); - } + if((val & 0x80) == 0 || size < 8) { + halt_printf("disk_nib_out, wrote %02x, size: %d\n", val, size); + } } -void -disk_nib_end_track(Disk *dsk) -{ - int qtr_track; +void disk_nib_end_track(Disk *dsk) { + int qtr_track; - dsk->nib_pos = 0; - qtr_track = dsk->cur_qtr_track; - dsk->trks[qtr_track].track_dirty = 0; + dsk->nib_pos = 0; + qtr_track = dsk->cur_qtr_track; + dsk->trks[qtr_track].track_dirty = 0; - dsk->disk_dirty = 0; + dsk->disk_dirty = 0; } -void -iwm_show_track(int slot_drive, int track) -{ - Disk *dsk; - Trk *trk; - int drive; - int sel35; - int qtr_track; +void iwm_show_track(int slot_drive, int track) { + Disk *dsk; + Trk *trk; + int drive; + int sel35; + int qtr_track; - if(slot_drive < 0) { - drive = iwm.drive_select; - sel35 = (g_c031_disk35 >> 6) & 1; - } else { - drive = slot_drive & 1; - sel35 = !((slot_drive >> 1) & 1); - } + if(slot_drive < 0) { + drive = iwm.drive_select; + sel35 = (g_c031_disk35 >> 6) & 1; + } else { + drive = slot_drive & 1; + sel35 = !((slot_drive >> 1) & 1); + } - if(sel35) { - dsk = &(iwm.drive35[drive]); - } else { - dsk = &(iwm.drive525[drive]); - } + if(sel35) { + dsk = &(iwm.drive35[drive]); + } else { + dsk = &(iwm.drive525[drive]); + } - if(track < 0) { - qtr_track = dsk->cur_qtr_track; - } else { - qtr_track = track; - } - if(dsk->trks == 0) { - return; - } - trk = &(dsk->trks[qtr_track]); + if(track < 0) { + qtr_track = dsk->cur_qtr_track; + } else { + qtr_track = track; + } + if(dsk->trks == 0) { + return; + } + trk = &(dsk->trks[qtr_track]); - if(trk->track_len <= 0) { - printf("Track_len: %d\n", trk->track_len); - printf("No track for type: %d, drive: %d, qtrk: 0x%02x\n", - sel35, drive, qtr_track); - return; - } + if(trk->track_len <= 0) { + printf("Track_len: %d\n", trk->track_len); + printf("No track for type: %d, drive: %d, qtrk: 0x%02x\n", + sel35, drive, qtr_track); + return; + } - printf("Current drive: %d, q_track: 0x%02x\n", drive, qtr_track); + printf("Current drive: %d, q_track: 0x%02x\n", drive, qtr_track); - iwm_show_a_track(trk); + iwm_show_a_track(trk); } -void -iwm_show_a_track(Trk *trk) -{ - int sum; - int len; - int pos; - int i; +void iwm_show_a_track(Trk *trk) { + int sum; + int len; + int pos; + int i; - printf(" Showtrack:dirty: %d, pos: %04x, ovfl: %04x, len: %04x\n", - trk->track_dirty, trk->dsk->nib_pos, - trk->overflow_size, trk->track_len); + printf(" Showtrack:dirty: %d, pos: %04x, ovfl: %04x, len: %04x\n", + trk->track_dirty, trk->dsk->nib_pos, + trk->overflow_size, trk->track_len); - len = trk->track_len; - printf("Track len in bytes: %04x\n", len); - if(len >= 2*15000) { - len = 2*15000; - printf("len too big, using %04x\n", len); - } + len = trk->track_len; + printf("Track len in bytes: %04x\n", len); + if(len >= 2*15000) { + len = 2*15000; + printf("len too big, using %04x\n", len); + } - pos = 0; - for(i = 0; i < len; i += 16) { - printf("%04x: %2d,%02x %2d,%02x %2d,%02x %2d,%02x " - "%2d,%02x %2d,%02x %2d,%02x %2d,%02x\n", pos, - trk->nib_area[pos], trk->nib_area[pos+1], - trk->nib_area[pos+2], trk->nib_area[pos+3], - trk->nib_area[pos+4], trk->nib_area[pos+5], - trk->nib_area[pos+6], trk->nib_area[pos+7], - trk->nib_area[pos+8], trk->nib_area[pos+9], - trk->nib_area[pos+10], trk->nib_area[pos+11], - trk->nib_area[pos+12], trk->nib_area[pos+13], - trk->nib_area[pos+14], trk->nib_area[pos+15]); - pos += 16; - if(pos >= len) { - pos -= len; - } - } + pos = 0; + for(i = 0; i < len; i += 16) { + printf("%04x: %2d,%02x %2d,%02x %2d,%02x %2d,%02x " + "%2d,%02x %2d,%02x %2d,%02x %2d,%02x\n", pos, + trk->nib_area[pos], trk->nib_area[pos+1], + trk->nib_area[pos+2], trk->nib_area[pos+3], + trk->nib_area[pos+4], trk->nib_area[pos+5], + trk->nib_area[pos+6], trk->nib_area[pos+7], + trk->nib_area[pos+8], trk->nib_area[pos+9], + trk->nib_area[pos+10], trk->nib_area[pos+11], + trk->nib_area[pos+12], trk->nib_area[pos+13], + trk->nib_area[pos+14], trk->nib_area[pos+15]); + pos += 16; + if(pos >= len) { + pos -= len; + } + } - sum = 0; - for(i = 0; i < len; i += 2) { - sum += trk->nib_area[i]; - } + sum = 0; + for(i = 0; i < len; i += 2) { + sum += trk->nib_area[i]; + } - printf("bit_sum: %d, expected: %d, overflow_size: %d\n", - sum, len*8/2, trk->overflow_size); + printf("bit_sum: %d, expected: %d, overflow_size: %d\n", + sum, len*8/2, trk->overflow_size); } diff --git a/src/joystick_driver.c b/src/joystick_driver.c index d9d2a95..a6018d7 100644 --- a/src/joystick_driver.c +++ b/src/joystick_driver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include "glog.h" @@ -28,41 +28,39 @@ SDL_Joystick *gGameController = NULL; -extern int g_joystick_native_type1; /* in paddles.c */ -extern int g_joystick_native_type2; /* in paddles.c */ -extern int g_joystick_native_type; /* in paddles.c */ +extern int g_joystick_native_type1; /* in paddles.c */ +extern int g_joystick_native_type2; /* in paddles.c */ +extern int g_joystick_native_type; /* in paddles.c */ extern int g_joystick_type; extern int g_paddle_buttons; extern int g_paddle_val[]; -const char *g_joystick_dev = "/dev/input/js0"; /* default joystick dev file */ -#define MAX_JOY_NAME 128 +const char *g_joystick_dev = "/dev/input/js0"; /* default joystick dev file */ +#define MAX_JOY_NAME 128 -int g_joystick_native_fd = -1; -int g_joystick_num_axes = 0; -int g_joystick_num_buttons = 0; -int g_joystick_number = 0; // SDL2 -int g_joystick_x_axis = 0; // SDL2 -int g_joystick_y_axis = 1; // SDL2 -int g_joystick_button_0 = 0; // SDL2 -int g_joystick_button_1 = 1; // SDL2 +int g_joystick_native_fd = -1; +int g_joystick_num_axes = 0; +int g_joystick_num_buttons = 0; +int g_joystick_number = 0; // SDL2 +int g_joystick_x_axis = 0; // SDL2 +int g_joystick_y_axis = 1; // SDL2 +int g_joystick_button_0 = 0; // SDL2 +int g_joystick_button_1 = 1; // SDL2 -int g_joystick_x2_axis = 2; // SDL2 -int g_joystick_y2_axis = 3; // SDL2 -int g_joystick_button_2 = 2; // SDL2 -int g_joystick_button_3 = 3; // SDL2 +int g_joystick_x2_axis = 2; // SDL2 +int g_joystick_y2_axis = 3; // SDL2 +int g_joystick_button_2 = 2; // SDL2 +int g_joystick_button_3 = 3; // SDL2 #define JOY2SUPPORT #if defined(HAVE_SDL) && !defined(JOYSTICK_DEFINED) # define JOYSTICK_DEFINED -void -joystick_init() -{ +void joystick_init() { int i; if( SDL_Init( SDL_INIT_JOYSTICK ) < 0 ) { - glogf( "SDL could not initialize joystick! SDL Error: %s", SDL_GetError() ); + glogf( "SDL could not initialize joystick! SDL Error: %s", SDL_GetError() ); } else { glog("SDL2 joystick initialized"); } @@ -70,16 +68,16 @@ joystick_init() glog("No joysticks detected"); SDL_QuitSubSystem(SDL_INIT_JOYSTICK); } else { - // @todo: make controller configurable - // @todo: add multiple controller support - gGameController = SDL_JoystickOpen( g_joystick_number ); - if( gGameController == NULL ) { - glogf( "Warning: Unable to open game controller! SDL Error: %s", SDL_GetError() ); - } - } + // @todo: make controller configurable + // @todo: add multiple controller support + gGameController = SDL_JoystickOpen( g_joystick_number ); + if( gGameController == NULL ) { + glogf( "Warning: Unable to open game controller! SDL Error: %s", SDL_GetError() ); + } + } g_joystick_native_type = 2; g_joystick_native_type1 = 2; - g_joystick_native_type2 = -1; + g_joystick_native_type2 = -1; for(i = 0; i < 4; i++) { g_paddle_val[i] = 180; } @@ -88,43 +86,39 @@ joystick_init() joystick_update(0.0); } -void -joystick_update(double dcycs) -{ +void joystick_update(double dcycs) { if (gGameController) { SDL_JoystickUpdate(); g_paddle_val[0] = (int)SDL_JoystickGetAxis(gGameController, g_joystick_x_axis); // default is 0 g_paddle_val[1] = (int)SDL_JoystickGetAxis(gGameController, g_joystick_y_axis); // default is 1 g_paddle_val[2] = (int)SDL_JoystickGetAxis(gGameController, g_joystick_x2_axis); // default is 2 g_paddle_val[3] = (int)SDL_JoystickGetAxis(gGameController, g_joystick_y2_axis); // default is 3 - + if (SDL_JoystickGetButton(gGameController, g_joystick_button_0)) { - g_paddle_buttons = g_paddle_buttons | 1; - } else { - g_paddle_buttons = g_paddle_buttons & (~1); - } + g_paddle_buttons = g_paddle_buttons | 1; + } else { + g_paddle_buttons = g_paddle_buttons & (~1); + } if (SDL_JoystickGetButton(gGameController, g_joystick_button_1)) { - g_paddle_buttons = g_paddle_buttons | 2; - } else { - g_paddle_buttons = g_paddle_buttons & (~2); - } + g_paddle_buttons = g_paddle_buttons | 2; + } else { + g_paddle_buttons = g_paddle_buttons & (~2); + } if (SDL_JoystickGetButton(gGameController, g_joystick_button_2)) { - g_paddle_buttons = g_paddle_buttons | 4; - } else { - g_paddle_buttons = g_paddle_buttons & (~4); - } + g_paddle_buttons = g_paddle_buttons | 4; + } else { + g_paddle_buttons = g_paddle_buttons & (~4); + } if (SDL_JoystickGetButton(gGameController, g_joystick_button_3)) { - g_paddle_buttons = g_paddle_buttons | 8; - } else { - g_paddle_buttons = g_paddle_buttons & (~8); - } + g_paddle_buttons = g_paddle_buttons | 8; + } else { + g_paddle_buttons = g_paddle_buttons & (~8); + } paddle_update_trigger_dcycs(dcycs); } } -void -joystick_update_buttons() -{ +void joystick_update_buttons() { } void joystick_shut() { @@ -139,202 +133,190 @@ void joystick_shut() { #if defined(__linux__) && !defined(JOYSTICK_DEFINED) # define JOYSTICK_DEFINED -void -joystick_init() -{ - char joy_name[MAX_JOY_NAME]; - int version; - int fd; - int i; +void joystick_init() { + char joy_name[MAX_JOY_NAME]; + int version; + int fd; + int i; - fd = open(g_joystick_dev, O_RDONLY | O_NONBLOCK); - if(fd < 0) { - printf("Unable to open joystick dev file: %s, errno: %d\n", - g_joystick_dev, errno); - printf("Defaulting to mouse joystick\n"); - return; - } + fd = open(g_joystick_dev, O_RDONLY | O_NONBLOCK); + if(fd < 0) { + printf("Unable to open joystick dev file: %s, errno: %d\n", + g_joystick_dev, errno); + printf("Defaulting to mouse joystick\n"); + return; + } - strcpy(&joy_name[0], "Unknown Joystick"); - version = 0x800; + strcpy(&joy_name[0], "Unknown Joystick"); + version = 0x800; - ioctl(fd, JSIOCGNAME(MAX_JOY_NAME), &joy_name[0]); - ioctl(fd, JSIOCGAXES, &g_joystick_num_axes); - ioctl(fd, JSIOCGBUTTONS, &g_joystick_num_buttons); - ioctl(fd, JSIOCGVERSION, &version); + ioctl(fd, JSIOCGNAME(MAX_JOY_NAME), &joy_name[0]); + ioctl(fd, JSIOCGAXES, &g_joystick_num_axes); + ioctl(fd, JSIOCGBUTTONS, &g_joystick_num_buttons); + ioctl(fd, JSIOCGVERSION, &version); - printf("Detected joystick: %s [%d axes, %d buttons vers: %08x]\n", - joy_name, g_joystick_num_axes, g_joystick_num_buttons, - version); + printf("Detected joystick: %s [%d axes, %d buttons vers: %08x]\n", + joy_name, g_joystick_num_axes, g_joystick_num_buttons, + version); - g_joystick_native_type1 = 1; - g_joystick_native_type2 = -1; - g_joystick_native_fd = fd; - for(i = 0; i < 4; i++) { - g_paddle_val[i] = 32767; - } - g_paddle_buttons = 0xc; + g_joystick_native_type1 = 1; + g_joystick_native_type2 = -1; + g_joystick_native_fd = fd; + for(i = 0; i < 4; i++) { + g_paddle_val[i] = 32767; + } + g_paddle_buttons = 0xc; - joystick_update(0.0); + joystick_update(0.0); } /* joystick_update_linux() called from paddles.c. Update g_paddle_val[] */ /* and g_paddle_buttons with current information */ -void -joystick_update(double dcycs) -{ - struct js_event js; /* the linux joystick event record */ - int mask; - int val; - int num; - int type; - int ret; - int len; - int i; +void joystick_update(double dcycs) { + struct js_event js; /* the linux joystick event record */ + int mask; + int val; + int num; + int type; + int ret; + int len; + int i; - /* suck up to 20 events, then give up */ - len = sizeof(struct js_event); - for(i = 0; i < 20; i++) { - ret = read(g_joystick_native_fd, &js, len); - if(ret != len) { - /* just get out */ - break; - } - type = js.type & ~JS_EVENT_INIT; - val = js.value; - num = js.number & 3; /* clamp to 0-3 */ - switch(type) { - case JS_EVENT_BUTTON: - mask = 1 << num; - if(val) { - val = mask; - } - g_paddle_buttons = (g_paddle_buttons & ~mask) | val; - break; - case JS_EVENT_AXIS: - /* val is -32767 to +32767 */ - g_paddle_val[num] = val; - break; - } - } + /* suck up to 20 events, then give up */ + len = sizeof(struct js_event); + for(i = 0; i < 20; i++) { + ret = read(g_joystick_native_fd, &js, len); + if(ret != len) { + /* just get out */ + break; + } + type = js.type & ~JS_EVENT_INIT; + val = js.value; + num = js.number & 3; /* clamp to 0-3 */ + switch(type) { + case JS_EVENT_BUTTON: + mask = 1 << num; + if(val) { + val = mask; + } + g_paddle_buttons = (g_paddle_buttons & ~mask) | val; + break; + case JS_EVENT_AXIS: + /* val is -32767 to +32767 */ + g_paddle_val[num] = val; + break; + } + } // if(i > 0) { // Note from Dave Schmenk: paddle_update_trigger_dcycles(dcycs) always has to be called to keep the triggers current. - paddle_update_trigger_dcycs(dcycs); + paddle_update_trigger_dcycs(dcycs); // } } -void -joystick_update_buttons() -{ +void joystick_update_buttons() { } #endif /* LINUX */ #if defined(_WIN32) && !defined(JOYSTICK_DEFINED) # define JOYSTICK_DEFINED -void -joystick_init() -{ - JOYINFO info; - JOYCAPS joycap; - MMRESULT ret1, ret2; - int i; +void joystick_init() { + JOYINFO info; + JOYCAPS joycap; + MMRESULT ret1, ret2; + int i; - // Check that there is a joystick device - if(joyGetNumDevs() <= 0) { - glog("No joystick hardware detected"); - g_joystick_native_type1 = -1; - g_joystick_native_type2 = -1; - return; - } + // Check that there is a joystick device + if(joyGetNumDevs() <= 0) { + glog("No joystick hardware detected"); + g_joystick_native_type1 = -1; + g_joystick_native_type2 = -1; + return; + } - g_joystick_native_type1 = -1; - g_joystick_native_type2 = -1; + g_joystick_native_type1 = -1; + g_joystick_native_type2 = -1; - // Check that at least joystick 1 or joystick 2 is available - ret1 = joyGetPos(JOYSTICKID1, &info); - ret2 = joyGetDevCaps(JOYSTICKID1, &joycap, sizeof(joycap)); - if(ret1 == JOYERR_NOERROR && ret2 == JOYERR_NOERROR) { - g_joystick_native_type1 = JOYSTICKID1; - printf("Joystick #1 = %s\n", joycap.szPname); - g_joystick_native_type = JOYSTICKID1; - } - ret1 = joyGetPos(JOYSTICKID2, &info); - ret2 = joyGetDevCaps(JOYSTICKID2, &joycap, sizeof(joycap)); - if(ret1 == JOYERR_NOERROR && ret2 == JOYERR_NOERROR) { - g_joystick_native_type2 = JOYSTICKID2; - printf("Joystick #2 = %s\n", joycap.szPname); - if(g_joystick_native_type < 0) { - g_joystick_native_type = JOYSTICKID2; - } - } + // Check that at least joystick 1 or joystick 2 is available + ret1 = joyGetPos(JOYSTICKID1, &info); + ret2 = joyGetDevCaps(JOYSTICKID1, &joycap, sizeof(joycap)); + if(ret1 == JOYERR_NOERROR && ret2 == JOYERR_NOERROR) { + g_joystick_native_type1 = JOYSTICKID1; + printf("Joystick #1 = %s\n", joycap.szPname); + g_joystick_native_type = JOYSTICKID1; + } + ret1 = joyGetPos(JOYSTICKID2, &info); + ret2 = joyGetDevCaps(JOYSTICKID2, &joycap, sizeof(joycap)); + if(ret1 == JOYERR_NOERROR && ret2 == JOYERR_NOERROR) { + g_joystick_native_type2 = JOYSTICKID2; + printf("Joystick #2 = %s\n", joycap.szPname); + if(g_joystick_native_type < 0) { + g_joystick_native_type = JOYSTICKID2; + } + } - if (g_joystick_native_type1<0 && g_joystick_native_type2 <0) { - glog("No joystick is attached"); - return; - } + if (g_joystick_native_type1<0 && g_joystick_native_type2 <0) { + glog("No joystick is attached"); + return; + } - for(i = 0; i < 4; i++) { - g_paddle_val[i] = 32767; - } - g_paddle_buttons = 0xc; + for(i = 0; i < 4; i++) { + g_paddle_val[i] = 32767; + } + g_paddle_buttons = 0xc; - joystick_update(0.0); + joystick_update(0.0); } -void -joystick_update(double dcycs) -{ - JOYCAPS joycap; - JOYINFO info; - UINT id; - MMRESULT ret1, ret2; +void joystick_update(double dcycs) { + JOYCAPS joycap; + JOYINFO info; + UINT id; + MMRESULT ret1, ret2; - id = g_joystick_native_type; + id = g_joystick_native_type; - ret1 = joyGetDevCaps(id, &joycap, sizeof(joycap)); - ret2 = joyGetPos(id, &info); - if(ret1 == JOYERR_NOERROR && ret2 == JOYERR_NOERROR) { - g_paddle_val[0] = (info.wXpos - joycap.wXmin) * 32768 / - (joycap.wXmax - joycap.wXmin); - g_paddle_val[1] = (info.wYpos - joycap.wYmin) * 32768 / - (joycap.wYmax - joycap.wYmin); - if(info.wButtons & JOY_BUTTON1) { - g_paddle_buttons = g_paddle_buttons | 1; - } else { - g_paddle_buttons = g_paddle_buttons & (~1); - } - if(info.wButtons & JOY_BUTTON2) { - g_paddle_buttons = g_paddle_buttons | 2; - } else { - g_paddle_buttons = g_paddle_buttons & (~2); - } - paddle_update_trigger_dcycs(dcycs); - } + ret1 = joyGetDevCaps(id, &joycap, sizeof(joycap)); + ret2 = joyGetPos(id, &info); + if(ret1 == JOYERR_NOERROR && ret2 == JOYERR_NOERROR) { + g_paddle_val[0] = (info.wXpos - joycap.wXmin) * 32768 / + (joycap.wXmax - joycap.wXmin); + g_paddle_val[1] = (info.wYpos - joycap.wYmin) * 32768 / + (joycap.wYmax - joycap.wYmin); + if(info.wButtons & JOY_BUTTON1) { + g_paddle_buttons = g_paddle_buttons | 1; + } else { + g_paddle_buttons = g_paddle_buttons & (~1); + } + if(info.wButtons & JOY_BUTTON2) { + g_paddle_buttons = g_paddle_buttons | 2; + } else { + g_paddle_buttons = g_paddle_buttons & (~2); + } + paddle_update_trigger_dcycs(dcycs); + } } -void -joystick_update_buttons() -{ - JOYINFOEX info; - UINT id; +void joystick_update_buttons() { + JOYINFOEX info; + UINT id; - id = g_joystick_native_type; + id = g_joystick_native_type; - info.dwSize = sizeof(JOYINFOEX); - info.dwFlags = JOY_RETURNBUTTONS; - if(joyGetPosEx(id, &info) == JOYERR_NOERROR) { - if(info.dwButtons & JOY_BUTTON1) { - g_paddle_buttons = g_paddle_buttons | 1; - } else { - g_paddle_buttons = g_paddle_buttons & (~1); - } - if(info.dwButtons & JOY_BUTTON2) { - g_paddle_buttons = g_paddle_buttons | 2; - } else { - g_paddle_buttons = g_paddle_buttons & (~2); - } - } + info.dwSize = sizeof(JOYINFOEX); + info.dwFlags = JOY_RETURNBUTTONS; + if(joyGetPosEx(id, &info) == JOYERR_NOERROR) { + if(info.dwButtons & JOY_BUTTON1) { + g_paddle_buttons = g_paddle_buttons | 1; + } else { + g_paddle_buttons = g_paddle_buttons & (~1); + } + if(info.dwButtons & JOY_BUTTON2) { + g_paddle_buttons = g_paddle_buttons | 2; + } else { + g_paddle_buttons = g_paddle_buttons & (~2); + } + } } #endif @@ -344,31 +326,24 @@ joystick_update_buttons() #ifndef JOYSTICK_DEFINED /* stubs for the routines */ -void -joystick_init() -{ - g_joystick_native_type1 = -1; - g_joystick_native_type2 = -1; - g_joystick_native_type = -1; +void joystick_init() { + g_joystick_native_type1 = -1; + g_joystick_native_type2 = -1; + g_joystick_native_type = -1; } -void -joystick_update(double dcycs) -{ - int i; +void joystick_update(double dcycs) { + int i; - for(i = 0; i < 4; i++) { - g_paddle_val[i] = 32767; - } - g_paddle_buttons = 0xc; + for(i = 0; i < 4; i++) { + g_paddle_val[i] = 32767; + } + g_paddle_buttons = 0xc; } -void -joystick_update_buttons() -{ +void joystick_update_buttons() { } -void joystick_shut() -{ +void joystick_shut() { } #endif diff --git a/src/macdriver_console.c b/src/macdriver_console.c index 0fb8847..8e02f43 100644 --- a/src/macdriver_console.c +++ b/src/macdriver_console.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #ifdef ACTIVEIPHONE #include @@ -25,14 +25,14 @@ pascal OSStatus my_win_handler(EventHandlerCallRef handlerRef, EventRef event, v pascal OSStatus dummy_event_handler(EventHandlerCallRef call_ref, EventRef in_event, void *ignore); -int g_quit_seen = 0; +int g_quit_seen = 0; -#define MAX_STATUS_LINES 7 -#define X_LINE_LENGTH 88 -#define MAX_MAC_ARGS 128 +#define MAX_STATUS_LINES 7 +#define X_LINE_LENGTH 88 +#define MAX_MAC_ARGS 128 -int g_mac_argc = 0; -char *g_mac_argv[MAX_MAC_ARGS]; +int g_mac_argc = 0; +char *g_mac_argv[MAX_MAC_ARGS]; extern char g_argv0_path[]; extern char *g_status_ptrs[MAX_STATUS_LINES]; @@ -40,12 +40,12 @@ extern const char g_gsplus_version_str[]; extern int g_warp_pointer; -extern WindowRef g_main_window; -WindowRef g_main_window_saved; +extern WindowRef g_main_window; +WindowRef g_main_window_saved; EventHandlerUPP g_quit_handler_UPP; EventHandlerUPP g_dummy_event_handler_UPP; -RgnHandle g_event_rgnhandle = 0; -FMFontFamily g_status_font_family; +RgnHandle g_event_rgnhandle = 0; +FMFontFamily g_status_font_family; extern word32 g_red_mask; @@ -59,11 +59,11 @@ extern int g_green_right_shift; extern int g_blue_right_shift; -int g_ignore_next_click = 0; +int g_ignore_next_click = 0; -int g_mainwin_active = 0; -int g_mac_mouse_x = 0; -int g_mac_mouse_y = 0; +int g_mainwin_active = 0; +int g_mac_mouse_x = 0; +int g_mac_mouse_y = 0; extern int g_video_act_width; extern int g_video_act_height; @@ -76,455 +76,437 @@ extern int g_force_depth; extern int g_screen_mdepth; -Ptr g_mac_fullscreen_state = 0; -Rect g_main_window_saved_rect; +Ptr g_mac_fullscreen_state = 0; +Rect g_main_window_saved_rect; extern char *g_fatal_log_strs[]; extern int g_fatal_log; -int -x_show_alert(int is_fatal, const char *str) -{ - DialogRef alert; - DialogItemIndex out_item_hit; - CFStringRef cfstrref, cfstrref2; - CFStringRef okstrref; - AlertStdCFStringAlertParamRec alert_param; - OSStatus osstat; - char *bufptr, *buf2ptr; - int sum, len; - int i; - - /* The dialog eats all events--including key-up events */ - /* Call adb_all_keys_up() to prevent annoying key-repeat problems */ - /* for instance, a key-down causes a dialog to appear--and the */ - /* eats the key-up event...then as soon as the dialog goes, adb.c */ - /* auto-repeat will repeat the key, and the dialog re-appears...*/ - adb_all_keys_up(); - - sum = 20; - for(i = 0; i < g_fatal_log; i++) { - sum += strlen(g_fatal_log_strs[i]); - } - bufptr = (char*)malloc(sum); - buf2ptr = bufptr; - for(i = 0; i < g_fatal_log; i++) { - len = strlen(g_fatal_log_strs[i]); - len = MIN(len, sum); - len = MAX(len, 0); - memcpy(bufptr, g_fatal_log_strs[i], MIN(len, sum)); - bufptr += len; - bufptr[0] = 0; - sum = sum - len; - } - - cfstrref = CFStringCreateWithCString(NULL, buf2ptr, - kCFStringEncodingMacRoman); - - printf("buf2ptr: :%s:\n", buf2ptr); - - osstat = GetStandardAlertDefaultParams(&alert_param, - kStdCFStringAlertVersionOne); - - if(str) { - // Provide an extra option--create a file - cfstrref2 = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, - CFSTR("Create ./%s"), str); - alert_param.otherText = cfstrref2; - } - okstrref = CFSTR("Click OK to continue"); - if(is_fatal) { - okstrref = CFSTR("Click OK to exit GSplus"); - } - CreateStandardAlert(kAlertStopAlert, cfstrref, okstrref, - &alert_param, &alert); - out_item_hit = -1; - RunStandardAlert(alert, NULL, &out_item_hit); - printf("out_item_hit: %d\n", out_item_hit); - free(buf2ptr); - - clear_fatal_logs(); /* free the fatal_log string memory */ - return (out_item_hit >= 3); - +int x_show_alert(int is_fatal, const char *str) { + DialogRef alert; + DialogItemIndex out_item_hit; + CFStringRef cfstrref, cfstrref2; + CFStringRef okstrref; + AlertStdCFStringAlertParamRec alert_param; + OSStatus osstat; + char *bufptr, *buf2ptr; + int sum, len; + int i; + + /* The dialog eats all events--including key-up events */ + /* Call adb_all_keys_up() to prevent annoying key-repeat problems */ + /* for instance, a key-down causes a dialog to appear--and the */ + /* eats the key-up event...then as soon as the dialog goes, adb.c */ + /* auto-repeat will repeat the key, and the dialog re-appears...*/ + adb_all_keys_up(); + + sum = 20; + for(i = 0; i < g_fatal_log; i++) { + sum += strlen(g_fatal_log_strs[i]); + } + bufptr = (char*)malloc(sum); + buf2ptr = bufptr; + for(i = 0; i < g_fatal_log; i++) { + len = strlen(g_fatal_log_strs[i]); + len = MIN(len, sum); + len = MAX(len, 0); + memcpy(bufptr, g_fatal_log_strs[i], MIN(len, sum)); + bufptr += len; + bufptr[0] = 0; + sum = sum - len; + } + + cfstrref = CFStringCreateWithCString(NULL, buf2ptr, + kCFStringEncodingMacRoman); + + printf("buf2ptr: :%s:\n", buf2ptr); + + osstat = GetStandardAlertDefaultParams(&alert_param, + kStdCFStringAlertVersionOne); + + if(str) { + // Provide an extra option--create a file + cfstrref2 = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, + CFSTR("Create ./%s"), str); + alert_param.otherText = cfstrref2; + } + okstrref = CFSTR("Click OK to continue"); + if(is_fatal) { + okstrref = CFSTR("Click OK to exit GSplus"); + } + CreateStandardAlert(kAlertStopAlert, cfstrref, okstrref, + &alert_param, &alert); + out_item_hit = -1; + RunStandardAlert(alert, NULL, &out_item_hit); + printf("out_item_hit: %d\n", out_item_hit); + free(buf2ptr); + + clear_fatal_logs(); /* free the fatal_log string memory */ + return (out_item_hit >= 3); + } -pascal OSStatus -quit_event_handler(EventHandlerCallRef call_ref, EventRef event, void *ignore) -{ - OSStatus err; - - err = CallNextEventHandler(call_ref, event); - if(err == noErr) { - g_quit_seen = 1; - } - return err; +pascal OSStatus quit_event_handler(EventHandlerCallRef call_ref, EventRef event, void *ignore) { + OSStatus err; + + err = CallNextEventHandler(call_ref, event); + if(err == noErr) { + g_quit_seen = 1; + } + return err; } -void -show_simple_alert(char *str1, char *str2, char *str3, int num) -{ - char buf[256]; +void show_simple_alert(char *str1, char *str2, char *str3, int num) { + char buf[256]; - g_fatal_log_strs[0] = gsplus_malloc_str(str1); - g_fatal_log_strs[1] = gsplus_malloc_str(str2); - g_fatal_log_strs[2] = gsplus_malloc_str(str3); - g_fatal_log = 3; - if(num != 0) { - snprintf(buf, 250, ": %d", num); - g_fatal_log_strs[g_fatal_log++] = gsplus_malloc_str(buf); - } - x_show_alert(0, 0); + g_fatal_log_strs[0] = gsplus_malloc_str(str1); + g_fatal_log_strs[1] = gsplus_malloc_str(str2); + g_fatal_log_strs[2] = gsplus_malloc_str(str3); + g_fatal_log = 3; + if(num != 0) { + snprintf(buf, 250, ": %d", num); + g_fatal_log_strs[g_fatal_log++] = gsplus_malloc_str(buf); + } + x_show_alert(0, 0); } -void -x_dialog_create_gsport_conf(const char *str) -{ - char *path; - char tmp_buf[512]; - int ret; +void x_dialog_create_gsport_conf(const char *str) { + char *path; + char tmp_buf[512]; + int ret; - ret = x_show_alert(1, str); - if(ret) { - config_write_config_gsplus_file(); - } + ret = x_show_alert(1, str); + if(ret) { + config_write_config_gsplus_file(); + } } -pascal OSStatus -my_cmd_handler( EventHandlerCallRef handlerRef, EventRef event, void *userdata) -{ - OSStatus osresult; - HICommand command; - word32 command_id; - - osresult = eventNotHandledErr; - - GetEventParameter(event, kEventParamDirectObject, typeHICommand, NULL, - sizeof(HICommand), NULL, &command); - - command_id = (word32)command.commandID; - switch(command_id) { - case 'Kbep': - SysBeep(10); - osresult = noErr; - break; - case 'abou': - show_simple_alert("GSport v", (char *)g_gsplus_version_str, - "\nCopyright 2010 - 2011 GSport Contributors\n" - "Latest version at http://gsport.sourceforge.net/\n", 0); - osresult = noErr; - break; - case 'KCFG': +pascal OSStatus my_cmd_handler( EventHandlerCallRef handlerRef, EventRef event, void *userdata) { + OSStatus osresult; + HICommand command; + word32 command_id; + + osresult = eventNotHandledErr; + + GetEventParameter(event, kEventParamDirectObject, typeHICommand, NULL, + sizeof(HICommand), NULL, &command); + + command_id = (word32)command.commandID; + switch(command_id) { + case 'Kbep': + SysBeep(10); + osresult = noErr; + break; + case 'abou': + show_simple_alert("GSport v", (char *)g_gsplus_version_str, + "\nCopyright 2010 - 2011 GSport Contributors\n" + "Latest version at http://gsport.sourceforge.net/\n", 0); + osresult = noErr; + break; + case 'KCFG': #ifdef ACTIVEGS #else - cfg_toggle_config_panel(); + cfg_toggle_config_panel(); #endif - osresult = noErr; - break; - case 'quit': - break; - case 'swin': - /* not sure what this is, but Panther sends it */ - break; - default: - printf("commandID %08x unknown\n", command_id); - SysBeep(90); - break; - } - return osresult; + osresult = noErr; + break; + case 'quit': + break; + case 'swin': + /* not sure what this is, but Panther sends it */ + break; + default: + printf("commandID %08x unknown\n", command_id); + SysBeep(90); + break; + } + return osresult; } -pascal OSStatus -my_win_handler(EventHandlerCallRef handlerRef, EventRef event, void *userdata) -{ - OSStatus os_result; - UInt32 event_kind; - - os_result = eventNotHandledErr; - - // SysBeep(1); - - event_kind = GetEventKind(event); - // show_alert("win handler", event_kind); - if(event_kind == kEventWindowDrawContent) - { - update_window(); - } if(event_kind == kEventWindowClose) { - - // OG Use HALT_WANTTOQUIT pardigme - //g_quit_sim_now = 1; - set_halt_act(HALT_WANTTOQUIT); - +pascal OSStatus my_win_handler(EventHandlerCallRef handlerRef, EventRef event, void *userdata) { + OSStatus os_result; + UInt32 event_kind; + + os_result = eventNotHandledErr; + + // SysBeep(1); + + event_kind = GetEventKind(event); + // show_alert("win handler", event_kind); + if(event_kind == kEventWindowDrawContent) + { + update_window(); + } if(event_kind == kEventWindowClose) { + + // OG Use HALT_WANTTOQUIT pardigme + //g_quit_sim_now = 1; + set_halt_act(HALT_WANTTOQUIT); + #ifndef ACTIVEGS - g_quit_seen = 1; - my_exit(0); + g_quit_seen = 1; + my_exit(0); #endif - } else { - //show_event(GetEventClass(event), event_kind, 0); - update_window(); - } - - return os_result; + } else { + //show_event(GetEventClass(event), event_kind, 0); + update_window(); + } + + return os_result; } -pascal OSStatus -dummy_event_handler(EventHandlerCallRef call_ref, EventRef in_event, - void *ignore) -{ - OSStatus err; - EventHandlerRef installed_handler; - EventTypeSpec event_spec = { kEventClassApplication, kEventAppQuit }; +pascal OSStatus dummy_event_handler(EventHandlerCallRef call_ref, EventRef in_event, + void *ignore) { + OSStatus err; + EventHandlerRef installed_handler; + EventTypeSpec event_spec = { kEventClassApplication, kEventAppQuit }; - // From http://developer.apple.com/qa/qa2001/qa1061.html - // Trick to move main event queue to use ReceiveNextEvent in an event - // handler called by RunApplicationEventLoop + // From http://developer.apple.com/qa/qa2001/qa1061.html + // Trick to move main event queue to use ReceiveNextEvent in an event + // handler called by RunApplicationEventLoop - err = InstallApplicationEventHandler(g_quit_handler_UPP, 1, &event_spec, - NULL, &installed_handler); + err = InstallApplicationEventHandler(g_quit_handler_UPP, 1, &event_spec, + NULL, &installed_handler); - gsplusmain(g_mac_argc, g_mac_argv); + gsplusmain(g_mac_argc, g_mac_argv); - return noErr; + return noErr; } -void -check_input_events() -{ - OSStatus err; - EventTargetRef target; - EventRef event; - UInt32 event_class, event_kind; - byte mac_keycode; - UInt32 keycode; - UInt32 modifiers; - Point mouse_point, mouse_delta_point; - WindowRef window_ref; - int button, button_state; - EventMouseButton mouse_button; - int handled; - int mouse_events; - int is_up; - int in_win; - int ignore; - - if(g_quit_seen) { - exit(0); - } - - SetPortWindowPort(g_main_window); - - mouse_events = 0; - target = GetEventDispatcherTarget(); - while(1) { - err = ReceiveNextEvent(0, NULL, kEventDurationNoWait, - true, &event); - - if(err == eventLoopTimedOutErr) { - break; - } - if(err != noErr) { - printf("ReceiveNextEvent err: %d\n", (int)err); - break; - } - - event_class = GetEventClass(event); - event_kind = GetEventKind(event); - handled = 0; - switch(event_class) { - case kEventClassKeyboard: - handled = 1; - keycode = 0; - modifiers = 0; - GetEventParameter(event, kEventParamKeyMacCharCodes, - typeChar, NULL, sizeof(byte), NULL, - &mac_keycode); - GetEventParameter(event, kEventParamKeyCode, - typeUInt32, NULL, sizeof(UInt32), NULL, - &keycode); - GetEventParameter(event, kEventParamKeyModifiers, - typeUInt32, NULL, sizeof(UInt32), NULL, - &modifiers); - - mac_update_modifiers((word32)modifiers); - - // Key up/down event - is_up = -1; - switch(event_kind) { - case kEventRawKeyDown: - is_up = 0; - //printf("key down: %02x, %08x\n", - // (int)mac_keycode, (int)keycode); - break; - case kEventRawKeyUp: - is_up = 1; - //printf("key up: %02x, %08x\n", - // (int)mac_keycode, (int)keycode); - break; - case kEventRawKeyModifiersChanged: - is_up = -1; - //printf("key xxx: %08x\n", (int)modifiers); - break; - } - if(is_up >= 0) { - adb_physical_key_update((int)keycode, is_up); - } - break; - case kEventClassMouse: - handled = 2; - mouse_events++; - GetEventParameter(event, kEventParamMouseLocation, - typeQDPoint, NULL, sizeof(Point), NULL, - &mouse_point); - GetWindowRegion(g_main_window, kWindowContentRgn, - g_event_rgnhandle); - in_win = PtInRgn(mouse_point, g_event_rgnhandle); - // in_win = 1 if it was in the contect region of window - err = GetEventParameter(event, kEventParamMouseDelta, - typeQDPoint, NULL, sizeof(Point), NULL, - &mouse_delta_point); - button = 0; - button_state = -1; - switch(event_kind) { - case kEventMouseDown: - button_state = 7; - handled = 3; - break; - case kEventMouseUp: - button_state = 0; - handled = 3; - break; - } - if(button_state >= 0) { - GetEventParameter(event, kEventParamMouseButton, - typeMouseButton, NULL, - sizeof(EventMouseButton), NULL, - &mouse_button); - button = mouse_button; - if(button > 1) { - button = 4 - button; - button = 1 << button; - } - ignore = (button_state != 0) && - (!in_win || g_ignore_next_click); - ignore = ignore || !g_mainwin_active; - if(ignore) { - // Outside of A2 window, ignore clicks - button = 0; - } - if(button_state == 0) { - g_ignore_next_click = 0; - } - } - - GlobalToLocal(&mouse_point); - - if(g_warp_pointer) { - if(err == 0) { - g_mac_mouse_x += mouse_delta_point.h; - g_mac_mouse_y += mouse_delta_point.v; - } - mac_warp_mouse(); - } else { - g_mac_mouse_x = mouse_point.h - - g_video_act_margin_left; - g_mac_mouse_y = mouse_point.v - - g_video_act_margin_top; - } - +void check_input_events() { + OSStatus err; + EventTargetRef target; + EventRef event; + UInt32 event_class, event_kind; + byte mac_keycode; + UInt32 keycode; + UInt32 modifiers; + Point mouse_point, mouse_delta_point; + WindowRef window_ref; + int button, button_state; + EventMouseButton mouse_button; + int handled; + int mouse_events; + int is_up; + int in_win; + int ignore; + + if(g_quit_seen) { + exit(0); + } + + SetPortWindowPort(g_main_window); + + mouse_events = 0; + target = GetEventDispatcherTarget(); + while(1) { + err = ReceiveNextEvent(0, NULL, kEventDurationNoWait, + true, &event); + + if(err == eventLoopTimedOutErr) { + break; + } + if(err != noErr) { + printf("ReceiveNextEvent err: %d\n", (int)err); + break; + } + + event_class = GetEventClass(event); + event_kind = GetEventKind(event); + handled = 0; + switch(event_class) { + case kEventClassKeyboard: + handled = 1; + keycode = 0; + modifiers = 0; + GetEventParameter(event, kEventParamKeyMacCharCodes, + typeChar, NULL, sizeof(byte), NULL, + &mac_keycode); + GetEventParameter(event, kEventParamKeyCode, + typeUInt32, NULL, sizeof(UInt32), NULL, + &keycode); + GetEventParameter(event, kEventParamKeyModifiers, + typeUInt32, NULL, sizeof(UInt32), NULL, + &modifiers); + + mac_update_modifiers((word32)modifiers); + + // Key up/down event + is_up = -1; + switch(event_kind) { + case kEventRawKeyDown: + is_up = 0; + //printf("key down: %02x, %08x\n", + // (int)mac_keycode, (int)keycode); + break; + case kEventRawKeyUp: + is_up = 1; + //printf("key up: %02x, %08x\n", + // (int)mac_keycode, (int)keycode); + break; + case kEventRawKeyModifiersChanged: + is_up = -1; + //printf("key xxx: %08x\n", (int)modifiers); + break; + } + if(is_up >= 0) { + adb_physical_key_update((int)keycode, is_up); + } + break; + case kEventClassMouse: + handled = 2; + mouse_events++; + GetEventParameter(event, kEventParamMouseLocation, + typeQDPoint, NULL, sizeof(Point), NULL, + &mouse_point); + GetWindowRegion(g_main_window, kWindowContentRgn, + g_event_rgnhandle); + in_win = PtInRgn(mouse_point, g_event_rgnhandle); + // in_win = 1 if it was in the contect region of window + err = GetEventParameter(event, kEventParamMouseDelta, + typeQDPoint, NULL, sizeof(Point), NULL, + &mouse_delta_point); + button = 0; + button_state = -1; + switch(event_kind) { + case kEventMouseDown: + button_state = 7; + handled = 3; + break; + case kEventMouseUp: + button_state = 0; + handled = 3; + break; + } + if(button_state >= 0) { + GetEventParameter(event, kEventParamMouseButton, + typeMouseButton, NULL, + sizeof(EventMouseButton), NULL, + &mouse_button); + button = mouse_button; + if(button > 1) { + button = 4 - button; + button = 1 << button; + } + ignore = (button_state != 0) && + (!in_win || g_ignore_next_click); + ignore = ignore || !g_mainwin_active; + if(ignore) { + // Outside of A2 window, ignore clicks + button = 0; + } + if(button_state == 0) { + g_ignore_next_click = 0; + } + } + + GlobalToLocal(&mouse_point); + + if(g_warp_pointer) { + if(err == 0) { + g_mac_mouse_x += mouse_delta_point.h; + g_mac_mouse_y += mouse_delta_point.v; + } + mac_warp_mouse(); + } else { + g_mac_mouse_x = mouse_point.h - + g_video_act_margin_left; + g_mac_mouse_y = mouse_point.v - + g_video_act_margin_top; + } + #if 0 - printf("Mouse %d at: %d,%d button:%d, button_st:%d\n", - mouse_events, g_mac_mouse_x, g_mac_mouse_y, - button, button_state); - printf("Mouse deltas: err:%d, %d,%d\n", (int)err, - mouse_delta_point.h, mouse_delta_point.v); + printf("Mouse %d at: %d,%d button:%d, button_st:%d\n", + mouse_events, g_mac_mouse_x, g_mac_mouse_y, + button, button_state); + printf("Mouse deltas: err:%d, %d,%d\n", (int)err, + mouse_delta_point.h, mouse_delta_point.v); #endif - - update_mouse(g_mac_mouse_x, g_mac_mouse_y, - button_state, button & 7); - if(g_warp_pointer) { - g_mac_mouse_x = A2_WINDOW_WIDTH/2; - g_mac_mouse_y = A2_WINDOW_HEIGHT/2; - update_mouse(g_mac_mouse_x, g_mac_mouse_y,0,-1); - } - break; - case kEventClassApplication: - switch(event_kind) { - case kEventAppActivated: - handled = 1; - g_mainwin_active = 1; - window_ref = 0; - GetEventParameter(event, kEventParamWindowRef, - typeWindowRef, NULL, sizeof(WindowRef), - NULL, &window_ref); - if(window_ref == g_main_window) { - g_ignore_next_click = 1; - } - break; - case kEventAppDeactivated: - handled = 1; - g_mainwin_active = 0; - g_ignore_next_click = 1; - break; - } - break; - } - // show_event(event_class, event_kind, handled); - if(handled != 1) { - (void)SendEventToEventTarget(event, target); - } - ReleaseEvent(event); - } - - return; + + update_mouse(g_mac_mouse_x, g_mac_mouse_y, + button_state, button & 7); + if(g_warp_pointer) { + g_mac_mouse_x = A2_WINDOW_WIDTH/2; + g_mac_mouse_y = A2_WINDOW_HEIGHT/2; + update_mouse(g_mac_mouse_x, g_mac_mouse_y,0,-1); + } + break; + case kEventClassApplication: + switch(event_kind) { + case kEventAppActivated: + handled = 1; + g_mainwin_active = 1; + window_ref = 0; + GetEventParameter(event, kEventParamWindowRef, + typeWindowRef, NULL, sizeof(WindowRef), + NULL, &window_ref); + if(window_ref == g_main_window) { + g_ignore_next_click = 1; + } + break; + case kEventAppDeactivated: + handled = 1; + g_mainwin_active = 0; + g_ignore_next_click = 1; + break; + } + break; + } + // show_event(event_class, event_kind, handled); + if(handled != 1) { + (void)SendEventToEventTarget(event, target); + } + ReleaseEvent(event); + } + + return; } -void -temp_run_application_event_loop(void) -{ - OSStatus err; - EventRef dummy_event; - EventHandlerRef install_handler; - EventTypeSpec event_spec = { 'KWIN', 'KWIN' }; - - // Create UPP for dummy_event_handler and for quit_event_handler - err = noErr; - dummy_event = 0; - - g_dummy_event_handler_UPP = NewEventHandlerUPP(dummy_event_handler); - g_quit_handler_UPP = NewEventHandlerUPP(quit_event_handler); - if((g_dummy_event_handler_UPP == 0) || (g_quit_handler_UPP == 0)) { - err = memFullErr; - } - - if(err == noErr) { - err = InstallApplicationEventHandler(g_dummy_event_handler_UPP, - 1, &event_spec, 0, &install_handler); - if(err == noErr) { - err = MacCreateEvent(NULL, 'KWIN', 'KWIN', - GetCurrentEventTime(), kEventAttributeNone, - &dummy_event); - if(err == noErr) { - err = PostEventToQueue(GetMainEventQueue(), - dummy_event, kEventPriorityHigh); - } - if(err == noErr) { - RunApplicationEventLoop(); - } - - (void)RemoveEventHandler(install_handler); - } - } - - if(dummy_event != NULL) { - ReleaseEvent(dummy_event); - } +void temp_run_application_event_loop(void) { + OSStatus err; + EventRef dummy_event; + EventHandlerRef install_handler; + EventTypeSpec event_spec = { 'KWIN', 'KWIN' }; + + // Create UPP for dummy_event_handler and for quit_event_handler + err = noErr; + dummy_event = 0; + + g_dummy_event_handler_UPP = NewEventHandlerUPP(dummy_event_handler); + g_quit_handler_UPP = NewEventHandlerUPP(quit_event_handler); + if((g_dummy_event_handler_UPP == 0) || (g_quit_handler_UPP == 0)) { + err = memFullErr; + } + + if(err == noErr) { + err = InstallApplicationEventHandler(g_dummy_event_handler_UPP, + 1, &event_spec, 0, &install_handler); + if(err == noErr) { + err = MacCreateEvent(NULL, 'KWIN', 'KWIN', + GetCurrentEventTime(), kEventAttributeNone, + &dummy_event); + if(err == noErr) { + err = PostEventToQueue(GetMainEventQueue(), + dummy_event, kEventPriorityHigh); + } + if(err == noErr) { + RunApplicationEventLoop(); + } + + (void)RemoveEventHandler(install_handler); + } + } + + if(dummy_event != NULL) { + ReleaseEvent(dummy_event); + } } @@ -535,312 +517,301 @@ macmain #else main #endif -(int argc, char* argv[]) -{ - ProcessSerialNumber my_psn; - - IBNibRef nibRef; - EventHandlerUPP handlerUPP; - EventTypeSpec cmd_event[3]; - GDHandle g_gdhandle ; - Rect win_rect; - OSStatus err; - char *argptr; - int slash_cnt; - int i; - + (int argc, char* argv[]) { + ProcessSerialNumber my_psn; + + IBNibRef nibRef; + EventHandlerUPP handlerUPP; + EventTypeSpec cmd_event[3]; + GDHandle g_gdhandle; + Rect win_rect; + OSStatus err; + char *argptr; + int slash_cnt; + int i; + #ifndef ACTIVEGS - /* Prepare argv0 */ - slash_cnt = 0; - argptr = argv[0]; - for(i = strlen(argptr); i >= 0; i--) { - if(argptr[i] == '/') { - slash_cnt++; - if(slash_cnt == 3) { - strncpy(&(g_argv0_path[0]), argptr, i); - g_argv0_path[i] = 0; - } - } - } - - printf("g_argv0_path is %s\n", g_argv0_path); - - g_mac_argv[0] = argv[0]; - g_mac_argc = 1; - i = 1; - while((i < argc) && (g_mac_argc < MAX_MAC_ARGS)) { - if(!strncmp(argv[i], "-psn", 4)) { - /* skip this argument */ - } else { - g_mac_argv[g_mac_argc++] = argv[i]; - } - i++; - } + /* Prepare argv0 */ + slash_cnt = 0; + argptr = argv[0]; + for(i = strlen(argptr); i >= 0; i--) { + if(argptr[i] == '/') { + slash_cnt++; + if(slash_cnt == 3) { + strncpy(&(g_argv0_path[0]), argptr, i); + g_argv0_path[i] = 0; + } + } + } + + printf("g_argv0_path is %s\n", g_argv0_path); + + g_mac_argv[0] = argv[0]; + g_mac_argc = 1; + i = 1; + while((i < argc) && (g_mac_argc < MAX_MAC_ARGS)) { + if(!strncmp(argv[i], "-psn", 4)) { + /* skip this argument */ + } else { + g_mac_argv[g_mac_argc++] = argv[i]; + } + i++; + } #endif - - InitCursor(); - g_event_rgnhandle = NewRgn(); - g_status_font_family = FMGetFontFamilyFromName("\pCourier"); - - SetRect(&win_rect, 0, 0, X_A2_WINDOW_WIDTH, X_A2_WINDOW_HEIGHT - // OG Remove status line from ActiveGS window + + InitCursor(); + g_event_rgnhandle = NewRgn(); + g_status_font_family = FMGetFontFamilyFromName("\pCourier"); + + SetRect(&win_rect, 0, 0, X_A2_WINDOW_WIDTH, X_A2_WINDOW_HEIGHT + // OG Remove status line from ActiveGS window #ifndef ACTIVEGS - + MAX_STATUS_LINES*16 + 8 + + MAX_STATUS_LINES*16 + 8 #endif - ); - OffsetRect(&win_rect, 64, 50); - - - // Create a Nib reference passing the name of the nib file - // CreateNibReference only searches into the application bundle. - err = CreateNibReference(CFSTR("main"), &nibRef); - require_noerr( err, CantGetNibRef ); - // Once the nib reference is created, set the menu bar. - err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar")); - require_noerr( err, CantSetMenuBar ); - - + ); + OffsetRect(&win_rect, 64, 50); + + + // Create a Nib reference passing the name of the nib file + // CreateNibReference only searches into the application bundle. + err = CreateNibReference(CFSTR("main"), &nibRef); + require_noerr( err, CantGetNibRef ); + // Once the nib reference is created, set the menu bar. + err = SetMenuBarFromNib(nibRef, CFSTR("MenuBar")); + require_noerr( err, CantSetMenuBar ); + + #ifndef ACTIVEGS - err = CreateNewWindow(kDocumentWindowClass, - kWindowStandardDocumentAttributes | - kWindowStandardHandlerAttribute, - &win_rect, &g_main_window); - - err = SetWindowTitleWithCFString(g_main_window, CFSTR("GSport")); + err = CreateNewWindow(kDocumentWindowClass, + kWindowStandardDocumentAttributes | + kWindowStandardHandlerAttribute, + &win_rect, &g_main_window); + + err = SetWindowTitleWithCFString(g_main_window, CFSTR("GSport")); #else - err = CreateNewWindow(kDocumentWindowClass, - (kWindowCloseBoxAttribute /*| kWindowFullZoomAttribute */| kWindowCollapseBoxAttribute /*| kWindowResizableAttribute*/) /*kWindowStandardDocumentAttributes*/ | - kWindowStandardHandlerAttribute, - &win_rect, &g_main_window); - extern CFStringRef activeGSversionSTR; - err = SetWindowTitleWithCFString(g_main_window, activeGSversionSTR); + err = CreateNewWindow(kDocumentWindowClass, + (kWindowCloseBoxAttribute /*| kWindowFullZoomAttribute */| kWindowCollapseBoxAttribute /*| kWindowResizableAttribute*/) /*kWindowStandardDocumentAttributes*/ | + kWindowStandardHandlerAttribute, + &win_rect, &g_main_window); + extern CFStringRef activeGSversionSTR; + err = SetWindowTitleWithCFString(g_main_window, activeGSversionSTR); #endif - - //printf("CreateNewWindow ret: %d, g_main_window: %p\n", (int)err, g_main_window); - - - // We don't need the nib reference anymore. - DisposeNibReference(nibRef); - - SysBeep(120); - - handlerUPP = NewEventHandlerUPP( my_cmd_handler ); - - cmd_event[0].eventClass = kEventClassCommand; - cmd_event[0].eventKind = kEventProcessCommand; - InstallWindowEventHandler(g_main_window, handlerUPP, 1, &cmd_event[0], - (void *)g_main_window, NULL); - - handlerUPP = NewEventHandlerUPP(my_win_handler); - cmd_event[0].eventClass = kEventClassWindow; - cmd_event[0].eventKind = kEventWindowDrawContent; - cmd_event[1].eventClass = kEventClassWindow; - cmd_event[1].eventKind = kEventWindowUpdate; - cmd_event[2].eventClass = kEventClassWindow; - cmd_event[2].eventKind = kEventWindowClose; - err = InstallWindowEventHandler(g_main_window, handlerUPP, 3, - &cmd_event[0], (void *)g_main_window, NULL); - require_noerr(err, CantCreateWindow); - // Get screen depth - g_gdhandle = GetGDevice(); - g_screen_mdepth = (**((**g_gdhandle).gdPMap)).pixelSize; - - g_screen_depth = g_screen_mdepth; + //printf("CreateNewWindow ret: %d, g_main_window: %p\n", (int)err, g_main_window); - if(g_screen_depth > 16) { - /* 32-bit display */ - g_red_mask = 0xff; - g_green_mask = 0xff; - g_blue_mask = 0xff; - - /* - if (macUsingCoreGraphics) - { - g_red_left_shift = 0; - g_green_left_shift = 8; - g_blue_left_shift = 16; - } - else - */ - { - g_red_left_shift = 16; - g_green_left_shift = 8; - g_blue_left_shift = 0; - - } - - g_red_right_shift = 0; - g_green_right_shift = 0; - g_blue_right_shift = 0; - } else if(g_screen_depth > 8) { - /* 16-bit display */ - g_red_mask = 0x1f; - g_green_mask = 0x1f; - g_blue_mask = 0x1f; - g_red_left_shift = 10; - g_green_left_shift = 5; - g_blue_left_shift = 0; - g_red_right_shift = 3; - g_green_right_shift = 3; - g_blue_right_shift = 3; - } - - // show_alert("About to show window", (int)g_main_window); - update_main_window_size(); - - update_window(); - - // The window was created hidden so show it. - ShowWindow( g_main_window ); - BringToFront( g_main_window ); - - update_window(); - - // Make us pop to the front a different way - err = GetCurrentProcess(&my_psn); - if(err == noErr) { - (void)SetFrontProcess(&my_psn); - } + // We don't need the nib reference anymore. + DisposeNibReference(nibRef); + + SysBeep(120); + + handlerUPP = NewEventHandlerUPP( my_cmd_handler ); + + cmd_event[0].eventClass = kEventClassCommand; + cmd_event[0].eventKind = kEventProcessCommand; + InstallWindowEventHandler(g_main_window, handlerUPP, 1, &cmd_event[0], + (void *)g_main_window, NULL); + + handlerUPP = NewEventHandlerUPP(my_win_handler); + cmd_event[0].eventClass = kEventClassWindow; + cmd_event[0].eventKind = kEventWindowDrawContent; + cmd_event[1].eventClass = kEventClassWindow; + cmd_event[1].eventKind = kEventWindowUpdate; + cmd_event[2].eventClass = kEventClassWindow; + cmd_event[2].eventKind = kEventWindowClose; + err = InstallWindowEventHandler(g_main_window, handlerUPP, 3, + &cmd_event[0], (void *)g_main_window, NULL); + require_noerr(err, CantCreateWindow); + + // Get screen depth + g_gdhandle = GetGDevice(); + g_screen_mdepth = (**((**g_gdhandle).gdPMap)).pixelSize; + + g_screen_depth = g_screen_mdepth; + + if(g_screen_depth > 16) { + /* 32-bit display */ + g_red_mask = 0xff; + g_green_mask = 0xff; + g_blue_mask = 0xff; + + /* + if (macUsingCoreGraphics) + { + g_red_left_shift = 0; + g_green_left_shift = 8; + g_blue_left_shift = 16; + } + else + */ + { + g_red_left_shift = 16; + g_green_left_shift = 8; + g_blue_left_shift = 0; + + } + + g_red_right_shift = 0; + g_green_right_shift = 0; + g_blue_right_shift = 0; + } else if(g_screen_depth > 8) { + /* 16-bit display */ + g_red_mask = 0x1f; + g_green_mask = 0x1f; + g_blue_mask = 0x1f; + g_red_left_shift = 10; + g_green_left_shift = 5; + g_blue_left_shift = 0; + g_red_right_shift = 3; + g_green_right_shift = 3; + g_blue_right_shift = 3; + } + + // show_alert("About to show window", (int)g_main_window); + update_main_window_size(); + + update_window(); + + + // The window was created hidden so show it. + ShowWindow( g_main_window ); + BringToFront( g_main_window ); + + update_window(); + + // Make us pop to the front a different way + err = GetCurrentProcess(&my_psn); + if(err == noErr) { + (void)SetFrontProcess(&my_psn); + } + + // Call the event loop + temp_run_application_event_loop(); - // Call the event loop - temp_run_application_event_loop(); - CantCreateWindow: CantSetMenuBar: CantGetNibRef: - show_simple_alert("ending", "", "error code", err); - return err; + show_simple_alert("ending", "", "error code", err); + return err; } -void -xdriver_end() -{ - - printf("xdriver_end\n"); - - if(g_fatal_log >= 0) { - x_show_alert(1, 0); - } +void xdriver_end() { + + printf("xdriver_end\n"); + + if(g_fatal_log >= 0) { + x_show_alert(1, 0); + } } -void -x_redraw_status_lines() -{ - // OG Disable status line +void x_redraw_status_lines() { + // OG Disable status line #ifndef ACTIVEGS - Rect rect; - Pattern white_pattern; - char tmp_buf[256]; - char *buf; - int len; - int line; - int height; - int margin; - - SetPortWindowPort(g_main_window); - PenNormal(); - height = 16; - margin = 0; - TextFont(g_status_font_family); - TextFace(normal); - TextSize(12); - - SetRect(&rect, 0, X_A2_WINDOW_HEIGHT + margin, X_A2_WINDOW_WIDTH, - X_A2_WINDOW_HEIGHT + margin + MAX_STATUS_LINES*height); - GetQDGlobalsWhite(&white_pattern); - FillRect(&rect, &white_pattern); - - for(line = 0; line < MAX_STATUS_LINES; line++) { - buf = g_status_ptrs[line]; - if(buf == 0) { - /* skip it */ - continue; - } - MoveTo(10, X_A2_WINDOW_HEIGHT + height*line + margin + height); - len = MIN(250, strlen(buf)); - strncpy(&tmp_buf[1], buf, len); - tmp_buf[0] = len; - DrawString((const unsigned char*)&tmp_buf[0]); - } + Rect rect; + Pattern white_pattern; + char tmp_buf[256]; + char *buf; + int len; + int line; + int height; + int margin; + + SetPortWindowPort(g_main_window); + PenNormal(); + height = 16; + margin = 0; + TextFont(g_status_font_family); + TextFace(normal); + TextSize(12); + + SetRect(&rect, 0, X_A2_WINDOW_HEIGHT + margin, X_A2_WINDOW_WIDTH, + X_A2_WINDOW_HEIGHT + margin + MAX_STATUS_LINES*height); + GetQDGlobalsWhite(&white_pattern); + FillRect(&rect, &white_pattern); + + for(line = 0; line < MAX_STATUS_LINES; line++) { + buf = g_status_ptrs[line]; + if(buf == 0) { + /* skip it */ + continue; + } + MoveTo(10, X_A2_WINDOW_HEIGHT + height*line + margin + height); + len = MIN(250, strlen(buf)); + strncpy(&tmp_buf[1], buf, len); + tmp_buf[0] = len; + DrawString((const unsigned char*)&tmp_buf[0]); + } #endif } -void -x_full_screen(int do_full) -{ - +void x_full_screen(int do_full) { + #if 0 - WindowRef new_window; - short width, height; - OSErr ret; - - width = 640; - height = 480; - if(do_full && (g_mac_fullscreen_state == 0)) { - g_main_window_saved = g_main_window; - - GetWindowBounds(g_main_window, kWindowContentRgn, - &g_main_window_saved_rect); - ret = BeginFullScreen(&g_mac_fullscreen_state, 0, - &width, &height, &new_window, 0, 0); - printf("Ret beginfullscreen: %d\n", (int)ret); - printf("New width: %d, new height: %d\n", width, height); - if(ret == noErr) { - g_main_window = new_window; - } else { - g_mac_fullscreen_state = 0; - } - } else if(!do_full && (g_mac_fullscreen_state != 0)) { - ret = EndFullScreen(g_mac_fullscreen_state, 0); - printf("ret endfullscreen: %d\n", (int)ret); - g_main_window = g_main_window_saved; - g_mac_fullscreen_state = 0; - //InitCursor(); - SetWindowBounds(g_main_window, kWindowContentRgn, - &g_main_window_saved_rect); - } - - update_main_window_size(); - - ShowWindow(g_main_window); - BringToFront(g_main_window); - update_window(); + WindowRef new_window; + short width, height; + OSErr ret; + + width = 640; + height = 480; + if(do_full && (g_mac_fullscreen_state == 0)) { + g_main_window_saved = g_main_window; + + GetWindowBounds(g_main_window, kWindowContentRgn, + &g_main_window_saved_rect); + ret = BeginFullScreen(&g_mac_fullscreen_state, 0, + &width, &height, &new_window, 0, 0); + printf("Ret beginfullscreen: %d\n", (int)ret); + printf("New width: %d, new height: %d\n", width, height); + if(ret == noErr) { + g_main_window = new_window; + } else { + g_mac_fullscreen_state = 0; + } + } else if(!do_full && (g_mac_fullscreen_state != 0)) { + ret = EndFullScreen(g_mac_fullscreen_state, 0); + printf("ret endfullscreen: %d\n", (int)ret); + g_main_window = g_main_window_saved; + g_mac_fullscreen_state = 0; + //InitCursor(); + SetWindowBounds(g_main_window, kWindowContentRgn, + &g_main_window_saved_rect); + } + + update_main_window_size(); + + ShowWindow(g_main_window); + BringToFront(g_main_window); + update_window(); #endif } -void -x_push_done() -{ +void x_push_done() { + + CGrafPtr window_port; + + SetPortWindowPort(g_main_window); + window_port = GetWindowPort(g_main_window); + + QDFlushPortBuffer(window_port, 0); - CGrafPtr window_port; - - SetPortWindowPort(g_main_window); - window_port = GetWindowPort(g_main_window); - - QDFlushPortBuffer(window_port, 0); - } -void -mac_warp_mouse() -{ +void mac_warp_mouse() { #ifndef ACTIVEGS - Rect port_rect; - Point win_origin_pt; - CGPoint cgpoint; - CGDisplayErr cg_err; - - GetPortBounds(GetWindowPort(g_main_window), &port_rect); - SetPt(&win_origin_pt, port_rect.left, port_rect.top); - LocalToGlobal(&win_origin_pt); - - cgpoint = CGPointMake( (float)(win_origin_pt.h + X_A2_WINDOW_WIDTH/2), - (float)(win_origin_pt.v + X_A2_WINDOW_HEIGHT/2)); - cg_err = CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint); + Rect port_rect; + Point win_origin_pt; + CGPoint cgpoint; + CGDisplayErr cg_err; + + GetPortBounds(GetWindowPort(g_main_window), &port_rect); + SetPt(&win_origin_pt, port_rect.left, port_rect.top); + LocalToGlobal(&win_origin_pt); + + cgpoint = CGPointMake( (float)(win_origin_pt.h + X_A2_WINDOW_WIDTH/2), + (float)(win_origin_pt.v + X_A2_WINDOW_HEIGHT/2)); + cg_err = CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint); #endif } diff --git a/src/macdriver_generic.c b/src/macdriver_generic.c index c968110..0902358 100644 --- a/src/macdriver_generic.c +++ b/src/macdriver_generic.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #ifdef ACTIVEIPHONE #include @@ -21,30 +21,30 @@ #include "protos_macdriver.h" -word32 g_mac_shift_control_state = 0; +word32 g_mac_shift_control_state = 0; int macUsingCoreGraphics=0; // Coregraphics context -CGContextRef offscreenContext = NULL; +CGContextRef offscreenContext = NULL; char * bitmapData=NULL; -int bitmapByteCount; -int bitmapBytesPerRow; +int bitmapByteCount; +int bitmapBytesPerRow; #ifdef ENABLEQD -WindowRef g_main_window; +WindowRef g_main_window; CGrafPtr mac_window_port; #endif char *g_clipboard = 0x00; -int g_clipboard_pos; +int g_clipboard_pos; extern Kimage g_mainwin_kimage; -int g_use_shmem = 0; +int g_use_shmem = 0; extern int Verbose; @@ -67,8 +67,8 @@ extern int g_send_sound_to_file; extern int g_config_control_panel; -int g_auto_repeat_on = -1; -int g_x_shift_control_state = 0; +int g_auto_repeat_on = -1; +int g_x_shift_control_state = 0; extern int Max_color_size; @@ -76,8 +76,8 @@ extern int Max_color_size; extern word32 g_palette_8to1624[256]; extern word32 g_a2palette_8to1624[256]; -int g_alt_left_up = 1; -int g_alt_right_up = 1; +int g_alt_left_up = 1; +int g_alt_right_up = 1; extern word32 g_full_refresh_needed; @@ -100,442 +100,413 @@ int g_upd_count = 0; -void -update_window(void) -{ - - // OG Not needed - /* - SetPortWindowPort(g_main_window); - PenNormal(); - */ - - g_full_refresh_needed = -1; - g_a2_screen_buffer_changed = -1; - g_status_refresh_needed = 1; - g_border_sides_refresh_needed = 1; - g_border_special_refresh_needed = 1; +void update_window(void) { - g_upd_count++; - if(g_upd_count > 250) { - g_upd_count = 0; - } + // OG Not needed + /* + SetPortWindowPort(g_main_window); + PenNormal(); + */ + + g_full_refresh_needed = -1; + g_a2_screen_buffer_changed = -1; + g_status_refresh_needed = 1; + g_border_sides_refresh_needed = 1; + g_border_special_refresh_needed = 1; + + g_upd_count++; + if(g_upd_count > 250) { + g_upd_count = 0; + } } -void -mac_update_modifiers(word32 state) -{ +void mac_update_modifiers(word32 state) { #ifndef ACTIVEIPHONE - word32 state_xor; - int is_up; + word32 state_xor; + int is_up; - state = state & ( - cmdKey | controlKey | - shiftKey | alphaLock | optionKey - ); - state_xor = g_mac_shift_control_state ^ state; - is_up = 0; - if(state_xor & controlKey) { - is_up = ((state & controlKey) == 0); - adb_physical_key_update(0x36, is_up); - } - if(state_xor & alphaLock) { - is_up = ((state & alphaLock) == 0); - adb_physical_key_update(0x39, is_up); - } - if(state_xor & shiftKey) { - is_up = ((state & shiftKey) == 0); - adb_physical_key_update(0x38, is_up); - } - if(state_xor & cmdKey) { - is_up = ((state & cmdKey) == 0); - adb_physical_key_update(0x37, is_up); - } - if(state_xor & optionKey) { - is_up = ((state & optionKey) == 0); - adb_physical_key_update(0x3a, is_up); - } + state = state & ( + cmdKey | controlKey | + shiftKey | alphaLock | optionKey + ); + state_xor = g_mac_shift_control_state ^ state; + is_up = 0; + if(state_xor & controlKey) { + is_up = ((state & controlKey) == 0); + adb_physical_key_update(0x36, is_up); + } + if(state_xor & alphaLock) { + is_up = ((state & alphaLock) == 0); + adb_physical_key_update(0x39, is_up); + } + if(state_xor & shiftKey) { + is_up = ((state & shiftKey) == 0); + adb_physical_key_update(0x38, is_up); + } + if(state_xor & cmdKey) { + is_up = ((state & cmdKey) == 0); + adb_physical_key_update(0x37, is_up); + } + if(state_xor & optionKey) { + is_up = ((state & optionKey) == 0); + adb_physical_key_update(0x3a, is_up); + } #endif - g_mac_shift_control_state = state; + g_mac_shift_control_state = state; } -void -x_update_color(int col_num, int red, int green, int blue, word32 rgb) -{ +void x_update_color(int col_num, int red, int green, int blue, word32 rgb) { } -void -x_update_physical_colormap() -{ +void x_update_physical_colormap() { } -void -show_xcolor_array() -{ - int i; +void show_xcolor_array() { + int i; - for(i = 0; i < 256; i++) { - printf("%02x: %08x\n", i, g_palette_8to1624[i]); - } + for(i = 0; i < 256; i++) { + printf("%02x: %08x\n", i, g_palette_8to1624[i]); + } } -void -x_get_kimage(Kimage *kimage_ptr) -{ +void x_get_kimage(Kimage *kimage_ptr) { #ifdef ENABLEQD - PixMapHandle pixmap_handle; - GWorldPtr world; - Rect world_rect; - OSStatus err; + PixMapHandle pixmap_handle; + GWorldPtr world; + Rect world_rect; + OSStatus err; #endif - word32 *wptr; - byte *ptr; - int row_bytes; - int width; - int height; - int depth, mdepth; - int size; + word32 *wptr; + byte *ptr; + int row_bytes; + int width; + int height; + int depth, mdepth; + int size; - width = kimage_ptr->width_req; - height = kimage_ptr->height; - depth = kimage_ptr->depth; - mdepth = kimage_ptr->mdepth; + width = kimage_ptr->width_req; + height = kimage_ptr->height; + depth = kimage_ptr->depth; + mdepth = kimage_ptr->mdepth; - size = 0; - if(depth == g_screen_depth) - { + size = 0; + if(depth == g_screen_depth) + { - if (!macUsingCoreGraphics) - - { + if (!macUsingCoreGraphics) + + { #ifdef ENABLEQD - SetRect(&world_rect, 0, 0, width, height); - err = NewGWorld( &world, 0, &world_rect, NULL, NULL, 0); - pixmap_handle = GetGWorldPixMap(world); - err = LockPixels(pixmap_handle); - ptr = (byte *)GetPixBaseAddr(pixmap_handle); - row_bytes = ((*pixmap_handle)->rowBytes & 0x3fff); - kimage_ptr->width_act = row_bytes / (mdepth >> 3); - mac_printf("Got depth: %d, bitmap_ptr: %p, width: %d\n", depth, ptr, kimage_ptr->width_act); - mac_printf("pixmap->base: %08x, rowbytes: %08x, pixType:%08x\n",(int)(*pixmap_handle)->baseAddr,(*pixmap_handle)->rowBytes,(*pixmap_handle)->pixelType); - wptr = (word32 *)(*pixmap_handle); - mac_printf("wptr: %p=%08x %08x %08x %08x %08x %08x %08x %08x\n",wptr,wptr[0], wptr[1], wptr[2], wptr[3],wptr[4], wptr[5], wptr[6], wptr[7]); - kimage_ptr->dev_handle = pixmap_handle; - kimage_ptr->data_ptr = ptr; + SetRect(&world_rect, 0, 0, width, height); + err = NewGWorld( &world, 0, &world_rect, NULL, NULL, 0); + pixmap_handle = GetGWorldPixMap(world); + err = LockPixels(pixmap_handle); + ptr = (byte *)GetPixBaseAddr(pixmap_handle); + row_bytes = ((*pixmap_handle)->rowBytes & 0x3fff); + kimage_ptr->width_act = row_bytes / (mdepth >> 3); + mac_printf("Got depth: %d, bitmap_ptr: %p, width: %d\n", depth, ptr, kimage_ptr->width_act); + mac_printf("pixmap->base: %08x, rowbytes: %08x, pixType:%08x\n",(int)(*pixmap_handle)->baseAddr,(*pixmap_handle)->rowBytes,(*pixmap_handle)->pixelType); + wptr = (word32 *)(*pixmap_handle); + mac_printf("wptr: %p=%08x %08x %08x %08x %08x %08x %08x %08x\n",wptr,wptr[0], wptr[1], wptr[2], wptr[3],wptr[4], wptr[5], wptr[6], wptr[7]); + kimage_ptr->dev_handle = pixmap_handle; + kimage_ptr->data_ptr = ptr; #endif - } - else - { - - - kimage_ptr->width_act = width ; - size = height* kimage_ptr->width_act * mdepth >> 3; - ptr = (byte *)malloc(size); - - if(ptr == 0) { - mac_printf("malloc for data fail, mdepth:%d\n", mdepth); - exit(2); - } - - kimage_ptr->data_ptr = ptr; - kimage_ptr->dev_handle = (void *)-1; - } - } - else { + } + else + { - /* allocate buffers for video.c to draw into */ - - kimage_ptr->width_act = width ; - size = height* kimage_ptr->width_act * mdepth >> 3 ; - ptr = (byte *)malloc(size); + kimage_ptr->width_act = width; + size = height* kimage_ptr->width_act * mdepth >> 3; + ptr = (byte *)malloc(size); - if(ptr == 0) { - mac_printf("malloc for data fail, mdepth:%d\n", mdepth); - exit(2); - } + if(ptr == 0) { + mac_printf("malloc for data fail, mdepth:%d\n", mdepth); + exit(2); + } - kimage_ptr->data_ptr = ptr; - kimage_ptr->dev_handle = (void *)-1; - } + kimage_ptr->data_ptr = ptr; + kimage_ptr->dev_handle = (void *)-1; + } + } + else { - mac_printf("kim: %p, dev:%p data: %p, size: %08x\n", kimage_ptr, - kimage_ptr->dev_handle, kimage_ptr->data_ptr, size); + /* allocate buffers for video.c to draw into */ + + + kimage_ptr->width_act = width; + size = height* kimage_ptr->width_act * mdepth >> 3; + ptr = (byte *)malloc(size); + + if(ptr == 0) { + mac_printf("malloc for data fail, mdepth:%d\n", mdepth); + exit(2); + } + + kimage_ptr->data_ptr = ptr; + kimage_ptr->dev_handle = (void *)-1; + } + + mac_printf("kim: %p, dev:%p data: %p, size: %08x\n", kimage_ptr, + kimage_ptr->dev_handle, kimage_ptr->data_ptr, size); } #ifdef ENABLEQD -PixMapHandle pixmap_backbuffer=NULL; -GWorldPtr backbuffer=NULL; +PixMapHandle pixmap_backbuffer=NULL; +GWorldPtr backbuffer=NULL; #endif -void -dev_video_init() -{ - int lores_col; - int i; +void dev_video_init() { + int lores_col; + int i; - printf("Preparing graphics system\n"); - - // OG Create backbuffer - if (!macUsingCoreGraphics) - { + printf("Preparing graphics system\n"); + + // OG Create backbuffer + if (!macUsingCoreGraphics) + { #ifdef ENABLEQD - Rect r; - SetRect(&r, 0, 0, 704, 462); - QDErr err = NewGWorld( &backbuffer, 0, &r, NULL, NULL, 0); - pixmap_backbuffer = GetGWorldPixMap(backbuffer); + Rect r; + SetRect(&r, 0, 0, 704, 462); + QDErr err = NewGWorld( &backbuffer, 0, &r, NULL, NULL, 0); + pixmap_backbuffer = GetGWorldPixMap(backbuffer); #endif - } - else - { - - int pixelsWide = 704; - int pixelsHigh = 462; - bitmapBytesPerRow = (pixelsWide * 4);// 1 - bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); - - CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); - bitmapData = (char*)malloc( bitmapByteCount );// 3 - if (bitmapData == NULL) - { - fprintf (stderr, "Memory not allocated!"); - return ; - } - offscreenContext = CGBitmapContextCreate (bitmapData,// 4 - pixelsWide, - pixelsHigh, - 8, // bits per component - bitmapBytesPerRow, - colorSpace, - kCGImageAlphaNoneSkipLast); - if (offscreenContext== NULL) - { - free (bitmapData);// 5 - fprintf (stderr, "Context not created!"); - return ; - } - - CGContextSetRGBFillColor (offscreenContext, 1, 0.5, 0.5, 1); - CGContextFillRect (offscreenContext, CGRectMake (0,0, 704, 462 )); - - CGColorSpaceRelease( colorSpace );// 6 - } + } + else + { - - video_get_kimages(); + int pixelsWide = 704; + int pixelsHigh = 462; + bitmapBytesPerRow = (pixelsWide * 4); // 1 + bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); - if(g_screen_depth != 8) { - // Get g_mainwin_kimage - video_get_kimage(&g_mainwin_kimage, 0, g_screen_depth, - g_screen_mdepth); - } + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + bitmapData = (char*)malloc( bitmapByteCount ); // 3 + if (bitmapData == NULL) + { + fprintf (stderr, "Memory not allocated!"); + return; + } + offscreenContext = CGBitmapContextCreate (bitmapData, // 4 + pixelsWide, + pixelsHigh, + 8, // bits per component + bitmapBytesPerRow, + colorSpace, + kCGImageAlphaNoneSkipLast); + if (offscreenContext== NULL) + { + free (bitmapData); // 5 + fprintf (stderr, "Context not created!"); + return; + } - for(i = 0; i < 256; i++) { - lores_col = g_lores_colors[i & 0xf]; - video_update_color_raw(i, lores_col); - g_a2palette_8to1624[i] = g_palette_8to1624[i]; - } + CGContextSetRGBFillColor (offscreenContext, 1, 0.5, 0.5, 1); + CGContextFillRect (offscreenContext, CGRectMake (0,0, 704, 462 )); - g_installed_full_superhires_colormap = 1; + CGColorSpaceRelease( colorSpace ); // 6 + } - fflush(stdout); + + video_get_kimages(); + + if(g_screen_depth != 8) { + // Get g_mainwin_kimage + video_get_kimage(&g_mainwin_kimage, 0, g_screen_depth, + g_screen_mdepth); + } + + for(i = 0; i < 256; i++) { + lores_col = g_lores_colors[i & 0xf]; + video_update_color_raw(i, lores_col); + g_a2palette_8to1624[i] = g_palette_8to1624[i]; + } + + g_installed_full_superhires_colormap = 1; + + fflush(stdout); } -void -x_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, - int width, int height) -{ - int i; +void x_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, + int width, int height) { + int i; - if (!macUsingCoreGraphics) - { + if (!macUsingCoreGraphics) + { #ifdef ENABLEQD - PixMapHandle pixmap_handle; - Rect src_rect, dest_rect; - CGrafPtr window_port; + PixMapHandle pixmap_handle; + Rect src_rect, dest_rect; + CGrafPtr window_port; - pixmap_handle = (PixMapHandle)kimage_ptr->dev_handle; - SetRect(&src_rect, srcx, srcy, srcx + width, srcy + height); - SetRect(&dest_rect, destx, desty, destx + width, desty + height); + pixmap_handle = (PixMapHandle)kimage_ptr->dev_handle; + SetRect(&src_rect, srcx, srcy, srcx + width, srcy + height); + SetRect(&dest_rect, destx, desty, destx + width, desty + height); -#ifndef ACTIVEGSPLUGIN - SetPortWindowPort(g_main_window); - window_port = GetWindowPort(g_main_window); +#ifndef ACTIVEGSPLUGIN + SetPortWindowPort(g_main_window); + window_port = GetWindowPort(g_main_window); - CopyBits( (BitMap *)(*pixmap_handle), - GetPortBitMapForCopyBits(window_port), &src_rect, &dest_rect, - srcCopy, NULL); + CopyBits( (BitMap *)(*pixmap_handle), + GetPortBitMapForCopyBits(window_port), &src_rect, &dest_rect, + srcCopy, NULL); #else // !ACTIVEGSPLUGIN - // OG Write to the back buffer instead of the display window - window_port = mac_window_port ; - SetGWorld(backbuffer,NULL); + // OG Write to the back buffer instead of the display window + window_port = mac_window_port; + SetGWorld(backbuffer,NULL); - CopyBits( (BitMap *)(*pixmap_handle), - GetPortBitMapForCopyBits(backbuffer), &src_rect, &dest_rect, - srcCopy, NULL); + CopyBits( (BitMap *)(*pixmap_handle), + GetPortBitMapForCopyBits(backbuffer), &src_rect, &dest_rect, + srcCopy, NULL); #endif // ACTIVEGSPLUGIN #endif - } - else - { - - int wd = kimage_ptr->width_act * kimage_ptr->mdepth>>3; - int w = width *4; - char* ptrdest = bitmapData + bitmapBytesPerRow*desty + destx*4; - char* srcdest = (char*)kimage_ptr->data_ptr + wd*srcy + srcx*4; - for(i=0;iwidth_act * kimage_ptr->mdepth>>3; + int w = width *4; + char* ptrdest = bitmapData + bitmapBytesPerRow*desty + destx*4; + char* srcdest = (char*)kimage_ptr->data_ptr + wd*srcy + srcx*4; + for(i=0; idev_handle == (void*)-1) - { - free(kimage_ptr->data_ptr); - kimage_ptr->data_ptr = NULL; - } - else - { - if (!macUsingCoreGraphics) - { +void x_release_kimage(Kimage* kimage_ptr) { + if (kimage_ptr->dev_handle == (void*)-1) + { + free(kimage_ptr->data_ptr); + kimage_ptr->data_ptr = NULL; + } + else + { + if (!macUsingCoreGraphics) + { #ifdef ENABLEQD - UnlockPixels((PixMapHandle)kimage_ptr->dev_handle); - kimage_ptr->dev_handle = NULL; - DisposeGWorld((GWorldPtr)kimage_ptr->dev_handle2); - kimage_ptr->dev_handle2 = NULL; + UnlockPixels((PixMapHandle)kimage_ptr->dev_handle); + kimage_ptr->dev_handle = NULL; + DisposeGWorld((GWorldPtr)kimage_ptr->dev_handle2); + kimage_ptr->dev_handle2 = NULL; #endif - } - } + } + } } // OG Addding ratio -int x_calc_ratio(float x,float y) -{ - return 1; +int x_calc_ratio(float x,float y) { + return 1; } -void -clipboard_paste(void) -{ +void clipboard_paste(void) { #define CHUNK_SIZE 1024 - char buffer[CHUNK_SIZE]; - int bufsize = 1; - void *expanding_buffer = 0x00; - if (g_clipboard) - { - g_clipboard_pos = 0; - free(g_clipboard); - g_clipboard = 0x00; - } - FILE *pipe = popen("pbpaste", "r"); - if (pipe) - { - expanding_buffer = calloc(CHUNK_SIZE+1,1); - bufsize = CHUNK_SIZE; - while (!feof(pipe)) - { - if (fgets(buffer, CHUNK_SIZE, pipe) != NULL) - { - while (strlen((char*)expanding_buffer) + strlen(buffer) > bufsize) - { - bufsize += CHUNK_SIZE + 1; - expanding_buffer = realloc(expanding_buffer, bufsize); - } - /* Skip the leading return character when this is the first line in the paste buffer */ - if (strlen((char*)expanding_buffer) > 0) - strcat((char*)expanding_buffer,"\r"); - strncat((char*)expanding_buffer,buffer,strlen(buffer)); - g_clipboard = (char*)expanding_buffer; - } - } - } + char buffer[CHUNK_SIZE]; + int bufsize = 1; + void *expanding_buffer = 0x00; + if (g_clipboard) + { + g_clipboard_pos = 0; + free(g_clipboard); + g_clipboard = 0x00; + } + FILE *pipe = popen("pbpaste", "r"); + if (pipe) + { + expanding_buffer = calloc(CHUNK_SIZE+1,1); + bufsize = CHUNK_SIZE; + while (!feof(pipe)) + { + if (fgets(buffer, CHUNK_SIZE, pipe) != NULL) + { + while (strlen((char*)expanding_buffer) + strlen(buffer) > bufsize) + { + bufsize += CHUNK_SIZE + 1; + expanding_buffer = realloc(expanding_buffer, bufsize); + } + /* Skip the leading return character when this is the first line in the paste buffer */ + if (strlen((char*)expanding_buffer) > 0) + strcat((char*)expanding_buffer,"\r"); + strncat((char*)expanding_buffer,buffer,strlen(buffer)); + g_clipboard = (char*)expanding_buffer; + } + } + } } -int clipboard_get_char(void) -{ - if (!g_clipboard) - return 0; - if (g_clipboard[g_clipboard_pos] == '\n') - g_clipboard_pos++; - if (g_clipboard[g_clipboard_pos] == '\0') - return 0; - return g_clipboard[g_clipboard_pos++] | 0x80; +int clipboard_get_char(void) { + if (!g_clipboard) + return 0; + if (g_clipboard[g_clipboard_pos] == '\n') + g_clipboard_pos++; + if (g_clipboard[g_clipboard_pos] == '\0') + return 0; + return g_clipboard[g_clipboard_pos++] | 0x80; } diff --git a/src/macsnd_driver.c b/src/macsnd_driver.c index 8c0dc67..f273d8c 100644 --- a/src/macsnd_driver.c +++ b/src/macsnd_driver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" @@ -19,11 +19,11 @@ int mac_send_audio(byte *ptr, int in_size) {} #include "sound.h" #include -#define MACSND_REBUF_SIZE (64*1024) -#define MACSND_QUANTA 512 +#define MACSND_REBUF_SIZE (64*1024) +#define MACSND_QUANTA 512 /* MACSND_QUANTA must be >= 128 and a power of 2 */ -word32 g_macsnd_rebuf[MACSND_REBUF_SIZE]; +word32 g_macsnd_rebuf[MACSND_REBUF_SIZE]; volatile word32 *g_macsnd_rebuf_ptr; volatile word32 *g_macsnd_rebuf_cur; volatile int g_macsnd_playing = 0; @@ -35,124 +35,116 @@ extern word32 *g_sound_shm_addr; extern int g_sound_size; -SndChannelPtr g_snd_channel_ptr; -ExtSoundHeader g_snd_hdr; -SndCommand g_snd_cmd; +SndChannelPtr g_snd_channel_ptr; +ExtSoundHeader g_snd_hdr; +SndCommand g_snd_cmd; -void -mac_snd_callback(SndChannelPtr snd_chan_ptr, SndCommand *in_sndcmd) -{ - OSStatus err; - int samps; +void mac_snd_callback(SndChannelPtr snd_chan_ptr, SndCommand *in_sndcmd) { + OSStatus err; + int samps; - // This is an interrupt routine--no printf, etc! + // This is an interrupt routine--no printf, etc! - samps = g_macsnd_rebuf_ptr - g_macsnd_rebuf_cur; - if(samps < 0) { - samps += MACSND_REBUF_SIZE; - } + samps = g_macsnd_rebuf_ptr - g_macsnd_rebuf_cur; + if(samps < 0) { + samps += MACSND_REBUF_SIZE; + } - samps = samps & -(MACSND_QUANTA); // quantize to 1024 samples - if(g_macsnd_rebuf_cur + samps > &(g_macsnd_rebuf[MACSND_REBUF_SIZE])) { - samps = &(g_macsnd_rebuf[MACSND_REBUF_SIZE]) - - g_macsnd_rebuf_cur; - } - if(samps > 0) { - g_macsnd_playing = 1; - g_snd_hdr.numFrames = samps; - g_snd_hdr.loopEnd = samps; - g_snd_hdr.samplePtr = (char *)g_macsnd_rebuf_cur; // OG Cast from byte* to ,char* + samps = samps & -(MACSND_QUANTA); // quantize to 1024 samples + if(g_macsnd_rebuf_cur + samps > &(g_macsnd_rebuf[MACSND_REBUF_SIZE])) { + samps = &(g_macsnd_rebuf[MACSND_REBUF_SIZE]) - + g_macsnd_rebuf_cur; + } + if(samps > 0) { + g_macsnd_playing = 1; + g_snd_hdr.numFrames = samps; + g_snd_hdr.loopEnd = samps; + g_snd_hdr.samplePtr = (char *)g_macsnd_rebuf_cur; // OG Cast from byte* to ,char* - g_snd_cmd.cmd = bufferCmd; - g_snd_cmd.param1 = 0; - g_snd_cmd.param2 = (long) &g_snd_hdr; + g_snd_cmd.cmd = bufferCmd; + g_snd_cmd.param1 = 0; + g_snd_cmd.param2 = (long) &g_snd_hdr; - g_macsnd_rebuf_cur += samps; - if(g_macsnd_rebuf_cur >= &(g_macsnd_rebuf[MACSND_REBUF_SIZE])) { - g_macsnd_rebuf_cur -= MACSND_REBUF_SIZE; - } + g_macsnd_rebuf_cur += samps; + if(g_macsnd_rebuf_cur >= &(g_macsnd_rebuf[MACSND_REBUF_SIZE])) { + g_macsnd_rebuf_cur -= MACSND_REBUF_SIZE; + } - err = SndDoImmediate(g_snd_channel_ptr, &g_snd_cmd); + err = SndDoImmediate(g_snd_channel_ptr, &g_snd_cmd); - // And set-up callback - g_snd_cmd.cmd = callBackCmd; - g_snd_cmd.param1 = 0; - g_snd_cmd.param2 = 0; - err = SndDoCommand(g_snd_channel_ptr, &g_snd_cmd, TRUE); - } else { - g_macsnd_playing = 0; - } + // And set-up callback + g_snd_cmd.cmd = callBackCmd; + g_snd_cmd.param1 = 0; + g_snd_cmd.param2 = 0; + err = SndDoCommand(g_snd_channel_ptr, &g_snd_cmd, TRUE); + } else { + g_macsnd_playing = 0; + } } -int -mac_send_audio(byte *ptr, int in_size) -{ - SndCommand snd_cmd = {0}; - word32 *wptr, *macptr; - word32 *eptr; - int samps; - int i; +int mac_send_audio(byte *ptr, int in_size) { + SndCommand snd_cmd = {0}; + word32 *wptr, *macptr; + word32 *eptr; + int samps; + int i; - samps = in_size / 4; - wptr = (word32 *)ptr; - macptr = (word32 *)g_macsnd_rebuf_ptr; - eptr = &g_macsnd_rebuf[MACSND_REBUF_SIZE]; - for(i = 0; i < samps; i++) { - *macptr++ = *wptr++; - if(macptr >= eptr) { - macptr = &g_macsnd_rebuf[0]; - } - } + samps = in_size / 4; + wptr = (word32 *)ptr; + macptr = (word32 *)g_macsnd_rebuf_ptr; + eptr = &g_macsnd_rebuf[MACSND_REBUF_SIZE]; + for(i = 0; i < samps; i++) { + *macptr++ = *wptr++; + if(macptr >= eptr) { + macptr = &g_macsnd_rebuf[0]; + } + } - g_macsnd_rebuf_ptr = macptr; + g_macsnd_rebuf_ptr = macptr; - if(!g_macsnd_playing) { - mac_snd_callback(g_snd_channel_ptr, &snd_cmd); - } + if(!g_macsnd_playing) { + mac_snd_callback(g_snd_channel_ptr, &snd_cmd); + } - return in_size; + return in_size; } -void -child_sound_init_mac() -{ - OSStatus err; +void child_sound_init_mac() { + OSStatus err; - mac_printf("In mac child\n"); - fflush(stdout); - mac_printf("pid: %d\n", getpid()); - fflush(stdout); + mac_printf("In mac child\n"); + fflush(stdout); + mac_printf("pid: %d\n", getpid()); + fflush(stdout); - //return; + //return; - //g_snd_channel_ptr = 0; - err = SndNewChannel(&g_snd_channel_ptr, sampledSynth, initStereo, - NewSndCallBackUPP(mac_snd_callback)); - mac_printf("SndNewChannel ret: %d\n", (int)err); - fflush(stdout); + //g_snd_channel_ptr = 0; + err = SndNewChannel(&g_snd_channel_ptr, sampledSynth, initStereo, + NewSndCallBackUPP(mac_snd_callback)); + mac_printf("SndNewChannel ret: %d\n", (int)err); + fflush(stdout); - memset(&g_snd_hdr, 0, sizeof(g_snd_hdr)); - g_snd_hdr.sampleSize = 16; - g_snd_hdr.numChannels = 2; - g_audio_rate = 44100; - g_snd_hdr.sampleRate = g_audio_rate << 16; - g_snd_hdr.numFrames = 0; // will be set in mac_send_audio - g_snd_hdr.encode = extSH; - g_snd_hdr.baseFrequency = 0; - g_snd_hdr.samplePtr = 0; + memset(&g_snd_hdr, 0, sizeof(g_snd_hdr)); + g_snd_hdr.sampleSize = 16; + g_snd_hdr.numChannels = 2; + g_audio_rate = 44100; + g_snd_hdr.sampleRate = g_audio_rate << 16; + g_snd_hdr.numFrames = 0; // will be set in mac_send_audio + g_snd_hdr.encode = extSH; + g_snd_hdr.baseFrequency = 0; + g_snd_hdr.samplePtr = 0; - set_audio_rate(g_audio_rate); + set_audio_rate(g_audio_rate); - mac_printf("End of child_sound_init_mac\n"); - fflush(stdout); + mac_printf("End of child_sound_init_mac\n"); + fflush(stdout); } -void -macsnd_init(word32 *shmaddr) -{ - g_macsnd_rebuf_cur = &g_macsnd_rebuf[0]; - g_macsnd_rebuf_ptr = &g_macsnd_rebuf[0]; - mac_printf("macsnd_init called\n"); - child_sound_loop(-1, -1, shmaddr); +void macsnd_init(word32 *shmaddr) { + g_macsnd_rebuf_cur = &g_macsnd_rebuf[0]; + g_macsnd_rebuf_ptr = &g_macsnd_rebuf[0]; + mac_printf("macsnd_init called\n"); + child_sound_loop(-1, -1, shmaddr); } #endif diff --git a/src/moremem.c b/src/moremem.c index 56d2ade..2f0209d 100644 --- a/src/moremem.c +++ b/src/moremem.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" @@ -36,101 +36,101 @@ extern int g_parallel; char c; /* from iwm.c */ -int g_num_shadow_all_banks = 0; +int g_num_shadow_all_banks = 0; #define IOR(val) ( (val) ? 0x80 : 0x00 ) extern int g_cur_a2_stat; -int g_em_emubyte_cnt = 0; -int g_paddle_buttons = 0; -int g_irq_pending = 0; +int g_em_emubyte_cnt = 0; +int g_paddle_buttons = 0; +int g_irq_pending = 0; -int g_c023_val = 0; -int g_c029_val_some = 0x41; -int g_c02b_val = 0x08; -int g_c02d_int_crom = 0; -int g_c031_disk35 = 0; -int g_c033_data = 0; -int g_c034_val = 0; -int g_c035_shadow_reg = 0x08; -int g_c036_val_speed = 0x80; -int g_c03ef_doc_ptr = 0; -int g_c041_val = 0; /* C041_EN_25SEC_INTS, C041_EN_MOVE_INTS */ -int g_c046_val = 0; -int g_c05x_annuncs = 0; -int g_c068_statereg = 0; -int g_c08x_wrdefram = 0; -int g_zipgs_unlock = 0; -int g_zipgs_reg_c059 = 0x5f; - // 7=LC cache dis, 6==5ms paddle del en, 5==5ms ext del en, - // 4==5ms c02e enab, 3==CPS follow enab, 2-0: 111 -int g_zipgs_reg_c05a = 0x0f; - // 7:4 = current ZIP speed, 0=100%, 1=93.75%, F=6.25% - // 3:0: always 1111 -int g_zipgs_reg_c05b = 0x40; - // 7==1ms clock, 6==cshupd: tag data at c05f updated - // 5==LC cache disable, 4==bd is disabled, 3==delay in effect, - // 2==rombank, 1-0==ram size (00:8K, 01=16K, 10=32K, 11=64K) -int g_zipgs_reg_c05c = 0x00; - // 7:1==slot delay enable (for 52-54ms), 0==speaker 5ms delay +int g_c023_val = 0; +int g_c029_val_some = 0x41; +int g_c02b_val = 0x08; +int g_c02d_int_crom = 0; +int g_c031_disk35 = 0; +int g_c033_data = 0; +int g_c034_val = 0; +int g_c035_shadow_reg = 0x08; +int g_c036_val_speed = 0x80; +int g_c03ef_doc_ptr = 0; +int g_c041_val = 0; /* C041_EN_25SEC_INTS, C041_EN_MOVE_INTS */ +int g_c046_val = 0; +int g_c05x_annuncs = 0; +int g_c068_statereg = 0; +int g_c08x_wrdefram = 0; +int g_zipgs_unlock = 0; +int g_zipgs_reg_c059 = 0x5f; +// 7=LC cache dis, 6==5ms paddle del en, 5==5ms ext del en, +// 4==5ms c02e enab, 3==CPS follow enab, 2-0: 111 +int g_zipgs_reg_c05a = 0x0f; +// 7:4 = current ZIP speed, 0=100%, 1=93.75%, F=6.25% +// 3:0: always 1111 +int g_zipgs_reg_c05b = 0x40; +// 7==1ms clock, 6==cshupd: tag data at c05f updated +// 5==LC cache disable, 4==bd is disabled, 3==delay in effect, +// 2==rombank, 1-0==ram size (00:8K, 01=16K, 10=32K, 11=64K) +int g_zipgs_reg_c05c = 0x00; +// 7:1==slot delay enable (for 52-54ms), 0==speaker 5ms delay -#define EMUSTATE(a) { #a, &a } +#define EMUSTATE(a) { #a, &a } Emustate_intlist g_emustate_intlist[] = { - EMUSTATE(g_cur_a2_stat), - EMUSTATE(g_paddle_buttons), + EMUSTATE(g_cur_a2_stat), + EMUSTATE(g_paddle_buttons), - EMUSTATE(g_em_emubyte_cnt), - EMUSTATE(g_irq_pending), - EMUSTATE(g_c023_val), - EMUSTATE(g_c029_val_some), - EMUSTATE(g_c02b_val), - EMUSTATE(g_c02d_int_crom), - EMUSTATE(g_c031_disk35), - EMUSTATE(g_c033_data), - EMUSTATE(g_c034_val), - EMUSTATE(g_c035_shadow_reg), - EMUSTATE(g_c036_val_speed), - EMUSTATE(g_c03ef_doc_ptr), - EMUSTATE(g_c041_val), - EMUSTATE(g_c046_val), - EMUSTATE(g_c05x_annuncs), - EMUSTATE(g_c068_statereg), - EMUSTATE(g_c08x_wrdefram), - EMUSTATE(g_zipgs_unlock), - EMUSTATE(g_zipgs_reg_c059), - EMUSTATE(g_zipgs_reg_c05a), - EMUSTATE(g_zipgs_reg_c05b), - EMUSTATE(g_zipgs_reg_c05c), - { 0, 0, } + EMUSTATE(g_em_emubyte_cnt), + EMUSTATE(g_irq_pending), + EMUSTATE(g_c023_val), + EMUSTATE(g_c029_val_some), + EMUSTATE(g_c02b_val), + EMUSTATE(g_c02d_int_crom), + EMUSTATE(g_c031_disk35), + EMUSTATE(g_c033_data), + EMUSTATE(g_c034_val), + EMUSTATE(g_c035_shadow_reg), + EMUSTATE(g_c036_val_speed), + EMUSTATE(g_c03ef_doc_ptr), + EMUSTATE(g_c041_val), + EMUSTATE(g_c046_val), + EMUSTATE(g_c05x_annuncs), + EMUSTATE(g_c068_statereg), + EMUSTATE(g_c08x_wrdefram), + EMUSTATE(g_zipgs_unlock), + EMUSTATE(g_zipgs_reg_c059), + EMUSTATE(g_zipgs_reg_c05a), + EMUSTATE(g_zipgs_reg_c05b), + EMUSTATE(g_zipgs_reg_c05c), + { 0, 0, } }; extern double g_paddle_trig_dcycs; extern double g_last_vbl_dcycs; Emustate_dbllist g_emustate_dbllist[] = { - EMUSTATE(g_paddle_trig_dcycs), - EMUSTATE(g_last_vbl_dcycs), - { 0, 0, } + EMUSTATE(g_paddle_trig_dcycs), + EMUSTATE(g_last_vbl_dcycs), + { 0, 0, } }; extern word32 g_mem_size_total; Emustate_word32list g_emustate_word32list[] = { - EMUSTATE(g_mem_size_total), - { 0, 0, } + EMUSTATE(g_mem_size_total), + { 0, 0, } }; -#define UNIMPL_READ \ - halt_printf("UNIMP READ to addr %08x\n", loc); \ - return 0; +#define UNIMPL_READ \ + halt_printf("UNIMP READ to addr %08x\n", loc); \ + return 0; -#define UNIMPL_WRITE \ - halt_printf("UNIMP WRITE to addr %08x, val: %04x\n", loc, val); \ - return; +#define UNIMPL_WRITE \ + halt_printf("UNIMP WRITE to addr %08x, val: %04x\n", loc, val); \ + return; //#ifdef _WINDOWS @@ -141,2387 +141,2322 @@ int transwarp_low_val = 0; __declspec(align(256)) #endif unsigned char transwarpcode[][32] -#if !defined(_WIN32) +#if !defined(_WIN32) __attribute__ ((aligned(256))) #endif -={ -{ + ={ + { /*0xBCFF00*/ 'T','W','G','S',0,0,0,0,0,0,0,0,0,0,0,0, -/*0xBCFF10*/ 0x5C,0x40,0xFF,0xBC, // JMP GetMaxSpeed -/*0xBCFF14*/ 0x5C,0x60,0xFF,0xBC, // JMP GetNumISpeed -/*0xBCFF18*/ 0x6B,0x00,0x00,0x00, // ??? -/*0xBCFF1C*/ 0x6B,0x00,0x00,0x00 // ??? -}, -{ -/*0xBCFF20*/ 0x5C,0x80,0xFF,0xBC, // JMP GetCurSpeed -/*0xBCFF24*/ 0x5C,0xA0,0xFF,0xBC, // JMP SetCurSpeed -/*0xBCFF28*/ 0x5C,0xC0,0xFF,0xBC, // JMP GetCurISpeed -/*0xBCFF2C*/ 0x5C,0xE0,0xFF,0xBC, // JMP SetCurISpeed -/*0xBCFF30*/ 0x6B,0x00,0x00,0x00, // ??? -/*0xBCFF34*/ 0x6B,0x00,0x00,0x00, // ??? -/*0xBCFF38*/ 0x6B,0x00,0x00,0x00, // ??? -/*0xBCFF3C*/ 0x6B,0x00,0x00,0x00 // GetTWConfig -}, -{ -/* 0xBCFF40*/ // GetMaxSpeed +/*0xBCFF10*/ 0x5C,0x40,0xFF,0xBC, // JMP GetMaxSpeed +/*0xBCFF14*/ 0x5C,0x60,0xFF,0xBC, // JMP GetNumISpeed +/*0xBCFF18*/ 0x6B,0x00,0x00,0x00, // ??? +/*0xBCFF1C*/ 0x6B,0x00,0x00,0x00 // ??? + }, + { +/*0xBCFF20*/ 0x5C,0x80,0xFF,0xBC, // JMP GetCurSpeed +/*0xBCFF24*/ 0x5C,0xA0,0xFF,0xBC, // JMP SetCurSpeed +/*0xBCFF28*/ 0x5C,0xC0,0xFF,0xBC, // JMP GetCurISpeed +/*0xBCFF2C*/ 0x5C,0xE0,0xFF,0xBC, // JMP SetCurISpeed +/*0xBCFF30*/ 0x6B,0x00,0x00,0x00, // ??? +/*0xBCFF34*/ 0x6B,0x00,0x00,0x00, // ??? +/*0xBCFF38*/ 0x6B,0x00,0x00,0x00, // ??? +/*0xBCFF3C*/ 0x6B,0x00,0x00,0x00 // GetTWConfig + }, + { +/* 0xBCFF40*/ // GetMaxSpeed #define ZIP_SPEED 8000 -0xA9, ZIP_SPEED & 0xFF, (ZIP_SPEED >> 8) &0xFF, // LDA 0x1F40 // Max Speed = 8.0Mhz -0x6B, // RTL -0x00,0x00,0x00,0x00, //4 -0x00,0x00,0x00,0x00, //8 -0x6B,0x00,0x00,0x00, //C Space Shark calls this address ??? -}, -{ -/* 0xBCFF60*/ //GetNumISpeed -0xA9,0x02,0x00, // LDA 0x0002 // 0=slow, 1=normal, 2=warp -0x6B, // RTL -}, -{ + 0xA9, ZIP_SPEED & 0xFF, (ZIP_SPEED >> 8) &0xFF, // LDA 0x1F40 // Max Speed = 8.0Mhz + 0x6B, // RTL + 0x00,0x00,0x00,0x00, //4 + 0x00,0x00,0x00,0x00, //8 + 0x6B,0x00,0x00,0x00, //C Space Shark calls this address ??? + }, + { +/* 0xBCFF60*/ //GetNumISpeed + 0xA9,0x02,0x00, // LDA 0x0002 // 0=slow, 1=normal, 2=warp + 0x6B, // RTL + }, + { /* 0xBCFF80*/ //GetCurSpeed -0xAF, 0x6A, 0xC0, 0x00, // LDA 0xC06A (/6B) -0x6B, // RTL -}, -{ + 0xAF, 0x6A, 0xC0, 0x00, // LDA 0xC06A (/6B) + 0x6B, // RTL + }, + { /* 0xBCFFA0*/ //SetCurSpeed -0x8F, 0x6A, 0xC0, 0x00, // STA 0xC06A (/6B) -0x6B, // RTL -}, -{ + 0x8F, 0x6A, 0xC0, 0x00, // STA 0xC06A (/6B) + 0x6B, // RTL + }, + { /* 0xBCFFC0*/ //GetCurISpeed -0x48, // PHA -0xAF, 0x6C, 0xC0, 0x00, // LDA 0xC06C (/6D) -0xAA, // TAX -0x68, // PLA -0x6B, // RTL -}, -{ + 0x48, // PHA + 0xAF, 0x6C, 0xC0, 0x00, // LDA 0xC06C (/6D) + 0xAA, // TAX + 0x68, // PLA + 0x6B, // RTL + }, + { /* 0xBCFFE0*/ //SetCurISpeed -0x48, // PHA -0x8A, // TXA -0x8F, 0x6C, 0xC0, 0x00, // STA 0xC06C (/6D) -0x68, // PLA -0x6B, // RTL -} -} + 0x48, // PHA + 0x8A, // TXA + 0x8F, 0x6C, 0xC0, 0x00, // STA 0xC06C (/6D) + 0x68, // PLA + 0x6B, // RTL + } + } ; // OG Added moremem_init() -void moremem_init() -{ - g_em_emubyte_cnt = 0; - g_paddle_buttons = 0; - g_irq_pending = 0; +void moremem_init() { + g_em_emubyte_cnt = 0; + g_paddle_buttons = 0; + g_irq_pending = 0; - g_c023_val = 0; - g_c029_val_some = 0x41; - g_c02b_val = 0x08; - g_c02d_int_crom = 0; - g_c031_disk35 = 0; - g_c034_val = 0; - g_c035_shadow_reg = 0x08; - g_c036_val_speed = 0x80; - g_c03ef_doc_ptr = 0; - g_c041_val = 0; /* C041_EN_25SEC_INTS, C041_EN_MOVE_INTS */ - g_c046_val = 0; - g_c05x_annuncs = 0; - g_c068_statereg = 0; - g_c08x_wrdefram = 0; - g_zipgs_unlock = 0; - g_zipgs_reg_c059 = 0x5f; - g_zipgs_reg_c05a = 0x0f; - g_zipgs_reg_c05b = 0x40; - g_zipgs_reg_c05c = 0x00; + g_c023_val = 0; + g_c029_val_some = 0x41; + g_c02b_val = 0x08; + g_c02d_int_crom = 0; + g_c031_disk35 = 0; + g_c034_val = 0; + g_c035_shadow_reg = 0x08; + g_c036_val_speed = 0x80; + g_c03ef_doc_ptr = 0; + g_c041_val = 0; /* C041_EN_25SEC_INTS, C041_EN_MOVE_INTS */ + g_c046_val = 0; + g_c05x_annuncs = 0; + g_c068_statereg = 0; + g_c08x_wrdefram = 0; + g_zipgs_unlock = 0; + g_zipgs_reg_c059 = 0x5f; + g_zipgs_reg_c05a = 0x0f; + g_zipgs_reg_c05b = 0x40; + g_zipgs_reg_c05c = 0x00; } -void -fixup_brks() -{ - word32 page; - word32 tmp, tmp2; - Pg_info val; - int is_wr_only; - int i, num; +void fixup_brks() { + word32 page; + word32 tmp, tmp2; + Pg_info val; + int is_wr_only; + int i, num; - num = g_num_breakpoints; - for(i = 0; i < num; i++) { - page = (g_breakpts[i] >> 8) & 0xffff; - is_wr_only = (g_breakpts[i] >> 24) & 1; - if(!is_wr_only) { - val = GET_PAGE_INFO_RD(page); - tmp = PTR2WORD(val) & 0xff; - tmp2 = tmp | BANK_IO_TMP | BANK_BREAK; - SET_PAGE_INFO_RD(page, val - tmp + tmp2); - } - val = GET_PAGE_INFO_WR(page); - tmp = PTR2WORD(val) & 0xff; - tmp2 = tmp | BANK_IO_TMP | BANK_BREAK; - SET_PAGE_INFO_WR(page, val - tmp + tmp2); - } + num = g_num_breakpoints; + for(i = 0; i < num; i++) { + page = (g_breakpts[i] >> 8) & 0xffff; + is_wr_only = (g_breakpts[i] >> 24) & 1; + if(!is_wr_only) { + val = GET_PAGE_INFO_RD(page); + tmp = PTR2WORD(val) & 0xff; + tmp2 = tmp | BANK_IO_TMP | BANK_BREAK; + SET_PAGE_INFO_RD(page, val - tmp + tmp2); + } + val = GET_PAGE_INFO_WR(page); + tmp = PTR2WORD(val) & 0xff; + tmp2 = tmp | BANK_IO_TMP | BANK_BREAK; + SET_PAGE_INFO_WR(page, val - tmp + tmp2); + } } -void -fixup_hires_on() -{ - if((g_cur_a2_stat & ALL_STAT_ST80) == 0) { - return; - } +void fixup_hires_on() { + if((g_cur_a2_stat & ALL_STAT_ST80) == 0) { + return; + } - fixup_bank0_2000_4000(); - fixup_brks(); + fixup_bank0_2000_4000(); + fixup_brks(); } -void -fixup_bank0_2000_4000() -{ - byte *mem0rd; - byte *mem0wr; +void fixup_bank0_2000_4000() { + byte *mem0rd; + byte *mem0wr; - mem0rd = &(g_memory_ptr[0x2000]); - mem0wr = mem0rd; - if((g_cur_a2_stat & ALL_STAT_ST80) && (g_cur_a2_stat & ALL_STAT_HIRES)){ - if(g_cur_a2_stat & ALL_STAT_PAGE2) { - mem0rd += 0x10000; - mem0wr += 0x10000; - if((g_c035_shadow_reg & 0x12) == 0 || - (g_c035_shadow_reg & 0x8) == 0) { - mem0wr += BANK_SHADOW2; - } - } else if((g_c035_shadow_reg & 0x02) == 0) { - mem0wr += BANK_SHADOW; - } + mem0rd = &(g_memory_ptr[0x2000]); + mem0wr = mem0rd; + if((g_cur_a2_stat & ALL_STAT_ST80) && (g_cur_a2_stat & ALL_STAT_HIRES)) { + if(g_cur_a2_stat & ALL_STAT_PAGE2) { + mem0rd += 0x10000; + mem0wr += 0x10000; + if((g_c035_shadow_reg & 0x12) == 0 || + (g_c035_shadow_reg & 0x8) == 0) { + mem0wr += BANK_SHADOW2; + } + } else if((g_c035_shadow_reg & 0x02) == 0) { + mem0wr += BANK_SHADOW; + } - } else { - if(RAMRD) { - mem0rd += 0x10000; - } - if(RAMWRT) { - mem0wr += 0x10000; - if((g_c035_shadow_reg & 0x12) == 0 || - (g_c035_shadow_reg & 0x8) == 0) { - mem0wr += BANK_SHADOW2; - } - } else if((g_c035_shadow_reg & 0x02) == 0) { - mem0wr += BANK_SHADOW; - } - } + } else { + if(RAMRD) { + mem0rd += 0x10000; + } + if(RAMWRT) { + mem0wr += 0x10000; + if((g_c035_shadow_reg & 0x12) == 0 || + (g_c035_shadow_reg & 0x8) == 0) { + mem0wr += BANK_SHADOW2; + } + } else if((g_c035_shadow_reg & 0x02) == 0) { + mem0wr += BANK_SHADOW; + } + } - fixup_any_bank_any_page(0x20, 0x20, mem0rd, mem0wr); + fixup_any_bank_any_page(0x20, 0x20, mem0rd, mem0wr); } -void -fixup_bank0_0400_0800() -{ - byte *mem0rd; - byte *mem0wr; - int shadow; +void fixup_bank0_0400_0800() { + byte *mem0rd; + byte *mem0wr; + int shadow; - mem0rd = &(g_memory_ptr[0x400]); - mem0wr = mem0rd; - shadow = BANK_SHADOW; - if(g_cur_a2_stat & ALL_STAT_ST80) { - if(g_cur_a2_stat & ALL_STAT_PAGE2) { - shadow = BANK_SHADOW2; - mem0rd += 0x10000; - mem0wr += 0x10000; - } - } else { - if(RAMWRT) { - shadow = BANK_SHADOW2; - mem0wr += 0x10000; - } - if(RAMRD) { - mem0rd += 0x10000; - } - } - if((g_c035_shadow_reg & 0x01) == 0) { - mem0wr += shadow; - } + mem0rd = &(g_memory_ptr[0x400]); + mem0wr = mem0rd; + shadow = BANK_SHADOW; + if(g_cur_a2_stat & ALL_STAT_ST80) { + if(g_cur_a2_stat & ALL_STAT_PAGE2) { + shadow = BANK_SHADOW2; + mem0rd += 0x10000; + mem0wr += 0x10000; + } + } else { + if(RAMWRT) { + shadow = BANK_SHADOW2; + mem0wr += 0x10000; + } + if(RAMRD) { + mem0rd += 0x10000; + } + } + if((g_c035_shadow_reg & 0x01) == 0) { + mem0wr += shadow; + } - fixup_any_bank_any_page(0x4, 4, mem0rd, mem0wr); + fixup_any_bank_any_page(0x4, 4, mem0rd, mem0wr); } -void -fixup_any_bank_any_page(int start_page, int num_pages, byte *mem0rd, - byte *mem0wr) -{ - int i; +void fixup_any_bank_any_page(int start_page, int num_pages, byte *mem0rd, + byte *mem0wr) { + int i; - for(i = 0; i < num_pages; i++) { - SET_PAGE_INFO_RD(i + start_page, mem0rd); - mem0rd += 0x100; - } + for(i = 0; i < num_pages; i++) { + SET_PAGE_INFO_RD(i + start_page, mem0rd); + mem0rd += 0x100; + } - for(i = 0; i < num_pages; i++) { - SET_PAGE_INFO_WR(i + start_page, mem0wr); - mem0wr += 0x100; - } + for(i = 0; i < num_pages; i++) { + SET_PAGE_INFO_WR(i + start_page, mem0wr); + mem0wr += 0x100; + } } -void -fixup_intcx() -{ - byte *rom10000; - byte *rom_inc; - int no_io_shadow; - int off; - int start_k; - word32 mask; - int j, k; +void fixup_intcx() { + byte *rom10000; + byte *rom_inc; + int no_io_shadow; + int off; + int start_k; + word32 mask; + int j, k; - rom10000 = &(g_rom_fc_ff_ptr[0x30000]); + rom10000 = &(g_rom_fc_ff_ptr[0x30000]); - no_io_shadow = (g_c035_shadow_reg & 0x40); + no_io_shadow = (g_c035_shadow_reg & 0x40); - start_k = 0; - if(no_io_shadow) { - /* if not shadowing, banks 0 and 1 are not affected by intcx */ - start_k = 2; - } + start_k = 0; + if(no_io_shadow) { + /* if not shadowing, banks 0 and 1 are not affected by intcx */ + start_k = 2; + } - for(k = start_k; k < 4; k++) { - off = k; - if(k >= 2) { - off += (0xe0 - 2); - } - /* step off through 0x00, 0x01, 0xe0, 0xe1 */ + for(k = start_k; k < 4; k++) { + off = k; + if(k >= 2) { + off += (0xe0 - 2); + } + /* step off through 0x00, 0x01, 0xe0, 0xe1 */ - off = off << 8; - SET_PAGE_INFO_RD(0xc0 + off, SET_BANK_IO); + off = off << 8; + SET_PAGE_INFO_RD(0xc0 + off, SET_BANK_IO); - for(j = 0xc1; j < 0xc8; j++) { - mask = 1 << (j & 0xf); - if(j < 0xc8) { - rom_inc = SET_BANK_IO; - if(((g_c02d_int_crom & mask) == 0) || INTCX) { - rom_inc = rom10000 + (j << 8); - } else { - // User-slot rom - rom_inc = &(g_rom_cards_ptr[0]) + - ((j - 0xc0) << 8); - } - SET_PAGE_INFO_RD(j + off, rom_inc); - } - } - for(j = 0xc8; j < 0xd0; j++) { + for(j = 0xc1; j < 0xc8; j++) { + mask = 1 << (j & 0xf); + if(j < 0xc8) { + rom_inc = SET_BANK_IO; + if(((g_c02d_int_crom & mask) == 0) || INTCX) { + rom_inc = rom10000 + (j << 8); + } else { + // User-slot rom + rom_inc = &(g_rom_cards_ptr[0]) + + ((j - 0xc0) << 8); + } + SET_PAGE_INFO_RD(j + off, rom_inc); + } + } + for(j = 0xc8; j < 0xd0; j++) { - /*c800 - cfff */ - if(((g_c02d_int_crom & (1 << 3)) == 0) || INTCX) - { - rom_inc = rom10000 + (j << 8); - } - else - { - rom_inc = rom10000 + (j << 8); + /*c800 - cfff */ + if(((g_c02d_int_crom & (1 << 3)) == 0) || INTCX) + { + rom_inc = rom10000 + (j << 8); + } + else + { + rom_inc = rom10000 + (j << 8); - } - SET_PAGE_INFO_RD(j + off, rom_inc); - } - iostrobe = 0; - for(j = 0xc0; j < 0xd0; j++) { - SET_PAGE_INFO_WR(j + off, SET_BANK_IO); - } - } - if(!no_io_shadow) { - SET_PAGE_INFO_RD(0xc7, SET_BANK_IO); /* smartport */ - } - fixup_brks(); + } + SET_PAGE_INFO_RD(j + off, rom_inc); + } + iostrobe = 0; + for(j = 0xc0; j < 0xd0; j++) { + SET_PAGE_INFO_WR(j + off, SET_BANK_IO); + } + } + if(!no_io_shadow) { + SET_PAGE_INFO_RD(0xc7, SET_BANK_IO); /* smartport */ + } + fixup_brks(); } -void -fixup_wrdefram(int new_wrdefram) -{ - byte *mem0wr; - byte *wrptr; - int j; +void fixup_wrdefram(int new_wrdefram) { + byte *mem0wr; + byte *wrptr; + int j; - g_c08x_wrdefram = new_wrdefram; + g_c08x_wrdefram = new_wrdefram; - if(g_c035_shadow_reg & 0x40) { - /* do nothing */ - return; - } + if(g_c035_shadow_reg & 0x40) { + /* do nothing */ + return; + } - /* if shadowing, banks 0 and 1 are affected by wrdefram */ - mem0wr = &(g_memory_ptr[0]); - if(!new_wrdefram) { - mem0wr += (BANK_IO_TMP | BANK_IO2_TMP); - } + /* if shadowing, banks 0 and 1 are affected by wrdefram */ + mem0wr = &(g_memory_ptr[0]); + if(!new_wrdefram) { + mem0wr += (BANK_IO_TMP | BANK_IO2_TMP); + } - wrptr = mem0wr + 0x1e000; - for(j = 0x1e0; j < 0x200; j++) { - SET_PAGE_INFO_WR(j, wrptr); - wrptr += 0x100; - } + wrptr = mem0wr + 0x1e000; + for(j = 0x1e0; j < 0x200; j++) { + SET_PAGE_INFO_WR(j, wrptr); + wrptr += 0x100; + } - wrptr = mem0wr + 0x0e000; - if(ALTZP) { - wrptr += 0x10000; - } - for(j = 0xe0; j < 0x100; j++) { - SET_PAGE_INFO_WR(j, wrptr); - wrptr += 0x100; - } + wrptr = mem0wr + 0x0e000; + if(ALTZP) { + wrptr += 0x10000; + } + for(j = 0xe0; j < 0x100; j++) { + SET_PAGE_INFO_WR(j, wrptr); + wrptr += 0x100; + } - wrptr = mem0wr + 0x1d000; - if(! LCBANK2) { - wrptr -= 0x1000; - } - for(j = 0x1d0; j < 0x1e0; j++) { - SET_PAGE_INFO_WR(j, wrptr); - wrptr += 0x100; - } + wrptr = mem0wr + 0x1d000; + if(!LCBANK2) { + wrptr -= 0x1000; + } + for(j = 0x1d0; j < 0x1e0; j++) { + SET_PAGE_INFO_WR(j, wrptr); + wrptr += 0x100; + } - wrptr = mem0wr + 0xd000; - if(! LCBANK2) { - wrptr -= 0x1000; - } - if(ALTZP) { - wrptr += 0x10000; - } - for(j = 0xd0; j < 0xe0; j++) { - SET_PAGE_INFO_WR(j, wrptr); - wrptr += 0x100; - } + wrptr = mem0wr + 0xd000; + if(!LCBANK2) { + wrptr -= 0x1000; + } + if(ALTZP) { + wrptr += 0x10000; + } + for(j = 0xd0; j < 0xe0; j++) { + SET_PAGE_INFO_WR(j, wrptr); + wrptr += 0x100; + } - fixup_brks(); + fixup_brks(); } -void -fixup_st80col(double dcycs) -{ - int cur_a2_stat; +void fixup_st80col(double dcycs) { + int cur_a2_stat; - cur_a2_stat = g_cur_a2_stat; + cur_a2_stat = g_cur_a2_stat; - fixup_bank0_0400_0800(); + fixup_bank0_0400_0800(); - if(cur_a2_stat & ALL_STAT_HIRES) { - /* fixup no matter what PAGE2 since PAGE2 and RAMRD/WR */ - /* can work against each other */ - fixup_bank0_2000_4000(); - } + if(cur_a2_stat & ALL_STAT_HIRES) { + /* fixup no matter what PAGE2 since PAGE2 and RAMRD/WR */ + /* can work against each other */ + fixup_bank0_2000_4000(); + } - if(cur_a2_stat & ALL_STAT_PAGE2) { - change_display_mode(dcycs); - } + if(cur_a2_stat & ALL_STAT_PAGE2) { + change_display_mode(dcycs); + } - fixup_brks(); + fixup_brks(); } -void -fixup_altzp() -{ - byte *mem0rd, *mem0wr; - int rdrom, c08x_wrdefram; - int altzp; +void fixup_altzp() { + byte *mem0rd, *mem0wr; + int rdrom, c08x_wrdefram; + int altzp; - altzp = ALTZP; - mem0rd = &(g_memory_ptr[0]); - if(altzp) { - mem0rd += 0x10000; - } - SET_PAGE_INFO_RD(0, mem0rd); - SET_PAGE_INFO_RD(1, mem0rd + 0x100); - SET_PAGE_INFO_WR(0, mem0rd); - SET_PAGE_INFO_WR(1, mem0rd + 0x100); + altzp = ALTZP; + mem0rd = &(g_memory_ptr[0]); + if(altzp) { + mem0rd += 0x10000; + } + SET_PAGE_INFO_RD(0, mem0rd); + SET_PAGE_INFO_RD(1, mem0rd + 0x100); + SET_PAGE_INFO_WR(0, mem0rd); + SET_PAGE_INFO_WR(1, mem0rd + 0x100); - mem0rd = &(g_memory_ptr[0xd000]); - mem0wr = mem0rd; - c08x_wrdefram = g_c08x_wrdefram; - rdrom = RDROM; + mem0rd = &(g_memory_ptr[0xd000]); + mem0wr = mem0rd; + c08x_wrdefram = g_c08x_wrdefram; + rdrom = RDROM; - if(g_c035_shadow_reg & 0x40) { - if(ALTZP) { - mem0rd += 0x10000; - } - fixup_any_bank_any_page(0xd0, 0x10, mem0rd - 0x1000, - mem0rd - 0x1000); - c08x_wrdefram = 1; - rdrom = 0; - } else { - if(!c08x_wrdefram) { - mem0wr += (BANK_IO_TMP | BANK_IO2_TMP); - } - if(ALTZP) { - mem0rd += 0x10000; - mem0wr += 0x10000; - } - if(! LCBANK2) { - mem0rd -= 0x1000; - mem0wr -= 0x1000; - } - if(rdrom) { - mem0rd = &(g_rom_fc_ff_ptr[0x3d000]); - } - fixup_any_bank_any_page(0xd0, 0x10, mem0rd, mem0wr); - } + if(g_c035_shadow_reg & 0x40) { + if(ALTZP) { + mem0rd += 0x10000; + } + fixup_any_bank_any_page(0xd0, 0x10, mem0rd - 0x1000, + mem0rd - 0x1000); + c08x_wrdefram = 1; + rdrom = 0; + } else { + if(!c08x_wrdefram) { + mem0wr += (BANK_IO_TMP | BANK_IO2_TMP); + } + if(ALTZP) { + mem0rd += 0x10000; + mem0wr += 0x10000; + } + if(!LCBANK2) { + mem0rd -= 0x1000; + mem0wr -= 0x1000; + } + if(rdrom) { + mem0rd = &(g_rom_fc_ff_ptr[0x3d000]); + } + fixup_any_bank_any_page(0xd0, 0x10, mem0rd, mem0wr); + } - mem0rd = &(g_memory_ptr[0xe000]); - mem0wr = mem0rd; - if(!c08x_wrdefram) { - mem0wr += (BANK_IO_TMP | BANK_IO2_TMP); - } - if(ALTZP) { - mem0rd += 0x10000; - mem0wr += 0x10000; - } - if(rdrom) { - mem0rd = &(g_rom_fc_ff_ptr[0x3e000]); - } - fixup_any_bank_any_page(0xe0, 0x20, mem0rd, mem0wr); + mem0rd = &(g_memory_ptr[0xe000]); + mem0wr = mem0rd; + if(!c08x_wrdefram) { + mem0wr += (BANK_IO_TMP | BANK_IO2_TMP); + } + if(ALTZP) { + mem0rd += 0x10000; + mem0wr += 0x10000; + } + if(rdrom) { + mem0rd = &(g_rom_fc_ff_ptr[0x3e000]); + } + fixup_any_bank_any_page(0xe0, 0x20, mem0rd, mem0wr); - /* No need for fixup_brks since called from set_statereg() */ + /* No need for fixup_brks since called from set_statereg() */ } -void -fixup_page2(double dcycs) -{ - if((g_cur_a2_stat & ALL_STAT_ST80)) { - fixup_bank0_0400_0800(); - if((g_cur_a2_stat & ALL_STAT_HIRES)) { - fixup_bank0_2000_4000(); - } - } else { - change_display_mode(dcycs); - } +void fixup_page2(double dcycs) { + if((g_cur_a2_stat & ALL_STAT_ST80)) { + fixup_bank0_0400_0800(); + if((g_cur_a2_stat & ALL_STAT_HIRES)) { + fixup_bank0_2000_4000(); + } + } else { + change_display_mode(dcycs); + } } -void -fixup_ramrd() -{ - byte *mem0rd; - int cur_a2_stat; - int j; +void fixup_ramrd() { + byte *mem0rd; + int cur_a2_stat; + int j; - cur_a2_stat = g_cur_a2_stat; + cur_a2_stat = g_cur_a2_stat; - if((cur_a2_stat & ALL_STAT_ST80) == 0) { - fixup_bank0_0400_0800(); - } - if( ((cur_a2_stat & ALL_STAT_ST80) == 0) || - ((cur_a2_stat & ALL_STAT_HIRES) == 0) ) { - fixup_bank0_2000_4000(); - } + if((cur_a2_stat & ALL_STAT_ST80) == 0) { + fixup_bank0_0400_0800(); + } + if( ((cur_a2_stat & ALL_STAT_ST80) == 0) || + ((cur_a2_stat & ALL_STAT_HIRES) == 0) ) { + fixup_bank0_2000_4000(); + } - mem0rd = &(g_memory_ptr[0x0000]); - if(RAMRD) { - mem0rd += 0x10000; - } + mem0rd = &(g_memory_ptr[0x0000]); + if(RAMRD) { + mem0rd += 0x10000; + } - SET_PAGE_INFO_RD(2, mem0rd + 0x200); - SET_PAGE_INFO_RD(3, mem0rd + 0x300); + SET_PAGE_INFO_RD(2, mem0rd + 0x200); + SET_PAGE_INFO_RD(3, mem0rd + 0x300); - for(j = 8; j < 0x20; j++) { - SET_PAGE_INFO_RD(j, mem0rd + j*0x100); - } + for(j = 8; j < 0x20; j++) { + SET_PAGE_INFO_RD(j, mem0rd + j*0x100); + } - for(j = 0x40; j < 0xc0; j++) { - SET_PAGE_INFO_RD(j, mem0rd + j*0x100); - } + for(j = 0x40; j < 0xc0; j++) { + SET_PAGE_INFO_RD(j, mem0rd + j*0x100); + } - /* No need for fixup_brks since only called from set_statereg() */ + /* No need for fixup_brks since only called from set_statereg() */ } -void -fixup_ramwrt() -{ - byte *mem0wr; - int cur_a2_stat; - int shadow; - int ramwrt; - int j; +void fixup_ramwrt() { + byte *mem0wr; + int cur_a2_stat; + int shadow; + int ramwrt; + int j; - cur_a2_stat = g_cur_a2_stat; + cur_a2_stat = g_cur_a2_stat; - if((cur_a2_stat & ALL_STAT_ST80) == 0) { - fixup_bank0_0400_0800(); - } - if( ((cur_a2_stat & ALL_STAT_ST80) == 0) || - ((cur_a2_stat & ALL_STAT_HIRES) == 0) ) { - fixup_bank0_2000_4000(); - } + if((cur_a2_stat & ALL_STAT_ST80) == 0) { + fixup_bank0_0400_0800(); + } + if( ((cur_a2_stat & ALL_STAT_ST80) == 0) || + ((cur_a2_stat & ALL_STAT_HIRES) == 0) ) { + fixup_bank0_2000_4000(); + } - mem0wr = &(g_memory_ptr[0x0000]); - ramwrt = RAMWRT; - if(ramwrt) { - mem0wr += 0x10000; - } + mem0wr = &(g_memory_ptr[0x0000]); + ramwrt = RAMWRT; + if(ramwrt) { + mem0wr += 0x10000; + } - SET_PAGE_INFO_WR(2, mem0wr + 0x200); - SET_PAGE_INFO_WR(3, mem0wr + 0x300); + SET_PAGE_INFO_WR(2, mem0wr + 0x200); + SET_PAGE_INFO_WR(3, mem0wr + 0x300); - shadow = BANK_SHADOW; - if(ramwrt) { - shadow = BANK_SHADOW2; - } - if( ((g_c035_shadow_reg & 0x20) != 0) || - ((g_rom_version < 3) && !g_user_page2_shadow)) { - shadow = 0; - } - for(j = 8; j < 0x0c; j++) { - SET_PAGE_INFO_WR(j, mem0wr + j*0x100 + shadow); - } + shadow = BANK_SHADOW; + if(ramwrt) { + shadow = BANK_SHADOW2; + } + if( ((g_c035_shadow_reg & 0x20) != 0) || + ((g_rom_version < 3) && !g_user_page2_shadow)) { + shadow = 0; + } + for(j = 8; j < 0x0c; j++) { + SET_PAGE_INFO_WR(j, mem0wr + j*0x100 + shadow); + } - for(j = 0xc; j < 0x20; j++) { - SET_PAGE_INFO_WR(j, mem0wr + j*0x100); - } + for(j = 0xc; j < 0x20; j++) { + SET_PAGE_INFO_WR(j, mem0wr + j*0x100); + } - shadow = 0; - if(ramwrt) { - if((g_c035_shadow_reg & 0x14) == 0 || - (g_c035_shadow_reg & 0x08) == 0) { - shadow = BANK_SHADOW2; - } - } else if((g_c035_shadow_reg & 0x04) == 0) { - shadow = BANK_SHADOW; - } - for(j = 0x40; j < 0x60; j++) { - SET_PAGE_INFO_WR(j, mem0wr + j*0x100 + shadow); - } + shadow = 0; + if(ramwrt) { + if((g_c035_shadow_reg & 0x14) == 0 || + (g_c035_shadow_reg & 0x08) == 0) { + shadow = BANK_SHADOW2; + } + } else if((g_c035_shadow_reg & 0x04) == 0) { + shadow = BANK_SHADOW; + } + for(j = 0x40; j < 0x60; j++) { + SET_PAGE_INFO_WR(j, mem0wr + j*0x100 + shadow); + } - shadow = 0; - if(ramwrt && (g_c035_shadow_reg & 0x08) == 0) { - /* shr shadowing */ - shadow = BANK_SHADOW2; - } - for(j = 0x60; j < 0xa0; j++) { - SET_PAGE_INFO_WR(j, mem0wr + j*0x100 + shadow); - } + shadow = 0; + if(ramwrt && (g_c035_shadow_reg & 0x08) == 0) { + /* shr shadowing */ + shadow = BANK_SHADOW2; + } + for(j = 0x60; j < 0xa0; j++) { + SET_PAGE_INFO_WR(j, mem0wr + j*0x100 + shadow); + } - for(j = 0xa0; j < 0xc0; j++) { - SET_PAGE_INFO_WR(j, mem0wr + j*0x100); - } + for(j = 0xa0; j < 0xc0; j++) { + SET_PAGE_INFO_WR(j, mem0wr + j*0x100); + } - /* No need for fixup_brks() since only called from set_statereg() */ + /* No need for fixup_brks() since only called from set_statereg() */ } -void -fixup_lcbank2() -{ - byte *mem0rd, *mem0wr; - int lcbank2, c08x_wrdefram, rdrom; - int off; - int k; +void fixup_lcbank2() { + byte *mem0rd, *mem0wr; + int lcbank2, c08x_wrdefram, rdrom; + int off; + int k; - for(k = 0; k < 4; k++) { - off = k; - if(k >= 2) { - off += (0xe0 - 2); - } - /* step off through 0x00, 0x01, 0xe0, 0xe1 */ + for(k = 0; k < 4; k++) { + off = k; + if(k >= 2) { + off += (0xe0 - 2); + } + /* step off through 0x00, 0x01, 0xe0, 0xe1 */ - if(k < 2) { - mem0rd = &(g_memory_ptr[k << 16]); - } else { - mem0rd = &(g_slow_memory_ptr[(k & 1) << 16]); - } - if((k == 0) && ALTZP) { - mem0rd += 0x10000; - } - lcbank2 = LCBANK2; - c08x_wrdefram = g_c08x_wrdefram; - rdrom = RDROM; - if((k < 2) && (g_c035_shadow_reg & 0x40)) { - lcbank2 = 0; - c08x_wrdefram = 1; - rdrom = 0; - } - if(! lcbank2) { - mem0rd -= 0x1000; /* lcbank1, use 0xc000-cfff */ - } - mem0wr = mem0rd; - if((k < 2) && !c08x_wrdefram) { - mem0wr += (BANK_IO_TMP | BANK_IO2_TMP); - } - if((k < 2) && rdrom) { - mem0rd = &(g_rom_fc_ff_ptr[0x30000]); - } - fixup_any_bank_any_page(off*0x100 + 0xd0, 0x10, - mem0rd + 0xd000, mem0wr + 0xd000); - } + if(k < 2) { + mem0rd = &(g_memory_ptr[k << 16]); + } else { + mem0rd = &(g_slow_memory_ptr[(k & 1) << 16]); + } + if((k == 0) && ALTZP) { + mem0rd += 0x10000; + } + lcbank2 = LCBANK2; + c08x_wrdefram = g_c08x_wrdefram; + rdrom = RDROM; + if((k < 2) && (g_c035_shadow_reg & 0x40)) { + lcbank2 = 0; + c08x_wrdefram = 1; + rdrom = 0; + } + if(!lcbank2) { + mem0rd -= 0x1000; /* lcbank1, use 0xc000-cfff */ + } + mem0wr = mem0rd; + if((k < 2) && !c08x_wrdefram) { + mem0wr += (BANK_IO_TMP | BANK_IO2_TMP); + } + if((k < 2) && rdrom) { + mem0rd = &(g_rom_fc_ff_ptr[0x30000]); + } + fixup_any_bank_any_page(off*0x100 + 0xd0, 0x10, + mem0rd + 0xd000, mem0wr + 0xd000); + } - /* No need for fixup_brks() since only called from set_statereg(), */ - /* or from other routines which will handle it */ + /* No need for fixup_brks() since only called from set_statereg(), */ + /* or from other routines which will handle it */ } -void -fixup_rdrom() -{ - byte *mem0rd; - int j, k; +void fixup_rdrom() { + byte *mem0rd; + int j, k; - /* fixup_lcbank2 handles 0xd000-dfff for rd & wr*/ - fixup_lcbank2(); + /* fixup_lcbank2 handles 0xd000-dfff for rd & wr*/ + fixup_lcbank2(); - for(k = 0; k < 2; k++) { - /* k is the bank */ - mem0rd = &(g_memory_ptr[k << 16]); - if((k == 0) && ALTZP) { - mem0rd += 0x10000; - } - if((g_c035_shadow_reg & 0x40) == 0) { - if(RDROM) { - mem0rd = &(g_rom_fc_ff_ptr[0x30000]); - } - } - for(j = 0xe0; j < 0x100; j++) { - SET_PAGE_INFO_RD(j + k*0x100, mem0rd + j*0x100); - } - } + for(k = 0; k < 2; k++) { + /* k is the bank */ + mem0rd = &(g_memory_ptr[k << 16]); + if((k == 0) && ALTZP) { + mem0rd += 0x10000; + } + if((g_c035_shadow_reg & 0x40) == 0) { + if(RDROM) { + mem0rd = &(g_rom_fc_ff_ptr[0x30000]); + } + } + for(j = 0xe0; j < 0x100; j++) { + SET_PAGE_INFO_RD(j + k*0x100, mem0rd + j*0x100); + } + } - /* No need for fixup_brks() since only called from set_statereg() */ + /* No need for fixup_brks() since only called from set_statereg() */ } -void -set_statereg(double dcycs, int val) -{ - int _xor; // OG renamed xor to _xor +void set_statereg(double dcycs, int val) { + int _xor; // OG renamed xor to _xor - _xor = val ^ g_c068_statereg; - g_c068_statereg = val; - if(_xor == 0) { - return; - } + _xor = val ^ g_c068_statereg; + g_c068_statereg = val; + if(_xor == 0) { + return; + } - if(_xor & 0x80) { - /* altzp */ - fixup_altzp(); - } - if(_xor & 0x40) { - /* page2 */ - g_cur_a2_stat = (g_cur_a2_stat & ~ALL_STAT_PAGE2) | - (val & ALL_STAT_PAGE2); - fixup_page2(dcycs); - } + if(_xor & 0x80) { + /* altzp */ + fixup_altzp(); + } + if(_xor & 0x40) { + /* page2 */ + g_cur_a2_stat = (g_cur_a2_stat & ~ALL_STAT_PAGE2) | + (val & ALL_STAT_PAGE2); + fixup_page2(dcycs); + } - if(_xor & 0x20) { - /* RAMRD */ - fixup_ramrd(); - } + if(_xor & 0x20) { + /* RAMRD */ + fixup_ramrd(); + } - if(_xor & 0x10) { - /* RAMWRT */ - fixup_ramwrt(); - } + if(_xor & 0x10) { + /* RAMWRT */ + fixup_ramwrt(); + } - if(_xor & 0x08) { - /* RDROM */ - fixup_rdrom(); - } + if(_xor & 0x08) { + /* RDROM */ + fixup_rdrom(); + } - if(_xor & 0x04) { - /* LCBANK2 */ - fixup_lcbank2(); - } + if(_xor & 0x04) { + /* LCBANK2 */ + fixup_lcbank2(); + } - if(_xor & 0x02) { - /* ROMBANK */ - halt_printf("Just set rombank = %d\n", ROMB); - } + if(_xor & 0x02) { + /* ROMBANK */ + halt_printf("Just set rombank = %d\n", ROMB); + } - if(_xor & 0x01) { - fixup_intcx(); - } + if(_xor & 0x01) { + fixup_intcx(); + } - if(_xor) { - fixup_brks(); - } + if(_xor) { + fixup_brks(); + } } -void -fixup_shadow_txt1() -{ - byte *mem0wr; - int j; +void fixup_shadow_txt1() { + byte *mem0wr; + int j; - fixup_bank0_0400_0800(); + fixup_bank0_0400_0800(); - mem0wr = &(g_memory_ptr[0x10000]); - if((g_c035_shadow_reg & 0x01) == 0) { - mem0wr += BANK_SHADOW2; - } - for(j = 4; j < 8; j++) { - SET_PAGE_INFO_WR(0x100 + j, mem0wr + j*0x100); - } + mem0wr = &(g_memory_ptr[0x10000]); + if((g_c035_shadow_reg & 0x01) == 0) { + mem0wr += BANK_SHADOW2; + } + for(j = 4; j < 8; j++) { + SET_PAGE_INFO_WR(0x100 + j, mem0wr + j*0x100); + } } -void -fixup_shadow_txt2() -{ - byte *mem0wr; - int shadow; - int j; +void fixup_shadow_txt2() { + byte *mem0wr; + int shadow; + int j; - /* bank 0 */ - mem0wr = &(g_memory_ptr[0x00000]); - shadow = BANK_SHADOW; - if(RAMWRT) { - mem0wr += 0x10000; - shadow = BANK_SHADOW2; - } - if(((g_c035_shadow_reg & 0x20) == 0) && - ((g_rom_version >= 3) || g_user_page2_shadow)) { - mem0wr += shadow; - } - for(j = 8; j < 0xc; j++) { - SET_PAGE_INFO_WR(j, mem0wr + j*0x100); - } + /* bank 0 */ + mem0wr = &(g_memory_ptr[0x00000]); + shadow = BANK_SHADOW; + if(RAMWRT) { + mem0wr += 0x10000; + shadow = BANK_SHADOW2; + } + if(((g_c035_shadow_reg & 0x20) == 0) && + ((g_rom_version >= 3) || g_user_page2_shadow)) { + mem0wr += shadow; + } + for(j = 8; j < 0xc; j++) { + SET_PAGE_INFO_WR(j, mem0wr + j*0x100); + } - /* and bank 1 */ - mem0wr = &(g_memory_ptr[0x10000]); - if(((g_c035_shadow_reg & 0x20) == 0) && - ((g_rom_version >= 3) || g_user_page2_shadow)) { - mem0wr += BANK_SHADOW2; - } - for(j = 8; j < 0xc; j++) { - SET_PAGE_INFO_WR(0x100 + j, mem0wr + j*0x100); - } + /* and bank 1 */ + mem0wr = &(g_memory_ptr[0x10000]); + if(((g_c035_shadow_reg & 0x20) == 0) && + ((g_rom_version >= 3) || g_user_page2_shadow)) { + mem0wr += BANK_SHADOW2; + } + for(j = 8; j < 0xc; j++) { + SET_PAGE_INFO_WR(0x100 + j, mem0wr + j*0x100); + } } -void -fixup_shadow_hires1() -{ - byte *mem0wr; - int j; +void fixup_shadow_hires1() { + byte *mem0wr; + int j; - fixup_bank0_2000_4000(); + fixup_bank0_2000_4000(); - /* and bank 1 */ - mem0wr = &(g_memory_ptr[0x10000]); - if((g_c035_shadow_reg & 0x12) == 0 || (g_c035_shadow_reg & 0x8) == 0) { - mem0wr += BANK_SHADOW2; - } - for(j = 0x20; j < 0x40; j++) { - SET_PAGE_INFO_WR(0x100 + j, mem0wr + j*0x100); - } + /* and bank 1 */ + mem0wr = &(g_memory_ptr[0x10000]); + if((g_c035_shadow_reg & 0x12) == 0 || (g_c035_shadow_reg & 0x8) == 0) { + mem0wr += BANK_SHADOW2; + } + for(j = 0x20; j < 0x40; j++) { + SET_PAGE_INFO_WR(0x100 + j, mem0wr + j*0x100); + } } -void -fixup_shadow_hires2() -{ - byte *mem0wr; - int j; +void fixup_shadow_hires2() { + byte *mem0wr; + int j; - /* bank 0 */ - mem0wr = &(g_memory_ptr[0x00000]); - if(RAMWRT) { - mem0wr += 0x10000; - if((g_c035_shadow_reg & 0x14) == 0 || - (g_c035_shadow_reg & 0x8) == 0) { - mem0wr += BANK_SHADOW2; - } - } else if((g_c035_shadow_reg & 0x04) == 0) { - mem0wr += BANK_SHADOW; - } - for(j = 0x40; j < 0x60; j++) { - SET_PAGE_INFO_WR(j, mem0wr + j*0x100); - } + /* bank 0 */ + mem0wr = &(g_memory_ptr[0x00000]); + if(RAMWRT) { + mem0wr += 0x10000; + if((g_c035_shadow_reg & 0x14) == 0 || + (g_c035_shadow_reg & 0x8) == 0) { + mem0wr += BANK_SHADOW2; + } + } else if((g_c035_shadow_reg & 0x04) == 0) { + mem0wr += BANK_SHADOW; + } + for(j = 0x40; j < 0x60; j++) { + SET_PAGE_INFO_WR(j, mem0wr + j*0x100); + } - /* and bank 1 */ - mem0wr = &(g_memory_ptr[0x10000]); - if((g_c035_shadow_reg & 0x14) == 0 || (g_c035_shadow_reg & 0x8) == 0) { - mem0wr += BANK_SHADOW2; - } - for(j = 0x40; j < 0x60; j++) { - SET_PAGE_INFO_WR(0x100 + j, mem0wr + j*0x100); - } + /* and bank 1 */ + mem0wr = &(g_memory_ptr[0x10000]); + if((g_c035_shadow_reg & 0x14) == 0 || (g_c035_shadow_reg & 0x8) == 0) { + mem0wr += BANK_SHADOW2; + } + for(j = 0x40; j < 0x60; j++) { + SET_PAGE_INFO_WR(0x100 + j, mem0wr + j*0x100); + } } -void -fixup_shadow_shr() -{ - byte *mem0wr; - int j; +void fixup_shadow_shr() { + byte *mem0wr; + int j; - /* bank 0, only pages 0x60 - 0xa0 */ - mem0wr = &(g_memory_ptr[0x00000]); - if(RAMWRT) { - mem0wr += 0x10000; - if((g_c035_shadow_reg & 0x8) == 0) { - mem0wr += BANK_SHADOW2; - } - } - for(j = 0x60; j < 0xa0; j++) { - SET_PAGE_INFO_WR(j, mem0wr + j*0x100); - } + /* bank 0, only pages 0x60 - 0xa0 */ + mem0wr = &(g_memory_ptr[0x00000]); + if(RAMWRT) { + mem0wr += 0x10000; + if((g_c035_shadow_reg & 0x8) == 0) { + mem0wr += BANK_SHADOW2; + } + } + for(j = 0x60; j < 0xa0; j++) { + SET_PAGE_INFO_WR(j, mem0wr + j*0x100); + } - /* and bank 1, only pages 0x60 - 0xa0 */ - mem0wr = &(g_memory_ptr[0x10000]); - if((g_c035_shadow_reg & 0x8) == 0) { - mem0wr += BANK_SHADOW2; - } - for(j = 0x60; j < 0xa0; j++) { - SET_PAGE_INFO_WR(0x100 + j, mem0wr + j*0x100); - } + /* and bank 1, only pages 0x60 - 0xa0 */ + mem0wr = &(g_memory_ptr[0x10000]); + if((g_c035_shadow_reg & 0x8) == 0) { + mem0wr += BANK_SHADOW2; + } + for(j = 0x60; j < 0xa0; j++) { + SET_PAGE_INFO_WR(0x100 + j, mem0wr + j*0x100); + } } -void -fixup_shadow_iolc() -{ - byte *mem0rd; - int k; +void fixup_shadow_iolc() { + byte *mem0rd; + int k; - if(g_c035_shadow_reg & 0x40) { - /* Disable language card area */ - for(k = 0; k < 2; k++) { - mem0rd = &(g_memory_ptr[k << 16]); - fixup_any_bank_any_page((k << 8) + 0xc0, 0x10, - mem0rd + 0xd000, mem0rd + 0xd000); - if(k == 0 && ALTZP) { - mem0rd += 0x10000; - } - fixup_any_bank_any_page((k << 8) + 0xd0, 0x10, - mem0rd + 0xc000, mem0rd + 0xc000); - fixup_any_bank_any_page((k << 8) + 0xe0, 0x20, - mem0rd + 0xe000, mem0rd + 0xe000); - } - } else { - /* 0xc000 area */ - fixup_intcx(); + if(g_c035_shadow_reg & 0x40) { + /* Disable language card area */ + for(k = 0; k < 2; k++) { + mem0rd = &(g_memory_ptr[k << 16]); + fixup_any_bank_any_page((k << 8) + 0xc0, 0x10, + mem0rd + 0xd000, mem0rd + 0xd000); + if(k == 0 && ALTZP) { + mem0rd += 0x10000; + } + fixup_any_bank_any_page((k << 8) + 0xd0, 0x10, + mem0rd + 0xc000, mem0rd + 0xc000); + fixup_any_bank_any_page((k << 8) + 0xe0, 0x20, + mem0rd + 0xe000, mem0rd + 0xe000); + } + } else { + /* 0xc000 area */ + fixup_intcx(); - /* 0xd000 area */ - /* fixup_lcbank2(); -- not needed since fixup_rdrom does it */ + /* 0xd000 area */ + /* fixup_lcbank2(); -- not needed since fixup_rdrom does it */ - /* Fix 0xd000-0xffff for reads, banks 0 and 1 */ - fixup_rdrom(); /* which calls fixup_lcbank2 */ + /* Fix 0xd000-0xffff for reads, banks 0 and 1 */ + fixup_rdrom(); /* which calls fixup_lcbank2 */ - /* Fix 0xd000-0xffff for writes, banks 0 and 1 */ - fixup_wrdefram(g_c08x_wrdefram); - } + /* Fix 0xd000-0xffff for writes, banks 0 and 1 */ + fixup_wrdefram(g_c08x_wrdefram); + } } -void -update_shadow_reg(int val) -{ - int _xor; +void update_shadow_reg(int val) { + int _xor; - if(g_c035_shadow_reg == val) { - return; - } + if(g_c035_shadow_reg == val) { + return; + } - _xor = g_c035_shadow_reg ^ val; - g_c035_shadow_reg = val; + _xor = g_c035_shadow_reg ^ val; + g_c035_shadow_reg = val; - if(_xor & 8) { - fixup_shadow_hires1(); - fixup_shadow_hires2(); - fixup_shadow_shr(); - _xor = _xor & (~0x16); - } - if(_xor & 0x10) { - fixup_shadow_hires1(); - fixup_shadow_hires2(); - _xor = _xor & (~0x6); - } - if(_xor & 2) { - fixup_shadow_hires1(); - } - if(_xor & 4) { - fixup_shadow_hires2(); - } - if(_xor & 1) { - fixup_shadow_txt1(); - } - if((_xor & 0x20) && ((g_rom_version >= 3) || g_user_page2_shadow)) { - fixup_shadow_txt2(); - } - if(_xor & 0x40) { - fixup_shadow_iolc(); - } - if(_xor) { - fixup_brks(); - } + if(_xor & 8) { + fixup_shadow_hires1(); + fixup_shadow_hires2(); + fixup_shadow_shr(); + _xor = _xor & (~0x16); + } + if(_xor & 0x10) { + fixup_shadow_hires1(); + fixup_shadow_hires2(); + _xor = _xor & (~0x6); + } + if(_xor & 2) { + fixup_shadow_hires1(); + } + if(_xor & 4) { + fixup_shadow_hires2(); + } + if(_xor & 1) { + fixup_shadow_txt1(); + } + if((_xor & 0x20) && ((g_rom_version >= 3) || g_user_page2_shadow)) { + fixup_shadow_txt2(); + } + if(_xor & 0x40) { + fixup_shadow_iolc(); + } + if(_xor) { + fixup_brks(); + } } -void -fixup_shadow_all_banks() -{ - byte *mem0rd; - int shadow; - int num_banks; - int j, k; +void fixup_shadow_all_banks() { + byte *mem0rd; + int shadow; + int num_banks; + int j, k; - /* Assume Ninja Force Megademo */ - /* only do banks 3 - num_banks by 2, shadowing into e1 */ + /* Assume Ninja Force Megademo */ + /* only do banks 3 - num_banks by 2, shadowing into e1 */ - shadow = 0; - if((g_c036_val_speed & 0x10) && ((g_c035_shadow_reg & 0x08) == 0)) { - shadow = BANK_SHADOW2; - } - num_banks = g_mem_size_total >> 16; - for(k = 3; k < num_banks; k += 2) { - mem0rd = &(g_memory_ptr[k*0x10000 + 0x2000]) + shadow; - for(j = 0x20; j < 0xa0; j++) { - SET_PAGE_INFO_WR(k*0x100 + j, mem0rd); - mem0rd += 0x100; - } - } + shadow = 0; + if((g_c036_val_speed & 0x10) && ((g_c035_shadow_reg & 0x08) == 0)) { + shadow = BANK_SHADOW2; + } + num_banks = g_mem_size_total >> 16; + for(k = 3; k < num_banks; k += 2) { + mem0rd = &(g_memory_ptr[k*0x10000 + 0x2000]) + shadow; + for(j = 0x20; j < 0xa0; j++) { + SET_PAGE_INFO_WR(k*0x100 + j, mem0rd); + mem0rd += 0x100; + } + } - fixup_brks(); + fixup_brks(); } -void -setup_pageinfo() -{ - byte *mem0rd; - word32 mem_size_pages; +void setup_pageinfo() { + byte *mem0rd; + word32 mem_size_pages; - /* first, set all of memory to point to itself */ + /* first, set all of memory to point to itself */ - mem_size_pages = g_mem_size_total >> 8; - mem0rd = &(g_memory_ptr[0]); - fixup_any_bank_any_page(0, mem_size_pages, mem0rd, mem0rd); + mem_size_pages = g_mem_size_total >> 8; + mem0rd = &(g_memory_ptr[0]); + fixup_any_bank_any_page(0, mem_size_pages, mem0rd, mem0rd); - /* mark unused memory as BAD_MEM */ - fixup_any_bank_any_page(mem_size_pages, 0xfc00-mem_size_pages, - BANK_BAD_MEM, BANK_BAD_MEM); + /* mark unused memory as BAD_MEM */ + fixup_any_bank_any_page(mem_size_pages, 0xfc00-mem_size_pages, + BANK_BAD_MEM, BANK_BAD_MEM); - fixup_shadow_all_banks(); + fixup_shadow_all_banks(); - /* ROM */ - mem0rd = &(g_rom_fc_ff_ptr[0]); - fixup_any_bank_any_page(0xfc00, 0x400, mem0rd, - mem0rd + (BANK_IO_TMP | BANK_IO2_TMP)); + /* ROM */ + mem0rd = &(g_rom_fc_ff_ptr[0]); + fixup_any_bank_any_page(0xfc00, 0x400, mem0rd, + mem0rd + (BANK_IO_TMP | BANK_IO2_TMP)); - /* banks e0, e1 */ - mem0rd = &(g_slow_memory_ptr[0]); - fixup_any_bank_any_page(0xe000, 0x04, mem0rd + 0x0000, mem0rd + 0x0000); - fixup_any_bank_any_page(0xe004, 0x08, mem0rd + 0x0400, - mem0rd + 0x0400 + BANK_SHADOW); - fixup_any_bank_any_page(0xe00c, 0x14, mem0rd + 0x0c00, mem0rd + 0x0c00); - fixup_any_bank_any_page(0xe020, 0x40, mem0rd + 0x2000, - mem0rd + 0x2000 + BANK_SHADOW); - fixup_any_bank_any_page(0xe060, 0xa0, mem0rd + 0x6000, mem0rd + 0x6000); + /* banks e0, e1 */ + mem0rd = &(g_slow_memory_ptr[0]); + fixup_any_bank_any_page(0xe000, 0x04, mem0rd + 0x0000, mem0rd + 0x0000); + fixup_any_bank_any_page(0xe004, 0x08, mem0rd + 0x0400, + mem0rd + 0x0400 + BANK_SHADOW); + fixup_any_bank_any_page(0xe00c, 0x14, mem0rd + 0x0c00, mem0rd + 0x0c00); + fixup_any_bank_any_page(0xe020, 0x40, mem0rd + 0x2000, + mem0rd + 0x2000 + BANK_SHADOW); + fixup_any_bank_any_page(0xe060, 0xa0, mem0rd + 0x6000, mem0rd + 0x6000); - mem0rd = &(g_slow_memory_ptr[0x10000]); - fixup_any_bank_any_page(0xe100, 0x04, mem0rd + 0x0000, mem0rd + 0x0000); - fixup_any_bank_any_page(0xe104, 0x08, mem0rd + 0x0400, - mem0rd + 0x0400 + BANK_SHADOW2); - fixup_any_bank_any_page(0xe10c, 0x14, mem0rd + 0x0c00, mem0rd + 0x0c00); - fixup_any_bank_any_page(0xe120, 0x80, mem0rd + 0x2000, - mem0rd + 0x2000 + BANK_SHADOW2); - fixup_any_bank_any_page(0xe1a0, 0x60, mem0rd + 0xa000, mem0rd + 0xa000); + mem0rd = &(g_slow_memory_ptr[0x10000]); + fixup_any_bank_any_page(0xe100, 0x04, mem0rd + 0x0000, mem0rd + 0x0000); + fixup_any_bank_any_page(0xe104, 0x08, mem0rd + 0x0400, + mem0rd + 0x0400 + BANK_SHADOW2); + fixup_any_bank_any_page(0xe10c, 0x14, mem0rd + 0x0c00, mem0rd + 0x0c00); + fixup_any_bank_any_page(0xe120, 0x80, mem0rd + 0x2000, + mem0rd + 0x2000 + BANK_SHADOW2); + fixup_any_bank_any_page(0xe1a0, 0x60, mem0rd + 0xa000, mem0rd + 0xa000); - fixup_intcx(); /* correct banks 0xe0,0xe1, 0xc000-0xcfff area */ - fixup_lcbank2(); /* correct 0xd000-0xdfff area */ + fixup_intcx(); /* correct banks 0xe0,0xe1, 0xc000-0xcfff area */ + fixup_lcbank2(); /* correct 0xd000-0xdfff area */ - fixup_bank0_2000_4000(); - fixup_bank0_0400_0800(); - fixup_wrdefram(g_c08x_wrdefram); - fixup_altzp(); - fixup_ramrd(); - fixup_ramwrt(); - fixup_rdrom(); - fixup_shadow_txt1(); - fixup_shadow_txt2(); - fixup_shadow_hires1(); - fixup_shadow_hires2(); - fixup_shadow_shr(); - fixup_shadow_iolc(); - fixup_brks(); + fixup_bank0_2000_4000(); + fixup_bank0_0400_0800(); + fixup_wrdefram(g_c08x_wrdefram); + fixup_altzp(); + fixup_ramrd(); + fixup_ramwrt(); + fixup_rdrom(); + fixup_shadow_txt1(); + fixup_shadow_txt2(); + fixup_shadow_hires1(); + fixup_shadow_hires2(); + fixup_shadow_shr(); + fixup_shadow_iolc(); + fixup_brks(); -#ifdef TRANSWARP // OG adding Transwarp code - SET_PAGE_INFO_RD(0xBCFF,transwarpcode); +#ifdef TRANSWARP // OG adding Transwarp code + SET_PAGE_INFO_RD(0xBCFF,transwarpcode); #endif } -void -show_bankptrs_bank0rdwr() -{ - show_bankptrs(0); - show_bankptrs(1); - show_bankptrs(0xe0); - show_bankptrs(0xe1); - printf("statereg: %02x\n", g_c068_statereg); +void show_bankptrs_bank0rdwr() { + show_bankptrs(0); + show_bankptrs(1); + show_bankptrs(0xe0); + show_bankptrs(0xe1); + printf("statereg: %02x\n", g_c068_statereg); } -void -show_bankptrs(int bnk) -{ - int i; - Pg_info rd, wr; - byte *ptr_rd, *ptr_wr; +void show_bankptrs(int bnk) { + int i; + Pg_info rd, wr; + byte *ptr_rd, *ptr_wr; - printf("g_memory_ptr: %p, dummy_mem: %p, slow_mem_ptr: %p\n", - g_memory_ptr, g_dummy_memory1_ptr, g_slow_memory_ptr); - printf("g_rom_fc_ff_ptr: %p\n", g_rom_fc_ff_ptr); + printf("g_memory_ptr: %p, dummy_mem: %p, slow_mem_ptr: %p\n", + g_memory_ptr, g_dummy_memory1_ptr, g_slow_memory_ptr); + printf("g_rom_fc_ff_ptr: %p\n", g_rom_fc_ff_ptr); - printf("Showing bank_info array for %02x\n", bnk); - for(i = 0; i < 256; i++) { - rd = GET_PAGE_INFO_RD(bnk*0x100 + i); - wr = GET_PAGE_INFO_WR(bnk*0x100 + i); - ptr_rd = (byte *)rd; - ptr_wr = (byte *)wr; - printf("%04x rd: ", bnk*256 + i); - show_addr(ptr_rd); - printf(" wr: "); - show_addr(ptr_wr); - printf("\n"); - } + printf("Showing bank_info array for %02x\n", bnk); + for(i = 0; i < 256; i++) { + rd = GET_PAGE_INFO_RD(bnk*0x100 + i); + wr = GET_PAGE_INFO_WR(bnk*0x100 + i); + ptr_rd = (byte *)rd; + ptr_wr = (byte *)wr; + printf("%04x rd: ", bnk*256 + i); + show_addr(ptr_rd); + printf(" wr: "); + show_addr(ptr_wr); + printf("\n"); + } } // function to get the pointer to memory where a gs page is located - db unsigned char * get_page_ptr_rd(int addr) { - Pg_info rd; - byte *ptr_rd; + Pg_info rd; + byte *ptr_rd; - int bank = ( addr & 0xFF0000 ) >> 16; // unnecessary conversion that we throw away below w/ "*0x100" - int page = ( addr & 0x00FF00 ) >> 8; + int bank = ( addr & 0xFF0000 ) >> 16; // unnecessary conversion that we throw away below w/ "*0x100" + int page = ( addr & 0x00FF00 ) >> 8; rd = GET_PAGE_INFO_RD(bank*0x100 + page); - ptr_rd = (byte *)rd; - //printf("ADDR %06x at pageptr %p\n",addr, ptr_rd); // seems to be correct. - /* code */ - return rd; + ptr_rd = (byte *)rd; + //printf("ADDR %06x at pageptr %p\n",addr, ptr_rd); // seems to be correct. + /* code */ + return rd; } // function to get a byte from a 24bit address - db -int get_byte_at_address(int addr) -{ - unsigned char *page_ptr_rd = get_page_ptr_rd(addr); - int offset = addr & 0xff; - int mem_byte = page_ptr_rd[offset] & 0xff; - //printf("0x%06X = $%02X \t(dec) %d\n", addr, (int) mem_byte, (int) mem_byte); +int get_byte_at_address(int addr) { + unsigned char *page_ptr_rd = get_page_ptr_rd(addr); + int offset = addr & 0xff; + int mem_byte = page_ptr_rd[offset] & 0xff; + //printf("0x%06X = $%02X \t(dec) %d\n", addr, (int) mem_byte, (int) mem_byte); return (int) mem_byte; } // function to set a byte from a 24bit address - db -void set_byte_at_address(int addr, int value) -{ - unsigned char *page_ptr_rd = get_page_ptr_rd(addr); - int offset = addr & 0xff; - page_ptr_rd[offset] = value; +void set_byte_at_address(int addr, int value) { + unsigned char *page_ptr_rd = get_page_ptr_rd(addr); + int offset = addr & 0xff; + page_ptr_rd[offset] = value; } -void -show_addr(byte *ptr) -{ - word32 mem_size; +void show_addr(byte *ptr) { + word32 mem_size; - mem_size = g_mem_size_total; - if(ptr >= g_memory_ptr && ptr < &g_memory_ptr[mem_size]) { - printf("%p--memory[%06x]", ptr, - (word32)(ptr - g_memory_ptr)); - } else if(ptr >= g_rom_fc_ff_ptr && ptr < &g_rom_fc_ff_ptr[256*1024]) { - printf("%p--rom_fc_ff[%06x]", ptr, - (word32)(ptr - g_rom_fc_ff_ptr)); - } else if(ptr >= g_slow_memory_ptr && ptr<&g_slow_memory_ptr[128*1024]){ - printf("%p--slow_memory[%06x]", ptr, - (word32)(ptr - g_slow_memory_ptr)); - } else if(ptr >=g_dummy_memory1_ptr && ptr < &g_dummy_memory1_ptr[256]){ - printf("%p--dummy_memory[%06x]", ptr, - (word32)(ptr - g_dummy_memory1_ptr)); - } else { - printf("%p--unknown", ptr); - } + mem_size = g_mem_size_total; + if(ptr >= g_memory_ptr && ptr < &g_memory_ptr[mem_size]) { + printf("%p--memory[%06x]", ptr, + (word32)(ptr - g_memory_ptr)); + } else if(ptr >= g_rom_fc_ff_ptr && ptr < &g_rom_fc_ff_ptr[256*1024]) { + printf("%p--rom_fc_ff[%06x]", ptr, + (word32)(ptr - g_rom_fc_ff_ptr)); + } else if(ptr >= g_slow_memory_ptr && ptr<&g_slow_memory_ptr[128*1024]) { + printf("%p--slow_memory[%06x]", ptr, + (word32)(ptr - g_slow_memory_ptr)); + } else if(ptr >=g_dummy_memory1_ptr && ptr < &g_dummy_memory1_ptr[256]) { + printf("%p--dummy_memory[%06x]", ptr, + (word32)(ptr - g_dummy_memory1_ptr)); + } else { + printf("%p--unknown", ptr); + } } -#define CALC_DCYCS_FROM_CYC_PTR(dcycs, cyc_ptr, fcyc, new_fcyc) \ - dcycs = g_last_vbl_dcycs + *cyc_ptr; +#define CALC_DCYCS_FROM_CYC_PTR(dcycs, cyc_ptr, fcyc, new_fcyc) \ + dcycs = g_last_vbl_dcycs + *cyc_ptr; -int -io_read(word32 loc, double *cyc_ptr) -{ - double dcycs; - word64 word64_tmp; - word32 mask; - int new_lcbank2; - int new_wrdefram; - int tmp; - int i; +int io_read(word32 loc, double *cyc_ptr) { + double dcycs; + word64 word64_tmp; + word32 mask; + int new_lcbank2; + int new_wrdefram; + int tmp; + int i; - CALC_DCYCS_FROM_CYC_PTR(dcycs, cyc_ptr, fcyc, new_fcyc); + CALC_DCYCS_FROM_CYC_PTR(dcycs, cyc_ptr, fcyc, new_fcyc); /* IO space */ - switch((loc >> 8) & 0xf) { - case 0: /* 0xc000 - 0xc0ff */ - switch(loc & 0xff) { - /* 0xc000 - 0xc00f */ - case 0x00: case 0x01: case 0x02: case 0x03: - case 0x04: case 0x05: case 0x06: case 0x07: - case 0x08: case 0x09: case 0x0a: case 0x0b: - case 0x0c: case 0x0d: case 0x0e: case 0x0f: - return(adb_read_c000()); + switch((loc >> 8) & 0xf) { + case 0: /* 0xc000 - 0xc0ff */ + switch(loc & 0xff) { + /* 0xc000 - 0xc00f */ + case 0x00: case 0x01: case 0x02: case 0x03: + case 0x04: case 0x05: case 0x06: case 0x07: + case 0x08: case 0x09: case 0x0a: case 0x0b: + case 0x0c: case 0x0d: case 0x0e: case 0x0f: + return(adb_read_c000()); - /* 0xc010 - 0xc01f */ - case 0x10: /* c010 */ - return(adb_access_c010()); - case 0x11: /* c011 = RDLCBANK2 */ - return IOR(LCBANK2); - case 0x12: /* c012= RDLCRAM */ - return IOR(!RDROM); - case 0x13: /* c013=rdramd */ - return IOR(RAMRD); - case 0x14: /* c014=rdramwrt */ - return IOR(RAMWRT); - case 0x15: /* c015 = INTCX */ - return IOR(INTCX); - case 0x16: /* c016: ALTZP */ - return IOR(ALTZP); - case 0x17: /* c017: rdc3rom */ - return IOR(g_c02d_int_crom & (1 << 3)); - case 0x18: /* c018: rd80c0l */ - return IOR((g_cur_a2_stat & ALL_STAT_ST80)); - case 0x19: /* c019: rdvblbar */ - tmp = in_vblank(dcycs); - return IOR(tmp); - case 0x1a: /* c01a: rdtext */ - return IOR(g_cur_a2_stat & ALL_STAT_TEXT); - case 0x1b: /* c01b: rdmix */ - return IOR(g_cur_a2_stat & ALL_STAT_MIX_T_GR); - case 0x1c: /* c01c: rdpage2 */ - return IOR(g_cur_a2_stat & ALL_STAT_PAGE2); - case 0x1d: /* c01d: rdhires */ - return IOR(g_cur_a2_stat & ALL_STAT_HIRES); - case 0x1e: /* c01e: altcharset on? */ - return IOR(g_cur_a2_stat & ALL_STAT_ALTCHARSET); - case 0x1f: /* c01f: rd80vid */ - return IOR(g_cur_a2_stat & ALL_STAT_VID80); + /* 0xc010 - 0xc01f */ + case 0x10: /* c010 */ + return(adb_access_c010()); + case 0x11: /* c011 = RDLCBANK2 */ + return IOR(LCBANK2); + case 0x12: /* c012= RDLCRAM */ + return IOR(!RDROM); + case 0x13: /* c013=rdramd */ + return IOR(RAMRD); + case 0x14: /* c014=rdramwrt */ + return IOR(RAMWRT); + case 0x15: /* c015 = INTCX */ + return IOR(INTCX); + case 0x16: /* c016: ALTZP */ + return IOR(ALTZP); + case 0x17: /* c017: rdc3rom */ + return IOR(g_c02d_int_crom & (1 << 3)); + case 0x18: /* c018: rd80c0l */ + return IOR((g_cur_a2_stat & ALL_STAT_ST80)); + case 0x19: /* c019: rdvblbar */ + tmp = in_vblank(dcycs); + return IOR(tmp); + case 0x1a: /* c01a: rdtext */ + return IOR(g_cur_a2_stat & ALL_STAT_TEXT); + case 0x1b: /* c01b: rdmix */ + return IOR(g_cur_a2_stat & ALL_STAT_MIX_T_GR); + case 0x1c: /* c01c: rdpage2 */ + return IOR(g_cur_a2_stat & ALL_STAT_PAGE2); + case 0x1d: /* c01d: rdhires */ + return IOR(g_cur_a2_stat & ALL_STAT_HIRES); + case 0x1e: /* c01e: altcharset on? */ + return IOR(g_cur_a2_stat & ALL_STAT_ALTCHARSET); + case 0x1f: /* c01f: rd80vid */ + return IOR(g_cur_a2_stat & ALL_STAT_VID80); - /* 0xc020 - 0xc02f */ - case 0x20: /* 0xc020 */ - /* Click cassette port */ - return float_bus(dcycs); - case 0x21: /* 0xc021 */ - /* Not documented, but let's return COLOR_C021 */ - return IOR(g_cur_a2_stat & ALL_STAT_COLOR_C021); - case 0x22: /* 0xc022 */ - return (g_cur_a2_stat >> BIT_ALL_STAT_BG_COLOR) & 0xff; - case 0x23: /* 0xc023 */ - return g_c023_val; - case 0x24: /* 0xc024 */ - return mouse_read_c024(dcycs); - case 0x25: /* 0xc025 */ - return adb_read_c025(); - case 0x26: /* 0xc026 */ - return adb_read_c026(); - case 0x27: /* 0xc027 */ - return adb_read_c027(); - case 0x28: /* 0xc028 */ - UNIMPL_READ; - case 0x29: /* 0xc029 */ - return((g_cur_a2_stat & 0xa0) | g_c029_val_some); - case 0x2a: /* 0xc02a */ + /* 0xc020 - 0xc02f */ + case 0x20: /* 0xc020 */ + /* Click cassette port */ + return float_bus(dcycs); + case 0x21: /* 0xc021 */ + /* Not documented, but let's return COLOR_C021 */ + return IOR(g_cur_a2_stat & ALL_STAT_COLOR_C021); + case 0x22: /* 0xc022 */ + return (g_cur_a2_stat >> BIT_ALL_STAT_BG_COLOR) & 0xff; + case 0x23: /* 0xc023 */ + return g_c023_val; + case 0x24: /* 0xc024 */ + return mouse_read_c024(dcycs); + case 0x25: /* 0xc025 */ + return adb_read_c025(); + case 0x26: /* 0xc026 */ + return adb_read_c026(); + case 0x27: /* 0xc027 */ + return adb_read_c027(); + case 0x28: /* 0xc028 */ + UNIMPL_READ; + case 0x29: /* 0xc029 */ + return((g_cur_a2_stat & 0xa0) | g_c029_val_some); + case 0x2a: /* 0xc02a */ #if 0 - printf("Reading c02a...returning 0\n"); + printf("Reading c02a...returning 0\n"); #endif - return 0; - case 0x2b: /* 0xc02b */ - return g_c02b_val; - case 0x2c: /* 0xc02c */ - /* printf("reading c02c, returning 0\n"); */ - return 0; - case 0x2d: /* 0xc02d */ - tmp = g_c02d_int_crom; - return tmp; - case 0x2e: /* 0xc02e */ - case 0x2f: /* 0xc02f */ - return read_vid_counters(loc, dcycs); + return 0; + case 0x2b: /* 0xc02b */ + return g_c02b_val; + case 0x2c: /* 0xc02c */ + /* printf("reading c02c, returning 0\n"); */ + return 0; + case 0x2d: /* 0xc02d */ + tmp = g_c02d_int_crom; + return tmp; + case 0x2e: /* 0xc02e */ + case 0x2f: /* 0xc02f */ + return read_vid_counters(loc, dcycs); - /* 0xc030 - 0xc03f */ - case 0x30: /* 0xc030 */ - /* click speaker */ - doc_read_c030(dcycs); - return float_bus(dcycs); - case 0x31: /* 0xc031 */ - /* 3.5" control */ - return g_c031_disk35; - case 0x32: /* 0xc032 */ - /* scan int */ - return 0; - case 0x33: /* 0xc033 = CLOCKDATA*/ - return g_c033_data; - case 0x34: /* 0xc034 = CLOCKCTL */ - return g_c034_val; - case 0x35: /* 0xc035 */ - return g_c035_shadow_reg; - case 0x36: /* 0xc036 = CYAREG */ - return g_c036_val_speed; - case 0x37: /* 0xc037 */ - return 0; - case 0x38: /* 0xc038 */ - return scc_read_reg(1, dcycs); - case 0x39: /* 0xc039 */ - return scc_read_reg(0, dcycs); - case 0x3a: /* 0xc03a */ - return scc_read_data(1, dcycs); - case 0x3b: /* 0xc03b */ - return scc_read_data(0, dcycs); - case 0x3c: /* 0xc03c */ - /* doc control */ - return doc_read_c03c(dcycs); - case 0x3d: /* 0xc03d */ - return doc_read_c03d(dcycs); - case 0x3e: /* 0xc03e */ - return (g_c03ef_doc_ptr & 0xff); - case 0x3f: /* 0xc03f */ - return (g_c03ef_doc_ptr >> 8); + /* 0xc030 - 0xc03f */ + case 0x30: /* 0xc030 */ + /* click speaker */ + doc_read_c030(dcycs); + return float_bus(dcycs); + case 0x31: /* 0xc031 */ + /* 3.5" control */ + return g_c031_disk35; + case 0x32: /* 0xc032 */ + /* scan int */ + return 0; + case 0x33: /* 0xc033 = CLOCKDATA*/ + return g_c033_data; + case 0x34: /* 0xc034 = CLOCKCTL */ + return g_c034_val; + case 0x35: /* 0xc035 */ + return g_c035_shadow_reg; + case 0x36: /* 0xc036 = CYAREG */ + return g_c036_val_speed; + case 0x37: /* 0xc037 */ + return 0; + case 0x38: /* 0xc038 */ + return scc_read_reg(1, dcycs); + case 0x39: /* 0xc039 */ + return scc_read_reg(0, dcycs); + case 0x3a: /* 0xc03a */ + return scc_read_data(1, dcycs); + case 0x3b: /* 0xc03b */ + return scc_read_data(0, dcycs); + case 0x3c: /* 0xc03c */ + /* doc control */ + return doc_read_c03c(dcycs); + case 0x3d: /* 0xc03d */ + return doc_read_c03d(dcycs); + case 0x3e: /* 0xc03e */ + return (g_c03ef_doc_ptr & 0xff); + case 0x3f: /* 0xc03f */ + return (g_c03ef_doc_ptr >> 8); - /* 0xc040 - 0xc04f */ - case 0x40: /* 0xc040 */ - /* cassette */ - return 0; - case 0x41: /* 0xc041 */ - return g_c041_val; - case 0x44: /* 0xc044 */ - // SCC LAD A - return scc_read_lad(0); - case 0x45: /* 0xc045 */ - // SCC LAD B - return scc_read_lad(1); - case 0x46: /* 0xc046 */ - tmp = g_c046_val; - g_c046_val = (tmp & 0xbf) + ((tmp & 0x80) >> 1); - return tmp; - case 0x47: /* 0xc047 */ - remove_irq(IRQ_PENDING_C046_25SEC | - IRQ_PENDING_C046_VBL); - g_c046_val &= 0xe7; /* clear vbl_int, 1/4sec int*/ - return 0; - case 0x42: /* 0xc042 */ - case 0x43: /* 0xc043 */ - return 0; - case 0x4f: /* 0xc04f */ - /* for information on c04f, see: */ - /* www.sheppyware.net/tech/hardware/softswitches.html */ - /* write to $c04f to start. Then read $c04f to get */ - /* emulator ($16=sweet16, $fe=bernie II). */ - /* Then read again to get version: $21 == 2.1 */ - switch(g_em_emubyte_cnt) { - case 1: - g_em_emubyte_cnt = 2; - return 'G'; - case 2: - g_em_emubyte_cnt = 0; - tmp = g_gsplus_version_str[0] - '0'; - i = g_gsplus_version_str[2] - '0'; - return ((tmp & 0xf) << 4) + (i & 0xf); - default: - g_em_emubyte_cnt = 0; - return 0; - } - case 0x48: /* 0xc048 */ - case 0x49: /* 0xc049 */ - case 0x4a: /* 0xc04a */ - case 0x4b: /* 0xc04b */ - case 0x4c: /* 0xc04c */ - case 0x4d: /* 0xc04d */ - case 0x4e: /* 0xc04e */ - UNIMPL_READ; + /* 0xc040 - 0xc04f */ + case 0x40: /* 0xc040 */ + /* cassette */ + return 0; + case 0x41: /* 0xc041 */ + return g_c041_val; + case 0x44: /* 0xc044 */ + // SCC LAD A + return scc_read_lad(0); + case 0x45: /* 0xc045 */ + // SCC LAD B + return scc_read_lad(1); + case 0x46: /* 0xc046 */ + tmp = g_c046_val; + g_c046_val = (tmp & 0xbf) + ((tmp & 0x80) >> 1); + return tmp; + case 0x47: /* 0xc047 */ + remove_irq(IRQ_PENDING_C046_25SEC | + IRQ_PENDING_C046_VBL); + g_c046_val &= 0xe7; /* clear vbl_int, 1/4sec int*/ + return 0; + case 0x42: /* 0xc042 */ + case 0x43: /* 0xc043 */ + return 0; + case 0x4f: /* 0xc04f */ + /* for information on c04f, see: */ + /* www.sheppyware.net/tech/hardware/softswitches.html */ + /* write to $c04f to start. Then read $c04f to get */ + /* emulator ($16=sweet16, $fe=bernie II). */ + /* Then read again to get version: $21 == 2.1 */ + switch(g_em_emubyte_cnt) { + case 1: + g_em_emubyte_cnt = 2; + return 'G'; + case 2: + g_em_emubyte_cnt = 0; + tmp = g_gsplus_version_str[0] - '0'; + i = g_gsplus_version_str[2] - '0'; + return ((tmp & 0xf) << 4) + (i & 0xf); + default: + g_em_emubyte_cnt = 0; + return 0; + } + case 0x48: /* 0xc048 */ + case 0x49: /* 0xc049 */ + case 0x4a: /* 0xc04a */ + case 0x4b: /* 0xc04b */ + case 0x4c: /* 0xc04c */ + case 0x4d: /* 0xc04d */ + case 0x4e: /* 0xc04e */ + UNIMPL_READ; - /* 0xc050 - 0xc05f */ - case 0x50: /* 0xc050 */ - if(g_cur_a2_stat & ALL_STAT_TEXT) { - g_cur_a2_stat &= (~ALL_STAT_TEXT); - change_display_mode(dcycs); - } - return float_bus(dcycs); - case 0x51: /* 0xc051 */ - if((g_cur_a2_stat & ALL_STAT_TEXT) == 0) { - g_cur_a2_stat |= (ALL_STAT_TEXT); - change_display_mode(dcycs); - } - return float_bus(dcycs); - case 0x52: /* 0xc052 */ - if(g_cur_a2_stat & ALL_STAT_MIX_T_GR) { - g_cur_a2_stat &= (~ALL_STAT_MIX_T_GR); - change_display_mode(dcycs); - } - return float_bus(dcycs); - case 0x53: /* 0xc053 */ - if((g_cur_a2_stat & ALL_STAT_MIX_T_GR) == 0) { - g_cur_a2_stat |= (ALL_STAT_MIX_T_GR); - change_display_mode(dcycs); - } - return float_bus(dcycs); - case 0x54: /* 0xc054 */ - set_statereg(dcycs, g_c068_statereg & (~0x40)); - return float_bus(dcycs); - case 0x55: /* 0xc055 */ - set_statereg(dcycs, g_c068_statereg | 0x40); - return float_bus(dcycs); - case 0x56: /* 0xc056 */ - if(g_cur_a2_stat & ALL_STAT_HIRES) { - g_cur_a2_stat &= (~ALL_STAT_HIRES); - fixup_hires_on(); - change_display_mode(dcycs); - } - return float_bus(dcycs); - case 0x57: /* 0xc057 */ - if((g_cur_a2_stat & ALL_STAT_HIRES) == 0) { - g_cur_a2_stat |= (ALL_STAT_HIRES); - fixup_hires_on(); - change_display_mode(dcycs); - } - return float_bus(dcycs); - case 0x58: /* 0xc058 */ - if(g_zipgs_unlock < 4) { - g_c05x_annuncs &= (~1); - } - return 0; - case 0x59: /* 0xc059 */ - if(g_zipgs_unlock >= 4) { - return g_zipgs_reg_c059; - } else { - g_c05x_annuncs |= 1; - } - return 0; - case 0x5a: /* 0xc05a */ - if(g_zipgs_unlock >= 4) { - return g_zipgs_reg_c05a; - } else { - g_c05x_annuncs &= (~2); - } - return 0; - case 0x5b: /* 0xc05b */ - if(g_zipgs_unlock >= 4) { - word64_tmp = (word64)dcycs; - tmp = (word64_tmp >> 9) & 1; - return (tmp << 7) + (g_zipgs_reg_c05b & 0x7f); - } else { - g_c05x_annuncs |= 2; - } - return 0; - case 0x5c: /* 0xc05c */ - if(g_zipgs_unlock >= 4) { - return g_zipgs_reg_c05c; - } else { - g_c05x_annuncs &= (~4); - } - return 0; - case 0x5d: /* 0xc05d */ - if(g_zipgs_unlock >= 4) { - halt_printf("Reading ZipGS $c05d!\n"); - } else { - g_c05x_annuncs |= 4; - } - return 0; - case 0x5e: /* 0xc05e */ - if(g_zipgs_unlock >= 4) { - halt_printf("Reading ZipGS $c05e!\n"); - } else if(g_cur_a2_stat & ALL_STAT_ANNUNC3) { - g_cur_a2_stat &= (~ALL_STAT_ANNUNC3); - change_display_mode(dcycs); - } - return 0; - case 0x5f: /* 0xc05f */ - if(g_zipgs_unlock >= 4) { - halt_printf("Reading ZipGS $c05f!\n"); - } else if((g_cur_a2_stat & ALL_STAT_ANNUNC3) == 0) { - g_cur_a2_stat |= (ALL_STAT_ANNUNC3); - change_display_mode(dcycs); - } - return 0; + /* 0xc050 - 0xc05f */ + case 0x50: /* 0xc050 */ + if(g_cur_a2_stat & ALL_STAT_TEXT) { + g_cur_a2_stat &= (~ALL_STAT_TEXT); + change_display_mode(dcycs); + } + return float_bus(dcycs); + case 0x51: /* 0xc051 */ + if((g_cur_a2_stat & ALL_STAT_TEXT) == 0) { + g_cur_a2_stat |= (ALL_STAT_TEXT); + change_display_mode(dcycs); + } + return float_bus(dcycs); + case 0x52: /* 0xc052 */ + if(g_cur_a2_stat & ALL_STAT_MIX_T_GR) { + g_cur_a2_stat &= (~ALL_STAT_MIX_T_GR); + change_display_mode(dcycs); + } + return float_bus(dcycs); + case 0x53: /* 0xc053 */ + if((g_cur_a2_stat & ALL_STAT_MIX_T_GR) == 0) { + g_cur_a2_stat |= (ALL_STAT_MIX_T_GR); + change_display_mode(dcycs); + } + return float_bus(dcycs); + case 0x54: /* 0xc054 */ + set_statereg(dcycs, g_c068_statereg & (~0x40)); + return float_bus(dcycs); + case 0x55: /* 0xc055 */ + set_statereg(dcycs, g_c068_statereg | 0x40); + return float_bus(dcycs); + case 0x56: /* 0xc056 */ + if(g_cur_a2_stat & ALL_STAT_HIRES) { + g_cur_a2_stat &= (~ALL_STAT_HIRES); + fixup_hires_on(); + change_display_mode(dcycs); + } + return float_bus(dcycs); + case 0x57: /* 0xc057 */ + if((g_cur_a2_stat & ALL_STAT_HIRES) == 0) { + g_cur_a2_stat |= (ALL_STAT_HIRES); + fixup_hires_on(); + change_display_mode(dcycs); + } + return float_bus(dcycs); + case 0x58: /* 0xc058 */ + if(g_zipgs_unlock < 4) { + g_c05x_annuncs &= (~1); + } + return 0; + case 0x59: /* 0xc059 */ + if(g_zipgs_unlock >= 4) { + return g_zipgs_reg_c059; + } else { + g_c05x_annuncs |= 1; + } + return 0; + case 0x5a: /* 0xc05a */ + if(g_zipgs_unlock >= 4) { + return g_zipgs_reg_c05a; + } else { + g_c05x_annuncs &= (~2); + } + return 0; + case 0x5b: /* 0xc05b */ + if(g_zipgs_unlock >= 4) { + word64_tmp = (word64)dcycs; + tmp = (word64_tmp >> 9) & 1; + return (tmp << 7) + (g_zipgs_reg_c05b & 0x7f); + } else { + g_c05x_annuncs |= 2; + } + return 0; + case 0x5c: /* 0xc05c */ + if(g_zipgs_unlock >= 4) { + return g_zipgs_reg_c05c; + } else { + g_c05x_annuncs &= (~4); + } + return 0; + case 0x5d: /* 0xc05d */ + if(g_zipgs_unlock >= 4) { + halt_printf("Reading ZipGS $c05d!\n"); + } else { + g_c05x_annuncs |= 4; + } + return 0; + case 0x5e: /* 0xc05e */ + if(g_zipgs_unlock >= 4) { + halt_printf("Reading ZipGS $c05e!\n"); + } else if(g_cur_a2_stat & ALL_STAT_ANNUNC3) { + g_cur_a2_stat &= (~ALL_STAT_ANNUNC3); + change_display_mode(dcycs); + } + return 0; + case 0x5f: /* 0xc05f */ + if(g_zipgs_unlock >= 4) { + halt_printf("Reading ZipGS $c05f!\n"); + } else if((g_cur_a2_stat & ALL_STAT_ANNUNC3) == 0) { + g_cur_a2_stat |= (ALL_STAT_ANNUNC3); + change_display_mode(dcycs); + } + return 0; - /* 0xc060 - 0xc06f */ - case 0x60: /* 0xc060 */ - return IOR(g_paddle_buttons & 8); - case 0x61: /* 0xc061 */ - return IOR(adb_is_cmd_key_down() || - g_paddle_buttons & 1); - case 0x62: /* 0xc062 */ - return IOR(adb_is_option_key_down() || - g_paddle_buttons & 2); - case 0x63: /* 0xc063 */ - return IOR(g_paddle_buttons & 4); - case 0x64: /* 0xc064 */ - return read_paddles(dcycs, 0); - case 0x65: /* 0xc065 */ - return read_paddles(dcycs, 1); - case 0x66: /* 0xc066 */ - return read_paddles(dcycs, 2); - case 0x67: /* 0xc067 */ - return read_paddles(dcycs, 3); - case 0x68: /* 0xc068 = STATEREG */ - return g_c068_statereg; - case 0x69: /* 0xc069 */ - /* Reserved reg, return 0 */ - return 0; - // OG Transwarp Read Interface - #ifdef TRANSWARP - case 0x6a: /* 0xc06a */ - { - extern double g_zip_pmhz; - return (int)(g_zip_pmhz*1000)&0xFF; - } - case 0x6b: /* 0xc06b */ - { - extern double g_zip_pmhz; - return (((int)(g_zip_pmhz*1000))>>8)&0xFF; - } - case 0x6c: /* 0xc06c */ - { - extern double g_zip_pmhz; - if (g_zip_pmhz==1.0) return 0; // slow - else if (g_zip_pmhz>=2.6) return 2; // warp - else return 1; // zip - } - #else - case 0x6a: /* 0xc06a */ - case 0x6b: /* 0xc06b */ - case 0x6c: /* 0xc06c */ - #endif - case 0x6d: /* 0xc06d */ - case 0x6e: /* 0xc06e */ - case 0x6f: /* 0xc06f */ - UNIMPL_READ; + /* 0xc060 - 0xc06f */ + case 0x60: /* 0xc060 */ + return IOR(g_paddle_buttons & 8); + case 0x61: /* 0xc061 */ + return IOR(adb_is_cmd_key_down() || + g_paddle_buttons & 1); + case 0x62: /* 0xc062 */ + return IOR(adb_is_option_key_down() || + g_paddle_buttons & 2); + case 0x63: /* 0xc063 */ + return IOR(g_paddle_buttons & 4); + case 0x64: /* 0xc064 */ + return read_paddles(dcycs, 0); + case 0x65: /* 0xc065 */ + return read_paddles(dcycs, 1); + case 0x66: /* 0xc066 */ + return read_paddles(dcycs, 2); + case 0x67: /* 0xc067 */ + return read_paddles(dcycs, 3); + case 0x68: /* 0xc068 = STATEREG */ + return g_c068_statereg; + case 0x69: /* 0xc069 */ + /* Reserved reg, return 0 */ + return 0; + // OG Transwarp Read Interface + #ifdef TRANSWARP + case 0x6a: /* 0xc06a */ + { + extern double g_zip_pmhz; + return (int)(g_zip_pmhz*1000)&0xFF; + } + case 0x6b: /* 0xc06b */ + { + extern double g_zip_pmhz; + return (((int)(g_zip_pmhz*1000))>>8)&0xFF; + } + case 0x6c: /* 0xc06c */ + { + extern double g_zip_pmhz; + if (g_zip_pmhz==1.0) return 0; // slow + else if (g_zip_pmhz>=2.6) return 2; // warp + else return 1; // zip + } + #else + case 0x6a: /* 0xc06a */ + case 0x6b: /* 0xc06b */ + case 0x6c: /* 0xc06c */ + #endif + case 0x6d: /* 0xc06d */ + case 0x6e: /* 0xc06e */ + case 0x6f: /* 0xc06f */ + UNIMPL_READ; - /* 0xc070 - 0xc07f */ - case 0x70: /* c070 */ - paddle_trigger(dcycs); - return 0; - case 0x71: /* 0xc071 */ - case 0x72: case 0x73: - case 0x74: case 0x75: case 0x76: case 0x77: - case 0x78: case 0x79: case 0x7a: case 0x7b: - case 0x7c: case 0x7d: case 0x7e: case 0x7f: - return g_rom_fc_ff_ptr[3*65536 + 0xc000 + (loc & 0xff)]; + /* 0xc070 - 0xc07f */ + case 0x70: /* c070 */ + paddle_trigger(dcycs); + return 0; + case 0x71: /* 0xc071 */ + case 0x72: case 0x73: + case 0x74: case 0x75: case 0x76: case 0x77: + case 0x78: case 0x79: case 0x7a: case 0x7b: + case 0x7c: case 0x7d: case 0x7e: case 0x7f: + return g_rom_fc_ff_ptr[3*65536 + 0xc000 + (loc & 0xff)]; - /* 0xc080 - 0xc08f */ - case 0x80: case 0x81: case 0x82: case 0x83: - case 0x84: case 0x85: case 0x86: case 0x87: - case 0x88: case 0x89: case 0x8a: case 0x8b: - case 0x8c: case 0x8d: case 0x8e: case 0x8f: - new_lcbank2 = ((loc & 0x8) >> 1) ^ 0x4; - new_wrdefram = (loc & 1); - if(new_wrdefram != g_c08x_wrdefram) { - fixup_wrdefram(new_wrdefram); - } - switch(loc & 0x3) { - case 0x1: /* 0xc081 */ - case 0x2: /* 0xc082 */ - /* Read rom, set lcbank2 */ - set_statereg(dcycs, (g_c068_statereg & ~(0x04))| - (new_lcbank2 | 0x08)); - break; - case 0x0: /* 0xc080 */ - case 0x3: /* 0xc083 */ - /* Read ram (clear RDROM), set lcbank2 */ - set_statereg(dcycs, (g_c068_statereg & ~(0x0c))| - (new_lcbank2)); - break; - } - return float_bus(dcycs); - /* 0xc090 - 0xc09f */ - case 0x90: case 0x91: case 0x92: case 0x93: - case 0x94: case 0x95: case 0x96: case 0x97: - case 0x98: case 0x99: case 0x9a: case 0x9b: - case 0x9c: case 0x9d: case 0x9e: case 0x9f: - if (g_parallel) - { - return parallel_read((word16)loc & 0xf); - } - else - { - UNIMPL_READ; - } + /* 0xc080 - 0xc08f */ + case 0x80: case 0x81: case 0x82: case 0x83: + case 0x84: case 0x85: case 0x86: case 0x87: + case 0x88: case 0x89: case 0x8a: case 0x8b: + case 0x8c: case 0x8d: case 0x8e: case 0x8f: + new_lcbank2 = ((loc & 0x8) >> 1) ^ 0x4; + new_wrdefram = (loc & 1); + if(new_wrdefram != g_c08x_wrdefram) { + fixup_wrdefram(new_wrdefram); + } + switch(loc & 0x3) { + case 0x1: /* 0xc081 */ + case 0x2: /* 0xc082 */ + /* Read rom, set lcbank2 */ + set_statereg(dcycs, (g_c068_statereg & ~(0x04))| + (new_lcbank2 | 0x08)); + break; + case 0x0: /* 0xc080 */ + case 0x3: /* 0xc083 */ + /* Read ram (clear RDROM), set lcbank2 */ + set_statereg(dcycs, (g_c068_statereg & ~(0x0c))| + (new_lcbank2)); + break; + } + return float_bus(dcycs); + /* 0xc090 - 0xc09f */ + case 0x90: case 0x91: case 0x92: case 0x93: + case 0x94: case 0x95: case 0x96: case 0x97: + case 0x98: case 0x99: case 0x9a: case 0x9b: + case 0x9c: case 0x9d: case 0x9e: case 0x9f: + if (g_parallel) + { + return parallel_read((word16)loc & 0xf); + } + else + { + UNIMPL_READ; + } - /* 0xc0a0 - 0xc0af */ - case 0xa0: case 0xa1: case 0xa2: case 0xa3: - case 0xa4: case 0xa5: case 0xa6: case 0xa7: - case 0xa8: case 0xa9: case 0xaa: case 0xab: - case 0xac: case 0xad: case 0xae: case 0xaf: - return 0; - /* UNIMPL_READ; */ + /* 0xc0a0 - 0xc0af */ + case 0xa0: case 0xa1: case 0xa2: case 0xa3: + case 0xa4: case 0xa5: case 0xa6: case 0xa7: + case 0xa8: case 0xa9: case 0xaa: case 0xab: + case 0xac: case 0xad: case 0xae: case 0xaf: + return 0; + /* UNIMPL_READ; */ - /* 0xc0b0 - 0xc0bf */ - //case 0xb0: - /* c0b0: female voice tool033 look at this */ - // return 0; - //case 0xb1: case 0xb2: case 0xb3: - //case 0xb4: case 0xb5: case 0xb6: case 0xb7: - //case 0xb9: case 0xba: case 0xbb: - //case 0xbc: case 0xbd: case 0xbe: case 0xbf: - /* UNIMPL_READ; */ - // return 0; - /* c0b8: Second Sight card stuff: return 0 */ - //case 0xb8: - // return 0; - // break; + /* 0xc0b0 - 0xc0bf */ + //case 0xb0: + /* c0b0: female voice tool033 look at this */ + // return 0; + //case 0xb1: case 0xb2: case 0xb3: + //case 0xb4: case 0xb5: case 0xb6: case 0xb7: + //case 0xb9: case 0xba: case 0xbb: + //case 0xbc: case 0xbd: case 0xbe: case 0xbf: + /* UNIMPL_READ; */ + // return 0; + /* c0b8: Second Sight card stuff: return 0 */ + //case 0xb8: + // return 0; + // break; #ifdef HAVE_TFE - /*Uthernet read access on slot 3*/ - case 0xb0: - case 0xb1: - case 0xb2: - case 0xb3: - case 0xb4: - case 0xb5: - case 0xb6: - case 0xb7: - case 0xb8: - case 0xb9: - case 0xba: - case 0xbb: - case 0xbc: - case 0xbd: - case 0xbe: - case 0xbf: - if (tfe_enabled){ - return tfe_read((word16)loc & 0xf); - } - else - {return 0;} + /*Uthernet read access on slot 3*/ + case 0xb0: + case 0xb1: + case 0xb2: + case 0xb3: + case 0xb4: + case 0xb5: + case 0xb6: + case 0xb7: + case 0xb8: + case 0xb9: + case 0xba: + case 0xbb: + case 0xbc: + case 0xbd: + case 0xbe: + case 0xbf: + if (tfe_enabled) { + return tfe_read((word16)loc & 0xf); + } + else + {return 0;} #endif - /* 0xc0c0 - 0xc0cf */ - case 0xc0: case 0xc1: case 0xc2: case 0xc3: - case 0xc4: case 0xc5: case 0xc6: case 0xc7: - case 0xc8: case 0xc9: case 0xca: case 0xcb: - case 0xcc: case 0xcd: case 0xce: case 0xcf: - return 0; - /* 0xc0d0 - 0xc0df */ - case 0xd0: case 0xd1: case 0xd2: case 0xd3: - case 0xd4: case 0xd5: case 0xd6: case 0xd7: - case 0xd8: case 0xd9: case 0xda: case 0xdb: - case 0xdc: case 0xdd: case 0xde: case 0xdf: - return 0; - /* 0xc0e0 - 0xc0ef */ - case 0xe0: case 0xe1: case 0xe2: case 0xe3: - case 0xe4: case 0xe5: case 0xe6: case 0xe7: - case 0xe8: case 0xe9: case 0xea: case 0xeb: - case 0xed: case 0xee: case 0xef: - return read_iwm(loc, dcycs); - case 0xec: - return iwm_read_c0ec(dcycs); - /* 0xc0f0 - 0xc0ff */ - case 0xf0: case 0xf1: case 0xf2: case 0xf3: - case 0xf4: case 0xf5: case 0xf6: case 0xf7: - case 0xf8: case 0xf9: case 0xfa: case 0xfb: - case 0xfc: case 0xfd: case 0xfe: case 0xff: - return 0; + /* 0xc0c0 - 0xc0cf */ + case 0xc0: case 0xc1: case 0xc2: case 0xc3: + case 0xc4: case 0xc5: case 0xc6: case 0xc7: + case 0xc8: case 0xc9: case 0xca: case 0xcb: + case 0xcc: case 0xcd: case 0xce: case 0xcf: + return 0; + /* 0xc0d0 - 0xc0df */ + case 0xd0: case 0xd1: case 0xd2: case 0xd3: + case 0xd4: case 0xd5: case 0xd6: case 0xd7: + case 0xd8: case 0xd9: case 0xda: case 0xdb: + case 0xdc: case 0xdd: case 0xde: case 0xdf: + return 0; + /* 0xc0e0 - 0xc0ef */ + case 0xe0: case 0xe1: case 0xe2: case 0xe3: + case 0xe4: case 0xe5: case 0xe6: case 0xe7: + case 0xe8: case 0xe9: case 0xea: case 0xeb: + case 0xed: case 0xee: case 0xef: + return read_iwm(loc, dcycs); + case 0xec: + return iwm_read_c0ec(dcycs); + /* 0xc0f0 - 0xc0ff */ + case 0xf0: case 0xf1: case 0xf2: case 0xf3: + case 0xf4: case 0xf5: case 0xf6: case 0xf7: + case 0xf8: case 0xf9: case 0xfa: case 0xfb: + case 0xfc: case 0xfd: case 0xfe: case 0xff: + return 0; - default: - printf("loc: %04x bad\n", loc); - UNIMPL_READ; - } - case 1: case 2: case 3: case 4: case 5: case 6: - /* c100 - c6ff */ - mask = (1 << ((loc >> 8) & 7)); - if(INTCX || ((g_c02d_int_crom & mask) == 0)) { - return(g_rom_fc_ff_ptr[0x3c000 + (loc & 0xfff)]); - } - return float_bus(dcycs); - case 7: - /* c700 */ - if(INTCX || ((g_c02d_int_crom & (1 << 7)) == 0)) { - return(g_rom_fc_ff_ptr[0x3c000 + (loc & 0xfff)]); - } - tmp = g_rom_fc_ff_ptr[0x3c500 + (loc & 0xff)]; - if((loc & 0xff) == 0xfb) { - tmp = tmp & 0xbf; /* clear bit 6 for ROM 03 */ - } - return tmp; - case 8: case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0xe: - if(INTCX || ((g_c02d_int_crom & (1 << 3)) == 0)) { - return(g_rom_fc_ff_ptr[0x3c000 + (loc & 0xfff)]); - } - UNIMPL_READ; - case 0xf: - if(INTCX || ((g_c02d_int_crom & (1 << 3)) == 0)) { - return(g_rom_fc_ff_ptr[0x3c000 + (loc & 0xfff)]); - } - if((loc & 0xfff) == 0xfff) { - return g_rom_fc_ff_ptr[0x3cfff]; - } - UNIMPL_READ; - } + default: + printf("loc: %04x bad\n", loc); + UNIMPL_READ; + } + case 1: case 2: case 3: case 4: case 5: case 6: + /* c100 - c6ff */ + mask = (1 << ((loc >> 8) & 7)); + if(INTCX || ((g_c02d_int_crom & mask) == 0)) { + return(g_rom_fc_ff_ptr[0x3c000 + (loc & 0xfff)]); + } + return float_bus(dcycs); + case 7: + /* c700 */ + if(INTCX || ((g_c02d_int_crom & (1 << 7)) == 0)) { + return(g_rom_fc_ff_ptr[0x3c000 + (loc & 0xfff)]); + } + tmp = g_rom_fc_ff_ptr[0x3c500 + (loc & 0xff)]; + if((loc & 0xff) == 0xfb) { + tmp = tmp & 0xbf; /* clear bit 6 for ROM 03 */ + } + return tmp; + case 8: case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0xe: + if(INTCX || ((g_c02d_int_crom & (1 << 3)) == 0)) { + return(g_rom_fc_ff_ptr[0x3c000 + (loc & 0xfff)]); + } + UNIMPL_READ; + case 0xf: + if(INTCX || ((g_c02d_int_crom & (1 << 3)) == 0)) { + return(g_rom_fc_ff_ptr[0x3c000 + (loc & 0xfff)]); + } + if((loc & 0xfff) == 0xfff) { + return g_rom_fc_ff_ptr[0x3cfff]; + } + UNIMPL_READ; + } - halt_printf("io_read: hit end, loc: %06x\n", loc); + halt_printf("io_read: hit end, loc: %06x\n", loc); - return 0xff; + return 0xff; } -void -io_write(word32 loc, int val, double *cyc_ptr) -{ - double dcycs; - int new_tmp; - int new_lcbank2; - int new_wrdefram; - int tmp; - int fixup; +void io_write(word32 loc, int val, double *cyc_ptr) { + double dcycs; + int new_tmp; + int new_lcbank2; + int new_wrdefram; + int tmp; + int fixup; - CALC_DCYCS_FROM_CYC_PTR(dcycs, cyc_ptr, fcyc, new_fcyc); + CALC_DCYCS_FROM_CYC_PTR(dcycs, cyc_ptr, fcyc, new_fcyc); - val = val & 0xff; - switch((loc >> 8) & 0xf) { - case 0: /* 0xc000 - 0xc0ff */ - //printf ("ioaddress: %x", (loc & 0xf)); - switch(loc & 0xff) { - /* 0xc000 - 0xc00f */ - case 0x00: /* 0xc000 */ - if(g_cur_a2_stat & ALL_STAT_ST80) { - g_cur_a2_stat &= (~ALL_STAT_ST80); - fixup_st80col(dcycs); - } - return; - case 0x01: /* 0xc001 */ - if((g_cur_a2_stat & ALL_STAT_ST80) == 0) { - g_cur_a2_stat |= (ALL_STAT_ST80); - fixup_st80col(dcycs); - } - return; - case 0x02: /* 0xc002 */ - set_statereg(dcycs, g_c068_statereg & ~0x20); - return; - case 0x03: /* 0xc003 */ - set_statereg(dcycs, g_c068_statereg | 0x20); - return; - case 0x04: /* 0xc004 */ - set_statereg(dcycs, g_c068_statereg & ~0x10); - return; - case 0x05: /* 0xc005 */ - set_statereg(dcycs, g_c068_statereg | 0x10); - return; - case 0x06: /* 0xc006 */ - set_statereg(dcycs, g_c068_statereg & ~0x01); - return; - case 0x07: /* 0xc007 */ - set_statereg(dcycs, g_c068_statereg | 0x01); - return; - case 0x08: /* 0xc008 */ - set_statereg(dcycs, g_c068_statereg & ~0x80); - return; - case 0x09: /* 0xc009 */ - set_statereg(dcycs, g_c068_statereg | 0x80); - return; - case 0x0a: /* 0xc00a */ - tmp = 1 << 3; - if((g_c02d_int_crom & tmp) != 0) { - g_c02d_int_crom &= ~tmp; - fixup_intcx(); - } - return; - case 0x0b: /* 0xc00b */ - tmp = 1 << 3; - if((g_c02d_int_crom & tmp) == 0) { - g_c02d_int_crom |= tmp; - fixup_intcx(); - } - return; - case 0x0c: /* 0xc00c */ - if(g_cur_a2_stat & ALL_STAT_VID80) { - g_cur_a2_stat &= (~ALL_STAT_VID80); - change_display_mode(dcycs); - } - return; - case 0x0d: /* 0xc00d */ - if((g_cur_a2_stat & ALL_STAT_VID80) == 0) { - g_cur_a2_stat |= (ALL_STAT_VID80); - change_display_mode(dcycs); - } - return; - case 0x0e: /* 0xc00e */ - if(g_cur_a2_stat & ALL_STAT_ALTCHARSET) { - g_cur_a2_stat &= (~ALL_STAT_ALTCHARSET); - change_display_mode(dcycs); - } - return; - case 0x0f: /* 0xc00f */ - if((g_cur_a2_stat & ALL_STAT_ALTCHARSET) == 0) { - g_cur_a2_stat |= (ALL_STAT_ALTCHARSET); - change_display_mode(dcycs); - } - return; - /* 0xc010 - 0xc01f */ - case 0x10: case 0x11: case 0x12: case 0x13: - case 0x14: case 0x15: case 0x16: case 0x17: - case 0x18: case 0x19: case 0x1a: case 0x1b: - case 0x1c: case 0x1d: case 0x1e: case 0x1f: - adb_access_c010(); - return; - /* 0xc020 - 0xc02f */ - case 0x20: /* 0xc020 */ - /* WRITE CASSETTE?? */ - return; - case 0x21: /* 0xc021 */ - new_tmp = ((val >> 7) & 1) << - (31 - BIT_ALL_STAT_COLOR_C021); - if((g_cur_a2_stat & ALL_STAT_COLOR_C021) != new_tmp) { - g_cur_a2_stat ^= new_tmp; - change_display_mode(dcycs); - } - return; - case 0x22: /* 0xc022 */ - /* change text color */ - tmp = (g_cur_a2_stat >> BIT_ALL_STAT_BG_COLOR) & 0xff; - if(val != tmp) { - /* change text/bg color! */ - g_cur_a2_stat &= ~(ALL_STAT_TEXT_COLOR | - ALL_STAT_BG_COLOR); - g_cur_a2_stat += (val << BIT_ALL_STAT_BG_COLOR); - change_display_mode(dcycs); - } - return; - case 0x23: /* 0xc023 */ - if((val & 0x19) != 0) { - halt_printf("c023 write of %02x!!!\n", val); - } - tmp = (g_c023_val & 0x70) | (val & 0x0f); - if((tmp & 0x22) == 0x22) { - add_irq(IRQ_PENDING_C023_SCAN); - } - if(!(tmp & 2)) { - remove_irq(IRQ_PENDING_C023_SCAN); - } - if((tmp & 0x44) == 0x44) { - add_irq(IRQ_PENDING_C023_1SEC); - } - if(!(tmp & 0x4)) { - remove_irq(IRQ_PENDING_C023_1SEC); - } + val = val & 0xff; + switch((loc >> 8) & 0xf) { + case 0: /* 0xc000 - 0xc0ff */ + //printf ("ioaddress: %x", (loc & 0xf)); + switch(loc & 0xff) { + /* 0xc000 - 0xc00f */ + case 0x00: /* 0xc000 */ + if(g_cur_a2_stat & ALL_STAT_ST80) { + g_cur_a2_stat &= (~ALL_STAT_ST80); + fixup_st80col(dcycs); + } + return; + case 0x01: /* 0xc001 */ + if((g_cur_a2_stat & ALL_STAT_ST80) == 0) { + g_cur_a2_stat |= (ALL_STAT_ST80); + fixup_st80col(dcycs); + } + return; + case 0x02: /* 0xc002 */ + set_statereg(dcycs, g_c068_statereg & ~0x20); + return; + case 0x03: /* 0xc003 */ + set_statereg(dcycs, g_c068_statereg | 0x20); + return; + case 0x04: /* 0xc004 */ + set_statereg(dcycs, g_c068_statereg & ~0x10); + return; + case 0x05: /* 0xc005 */ + set_statereg(dcycs, g_c068_statereg | 0x10); + return; + case 0x06: /* 0xc006 */ + set_statereg(dcycs, g_c068_statereg & ~0x01); + return; + case 0x07: /* 0xc007 */ + set_statereg(dcycs, g_c068_statereg | 0x01); + return; + case 0x08: /* 0xc008 */ + set_statereg(dcycs, g_c068_statereg & ~0x80); + return; + case 0x09: /* 0xc009 */ + set_statereg(dcycs, g_c068_statereg | 0x80); + return; + case 0x0a: /* 0xc00a */ + tmp = 1 << 3; + if((g_c02d_int_crom & tmp) != 0) { + g_c02d_int_crom &= ~tmp; + fixup_intcx(); + } + return; + case 0x0b: /* 0xc00b */ + tmp = 1 << 3; + if((g_c02d_int_crom & tmp) == 0) { + g_c02d_int_crom |= tmp; + fixup_intcx(); + } + return; + case 0x0c: /* 0xc00c */ + if(g_cur_a2_stat & ALL_STAT_VID80) { + g_cur_a2_stat &= (~ALL_STAT_VID80); + change_display_mode(dcycs); + } + return; + case 0x0d: /* 0xc00d */ + if((g_cur_a2_stat & ALL_STAT_VID80) == 0) { + g_cur_a2_stat |= (ALL_STAT_VID80); + change_display_mode(dcycs); + } + return; + case 0x0e: /* 0xc00e */ + if(g_cur_a2_stat & ALL_STAT_ALTCHARSET) { + g_cur_a2_stat &= (~ALL_STAT_ALTCHARSET); + change_display_mode(dcycs); + } + return; + case 0x0f: /* 0xc00f */ + if((g_cur_a2_stat & ALL_STAT_ALTCHARSET) == 0) { + g_cur_a2_stat |= (ALL_STAT_ALTCHARSET); + change_display_mode(dcycs); + } + return; + /* 0xc010 - 0xc01f */ + case 0x10: case 0x11: case 0x12: case 0x13: + case 0x14: case 0x15: case 0x16: case 0x17: + case 0x18: case 0x19: case 0x1a: case 0x1b: + case 0x1c: case 0x1d: case 0x1e: case 0x1f: + adb_access_c010(); + return; + /* 0xc020 - 0xc02f */ + case 0x20: /* 0xc020 */ + /* WRITE CASSETTE?? */ + return; + case 0x21: /* 0xc021 */ + new_tmp = ((val >> 7) & 1) << + (31 - BIT_ALL_STAT_COLOR_C021); + if((g_cur_a2_stat & ALL_STAT_COLOR_C021) != new_tmp) { + g_cur_a2_stat ^= new_tmp; + change_display_mode(dcycs); + } + return; + case 0x22: /* 0xc022 */ + /* change text color */ + tmp = (g_cur_a2_stat >> BIT_ALL_STAT_BG_COLOR) & 0xff; + if(val != tmp) { + /* change text/bg color! */ + g_cur_a2_stat &= ~(ALL_STAT_TEXT_COLOR | + ALL_STAT_BG_COLOR); + g_cur_a2_stat += (val << BIT_ALL_STAT_BG_COLOR); + change_display_mode(dcycs); + } + return; + case 0x23: /* 0xc023 */ + if((val & 0x19) != 0) { + halt_printf("c023 write of %02x!!!\n", val); + } + tmp = (g_c023_val & 0x70) | (val & 0x0f); + if((tmp & 0x22) == 0x22) { + add_irq(IRQ_PENDING_C023_SCAN); + } + if(!(tmp & 2)) { + remove_irq(IRQ_PENDING_C023_SCAN); + } + if((tmp & 0x44) == 0x44) { + add_irq(IRQ_PENDING_C023_1SEC); + } + if(!(tmp & 0x4)) { + remove_irq(IRQ_PENDING_C023_1SEC); + } - if(g_irq_pending & (IRQ_PENDING_C023_SCAN | - IRQ_PENDING_C023_1SEC)) { - tmp |= 0x80; - } - g_c023_val = tmp; - return; - case 0x24: /* 0xc024 */ - /* Write to mouse reg: Throw it away */ - return; - case 0x26: /* 0xc026 */ - adb_write_c026(val); - return; - case 0x27: /* 0xc027 */ - adb_write_c027(val); - return; - case 0x29: /* 0xc029 */ - g_c029_val_some = val & 0x41; - if((val & 1) == 0) { - halt_printf("c029: %02x\n", val); - } - new_tmp = val & 0xa0; - if(new_tmp != (g_cur_a2_stat & 0xa0)) { - g_cur_a2_stat = (g_cur_a2_stat & (~0xa0)) + - new_tmp; - change_display_mode(dcycs); - } - return; - case 0x2a: /* 0xc02a */ + if(g_irq_pending & (IRQ_PENDING_C023_SCAN | + IRQ_PENDING_C023_1SEC)) { + tmp |= 0x80; + } + g_c023_val = tmp; + return; + case 0x24: /* 0xc024 */ + /* Write to mouse reg: Throw it away */ + return; + case 0x26: /* 0xc026 */ + adb_write_c026(val); + return; + case 0x27: /* 0xc027 */ + adb_write_c027(val); + return; + case 0x29: /* 0xc029 */ + g_c029_val_some = val & 0x41; + if((val & 1) == 0) { + halt_printf("c029: %02x\n", val); + } + new_tmp = val & 0xa0; + if(new_tmp != (g_cur_a2_stat & 0xa0)) { + g_cur_a2_stat = (g_cur_a2_stat & (~0xa0)) + + new_tmp; + change_display_mode(dcycs); + } + return; + case 0x2a: /* 0xc02a */ #if 0 - printf("Writing c02a with %02x\n", val); + printf("Writing c02a with %02x\n", val); #endif - return; - case 0x2b: /* 0xc02b */ - g_c02b_val = val; - if(val != 0x08 && val != 0x00) { - printf("Writing c02b with %02x\n", val); - } - return; - case 0x2d: /* 0xc02d */ - if((val & 0x9) != 0) { - halt_printf("Illegal c02d write: %02x!\n", val); - } - fixup = (val != g_c02d_int_crom); - g_c02d_int_crom = val; - if(fixup) { - vid_printf("Write c02d of %02x\n", val); - fixup_intcx(); - } - return; - case 0x28: /* 0xc028 */ - case 0x2c: /* 0xc02c */ - UNIMPL_WRITE; - case 0x25: /* 0xc025 */ - /* Space Shark writes to c025--ignore */ - case 0x2e: /* 0xc02e */ - case 0x2f: /* 0xc02f */ - /* Modulae writes to this--just ignore them */ - return; - break; + return; + case 0x2b: /* 0xc02b */ + g_c02b_val = val; + if(val != 0x08 && val != 0x00) { + printf("Writing c02b with %02x\n", val); + } + return; + case 0x2d: /* 0xc02d */ + if((val & 0x9) != 0) { + halt_printf("Illegal c02d write: %02x!\n", val); + } + fixup = (val != g_c02d_int_crom); + g_c02d_int_crom = val; + if(fixup) { + vid_printf("Write c02d of %02x\n", val); + fixup_intcx(); + } + return; + case 0x28: /* 0xc028 */ + case 0x2c: /* 0xc02c */ + UNIMPL_WRITE; + case 0x25: /* 0xc025 */ + /* Space Shark writes to c025--ignore */ + case 0x2e: /* 0xc02e */ + case 0x2f: /* 0xc02f */ + /* Modulae writes to this--just ignore them */ + return; + break; - /* 0xc030 - 0xc03f */ - case 0x30: /* 0xc030 */ + /* 0xc030 - 0xc03f */ + case 0x30: /* 0xc030 */ #if 0 - printf("Write speaker?\n"); + printf("Write speaker?\n"); #endif - (void)doc_read_c030(dcycs); - return; - case 0x31: /* 0xc031 */ - tmp = val ^ g_c031_disk35; - if(tmp & 0x40) { - /* apple35_sel changed, maybe speed change */ - set_halt(HALT_EVENT); - } - g_c031_disk35 = val & 0xc0; - return; - case 0x32: /* 0xc032 */ - tmp = g_c023_val & 0x7f; - if(((val & 0x40) == 0) && (tmp & 0x40)) { - /* clear 1 sec int */ - remove_irq(IRQ_PENDING_C023_1SEC); - tmp &= 0xbf; - g_c023_val = tmp; - } - if(((val & 0x20) == 0) && (tmp & 0x20)) { - /* clear scan line int */ - remove_irq(IRQ_PENDING_C023_SCAN); - g_c023_val = tmp & 0xdf; - check_for_new_scan_int(dcycs); - } - if(g_irq_pending & (IRQ_PENDING_C023_1SEC | - IRQ_PENDING_C023_SCAN)) { - g_c023_val |= 0x80; - } - if((val & 0x9f) != 0x9f) { - irq_printf("c032: wrote %02x!\n", val); - } - return; - case 0x33: /* 0xc033 = CLOCKDATA*/ - g_c033_data = val; - return; - case 0x34: /* 0xc034 = CLOCKCTL */ - tmp = val ^ g_c034_val; - clock_write_c034(val); - if(tmp & 0xf) { - change_border_color(dcycs, val & 0xf); - } - return; - case 0x35: /* 0xc035 */ - update_shadow_reg(val); - return; - case 0x36: /* 0xc036 = CYAREG */ - tmp = val ^ g_c036_val_speed; - g_c036_val_speed = (val & ~0x20); /* clr bit 5 */ - if(tmp & 0x80) { - /* to recalculate times since speed changing */ - set_halt(HALT_EVENT); - } - if(tmp & 0xf) { - /* slot_motor_detect changed */ - set_halt(HALT_EVENT); - } + (void)doc_read_c030(dcycs); + return; + case 0x31: /* 0xc031 */ + tmp = val ^ g_c031_disk35; + if(tmp & 0x40) { + /* apple35_sel changed, maybe speed change */ + set_halt(HALT_EVENT); + } + g_c031_disk35 = val & 0xc0; + return; + case 0x32: /* 0xc032 */ + tmp = g_c023_val & 0x7f; + if(((val & 0x40) == 0) && (tmp & 0x40)) { + /* clear 1 sec int */ + remove_irq(IRQ_PENDING_C023_1SEC); + tmp &= 0xbf; + g_c023_val = tmp; + } + if(((val & 0x20) == 0) && (tmp & 0x20)) { + /* clear scan line int */ + remove_irq(IRQ_PENDING_C023_SCAN); + g_c023_val = tmp & 0xdf; + check_for_new_scan_int(dcycs); + } + if(g_irq_pending & (IRQ_PENDING_C023_1SEC | + IRQ_PENDING_C023_SCAN)) { + g_c023_val |= 0x80; + } + if((val & 0x9f) != 0x9f) { + irq_printf("c032: wrote %02x!\n", val); + } + return; + case 0x33: /* 0xc033 = CLOCKDATA*/ + g_c033_data = val; + return; + case 0x34: /* 0xc034 = CLOCKCTL */ + tmp = val ^ g_c034_val; + clock_write_c034(val); + if(tmp & 0xf) { + change_border_color(dcycs, val & 0xf); + } + return; + case 0x35: /* 0xc035 */ + update_shadow_reg(val); + return; + case 0x36: /* 0xc036 = CYAREG */ + tmp = val ^ g_c036_val_speed; + g_c036_val_speed = (val & ~0x20); /* clr bit 5 */ + if(tmp & 0x80) { + /* to recalculate times since speed changing */ + set_halt(HALT_EVENT); + } + if(tmp & 0xf) { + /* slot_motor_detect changed */ + set_halt(HALT_EVENT); + } - if((val & 0x60) != 0) { - /* for ROM 03, 0x40 is the power-on status */ - /* and can be read/write */ - if(((val & 0x60) != 0x40) || - (g_rom_version < 3)) { - g_c036_val_speed &= (~0x60); - halt_printf("c036: %2x\n", val); - } - } - if(tmp & 0x10) { /* shadow in all banks! */ - if(g_num_shadow_all_banks++ == 0) { - printf("Shadowing all banks...This " - "must be the NFC Megademo\n"); - } - fixup_shadow_all_banks(); - } - return; - case 0x37: /* 0xc037 */ - /* just ignore, probably someone writing c036 m=0 */ - return; - case 0x38: /* 0xc038 */ - scc_write_reg(1, val, dcycs); - return; - case 0x39: /* 0xc039 */ - scc_write_reg(0, val, dcycs); - return; - case 0x3a: /* 0xc03a */ - scc_write_data(1, val, dcycs); - return; - case 0x3b: /* 0xc03b */ - scc_write_data(0, val, dcycs); - return; - case 0x3c: /* 0xc03c */ - /* doc ctl */ - doc_write_c03c(val, dcycs); - return; - case 0x3d: /* 0xc03d */ - /* doc data reg */ - doc_write_c03d(val, dcycs); - return; - case 0x3e: /* 0xc03e */ - g_c03ef_doc_ptr = (g_c03ef_doc_ptr & 0xff00) + val; - return; - case 0x3f: /* 0xc03f */ - g_c03ef_doc_ptr = (g_c03ef_doc_ptr & 0xff) + (val << 8); - return; + if((val & 0x60) != 0) { + /* for ROM 03, 0x40 is the power-on status */ + /* and can be read/write */ + if(((val & 0x60) != 0x40) || + (g_rom_version < 3)) { + g_c036_val_speed &= (~0x60); + halt_printf("c036: %2x\n", val); + } + } + if(tmp & 0x10) { /* shadow in all banks! */ + if(g_num_shadow_all_banks++ == 0) { + printf("Shadowing all banks...This " + "must be the NFC Megademo\n"); + } + fixup_shadow_all_banks(); + } + return; + case 0x37: /* 0xc037 */ + /* just ignore, probably someone writing c036 m=0 */ + return; + case 0x38: /* 0xc038 */ + scc_write_reg(1, val, dcycs); + return; + case 0x39: /* 0xc039 */ + scc_write_reg(0, val, dcycs); + return; + case 0x3a: /* 0xc03a */ + scc_write_data(1, val, dcycs); + return; + case 0x3b: /* 0xc03b */ + scc_write_data(0, val, dcycs); + return; + case 0x3c: /* 0xc03c */ + /* doc ctl */ + doc_write_c03c(val, dcycs); + return; + case 0x3d: /* 0xc03d */ + /* doc data reg */ + doc_write_c03d(val, dcycs); + return; + case 0x3e: /* 0xc03e */ + g_c03ef_doc_ptr = (g_c03ef_doc_ptr & 0xff00) + val; + return; + case 0x3f: /* 0xc03f */ + g_c03ef_doc_ptr = (g_c03ef_doc_ptr & 0xff) + (val << 8); + return; - /* 0xc040 - 0xc04f */ - case 0x41: /* c041 */ - g_c041_val = val & 0x1f; - if((val & 0xe6) != 0) { - halt_printf("write c041: %02x\n", val); - } + /* 0xc040 - 0xc04f */ + case 0x41: /* c041 */ + g_c041_val = val & 0x1f; + if((val & 0xe6) != 0) { + halt_printf("write c041: %02x\n", val); + } - if (val & C041_EN_MOUSE) - { - // Enable Mega II mouse - } + if (val & C041_EN_MOUSE) + { + // Enable Mega II mouse + } - if(!(val & C041_EN_VBL_INTS)) { - /* no more vbl interrupt */ - remove_irq(IRQ_PENDING_C046_VBL); - } - if(!(val & C041_EN_25SEC_INTS)) { - remove_irq(IRQ_PENDING_C046_25SEC); - } - return; - case 0x46: /* c046 */ - /* ignore writes to c046 */ - return; - case 0x47: /* c047 */ - remove_irq(IRQ_PENDING_C046_VBL | - IRQ_PENDING_C046_25SEC); - g_c046_val &= 0xe7; /* clear vblint, 1/4sec int*/ - return; - case 0x48: /* c048 */ - /* diversitune writes this--ignore it */ - return; - case 0x42: /* c042 */ - case 0x43: /* c043 */ - return; - case 0x4f: /* c04f */ - g_em_emubyte_cnt = 1; - return; - case 0x40: /* c040 */ - case 0x44: /* c044 */ - case 0x45: /* c045 */ - case 0x49: /* c049 */ - case 0x4a: /* c04a */ - case 0x4b: /* c04b */ - case 0x4c: /* c04c */ - case 0x4d: /* c04d */ - case 0x4e: /* c04e */ - UNIMPL_WRITE; + if(!(val & C041_EN_VBL_INTS)) { + /* no more vbl interrupt */ + remove_irq(IRQ_PENDING_C046_VBL); + } + if(!(val & C041_EN_25SEC_INTS)) { + remove_irq(IRQ_PENDING_C046_25SEC); + } + return; + case 0x46: /* c046 */ + /* ignore writes to c046 */ + return; + case 0x47: /* c047 */ + remove_irq(IRQ_PENDING_C046_VBL | + IRQ_PENDING_C046_25SEC); + g_c046_val &= 0xe7; /* clear vblint, 1/4sec int*/ + return; + case 0x48: /* c048 */ + /* diversitune writes this--ignore it */ + return; + case 0x42: /* c042 */ + case 0x43: /* c043 */ + return; + case 0x4f: /* c04f */ + g_em_emubyte_cnt = 1; + return; + case 0x40: /* c040 */ + case 0x44: /* c044 */ + case 0x45: /* c045 */ + case 0x49: /* c049 */ + case 0x4a: /* c04a */ + case 0x4b: /* c04b */ + case 0x4c: /* c04c */ + case 0x4d: /* c04d */ + case 0x4e: /* c04e */ + UNIMPL_WRITE; - /* 0xc050 - 0xc05f */ - case 0x50: /* 0xc050 */ - if(g_cur_a2_stat & ALL_STAT_TEXT) { - g_cur_a2_stat &= (~ALL_STAT_TEXT); - change_display_mode(dcycs); - } - return; - case 0x51: /* 0xc051 */ - if((g_cur_a2_stat & ALL_STAT_TEXT) == 0) { - g_cur_a2_stat |= (ALL_STAT_TEXT); - change_display_mode(dcycs); - } - return; - case 0x52: /* 0xc052 */ - if(g_cur_a2_stat & ALL_STAT_MIX_T_GR) { - g_cur_a2_stat &= (~ALL_STAT_MIX_T_GR); - change_display_mode(dcycs); - } - return; - case 0x53: /* 0xc053 */ - if((g_cur_a2_stat & ALL_STAT_MIX_T_GR) == 0) { - g_cur_a2_stat |= (ALL_STAT_MIX_T_GR); - change_display_mode(dcycs); - } - return; - case 0x54: /* 0xc054 */ - set_statereg(dcycs, g_c068_statereg & (~0x40)); - return; - case 0x55: /* 0xc055 */ - set_statereg(dcycs, g_c068_statereg | 0x40); - return; - case 0x56: /* 0xc056 */ - if(g_cur_a2_stat & ALL_STAT_HIRES) { - g_cur_a2_stat &= (~ALL_STAT_HIRES); - fixup_hires_on(); - change_display_mode(dcycs); - } - return; - case 0x57: /* 0xc057 */ - if((g_cur_a2_stat & ALL_STAT_HIRES) == 0) { - g_cur_a2_stat |= (ALL_STAT_HIRES); - fixup_hires_on(); - change_display_mode(dcycs); - } - return; - case 0x58: /* 0xc058 */ - if(g_zipgs_unlock >= 4) { - g_zipgs_reg_c059 &= 0x4; /* last reset cold */ - } else { - g_c05x_annuncs &= (~1); - } - return; - case 0x59: /* 0xc059 */ - if(g_zipgs_unlock >= 4) { - g_zipgs_reg_c059 = (val & 0xf8) | - (g_zipgs_reg_c059 & 0x7); - } else { - g_c05x_annuncs |= 1; - } - return; - case 0x5a: /* 0xc05a */ - g_c05x_annuncs &= (~2); - if((val & 0xf0) == 0x50) { - g_zipgs_unlock++; - } else if((val & 0xf0) == 0xa0) { - g_zipgs_unlock = 0; - } else if(g_zipgs_unlock >= 4) { - if((g_zipgs_reg_c05b & 0x10) == 0) { - /* to recalculate times */ - set_halt(HALT_EVENT); - } - g_zipgs_reg_c05b |= 0x10; // disable - } - return; - case 0x5b: /* 0xc05b */ - if(g_zipgs_unlock >= 4) { - if((g_zipgs_reg_c05b & 0x10) != 0) { - /* to recalculate times */ - set_halt(HALT_EVENT); - } - g_zipgs_reg_c05b &= (~0x10); // enable - } else { - g_c05x_annuncs |= 2; - } - return; - case 0x5c: /* 0xc05c */ - if(g_zipgs_unlock >= 4) { - g_zipgs_reg_c05c = val; - } else { - g_c05x_annuncs &= (~4); - } - return; - case 0x5d: /* 0xc05d */ - if(g_zipgs_unlock >= 4) { - if(((g_zipgs_reg_c05a ^ val) >= 0x10) && - ((g_zipgs_reg_c05b & 0x10) == 0)) { - set_halt(HALT_EVENT); - } - g_zipgs_reg_c05a = val | 0xf; - } else { - g_c05x_annuncs |= 4; - } - return; - case 0x5e: /* 0xc05e */ - if(g_zipgs_unlock >= 4) { - /* Zippy writes 0x80 and 0x00 here... */ - } else if(g_cur_a2_stat & ALL_STAT_ANNUNC3) { - g_cur_a2_stat &= (~ALL_STAT_ANNUNC3); - change_display_mode(dcycs); - } - return; - case 0x5f: /* 0xc05f */ - if(g_zipgs_unlock >= 4) { - halt_printf("Wrote ZipGS $c05f: %02x\n", val); - } else if((g_cur_a2_stat & ALL_STAT_ANNUNC3) == 0) { - g_cur_a2_stat |= (ALL_STAT_ANNUNC3); - change_display_mode(dcycs); - } - return; + /* 0xc050 - 0xc05f */ + case 0x50: /* 0xc050 */ + if(g_cur_a2_stat & ALL_STAT_TEXT) { + g_cur_a2_stat &= (~ALL_STAT_TEXT); + change_display_mode(dcycs); + } + return; + case 0x51: /* 0xc051 */ + if((g_cur_a2_stat & ALL_STAT_TEXT) == 0) { + g_cur_a2_stat |= (ALL_STAT_TEXT); + change_display_mode(dcycs); + } + return; + case 0x52: /* 0xc052 */ + if(g_cur_a2_stat & ALL_STAT_MIX_T_GR) { + g_cur_a2_stat &= (~ALL_STAT_MIX_T_GR); + change_display_mode(dcycs); + } + return; + case 0x53: /* 0xc053 */ + if((g_cur_a2_stat & ALL_STAT_MIX_T_GR) == 0) { + g_cur_a2_stat |= (ALL_STAT_MIX_T_GR); + change_display_mode(dcycs); + } + return; + case 0x54: /* 0xc054 */ + set_statereg(dcycs, g_c068_statereg & (~0x40)); + return; + case 0x55: /* 0xc055 */ + set_statereg(dcycs, g_c068_statereg | 0x40); + return; + case 0x56: /* 0xc056 */ + if(g_cur_a2_stat & ALL_STAT_HIRES) { + g_cur_a2_stat &= (~ALL_STAT_HIRES); + fixup_hires_on(); + change_display_mode(dcycs); + } + return; + case 0x57: /* 0xc057 */ + if((g_cur_a2_stat & ALL_STAT_HIRES) == 0) { + g_cur_a2_stat |= (ALL_STAT_HIRES); + fixup_hires_on(); + change_display_mode(dcycs); + } + return; + case 0x58: /* 0xc058 */ + if(g_zipgs_unlock >= 4) { + g_zipgs_reg_c059 &= 0x4; /* last reset cold */ + } else { + g_c05x_annuncs &= (~1); + } + return; + case 0x59: /* 0xc059 */ + if(g_zipgs_unlock >= 4) { + g_zipgs_reg_c059 = (val & 0xf8) | + (g_zipgs_reg_c059 & 0x7); + } else { + g_c05x_annuncs |= 1; + } + return; + case 0x5a: /* 0xc05a */ + g_c05x_annuncs &= (~2); + if((val & 0xf0) == 0x50) { + g_zipgs_unlock++; + } else if((val & 0xf0) == 0xa0) { + g_zipgs_unlock = 0; + } else if(g_zipgs_unlock >= 4) { + if((g_zipgs_reg_c05b & 0x10) == 0) { + /* to recalculate times */ + set_halt(HALT_EVENT); + } + g_zipgs_reg_c05b |= 0x10; // disable + } + return; + case 0x5b: /* 0xc05b */ + if(g_zipgs_unlock >= 4) { + if((g_zipgs_reg_c05b & 0x10) != 0) { + /* to recalculate times */ + set_halt(HALT_EVENT); + } + g_zipgs_reg_c05b &= (~0x10); // enable + } else { + g_c05x_annuncs |= 2; + } + return; + case 0x5c: /* 0xc05c */ + if(g_zipgs_unlock >= 4) { + g_zipgs_reg_c05c = val; + } else { + g_c05x_annuncs &= (~4); + } + return; + case 0x5d: /* 0xc05d */ + if(g_zipgs_unlock >= 4) { + if(((g_zipgs_reg_c05a ^ val) >= 0x10) && + ((g_zipgs_reg_c05b & 0x10) == 0)) { + set_halt(HALT_EVENT); + } + g_zipgs_reg_c05a = val | 0xf; + } else { + g_c05x_annuncs |= 4; + } + return; + case 0x5e: /* 0xc05e */ + if(g_zipgs_unlock >= 4) { + /* Zippy writes 0x80 and 0x00 here... */ + } else if(g_cur_a2_stat & ALL_STAT_ANNUNC3) { + g_cur_a2_stat &= (~ALL_STAT_ANNUNC3); + change_display_mode(dcycs); + } + return; + case 0x5f: /* 0xc05f */ + if(g_zipgs_unlock >= 4) { + halt_printf("Wrote ZipGS $c05f: %02x\n", val); + } else if((g_cur_a2_stat & ALL_STAT_ANNUNC3) == 0) { + g_cur_a2_stat |= (ALL_STAT_ANNUNC3); + change_display_mode(dcycs); + } + return; - /* 0xc060 - 0xc06f */ - case 0x60: /* 0xc060 */ - case 0x61: /* 0xc061 */ - case 0x62: /* 0xc062 */ - case 0x63: /* 0xc063 */ - case 0x64: /* 0xc064 */ - case 0x65: /* 0xc065 */ - case 0x66: /* 0xc066 */ - case 0x67: /* 0xc067 */ - /* all the above do nothing--return */ - return; - case 0x68: /* 0xc068 = STATEREG */ - set_statereg(dcycs, val); - return; - case 0x69: /* 0xc069 */ - /* just ignore, someone writing c068 with m=0 */ - return; + /* 0xc060 - 0xc06f */ + case 0x60: /* 0xc060 */ + case 0x61: /* 0xc061 */ + case 0x62: /* 0xc062 */ + case 0x63: /* 0xc063 */ + case 0x64: /* 0xc064 */ + case 0x65: /* 0xc065 */ + case 0x66: /* 0xc066 */ + case 0x67: /* 0xc067 */ + /* all the above do nothing--return */ + return; + case 0x68: /* 0xc068 = STATEREG */ + set_statereg(dcycs, val); + return; + case 0x69: /* 0xc069 */ + /* just ignore, someone writing c068 with m=0 */ + return; #ifdef TRANSWARP - // OG writeTranswarp pseudo-register - case 0x6a: /* 0xc06a */ - transwarp_low_val = val; - return ; - case 0x6b: /* 0xc06b */ - val = (val<<8) + transwarp_low_val; - if ((val==2600) || (val==0x0028)) // Bug for demo ... - { - printf("Disabling Transwarp!\n"); - g_zipgs_reg_c05b |= 0x10; // disable - set_halt(HALT_EVENT); - } - else if (val==8000) - { - printf("Enabling Transwarp!\n"); - g_zipgs_reg_c05b &= ~0x10; // enable - set_halt(HALT_EVENT); - } - else - printf("unknown twgs speed:%d\n",val); - return; - case 0x6c: /* 0xc06c */ - if (val==0) - ; // set slow ? - else if (val==1) - { - // disable zip - g_zipgs_reg_c05b |= 0x10; // disable - set_halt(HALT_EVENT); - } - else if (val==2) - { - // enable zip - g_zipgs_reg_c05b &= ~0x10; // enable - set_halt(HALT_EVENT); - } - else - printf("unknown twgs index:%d\n",val); - return ; + // OG writeTranswarp pseudo-register + case 0x6a: /* 0xc06a */ + transwarp_low_val = val; + return; + case 0x6b: /* 0xc06b */ + val = (val<<8) + transwarp_low_val; + if ((val==2600) || (val==0x0028)) // Bug for demo ... + { + printf("Disabling Transwarp!\n"); + g_zipgs_reg_c05b |= 0x10; // disable + set_halt(HALT_EVENT); + } + else if (val==8000) + { + printf("Enabling Transwarp!\n"); + g_zipgs_reg_c05b &= ~0x10; // enable + set_halt(HALT_EVENT); + } + else + printf("unknown twgs speed:%d\n",val); + return; + case 0x6c: /* 0xc06c */ + if (val==0) + ; // set slow ? + else if (val==1) + { + // disable zip + g_zipgs_reg_c05b |= 0x10; // disable + set_halt(HALT_EVENT); + } + else if (val==2) + { + // enable zip + g_zipgs_reg_c05b &= ~0x10; // enable + set_halt(HALT_EVENT); + } + else + printf("unknown twgs index:%d\n",val); + return; #else - case 0x6a: /* 0xc06a */ - case 0x6b: /* 0xc06b */ - case 0x6c: /* 0xc06c */ + case 0x6a: /* 0xc06a */ + case 0x6b: /* 0xc06b */ + case 0x6c: /* 0xc06c */ #endif - case 0x6d: /* 0xc06d */ - case 0x6e: /* 0xc06e */ - case 0x6f: /* 0xc06f */ - UNIMPL_WRITE; + case 0x6d: /* 0xc06d */ + case 0x6e: /* 0xc06e */ + case 0x6f: /* 0xc06f */ + UNIMPL_WRITE; - /* 0xc070 - 0xc07f */ - case 0x70: /* 0xc070 = Trigger paddles */ - paddle_trigger(dcycs); - return; - case 0x73: /* 0xc073 = multibank ram card bank addr? */ - return; - case 0x71: /* 0xc071 = another multibank ram card enable? */ - case 0x7e: /* 0xc07e */ - case 0x7f: /* 0xc07f */ - return; - case 0x72: /* 0xc072 */ - case 0x74: /* 0xc074 */ - case 0x75: /* 0xc075 */ - case 0x76: /* 0xc076 */ - case 0x77: /* 0xc077 */ - case 0x78: /* 0xc078 */ - case 0x79: /* 0xc079 */ - case 0x7a: /* 0xc07a */ - case 0x7b: /* 0xc07b */ - case 0x7c: /* 0xc07c */ - case 0x7d: /* 0xc07d */ - UNIMPL_WRITE; + /* 0xc070 - 0xc07f */ + case 0x70: /* 0xc070 = Trigger paddles */ + paddle_trigger(dcycs); + return; + case 0x73: /* 0xc073 = multibank ram card bank addr? */ + return; + case 0x71: /* 0xc071 = another multibank ram card enable? */ + case 0x7e: /* 0xc07e */ + case 0x7f: /* 0xc07f */ + return; + case 0x72: /* 0xc072 */ + case 0x74: /* 0xc074 */ + case 0x75: /* 0xc075 */ + case 0x76: /* 0xc076 */ + case 0x77: /* 0xc077 */ + case 0x78: /* 0xc078 */ + case 0x79: /* 0xc079 */ + case 0x7a: /* 0xc07a */ + case 0x7b: /* 0xc07b */ + case 0x7c: /* 0xc07c */ + case 0x7d: /* 0xc07d */ + UNIMPL_WRITE; - /* 0xc080 - 0xc08f */ - case 0x80: case 0x81: case 0x82: case 0x83: - case 0x84: case 0x85: case 0x86: case 0x87: - case 0x88: case 0x89: case 0x8a: case 0x8b: - case 0x8c: case 0x8d: case 0x8e: case 0x8f: - new_lcbank2 = ((loc >> 1) & 0x4) ^ 0x4; - new_wrdefram = (loc & 1); - if(new_wrdefram != g_c08x_wrdefram) { - fixup_wrdefram(new_wrdefram); - } - switch(loc & 0xf) { - case 0x1: /* 0xc081 */ - case 0x2: /* 0xc082 */ - case 0x5: /* 0xc085 */ - case 0x6: /* 0xc086 */ - case 0x9: /* 0xc089 */ - case 0xa: /* 0xc08a */ - case 0xd: /* 0xc08d */ - case 0xe: /* 0xc08e */ - /* Read rom, set lcbank2 */ - set_statereg(dcycs, (g_c068_statereg & ~(0x04))| - (new_lcbank2 | 0x08)); - break; - case 0x0: /* 0xc080 */ - case 0x3: /* 0xc083 */ - case 0x4: /* 0xc084 */ - case 0x7: /* 0xc087 */ - case 0x8: /* 0xc088 */ - case 0xb: /* 0xc08b */ - case 0xc: /* 0xc08c */ - case 0xf: /* 0xc08f */ - /* Read ram (clear RDROM), set lcbank2 */ - set_statereg(dcycs, (g_c068_statereg & ~(0x0c))| - (new_lcbank2)); - break; - } - return; + /* 0xc080 - 0xc08f */ + case 0x80: case 0x81: case 0x82: case 0x83: + case 0x84: case 0x85: case 0x86: case 0x87: + case 0x88: case 0x89: case 0x8a: case 0x8b: + case 0x8c: case 0x8d: case 0x8e: case 0x8f: + new_lcbank2 = ((loc >> 1) & 0x4) ^ 0x4; + new_wrdefram = (loc & 1); + if(new_wrdefram != g_c08x_wrdefram) { + fixup_wrdefram(new_wrdefram); + } + switch(loc & 0xf) { + case 0x1: /* 0xc081 */ + case 0x2: /* 0xc082 */ + case 0x5: /* 0xc085 */ + case 0x6: /* 0xc086 */ + case 0x9: /* 0xc089 */ + case 0xa: /* 0xc08a */ + case 0xd: /* 0xc08d */ + case 0xe: /* 0xc08e */ + /* Read rom, set lcbank2 */ + set_statereg(dcycs, (g_c068_statereg & ~(0x04))| + (new_lcbank2 | 0x08)); + break; + case 0x0: /* 0xc080 */ + case 0x3: /* 0xc083 */ + case 0x4: /* 0xc084 */ + case 0x7: /* 0xc087 */ + case 0x8: /* 0xc088 */ + case 0xb: /* 0xc08b */ + case 0xc: /* 0xc08c */ + case 0xf: /* 0xc08f */ + /* Read ram (clear RDROM), set lcbank2 */ + set_statereg(dcycs, (g_c068_statereg & ~(0x0c))| + (new_lcbank2)); + break; + } + return; - /* 0xc090 - 0xc09f */ - case 0x90: case 0x91: case 0x92: case 0x93: - case 0x94: case 0x95: case 0x96: case 0x97: - case 0x98: case 0x99: case 0x9a: case 0x9b: - case 0x9c: case 0x9d: case 0x9e: case 0x9f: - if (g_parallel) - { - parallel_write((word16)loc & 0xf, (byte)val); - return; - } - else - { - UNIMPL_WRITE; - } + /* 0xc090 - 0xc09f */ + case 0x90: case 0x91: case 0x92: case 0x93: + case 0x94: case 0x95: case 0x96: case 0x97: + case 0x98: case 0x99: case 0x9a: case 0x9b: + case 0x9c: case 0x9d: case 0x9e: case 0x9f: + if (g_parallel) + { + parallel_write((word16)loc & 0xf, (byte)val); + return; + } + else + { + UNIMPL_WRITE; + } - /* 0xc0a0 - 0xc0af */ - case 0xa0: case 0xa1: case 0xa3: - case 0xa4: case 0xa5: case 0xa6: case 0xa7: - case 0xa9: case 0xaa: case 0xab: - case 0xac: case 0xad: case 0xae: case 0xaf: - UNIMPL_WRITE; - case 0xa2: /* Burger Times writes here on error */ - case 0xa8: - /* Kurzweil SMP writes to 0xc0a8, ignore it */ - UNIMPL_WRITE; - return; + /* 0xc0a0 - 0xc0af */ + case 0xa0: case 0xa1: case 0xa3: + case 0xa4: case 0xa5: case 0xa6: case 0xa7: + case 0xa9: case 0xaa: case 0xab: + case 0xac: case 0xad: case 0xae: case 0xaf: + UNIMPL_WRITE; + case 0xa2: /* Burger Times writes here on error */ + case 0xa8: + /* Kurzweil SMP writes to 0xc0a8, ignore it */ + UNIMPL_WRITE; + return; - /* 0xc0b0 - 0xc0bf */ - //case 0xb0: - /* Second sight stuff--ignore it */ - return; - //case 0xb1: case 0xb2: case 0xb3: - //case 0xb4: case 0xb5: case 0xb6: case 0xb7: - //case 0xb8: case 0xb9: case 0xba: case 0xbb: - //case 0xbc: case 0xbd: case 0xbe: case 0xbf: - // UNIMPL_WRITE; + /* 0xc0b0 - 0xc0bf */ + //case 0xb0: + /* Second sight stuff--ignore it */ + return; + //case 0xb1: case 0xb2: case 0xb3: + //case 0xb4: case 0xb5: case 0xb6: case 0xb7: + //case 0xb8: case 0xb9: case 0xba: case 0xbb: + //case 0xbc: case 0xbd: case 0xbe: case 0xbf: + // UNIMPL_WRITE; #ifdef HAVE_TFE - /*Uthernet write access on slot 3*/ - case 0xb0: - case 0xb1: - case 0xb2: - case 0xb3: - case 0xb4: - case 0xb5: - case 0xb6: - case 0xb7: - case 0xb8: - case 0xb9: - case 0xba: - case 0xbb: - case 0xbc: - case 0xbd: - case 0xbe: - case 0xbf: - if (tfe_enabled) - { - tfe_store((word16)loc & 0xf, (byte)val); - return; - } - else - { - UNIMPL_WRITE; - } + /*Uthernet write access on slot 3*/ + case 0xb0: + case 0xb1: + case 0xb2: + case 0xb3: + case 0xb4: + case 0xb5: + case 0xb6: + case 0xb7: + case 0xb8: + case 0xb9: + case 0xba: + case 0xbb: + case 0xbc: + case 0xbd: + case 0xbe: + case 0xbf: + if (tfe_enabled) + { + tfe_store((word16)loc & 0xf, (byte)val); + return; + } + else + { + UNIMPL_WRITE; + } #endif - /* 0xc0c0 - 0xc0cf */ - case 0xc0: case 0xc1: case 0xc2: case 0xc3: - case 0xc4: case 0xc5: case 0xc6: case 0xc7: - case 0xc8: case 0xc9: case 0xca: case 0xcb: - case 0xcc: case 0xcd: case 0xce: case 0xcf: - UNIMPL_WRITE; + /* 0xc0c0 - 0xc0cf */ + case 0xc0: case 0xc1: case 0xc2: case 0xc3: + case 0xc4: case 0xc5: case 0xc6: case 0xc7: + case 0xc8: case 0xc9: case 0xca: case 0xcb: + case 0xcc: case 0xcd: case 0xce: case 0xcf: + UNIMPL_WRITE; - /* 0xc0d0 - 0xc0df */ - case 0xd0: case 0xd1: case 0xd2: case 0xd3: - case 0xd4: case 0xd5: case 0xd6: case 0xd7: - case 0xd8: case 0xd9: case 0xda: case 0xdb: - case 0xdc: case 0xdd: case 0xde: case 0xdf: - UNIMPL_WRITE; + /* 0xc0d0 - 0xc0df */ + case 0xd0: case 0xd1: case 0xd2: case 0xd3: + case 0xd4: case 0xd5: case 0xd6: case 0xd7: + case 0xd8: case 0xd9: case 0xda: case 0xdb: + case 0xdc: case 0xdd: case 0xde: case 0xdf: + UNIMPL_WRITE; - /* 0xc0e0 - 0xc0ef */ - case 0xe0: case 0xe1: case 0xe2: case 0xe3: - case 0xe4: case 0xe5: case 0xe6: case 0xe7: - case 0xe8: case 0xe9: case 0xea: case 0xeb: - case 0xec: case 0xed: case 0xee: case 0xef: - write_iwm(loc, val, dcycs); - return; + /* 0xc0e0 - 0xc0ef */ + case 0xe0: case 0xe1: case 0xe2: case 0xe3: + case 0xe4: case 0xe5: case 0xe6: case 0xe7: + case 0xe8: case 0xe9: case 0xea: case 0xeb: + case 0xec: case 0xed: case 0xee: case 0xef: + write_iwm(loc, val, dcycs); + return; - /* 0xc0f0 - 0xc0ff */ - case 0xf0: case 0xf1: case 0xf2: case 0xf3: - case 0xf4: case 0xf5: case 0xf6: case 0xf7: - case 0xf8: case 0xf9: case 0xfa: case 0xfb: - case 0xfc: case 0xfd: case 0xfe: case 0xff: - UNIMPL_WRITE; - default: - printf("WRite loc: %x\n",loc); - exit(-300); - } - break; - case 1: case 2: case 3: case 4: case 5: case 6: case 7: - /* c1000 - c7ff */ - UNIMPL_WRITE; - case 8: case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0xe: - UNIMPL_WRITE; - case 0xf: - if((loc & 0xfff) == 0xfff) { - /* cfff */ - return; - } - UNIMPL_WRITE; - } - printf("Huh2? Write loc: %x\n", loc); - exit(-290); + /* 0xc0f0 - 0xc0ff */ + case 0xf0: case 0xf1: case 0xf2: case 0xf3: + case 0xf4: case 0xf5: case 0xf6: case 0xf7: + case 0xf8: case 0xf9: case 0xfa: case 0xfb: + case 0xfc: case 0xfd: case 0xfe: case 0xff: + UNIMPL_WRITE; + default: + printf("WRite loc: %x\n",loc); + exit(-300); + } + break; + case 1: case 2: case 3: case 4: case 5: case 6: case 7: + /* c1000 - c7ff */ + UNIMPL_WRITE; + case 8: case 9: case 0xa: case 0xb: case 0xc: case 0xd: case 0xe: + UNIMPL_WRITE; + case 0xf: + if((loc & 0xfff) == 0xfff) { + /* cfff */ + return; + } + UNIMPL_WRITE; + } + printf("Huh2? Write loc: %x\n", loc); + exit(-290); } #if 0 -int -get_slow_mem(word32 loc, int duff_cycles) -{ - int val; +int get_slow_mem(word32 loc, int duff_cycles) { + int val; - loc = loc & 0x1ffff; + loc = loc & 0x1ffff; - if((loc &0xf000) == 0xc000) { - return(io_read(loc &0xfff, duff_cycles)); - } - if((loc & 0xf000) >= 0xd000) { - if((loc & 0xf000) == 0xd000) { - if(!LCBANK2) { - /* Not LCBANK2 == be 0xc000 - 0xd000 */ - loc = loc - 0x1000; - } - } - } + if((loc &0xf000) == 0xc000) { + return(io_read(loc &0xfff, duff_cycles)); + } + if((loc & 0xf000) >= 0xd000) { + if((loc & 0xf000) == 0xd000) { + if(!LCBANK2) { + /* Not LCBANK2 == be 0xc000 - 0xd000 */ + loc = loc - 0x1000; + } + } + } - val = g_slow_memory_ptr[loc]; + val = g_slow_memory_ptr[loc]; - halt_printf("get_slow_mem: %06x = %02x\n", loc, val); + halt_printf("get_slow_mem: %06x = %02x\n", loc, val); - return val; + return val; } -int -set_slow_mem(word32 loc, int val, int duff_cycles) -{ - int or_pos; - word32 or_val; +int set_slow_mem(word32 loc, int val, int duff_cycles) { + int or_pos; + word32 or_val; - loc = loc & 0x1ffff; - if((loc & 0xf000) == 0xc000) { - return(io_write(loc & 0xfff, val, duff_cycles)); - } + loc = loc & 0x1ffff; + if((loc & 0xf000) == 0xc000) { + return(io_write(loc & 0xfff, val, duff_cycles)); + } - if((loc & 0xf000) == 0xd000) { - if(!LCBANK2) { - /* Not LCBANK2 == be 0xc000 - 0xd000 */ - loc = loc - 0x1000; - } - } + if((loc & 0xf000) == 0xd000) { + if(!LCBANK2) { + /* Not LCBANK2 == be 0xc000 - 0xd000 */ + loc = loc - 0x1000; + } + } - if(g_slow_memory_ptr[loc] != val) { - or_pos = (loc >> SHIFT_PER_CHANGE) & 0x1f; - or_val = DEP1(1, or_pos, 0); - if((loc >> CHANGE_SHIFT) >= SLOW_MEM_CH_SIZE || loc < 0) { - printf("loc: %08x!!\n", loc); - exit(11); - } - slow_mem_changed[(loc & 0xffff) >> CHANGE_SHIFT] |= or_val; - } + if(g_slow_memory_ptr[loc] != val) { + or_pos = (loc >> SHIFT_PER_CHANGE) & 0x1f; + or_val = DEP1(1, or_pos, 0); + if((loc >> CHANGE_SHIFT) >= SLOW_MEM_CH_SIZE || loc < 0) { + printf("loc: %08x!!\n", loc); + exit(11); + } + slow_mem_changed[(loc & 0xffff) >> CHANGE_SHIFT] |= or_val; + } /* doesn't shadow text/hires graphics properly! */ - g_slow_memory_ptr[loc] = val; + g_slow_memory_ptr[loc] = val; - return val; + return val; } #endif @@ -2534,87 +2469,81 @@ set_slow_mem(word32 loc, int val, int duff_cycles) /* vertical blanking engages on line 192, even if in super hires mode */ /* (Last 8 lines in SHR are drawn with vbl_active set */ -word32 -get_lines_since_vbl(double dcycs) -{ - double dcycs_since_last_vbl; - double dlines_since_vbl; - double dcyc_line_start; - word32 lines_since_vbl; - int offset; +word32 get_lines_since_vbl(double dcycs) { + double dcycs_since_last_vbl; + double dlines_since_vbl; + double dcyc_line_start; + word32 lines_since_vbl; + int offset; - dcycs_since_last_vbl = dcycs - g_last_vbl_dcycs; + dcycs_since_last_vbl = dcycs - g_last_vbl_dcycs; - dlines_since_vbl = dcycs_since_last_vbl * (1.0 / 65.0); - lines_since_vbl = (int)dlines_since_vbl; - dcyc_line_start = (double)lines_since_vbl * 65.0; + dlines_since_vbl = dcycs_since_last_vbl * (1.0 / 65.0); + lines_since_vbl = (int)dlines_since_vbl; + dcyc_line_start = (double)lines_since_vbl * 65.0; - offset = ((int)(dcycs_since_last_vbl - dcyc_line_start)) & 0xff; + offset = ((int)(dcycs_since_last_vbl - dcyc_line_start)) & 0xff; - lines_since_vbl = (lines_since_vbl << 8) + offset; + lines_since_vbl = (lines_since_vbl << 8) + offset; - if(lines_since_vbl < 0x10680) { - return lines_since_vbl; - } else { - halt_printf("lines_since_vbl: %08x!\n", lines_since_vbl); - printf("dc_s_l_v: %f, dcycs: %f, last_vbl_cycs: %f\n", - dcycs_since_last_vbl, dcycs, g_last_vbl_dcycs); - show_dtime_array(); - show_all_events(); - /* U_STACK_TRACE(); */ - } + if(lines_since_vbl < 0x10680) { + return lines_since_vbl; + } else { + halt_printf("lines_since_vbl: %08x!\n", lines_since_vbl); + printf("dc_s_l_v: %f, dcycs: %f, last_vbl_cycs: %f\n", + dcycs_since_last_vbl, dcycs, g_last_vbl_dcycs); + show_dtime_array(); + show_all_events(); + /* U_STACK_TRACE(); */ + } - return lines_since_vbl; + return lines_since_vbl; } -int -in_vblank(double dcycs) -{ - int lines_since_vbl; +int in_vblank(double dcycs) { + int lines_since_vbl; - lines_since_vbl = get_lines_since_vbl(dcycs); + lines_since_vbl = get_lines_since_vbl(dcycs); - if(lines_since_vbl >= 0xc000) { - return 1; - } + if(lines_since_vbl >= 0xc000) { + return 1; + } - return 0; + return 0; } /* horizontal video counter goes from 0x00,0x40 - 0x7f, then 0x80,0xc0-0xff */ /* over 2*65 cycles. The last visible screen pos is 0x7f and 0xff */ /* This matches GSport starting line 0 at the border for line -1 */ -int -read_vid_counters(int loc, double dcycs) -{ - word32 mask; - int lines_since_vbl; +int read_vid_counters(int loc, double dcycs) { + word32 mask; + int lines_since_vbl; - loc = loc & 0xf; + loc = loc & 0xf; - lines_since_vbl = get_lines_since_vbl(dcycs); + lines_since_vbl = get_lines_since_vbl(dcycs); - lines_since_vbl += 0x10000; - if(lines_since_vbl >= 0x20000) { - lines_since_vbl = lines_since_vbl - 0x20000 + 0xfa00; - } + lines_since_vbl += 0x10000; + if(lines_since_vbl >= 0x20000) { + lines_since_vbl = lines_since_vbl - 0x20000 + 0xfa00; + } - if(lines_since_vbl > 0x1ffff) { - halt_printf("lines_since_vbl: %04x, dcycs: %f, last_vbl: %f\n", - lines_since_vbl, dcycs, g_last_vbl_dcycs); - } + if(lines_since_vbl > 0x1ffff) { + halt_printf("lines_since_vbl: %04x, dcycs: %f, last_vbl: %f\n", + lines_since_vbl, dcycs, g_last_vbl_dcycs); + } - if(loc == 0xe) { - /* Vertical count */ - return (lines_since_vbl >> 9) & 0xff; - } + if(loc == 0xe) { + /* Vertical count */ + return (lines_since_vbl >> 9) & 0xff; + } - mask = (lines_since_vbl >> 1) & 0x80; + mask = (lines_since_vbl >> 1) & 0x80; - lines_since_vbl = (lines_since_vbl & 0xff); - if(lines_since_vbl >= 0x01) { - lines_since_vbl = (lines_since_vbl + 0x3f) & 0x7f; - } - return (mask | (lines_since_vbl & 0xff)); + lines_since_vbl = (lines_since_vbl & 0xff); + if(lines_since_vbl >= 0x01) { + lines_since_vbl = (lines_since_vbl + 0x3f) & 0x7f; + } + return (mask | (lines_since_vbl & 0xff)); } diff --git a/src/options.c b/src/options.c index 75b863e..64b5502 100644 --- a/src/options.c +++ b/src/options.c @@ -1,4 +1,10 @@ - +/* + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ + #include #include #include @@ -11,9 +17,9 @@ // Halts on bad reads. Sets flags via engine_s.s:set_halt_act() function extern int g_halt_on_bad_read; // defined in sim65816.c -// Ignore bad memory accesses. +// Ignore bad memory accesses. extern int g_ignore_bad_acc; // defined in sim65816.c -// Ignore red alert halts. +// Ignore red alert halts. extern int g_ignore_halts; // defined in sim65816.c // Size of RAM memory expansion card in bytes (default = 8*1024*1024 = 8MB) extern int g_mem_size_exp; // defined in sim65816.c @@ -41,7 +47,7 @@ extern int g_ethernet; // defined in sim65816.c extern int g_dbg_enable_port; // defined in debug.c // Set preferred audio frequency extern int g_preferred_rate; // defined in sound_driver.c, implemented in various driver files -// Enable/disable audio +// Enable/disable audio extern int g_audio_enable; // defined in sound.c // Start in fullscreen mode extern int g_fullscreen; // defined in adb.c, because weird driver writing for x @@ -102,7 +108,7 @@ int parse_int(const char *str1, int min, int max) printf ( "TMP %d\n", tmp); return tmp; } -int parse_cli_options(int argc, char **argv) { +int parse_cli_options(int argc, char **argv) { int i; int tmp1; int skip_amt; @@ -206,7 +212,7 @@ int parse_cli_options(int argc, char **argv) { tmp1 = strtol(argv[i+1], 0, 0); // no bounds check, not sure what ids we get glogf("%s Setting joystick X axis %d", parse_log_prefix, tmp1); g_joystick_x_axis = tmp1; - i++; + i++; } else if(!strcmp("-joy_y", argv[i])) { if((i+1) >= argc) { glogf("%s Error, option '-joy_y' missing argument", parse_log_prefix); @@ -224,7 +230,7 @@ int parse_cli_options(int argc, char **argv) { tmp1 = strtol(argv[i+1], 0, 0); // no bounds check, not sure what ids we get glogf("%s Setting joystick X2 axis %d", parse_log_prefix, tmp1); g_joystick_x2_axis = tmp1; - i++; + i++; } else if(!strcmp("-joy_y2", argv[i])) { if((i+1) >= argc) { glogf("%s Error, option '-joy_y2' missing argument", parse_log_prefix); diff --git a/src/paddles.c b/src/paddles.c index 22ae9a1..52aa9ed 100644 --- a/src/paddles.c +++ b/src/paddles.c @@ -1,189 +1,175 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" -extern int g_mouse_raw_x; /* from adb.c */ +extern int g_mouse_raw_x; /* from adb.c */ extern int g_mouse_raw_y; -double g_paddle_trig_dcycs = 0.0; -int g_swap_paddles = 0; -int g_invert_paddles = 0; -int g_joystick_scale_factor_x = 0x100; -int g_joystick_scale_factor_y = 0x100; -int g_joystick_trim_amount_x = 0; -int g_joystick_trim_amount_y = 0; +double g_paddle_trig_dcycs = 0.0; +int g_swap_paddles = 0; +int g_invert_paddles = 0; +int g_joystick_scale_factor_x = 0x100; +int g_joystick_scale_factor_y = 0x100; +int g_joystick_trim_amount_x = 0; +int g_joystick_trim_amount_y = 0; -int g_joystick_type = JOYSTICK_TYPE_NATIVE_1; // OG Trying to set native joystick as default -int g_joystick_native_type1 = -1; -int g_joystick_native_type2 = -1; -int g_joystick_native_type = -1; +int g_joystick_type = JOYSTICK_TYPE_NATIVE_1; // OG Trying to set native joystick as default +int g_joystick_native_type1 = -1; +int g_joystick_native_type2 = -1; +int g_joystick_native_type = -1; extern int g_paddle_buttons; -int g_paddle_val[4] = { 0, 0, 0, 0 }; - /* g_paddle_val[0]: Joystick X coord, [1]:Y coord */ +int g_paddle_val[4] = { 0, 0, 0, 0 }; +/* g_paddle_val[0]: Joystick X coord, [1]:Y coord */ -double g_paddle_dcycs[4] = { 0.0, 0.0, 0.0, 0.0 }; - /* g_paddle_dcycs are the dcycs the paddle goes to 0 */ +double g_paddle_dcycs[4] = { 0.0, 0.0, 0.0, 0.0 }; +/* g_paddle_dcycs are the dcycs the paddle goes to 0 */ -void -paddle_fixup_joystick_type() -{ - /* If g_joystick_type points to an illegal value, change it */ - if(g_joystick_type == JOYSTICK_TYPE_NATIVE_1) { - g_joystick_native_type = g_joystick_native_type1; - if(g_joystick_native_type1 < 0) { - g_joystick_type = JOYSTICK_TYPE_KEYPAD; - printf("no joy 1 --- switching to keypad\n"); - } - } - if(g_joystick_type == JOYSTICK_TYPE_NATIVE_2) { - g_joystick_native_type = g_joystick_native_type2; - if(g_joystick_native_type2 < 0) { - g_joystick_type = JOYSTICK_TYPE_KEYPAD; - printf("no joy 2 --- switching to keypad\n"); - } - } +void paddle_fixup_joystick_type() { + /* If g_joystick_type points to an illegal value, change it */ + if(g_joystick_type == JOYSTICK_TYPE_NATIVE_1) { + g_joystick_native_type = g_joystick_native_type1; + if(g_joystick_native_type1 < 0) { + g_joystick_type = JOYSTICK_TYPE_KEYPAD; + printf("no joy 1 --- switching to keypad\n"); + } + } + if(g_joystick_type == JOYSTICK_TYPE_NATIVE_2) { + g_joystick_native_type = g_joystick_native_type2; + if(g_joystick_native_type2 < 0) { + g_joystick_type = JOYSTICK_TYPE_KEYPAD; + printf("no joy 2 --- switching to keypad\n"); + } + } } -void -paddle_trigger(double dcycs) -{ - /* Called by read/write to $c070 */ - g_paddle_trig_dcycs = dcycs; +void paddle_trigger(double dcycs) { + /* Called by read/write to $c070 */ + g_paddle_trig_dcycs = dcycs; - /* Determine what all the paddle values are right now */ - paddle_fixup_joystick_type(); + /* Determine what all the paddle values are right now */ + paddle_fixup_joystick_type(); - switch(g_joystick_type) { - case JOYSTICK_TYPE_KEYPAD: /* Keypad Joystick */ - paddle_trigger_keypad(dcycs); - break; - case JOYSTICK_TYPE_MOUSE: /* Mouse Joystick */ - paddle_trigger_mouse(dcycs); - break; - case JOYSTICK_TYPE_NONE: /* Mouse Joystick */ - paddle_trigger_mouse(dcycs); - break; - default: - joystick_update(dcycs); - } + switch(g_joystick_type) { + case JOYSTICK_TYPE_KEYPAD: /* Keypad Joystick */ + paddle_trigger_keypad(dcycs); + break; + case JOYSTICK_TYPE_MOUSE: /* Mouse Joystick */ + paddle_trigger_mouse(dcycs); + break; + case JOYSTICK_TYPE_NONE: /* Mouse Joystick */ + paddle_trigger_mouse(dcycs); + break; + default: + joystick_update(dcycs); + } } -void -paddle_trigger_mouse(double dcycs) -{ - int val_x, val_y; - int mouse_x, mouse_y; +void paddle_trigger_mouse(double dcycs) { + int val_x, val_y; + int mouse_x, mouse_y; - val_x = 0; + val_x = 0; - mouse_x = g_mouse_raw_x; - mouse_y = g_mouse_raw_y; - /* mous_phys_x is 0->560, convert that to -32768 to + 32767 cyc */ - /* So subtract 280 then mult by 117 */ - val_x = (mouse_x - 280) * 117; + mouse_x = g_mouse_raw_x; + mouse_y = g_mouse_raw_y; + /* mous_phys_x is 0->560, convert that to -32768 to + 32767 cyc */ + /* So subtract 280 then mult by 117 */ + val_x = (mouse_x - 280) * 117; - /* mous_phys_y is 0->384, convert that to -32768 to + 32767 cyc */ - /* so subtract 192 then mult by 180 to overscale it a bit */ - val_y = (mouse_y - 192) * 180; + /* mous_phys_y is 0->384, convert that to -32768 to + 32767 cyc */ + /* so subtract 192 then mult by 180 to overscale it a bit */ + val_y = (mouse_y - 192) * 180; - g_paddle_val[0] = val_x; - g_paddle_val[1] = val_y; - g_paddle_val[2] = 32767; - g_paddle_val[3] = 32767; - g_paddle_buttons |= 0xc; - paddle_update_trigger_dcycs(dcycs); + g_paddle_val[0] = val_x; + g_paddle_val[1] = val_y; + g_paddle_val[2] = 32767; + g_paddle_val[3] = 32767; + g_paddle_buttons |= 0xc; + paddle_update_trigger_dcycs(dcycs); } -void -paddle_trigger_keypad(double dcycs) -{ - int get_y; - int val_x, val_y; +void paddle_trigger_keypad(double dcycs) { + int get_y; + int val_x, val_y; - val_x = adb_get_keypad_xy(get_y=0); - val_y = adb_get_keypad_xy(get_y=1); - /* val_x and val_y are already scale -32768 to +32768 */ + val_x = adb_get_keypad_xy(get_y=0); + val_y = adb_get_keypad_xy(get_y=1); + /* val_x and val_y are already scale -32768 to +32768 */ - g_paddle_val[0] = val_x; - g_paddle_val[1] = val_y; - g_paddle_val[2] = 32767; - g_paddle_val[3] = 32767; - g_paddle_buttons |= 0xc; - paddle_update_trigger_dcycs(dcycs); + g_paddle_val[0] = val_x; + g_paddle_val[1] = val_y; + g_paddle_val[2] = 32767; + g_paddle_val[3] = 32767; + g_paddle_buttons |= 0xc; + paddle_update_trigger_dcycs(dcycs); } -void -paddle_update_trigger_dcycs(double dcycs) -{ - double trig_dcycs; - int val; - int paddle_num; - int scale, trim; - int i; +void paddle_update_trigger_dcycs(double dcycs) { + double trig_dcycs; + int val; + int paddle_num; + int scale, trim; + int i; - for(i = 0; i < 4; i++) { - paddle_num = i; - if(g_swap_paddles) { - paddle_num = i ^ 1; - } - val = g_paddle_val[paddle_num]; - if(g_invert_paddles) { - val = -val; - } - /* convert -32768 to +32768 into 0->2816.0 cycles (the */ - /* paddle delay const) */ - /* First multiply by the scale factor to adjust range */ - if(paddle_num & 1) { - scale = g_joystick_scale_factor_y; - trim = g_joystick_trim_amount_y; - } else { - scale = g_joystick_scale_factor_x; - trim = g_joystick_trim_amount_x; - } + for(i = 0; i < 4; i++) { + paddle_num = i; + if(g_swap_paddles) { + paddle_num = i ^ 1; + } + val = g_paddle_val[paddle_num]; + if(g_invert_paddles) { + val = -val; + } + /* convert -32768 to +32768 into 0->2816.0 cycles (the */ + /* paddle delay const) */ + /* First multiply by the scale factor to adjust range */ + if(paddle_num & 1) { + scale = g_joystick_scale_factor_y; + trim = g_joystick_trim_amount_y; + } else { + scale = g_joystick_scale_factor_x; + trim = g_joystick_trim_amount_x; + } #if 0 - if(i == 0) { - printf("val was %04x(%d) * scale %03x = %d\n", - val, val, scale, (val*scale)>>16); - } + if(i == 0) { + printf("val was %04x(%d) * scale %03x = %d\n", + val, val, scale, (val*scale)>>16); + } #endif - val = val * scale >> 16; - /* Val is now from -128 to + 128 since scale is */ - /* 256=1.0, 128 = 0.5 */ - val = val + 128 + trim; - if(val >= 255) { - val = 280; /* increase range */ - } - trig_dcycs = dcycs + (val * 11.04); - g_paddle_dcycs[i] = trig_dcycs; - } + val = val * scale >> 16; + /* Val is now from -128 to + 128 since scale is */ + /* 256=1.0, 128 = 0.5 */ + val = val + 128 + trim; + if(val >= 255) { + val = 280; /* increase range */ + } + trig_dcycs = dcycs + (val * 11.04); + g_paddle_dcycs[i] = trig_dcycs; + } } -int -read_paddles(double dcycs, int paddle) -{ - double trig_dcycs; +int read_paddles(double dcycs, int paddle) { + double trig_dcycs; - trig_dcycs = g_paddle_dcycs[paddle & 3]; + trig_dcycs = g_paddle_dcycs[paddle & 3]; - if(dcycs < trig_dcycs) { - return 0x80; - } else { - return 0x00; - } + if(dcycs < trig_dcycs) { + return 0x80; + } else { + return 0x00; + } } -void -paddle_update_buttons() -{ - paddle_fixup_joystick_type(); - joystick_update_buttons(); +void paddle_update_buttons() { + paddle_fixup_joystick_type(); + joystick_update_buttons(); } diff --git a/src/parallel.c b/src/parallel.c index 8920c04..2e2c385 100644 --- a/src/parallel.c +++ b/src/parallel.c @@ -1,17 +1,15 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /* -parallel.c - -This file handles the Apple II Parallel Card emulation in slot 1. Its very -basic, but allows for future support of redirecting the output to a real -parallel port, files, and additional types of emulated printers. -*/ + This file handles the Apple II Parallel Card emulation in slot 1. Its very + basic, but allows for future support of redirecting the output to a real + parallel port, files, and additional types of emulated printers. + */ #include "defc.h" #include "printer.h" @@ -21,42 +19,40 @@ extern int g_printer_timeout; word32 printer_vbl_count = 0; int port_block = 0; -byte parallel_read(word16 io_address) -{ - //printf("parallel card status called at %x\n", io_address); - //since we only have a virtual printer, always return state as "Ready" - return 0xff; +byte parallel_read(word16 io_address) { + //printf("parallel card status called at %x\n", io_address); + //since we only have a virtual printer, always return state as "Ready" + return 0xff; } -void parallel_write(word16 io_address, byte val) -{ - //Mask MSB if user has it set. - if(g_parallel_out_masking) { - val = val & 0x7f; - } - //printf("parallel card called at %x\n", io_address); - //send a byte to the virtual printer - //By default all output to $C090 gets sent to the printer - if (io_address == 0x00) - { - port_block = 1; - printer_loop(val); - printer_vbl_count = g_vbl_count+(g_printer_timeout*60); - port_block = 0; - } - return; + +void parallel_write(word16 io_address, byte val) { + //Mask MSB if user has it set. + if(g_parallel_out_masking) { + val = val & 0x7f; + } + //printf("parallel card called at %x\n", io_address); + //send a byte to the virtual printer + //By default all output to $C090 gets sent to the printer + if (io_address == 0x00) + { + port_block = 1; + printer_loop(val); + printer_vbl_count = g_vbl_count+(g_printer_timeout*60); + port_block = 0; + } + return; } //This function handles the automatic timeout of the virtual printer if an //application doesn't send a form feed at the end of the page. It also //allows multipage mode Postscript and native printer documents to //print somewhat how a regular application would. -void printer_update() -{ - if (port_block != 1 && printer_vbl_count != 0 && g_vbl_count >= printer_vbl_count) - { - printf("Calling printer_update and flushing!\n"); - printer_feed(); - printer_vbl_count = 0; - } - return; +void printer_update() { + if (port_block != 1 && printer_vbl_count != 0 && g_vbl_count >= printer_vbl_count) + { + printf("Calling printer_update and flushing!\n"); + printer_feed(); + printer_vbl_count = 0; + } + return; } diff --git a/src/partls.c b/src/partls.c index dc71782..43338b8 100644 --- a/src/partls.c +++ b/src/partls.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include @@ -11,131 +11,127 @@ #include -#define BUF_SIZE 65536 -char buf[BUF_SIZE]; +#define BUF_SIZE 65536 +char buf[BUF_SIZE]; -void -read_block(int fd, char *buf, int blk, int blk_size) -{ - int ret; +void read_block(int fd, char *buf, int blk, int blk_size) { + int ret; - ret = lseek(fd, blk * blk_size, SEEK_SET); - if(ret != blk * blk_size) { - printf("lseek: %d, errno: %d\n", ret, errno); - exit(1); - } + ret = lseek(fd, blk * blk_size, SEEK_SET); + if(ret != blk * blk_size) { + printf("lseek: %d, errno: %d\n", ret, errno); + exit(1); + } - ret = read(fd, buf, blk_size); - if(ret != blk_size) { - printf("ret: %d, errno: %d\n", ret, errno); - exit(1); - } + ret = read(fd, buf, blk_size); + if(ret != blk_size) { + printf("ret: %d, errno: %d\n", ret, errno); + exit(1); + } } -int -main(int argc, char **argv) -{ - Driver_desc *driver_desc_ptr; - Part_map *part_map_ptr; - double dsize; - int fd; - int block_size; - word32 sig; - word32 map_blk_cnt; - word32 phys_part_start; - word32 part_blk_cnt; - word32 data_start; - word32 data_cnt; - int map_blocks; - int cur; - int long_form; - int last_arg; - int i; +int main(int argc, char **argv) { + Driver_desc *driver_desc_ptr; + Part_map *part_map_ptr; + double dsize; + int fd; + int block_size; + word32 sig; + word32 map_blk_cnt; + word32 phys_part_start; + word32 part_blk_cnt; + word32 data_start; + word32 data_cnt; + int map_blocks; + int cur; + int long_form; + int last_arg; + int i; - /* parse args */ - long_form = 0; - last_arg = 1; - for(i = 1; i < argc; i++) { - if(!strcmp("-l", argv[i])) { - long_form = 1; - } else { - last_arg = i; - break; - } - } + /* parse args */ + long_form = 0; + last_arg = 1; + for(i = 1; i < argc; i++) { + if(!strcmp("-l", argv[i])) { + long_form = 1; + } else { + last_arg = i; + break; + } + } - fd = open(argv[last_arg], O_RDONLY | O_BINARY, 0x1b6); - if(fd < 0) { - printf("open %s, ret: %d, errno:%d\n", argv[last_arg],fd,errno); - exit(1); - } - if(long_form) { - printf("fd: %d\n", fd); - } + fd = open(argv[last_arg], O_RDONLY | O_BINARY, 0x1b6); + if(fd < 0) { + printf("open %s, ret: %d, errno:%d\n", argv[last_arg],fd,errno); + exit(1); + } + if(long_form) { + printf("fd: %d\n", fd); + } - block_size = 512; - read_block(fd, buf, 0, block_size); + block_size = 512; + read_block(fd, buf, 0, block_size); - driver_desc_ptr = (Driver_desc *)buf; - sig = GET_BE_WORD16(driver_desc_ptr->sig); - block_size = GET_BE_WORD16(driver_desc_ptr->blk_size); - if(long_form) { - printf("sig: %04x, blksize: %04x\n", sig, block_size); - } - if(block_size == 0) { - block_size = 512; - } + driver_desc_ptr = (Driver_desc *)buf; + sig = GET_BE_WORD16(driver_desc_ptr->sig); + block_size = GET_BE_WORD16(driver_desc_ptr->blk_size); + if(long_form) { + printf("sig: %04x, blksize: %04x\n", sig, block_size); + } + if(block_size == 0) { + block_size = 512; + } - if(sig == 0x4552 && block_size >= 0x200) { - if(long_form) { - printf("good!\n"); - } - } else { - printf("bad sig:%04x or block_size:%04x!\n", sig, block_size); - exit(1); - } + if(sig == 0x4552 && block_size >= 0x200) { + if(long_form) { + printf("good!\n"); + } + } else { + printf("bad sig:%04x or block_size:%04x!\n", sig, block_size); + exit(1); + } - map_blocks = 1; - cur = 0; - while(cur < map_blocks) { - read_block(fd, buf, cur + 1, block_size); - part_map_ptr = (Part_map *)buf; - sig = GET_BE_WORD16(part_map_ptr->sig); - map_blk_cnt = GET_BE_WORD32(part_map_ptr->map_blk_cnt); - phys_part_start = GET_BE_WORD32(part_map_ptr->phys_part_start); - part_blk_cnt = GET_BE_WORD32(part_map_ptr->part_blk_cnt); - data_start = GET_BE_WORD32(part_map_ptr->data_start); - data_cnt = GET_BE_WORD32(part_map_ptr->data_cnt); + map_blocks = 1; + cur = 0; + while(cur < map_blocks) { + read_block(fd, buf, cur + 1, block_size); + part_map_ptr = (Part_map *)buf; + sig = GET_BE_WORD16(part_map_ptr->sig); + map_blk_cnt = GET_BE_WORD32(part_map_ptr->map_blk_cnt); + phys_part_start = GET_BE_WORD32(part_map_ptr->phys_part_start); + part_blk_cnt = GET_BE_WORD32(part_map_ptr->part_blk_cnt); + data_start = GET_BE_WORD32(part_map_ptr->data_start); + data_cnt = GET_BE_WORD32(part_map_ptr->data_cnt); - if(cur == 0) { - map_blocks = MIN(100, map_blk_cnt); - } + if(cur == 0) { + map_blocks = MIN(100, map_blk_cnt); + } - if(long_form) { - printf("%2d: sig: %04x, map_blk_cnt: %d, " - "phys_part_start: %08x, part_blk_cnt: %08x\n", - cur, sig, map_blk_cnt, phys_part_start, - part_blk_cnt); - printf(" part_name: %s, part_type: %s\n", - part_map_ptr->part_name, - part_map_ptr->part_type); - printf(" data_start:%08x, data_cnt:%08x status:%08x\n", - GET_BE_WORD32(part_map_ptr->data_start), - GET_BE_WORD32(part_map_ptr->data_cnt), - GET_BE_WORD32(part_map_ptr->part_status)); - printf(" processor: %s\n", part_map_ptr->processor); - } else { - dsize = (double)GET_BE_WORD32(part_map_ptr->data_cnt); - printf("%2d:%-20s size=%6.2fMB type=%s\n", cur, - part_map_ptr->part_name, - (dsize/(1024.0*2.0)), - part_map_ptr->part_type); - } + if(long_form) { + printf("%2d: sig: %04x, map_blk_cnt: %d, " + "phys_part_start: %08x, part_blk_cnt: %08x\n", + cur, sig, map_blk_cnt, phys_part_start, + part_blk_cnt); + printf(" part_name: %s, part_type: %s\n", + part_map_ptr->part_name, + part_map_ptr->part_type); + printf(" data_start:%08x, data_cnt:%08x status:%08x\n", + GET_BE_WORD32(part_map_ptr->data_start), + GET_BE_WORD32(part_map_ptr->data_cnt), + GET_BE_WORD32(part_map_ptr->part_status)); + printf(" processor: %s\n", part_map_ptr->processor); + } else { + dsize = (double)GET_BE_WORD32(part_map_ptr->data_cnt); + printf("%2d:%-20s size=%6.2fMB type=%s\n", cur, + part_map_ptr->part_name, + (dsize/(1024.0*2.0)), + part_map_ptr->part_type); + } - cur++; - } + cur++; + } - close(fd); - return 0; + close(fd); + return 0; } diff --git a/src/printer.cpp b/src/printer.cpp index 8b29369..18afa72 100644 --- a/src/printer.cpp +++ b/src/printer.cpp @@ -1,23 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Copyright (C) 2010 - 2011 by GSport contributors - - Based on the KEGS emulator written by and Copyright (C) 2003 Kent Dickey - - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the - Free Software Foundation; either version 2 of the License, or (at your - option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /* * Copyright (C) 2002-2004 The DOSBox Team diff --git a/src/scc.c b/src/scc.c index 7ada739..4d4ea3b 100644 --- a/src/scc.c +++ b/src/scc.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include "scc_llap.h" @@ -26,1588 +26,1525 @@ extern int g_appletalk_turbo; /* port 1 == channel B = slot 2 = c038/c03a */ #include "scc.h" -#define SCC_R14_DPLL_SOURCE_BRG 0x100 -#define SCC_R14_FM_MODE 0x200 +#define SCC_R14_DPLL_SOURCE_BRG 0x100 +#define SCC_R14_FM_MODE 0x200 -#define SCC_DCYCS_PER_PCLK ((DCYCS_1_MHZ) / ((DCYCS_28_MHZ) /8)) -#define SCC_DCYCS_PER_XTAL ((DCYCS_1_MHZ) / 3686400.0) +#define SCC_DCYCS_PER_PCLK ((DCYCS_1_MHZ) / ((DCYCS_28_MHZ) /8)) +#define SCC_DCYCS_PER_XTAL ((DCYCS_1_MHZ) / 3686400.0) -#define SCC_BR_EVENT 1 -#define SCC_TX_EVENT 2 -#define SCC_RX_EVENT 3 -#define SCC_MAKE_EVENT(port, a) (((a) << 1) + (port)) +#define SCC_BR_EVENT 1 +#define SCC_TX_EVENT 2 +#define SCC_RX_EVENT 3 +#define SCC_MAKE_EVENT(port, a) (((a) << 1) + (port)) -Scc scc_stat[2]; +Scc scc_stat[2]; int g_baud_table[] = { - 110, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400 + 110, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400 }; static char* wr_names[] = { - "command", // 0 - "interrupt and transfer mode", // 1 - "interrupt vector", // 2 - "receive params", // 3 - "misc params", // 4 - "trasmit params", // 5 - "sync/addr field", // 6 - "sync/flag", // 7 - "transmit", // 8 - "master interrupt", // 9 - "trans/recv control", // 10 - "clock mode", // 11 - "baud rate (lower)", // 12 - "baud rate (upper)", // 13 - "misc control", // 14 - "ext/status interrupt" // 15 + "command", // 0 + "interrupt and transfer mode", // 1 + "interrupt vector", // 2 + "receive params", // 3 + "misc params", // 4 + "trasmit params", // 5 + "sync/addr field", // 6 + "sync/flag", // 7 + "transmit", // 8 + "master interrupt", // 9 + "trans/recv control", // 10 + "clock mode", // 11 + "baud rate (lower)", // 12 + "baud rate (upper)", // 13 + "misc control", // 14 + "ext/status interrupt" // 15 }; static char* rr_names[] = { - "status", - "special condition", - "interrupt vector", - "pending", - "RR4", - "RR5", - "RR6", - "RR7", - "receive data", - "RR9", - "misc status", - "time constant (lower)", - "time constant (upper)", - "RR14", - "ext/status interrupt" + "status", + "special condition", + "interrupt vector", + "pending", + "RR4", + "RR5", + "RR6", + "RR7", + "receive data", + "RR9", + "misc status", + "time constant (lower)", + "time constant (upper)", + "RR14", + "ext/status interrupt" }; int g_scc_overflow = 0; -void -scc_init() -{ - Scc *scc_ptr; - int i, j; +void scc_init() { + Scc *scc_ptr; + int i, j; - for(i = 0; i < 2; i++) { - scc_ptr = &(scc_stat[i]); - scc_ptr->accfd = -1; - scc_ptr->sockfd = -1; - scc_ptr->socket_state = -1; - scc_ptr->rdwrfd = -1; - scc_ptr->state = 0; - scc_ptr->host_handle = 0; - scc_ptr->host_handle2 = 0; - scc_ptr->br_event_pending = 0; - scc_ptr->rx_event_pending = 0; - scc_ptr->tx_event_pending = 0; - scc_ptr->char_size = 8; - scc_ptr->baud_rate = 115200; - scc_ptr->telnet_mode = 0; - scc_ptr->telnet_iac = 0; - scc_ptr->out_char_dcycs = 0.0; - scc_ptr->socket_num_rings = 0; - scc_ptr->socket_last_ring_dcycs = 0; - scc_ptr->modem_mode = 0; - scc_ptr->modem_dial_or_acc_mode = 0; - scc_ptr->modem_plus_mode = 0; - scc_ptr->modem_s0_val = 0; - scc_ptr->modem_cmd_len = 0; - scc_ptr->modem_cmd_str[0] = 0; - for(j = 0; j < 2; j++) { - scc_ptr->telnet_local_mode[j] = 0; - scc_ptr->telnet_remote_mode[j] = 0; - scc_ptr->telnet_reqwill_mode[j] = 0; - scc_ptr->telnet_reqdo_mode[j] = 0; - } - } + for(i = 0; i < 2; i++) { + scc_ptr = &(scc_stat[i]); + scc_ptr->accfd = -1; + scc_ptr->sockfd = -1; + scc_ptr->socket_state = -1; + scc_ptr->rdwrfd = -1; + scc_ptr->state = 0; + scc_ptr->host_handle = 0; + scc_ptr->host_handle2 = 0; + scc_ptr->br_event_pending = 0; + scc_ptr->rx_event_pending = 0; + scc_ptr->tx_event_pending = 0; + scc_ptr->char_size = 8; + scc_ptr->baud_rate = 115200; + scc_ptr->telnet_mode = 0; + scc_ptr->telnet_iac = 0; + scc_ptr->out_char_dcycs = 0.0; + scc_ptr->socket_num_rings = 0; + scc_ptr->socket_last_ring_dcycs = 0; + scc_ptr->modem_mode = 0; + scc_ptr->modem_dial_or_acc_mode = 0; + scc_ptr->modem_plus_mode = 0; + scc_ptr->modem_s0_val = 0; + scc_ptr->modem_cmd_len = 0; + scc_ptr->modem_cmd_str[0] = 0; + for(j = 0; j < 2; j++) { + scc_ptr->telnet_local_mode[j] = 0; + scc_ptr->telnet_remote_mode[j] = 0; + scc_ptr->telnet_reqwill_mode[j] = 0; + scc_ptr->telnet_reqdo_mode[j] = 0; + } + } - scc_reset(); + scc_reset(); } -void -scc_reset() -{ - Scc *scc_ptr; - int i; +void scc_reset() { + Scc *scc_ptr; + int i; - for(i = 0; i < 2; i++) { - scc_ptr = &(scc_stat[i]); + for(i = 0; i < 2; i++) { + scc_ptr = &(scc_stat[i]); - scc_ptr->port = i; - scc_ptr->mode = 0; - scc_ptr->reg_ptr = 0; - scc_ptr->in_rdptr = 0; - scc_ptr->in_wrptr = 0; - scc_ptr->lad = 0; - scc_ptr->out_rdptr = 0; - scc_ptr->out_wrptr = 0; - scc_ptr->dcd = 0; - scc_ptr->wantint_rx = 0; - scc_ptr->wantint_tx = 0; - scc_ptr->wantint_zerocnt = 0; - scc_ptr->did_int_rx_first = 0; - scc_ptr->irq_pending = 0; - scc_ptr->read_called_this_vbl = 0; - scc_ptr->write_called_this_vbl = 0; - scc_evaluate_ints(i); - scc_hard_reset_port(i); - } + scc_ptr->port = i; + scc_ptr->mode = 0; + scc_ptr->reg_ptr = 0; + scc_ptr->in_rdptr = 0; + scc_ptr->in_wrptr = 0; + scc_ptr->lad = 0; + scc_ptr->out_rdptr = 0; + scc_ptr->out_wrptr = 0; + scc_ptr->dcd = 0; + scc_ptr->wantint_rx = 0; + scc_ptr->wantint_tx = 0; + scc_ptr->wantint_zerocnt = 0; + scc_ptr->did_int_rx_first = 0; + scc_ptr->irq_pending = 0; + scc_ptr->read_called_this_vbl = 0; + scc_ptr->write_called_this_vbl = 0; + scc_evaluate_ints(i); + scc_hard_reset_port(i); + } } -void -scc_hard_reset_port(int port) -{ - Scc *scc_ptr; +void scc_hard_reset_port(int port) { + Scc *scc_ptr; - scc_reset_port(port); + scc_reset_port(port); - scc_ptr = &(scc_stat[port]); - scc_ptr->reg[14] = 0; /* zero bottom two bits */ - scc_ptr->reg[13] = 0; - scc_ptr->reg[12] = 0; - scc_ptr->reg[11] = 0x08; - scc_ptr->reg[10] = 0; - scc_ptr->reg[7] = 0; - scc_ptr->reg[6] = 0; - scc_ptr->reg[5] = 0; - scc_ptr->reg[4] = 0x04; - scc_ptr->reg[3] = 0; - scc_ptr->reg[2] = 0; - scc_ptr->reg[1] = 0; + scc_ptr = &(scc_stat[port]); + scc_ptr->reg[14] = 0; /* zero bottom two bits */ + scc_ptr->reg[13] = 0; + scc_ptr->reg[12] = 0; + scc_ptr->reg[11] = 0x08; + scc_ptr->reg[10] = 0; + scc_ptr->reg[7] = 0; + scc_ptr->reg[6] = 0; + scc_ptr->reg[5] = 0; + scc_ptr->reg[4] = 0x04; + scc_ptr->reg[3] = 0; + scc_ptr->reg[2] = 0; + scc_ptr->reg[1] = 0; - /* HACK HACK: */ - scc_stat[0].reg[9] = 0; /* Clear all interrupts */ + /* HACK HACK: */ + scc_stat[0].reg[9] = 0; /* Clear all interrupts */ - scc_evaluate_ints(port); + scc_evaluate_ints(port); - scc_regen_clocks(port); + scc_regen_clocks(port); } -void -scc_reset_port(int port) -{ - Scc *scc_ptr; +void scc_reset_port(int port) { + Scc *scc_ptr; - scc_ptr = &(scc_stat[port]); - scc_ptr->reg[15] = 0xf8; - scc_ptr->reg[14] &= 0x03; /* 0 most (including >= 0x100) bits */ - scc_ptr->reg[10] = 0; - scc_ptr->reg[5] &= 0x65; /* leave tx bits and sdlc/crc bits */ - scc_ptr->reg[4] |= 0x04; /* Set async mode */ - scc_ptr->reg[3] &= 0xfe; /* clear receiver enable */ - scc_ptr->reg[1] &= 0xfe; /* clear ext int enable */ + scc_ptr = &(scc_stat[port]); + scc_ptr->reg[15] = 0xf8; + scc_ptr->reg[14] &= 0x03; /* 0 most (including >= 0x100) bits */ + scc_ptr->reg[10] = 0; + scc_ptr->reg[5] &= 0x65; /* leave tx bits and sdlc/crc bits */ + scc_ptr->reg[4] |= 0x04; /* Set async mode */ + scc_ptr->reg[3] &= 0xfe; /* clear receiver enable */ + scc_ptr->reg[1] &= 0xfe; /* clear ext int enable */ - scc_ptr->br_is_zero = 0; - scc_ptr->tx_buf_empty = 1; + scc_ptr->br_is_zero = 0; + scc_ptr->tx_buf_empty = 1; - scc_ptr->wantint_rx = 0; - scc_ptr->wantint_tx = 0; - scc_ptr->wantint_zerocnt = 0; + scc_ptr->wantint_rx = 0; + scc_ptr->wantint_tx = 0; + scc_ptr->wantint_zerocnt = 0; - scc_ptr->rx_queue_depth = 0; - scc_ptr->sdlc_eof = 0; - scc_ptr->eom = 1; + scc_ptr->rx_queue_depth = 0; + scc_ptr->sdlc_eof = 0; + scc_ptr->eom = 1; - scc_evaluate_ints(port); + scc_evaluate_ints(port); - scc_regen_clocks(port); + scc_regen_clocks(port); - scc_clr_tx_int(port); - scc_clr_rx_int(port); + scc_clr_tx_int(port); + scc_clr_rx_int(port); } -void -scc_regen_clocks(int port) -{ - Scc *scc_ptr; - double br_dcycs, tx_dcycs, rx_dcycs; - double rx_char_size, tx_char_size; - double clock_mult; - word32 reg4; - word32 reg14; - word32 reg11; - word32 br_const; - word32 baud; - word32 max_diff; - word32 diff; - int sync_mode = 0; - int baud_entries; - int pos; - int i; +void scc_regen_clocks(int port) { + Scc *scc_ptr; + double br_dcycs, tx_dcycs, rx_dcycs; + double rx_char_size, tx_char_size; + double clock_mult; + word32 reg4; + word32 reg14; + word32 reg11; + word32 br_const; + word32 baud; + word32 max_diff; + word32 diff; + int sync_mode = 0; + int baud_entries; + int pos; + int i; - /* Always do baud rate generator */ - scc_ptr = &(scc_stat[port]); - br_const = (scc_ptr->reg[13] << 8) + scc_ptr->reg[12]; - br_const += 2; /* counts down past 0 */ + /* Always do baud rate generator */ + scc_ptr = &(scc_stat[port]); + br_const = (scc_ptr->reg[13] << 8) + scc_ptr->reg[12]; + br_const += 2; /* counts down past 0 */ - reg4 = scc_ptr->reg[4]; - clock_mult = 1.0; - switch((reg4 >> 6) & 3) { - case 0: /* x1 */ - clock_mult = 1.0; - break; - case 1: /* x16 */ - clock_mult = 16.0; - break; - case 2: /* x32 */ - clock_mult = 32.0; - break; - case 3: /* x64 */ - clock_mult = 64.0; - break; - } + reg4 = scc_ptr->reg[4]; + clock_mult = 1.0; + switch((reg4 >> 6) & 3) { + case 0: /* x1 */ + clock_mult = 1.0; + break; + case 1: /* x16 */ + clock_mult = 16.0; + break; + case 2: /* x32 */ + clock_mult = 32.0; + break; + case 3: /* x64 */ + clock_mult = 64.0; + break; + } - br_dcycs = 0.01; - reg14 = scc_ptr->reg[14]; - if(reg14 & 0x1) { - br_dcycs = SCC_DCYCS_PER_XTAL; - if(reg14 & 0x2) { - br_dcycs = SCC_DCYCS_PER_PCLK; - } - } + br_dcycs = 0.01; + reg14 = scc_ptr->reg[14]; + if(reg14 & 0x1) { + br_dcycs = SCC_DCYCS_PER_XTAL; + if(reg14 & 0x2) { + br_dcycs = SCC_DCYCS_PER_PCLK; + } + } - br_dcycs = br_dcycs * (double)br_const; + br_dcycs = br_dcycs * (double)br_const; - tx_dcycs = 1; - rx_dcycs = 1; - reg11 = scc_ptr->reg[11]; - if(((reg11 >> 3) & 3) == 2) { - tx_dcycs = 2.0 * br_dcycs * clock_mult; - } - switch ((reg11 >> 5) & 3) { - case 0: - // Receive clock = RTxC pin (not emulated) - case 1: - // Receive clock = TRxC pin (not emulated) - // The real SCC has external pins that could provide the clock. But, this is not emulated. - break; - case 3: - // Receive clock = DPLL output - // Only LocalTalk uses the DPLL receive clock. We do not, however, emulate the DPLL. - // In this case, the receive clock should be about the same as the transmit clock. - rx_dcycs = tx_dcycs; - break; - case 2: - // Receive clock = BRG output - rx_dcycs = 2.0 * br_dcycs * clock_mult; - break; - } + tx_dcycs = 1; + rx_dcycs = 1; + reg11 = scc_ptr->reg[11]; + if(((reg11 >> 3) & 3) == 2) { + tx_dcycs = 2.0 * br_dcycs * clock_mult; + } + switch ((reg11 >> 5) & 3) { + case 0: + // Receive clock = RTxC pin (not emulated) + case 1: + // Receive clock = TRxC pin (not emulated) + // The real SCC has external pins that could provide the clock. But, this is not emulated. + break; + case 3: + // Receive clock = DPLL output + // Only LocalTalk uses the DPLL receive clock. We do not, however, emulate the DPLL. + // In this case, the receive clock should be about the same as the transmit clock. + rx_dcycs = tx_dcycs; + break; + case 2: + // Receive clock = BRG output + rx_dcycs = 2.0 * br_dcycs * clock_mult; + break; + } - tx_char_size = 8.0; - switch((scc_ptr->reg[5] >> 5) & 0x3) { - case 0: // 5 bits - tx_char_size = 5.0; - break; - case 1: // 7 bits - tx_char_size = 7.0; - break; - case 2: // 6 bits - tx_char_size = 6.0; - break; - } + tx_char_size = 8.0; + switch((scc_ptr->reg[5] >> 5) & 0x3) { + case 0: // 5 bits + tx_char_size = 5.0; + break; + case 1: // 7 bits + tx_char_size = 7.0; + break; + case 2: // 6 bits + tx_char_size = 6.0; + break; + } - scc_ptr->char_size = (int)tx_char_size; + scc_ptr->char_size = (int)tx_char_size; - switch((scc_ptr->reg[4] >> 2) & 0x3) { - case 0: // sync mode (no start or stop bits) - sync_mode = 1; - break; - case 1: // 1 stop bit - tx_char_size += 2.0; // 1 stop + 1 start bit - break; - case 2: // 1.5 stop bit - tx_char_size += 2.5; // 1.5 stop + 1 start bit - break; - case 3: // 2 stop bits - tx_char_size += 3.0; // 2.0 stop + 1 start bit - break; - } + switch((scc_ptr->reg[4] >> 2) & 0x3) { + case 0: // sync mode (no start or stop bits) + sync_mode = 1; + break; + case 1: // 1 stop bit + tx_char_size += 2.0; // 1 stop + 1 start bit + break; + case 2: // 1.5 stop bit + tx_char_size += 2.5; // 1.5 stop + 1 start bit + break; + case 3: // 2 stop bits + tx_char_size += 3.0; // 2.0 stop + 1 start bit + break; + } - if((scc_ptr->reg[4] & 1) && !sync_mode) { - // parity enabled - tx_char_size += 1.0; - } + if((scc_ptr->reg[4] & 1) && !sync_mode) { + // parity enabled + tx_char_size += 1.0; + } - if(scc_ptr->reg[14] & 0x10) { - /* loopback mode, make it go faster...*/ - rx_char_size = 1.0; - tx_char_size = 1.0; - } + if(scc_ptr->reg[14] & 0x10) { + /* loopback mode, make it go faster...*/ + rx_char_size = 1.0; + tx_char_size = 1.0; + } - rx_char_size = tx_char_size; /* HACK */ + rx_char_size = tx_char_size; /* HACK */ - baud = (int)(DCYCS_1_MHZ / tx_dcycs); - max_diff = 5000000; - pos = 0; - baud_entries = sizeof(g_baud_table)/sizeof(g_baud_table[0]); - for(i = 0; i < baud_entries; i++) { - diff = abs((int)(g_baud_table[i] - baud)); - if(diff < max_diff) { - pos = i; - max_diff = diff; - } - } + baud = (int)(DCYCS_1_MHZ / tx_dcycs); + max_diff = 5000000; + pos = 0; + baud_entries = sizeof(g_baud_table)/sizeof(g_baud_table[0]); + for(i = 0; i < baud_entries; i++) { + diff = abs((int)(g_baud_table[i] - baud)); + if(diff < max_diff) { + pos = i; + max_diff = diff; + } + } - scc_ptr->baud_rate = g_baud_table[pos]; + scc_ptr->baud_rate = g_baud_table[pos]; - scc_ptr->br_dcycs = br_dcycs; - scc_ptr->tx_dcycs = tx_dcycs * tx_char_size; - scc_ptr->rx_dcycs = rx_dcycs * rx_char_size; + scc_ptr->br_dcycs = br_dcycs; + scc_ptr->tx_dcycs = tx_dcycs * tx_char_size; + scc_ptr->rx_dcycs = rx_dcycs * rx_char_size; - switch (scc_ptr->state) { - case 1: /* socket */ - scc_socket_change_params(port); - break; - case 2: /* real serial ports */ + switch (scc_ptr->state) { + case 1: /* socket */ + scc_socket_change_params(port); + break; + case 2: /* real serial ports */ #ifdef MAC - scc_serial_mac_change_params(port); + scc_serial_mac_change_params(port); #endif #ifdef _WIN32 - scc_serial_win_change_params(port); + scc_serial_win_change_params(port); #endif - break; - case 3: /* localtalk */ - if (g_appletalk_turbo) - { - // If the user has selected AppleTalk "turbo" mode, increase the baud - // rate to be as fast as possible, limited primarily by the ability of - // the emulated GS to handle data. - scc_ptr->baud_rate = 0; - scc_ptr->br_dcycs = 1; - scc_ptr->tx_dcycs = 1; - scc_ptr->rx_dcycs = 1; - } - break; - case 4: /* Imagewriter */ - scc_ptr->baud_rate = 0; - scc_ptr->tx_dcycs = tx_dcycs * 1.2; //Somehow this speeds up serial transfer without overrunning the buffer - scc_ptr->rx_dcycs = rx_dcycs * 1.2; - break; - } + break; + case 3: /* localtalk */ + if (g_appletalk_turbo) + { + // If the user has selected AppleTalk "turbo" mode, increase the baud + // rate to be as fast as possible, limited primarily by the ability of + // the emulated GS to handle data. + scc_ptr->baud_rate = 0; + scc_ptr->br_dcycs = 1; + scc_ptr->tx_dcycs = 1; + scc_ptr->rx_dcycs = 1; + } + break; + case 4: /* Imagewriter */ + scc_ptr->baud_rate = 0; + scc_ptr->tx_dcycs = tx_dcycs * 1.2; //Somehow this speeds up serial transfer without overrunning the buffer + scc_ptr->rx_dcycs = rx_dcycs * 1.2; + break; + } } -void -scc_port_init(int port) -{ - int state; - state = 0; - switch (g_serial_type[port]) { - case 0: - break; - case 1: - #ifdef MAC - state = scc_serial_mac_init(port); - #endif - #ifdef _WIN32 - state = scc_serial_win_init(port); - #endif - break; - case 2: - state = scc_imagewriter_init(port); - break; - default: - break; - } +void scc_port_init(int port) { + int state; + state = 0; + switch (g_serial_type[port]) { + case 0: + break; + case 1: + #ifdef MAC + state = scc_serial_mac_init(port); + #endif + #ifdef _WIN32 + state = scc_serial_win_init(port); + #endif + break; + case 2: + state = scc_imagewriter_init(port); + break; + default: + break; + } - if(state <= 0) { - scc_socket_init(port); - } + if(state <= 0) { + scc_socket_init(port); + } } -void -scc_try_to_empty_writebuf(int port, double dcycs) -{ - Scc *scc_ptr; - int state; +void scc_try_to_empty_writebuf(int port, double dcycs) { + Scc *scc_ptr; + int state; - scc_ptr = &(scc_stat[port]); - state = scc_ptr->state; - if(scc_ptr->write_called_this_vbl) { - return; - } + scc_ptr = &(scc_stat[port]); + state = scc_ptr->state; + if(scc_ptr->write_called_this_vbl) { + return; + } - scc_ptr->write_called_this_vbl = 1; + scc_ptr->write_called_this_vbl = 1; - switch (state) - { - case 2: + switch (state) + { + case 2: #if defined(MAC) - scc_serial_mac_empty_writebuf(port); + scc_serial_mac_empty_writebuf(port); #endif #if defined(_WIN32) - scc_serial_win_empty_writebuf(port); + scc_serial_win_empty_writebuf(port); #endif - break; - - case 1: - scc_socket_empty_writebuf(port, dcycs); - break; + break; - case 3: - // When we're doing LocalTalk, the write buffer gets emptied at the end of the frame and does not use this function. - break; + case 1: + scc_socket_empty_writebuf(port, dcycs); + break; - case 4: - scc_imagewriter_empty_writebuf(port, dcycs); - break; - } + case 3: + // When we're doing LocalTalk, the write buffer gets emptied at the end of the frame and does not use this function. + break; + + case 4: + scc_imagewriter_empty_writebuf(port, dcycs); + break; + } } -void -scc_try_fill_readbuf(int port, double dcycs) -{ - Scc *scc_ptr; - int space_used_before_rx, space_left; - int space_used_after_rx; - int state; +void scc_try_fill_readbuf(int port, double dcycs) { + Scc *scc_ptr; + int space_used_before_rx, space_left; + int space_used_after_rx; + int state; - scc_ptr = &(scc_stat[port]); - state = scc_ptr->state; + scc_ptr = &(scc_stat[port]); + state = scc_ptr->state; - space_used_before_rx = scc_ptr->in_wrptr - scc_ptr->in_rdptr; - if(space_used_before_rx < 0) { - space_used_before_rx += SCC_INBUF_SIZE; - } - space_left = (7*SCC_INBUF_SIZE/8) - space_used_before_rx; - if(space_left < 1) { - /* Buffer is pretty full, don't try to get more */ - return; - } + space_used_before_rx = scc_ptr->in_wrptr - scc_ptr->in_rdptr; + if(space_used_before_rx < 0) { + space_used_before_rx += SCC_INBUF_SIZE; + } + space_left = (7*SCC_INBUF_SIZE/8) - space_used_before_rx; + if(space_left < 1) { + /* Buffer is pretty full, don't try to get more */ + return; + } #if 0 - if(scc_ptr->read_called_this_vbl) { - return; - } + if(scc_ptr->read_called_this_vbl) { + return; + } #endif - scc_ptr->read_called_this_vbl = 1; + scc_ptr->read_called_this_vbl = 1; - switch (state) - { - case 2: + switch (state) + { + case 2: #if defined(MAC) - scc_serial_mac_fill_readbuf(port, space_left, dcycs); + scc_serial_mac_fill_readbuf(port, space_left, dcycs); #endif #if defined(_WIN32) - scc_serial_win_fill_readbuf(port, space_left, dcycs); + scc_serial_win_fill_readbuf(port, space_left, dcycs); #endif - break; - - case 1: - scc_socket_fill_readbuf(port, space_left, dcycs); - break; + break; - case 3: - // LLAP deals with packets, and we only allow one packet in the read buffer at a time. - // If the buffer is empty, try to fill it with another packet. - if (g_appletalk_bridging && (space_used_before_rx == 0) && (scc_ptr->rx_queue_depth == 0) && !(scc_ptr->sdlc_eof)) - { - scc_llap_fill_readbuf(port, space_left, dcycs); - //scc_maybe_rx_event(port, dcycs); - scc_ptr->sdlc_eof = 0; - break; + case 1: + scc_socket_fill_readbuf(port, space_left, dcycs); + break; - case 4: - scc_imagewriter_fill_readbuf(port, space_left, dcycs); - break; - } - break; - } + case 3: + // LLAP deals with packets, and we only allow one packet in the read buffer at a time. + // If the buffer is empty, try to fill it with another packet. + if (g_appletalk_bridging && (space_used_before_rx == 0) && (scc_ptr->rx_queue_depth == 0) && !(scc_ptr->sdlc_eof)) + { + scc_llap_fill_readbuf(port, space_left, dcycs); + //scc_maybe_rx_event(port, dcycs); + scc_ptr->sdlc_eof = 0; + break; - // Update the LAD (link activity detector), which LocalTalk uses in the CSMA/CA algorithm. - // The real LAD depends on the line coding and data, but the "get bigger when data on RX line" - // emulation is good enough since no software depends on the specific value of the LAD counter. - // Practically, the emulated LLAP interface never has collisions and the LAD, therefore, is not - // useful, but, for sake of correctness and more realisitic timing, emulate the LAD anyway. - space_used_after_rx = scc_ptr->in_wrptr - scc_ptr->in_rdptr; - if(space_used_after_rx < 0) { - space_used_after_rx += SCC_INBUF_SIZE; - } - scc_ptr->lad += space_used_after_rx - space_used_before_rx; + case 4: + scc_imagewriter_fill_readbuf(port, space_left, dcycs); + break; + } + break; + } + + // Update the LAD (link activity detector), which LocalTalk uses in the CSMA/CA algorithm. + // The real LAD depends on the line coding and data, but the "get bigger when data on RX line" + // emulation is good enough since no software depends on the specific value of the LAD counter. + // Practically, the emulated LLAP interface never has collisions and the LAD, therefore, is not + // useful, but, for sake of correctness and more realisitic timing, emulate the LAD anyway. + space_used_after_rx = scc_ptr->in_wrptr - scc_ptr->in_rdptr; + if(space_used_after_rx < 0) { + space_used_after_rx += SCC_INBUF_SIZE; + } + scc_ptr->lad += space_used_after_rx - space_used_before_rx; } -void -scc_update(double dcycs) -{ - if (g_appletalk_bridging && (scc_stat[0].state == 3 || scc_stat[1].state == 3)) - scc_llap_update(); +void scc_update(double dcycs) { + if (g_appletalk_bridging && (scc_stat[0].state == 3 || scc_stat[1].state == 3)) + scc_llap_update(); - /* called each VBL update */ - scc_stat[0].write_called_this_vbl = 0; - scc_stat[1].write_called_this_vbl = 0; - scc_stat[0].read_called_this_vbl = 0; - scc_stat[1].read_called_this_vbl = 0; + /* called each VBL update */ + scc_stat[0].write_called_this_vbl = 0; + scc_stat[1].write_called_this_vbl = 0; + scc_stat[0].read_called_this_vbl = 0; + scc_stat[1].read_called_this_vbl = 0; - scc_try_fill_readbuf(0, dcycs); - scc_try_fill_readbuf(1, dcycs); - scc_stat[0].read_called_this_vbl = 0; - scc_stat[1].read_called_this_vbl = 0; + scc_try_fill_readbuf(0, dcycs); + scc_try_fill_readbuf(1, dcycs); + scc_stat[0].read_called_this_vbl = 0; + scc_stat[1].read_called_this_vbl = 0; - /* LLAP mode only transfers complete packets. Retain the data in the - transmit and receive buffers until the buffers contain one complete packet */ - if (scc_stat[0].state != 3) - { - scc_try_to_empty_writebuf(0, dcycs); - scc_stat[0].write_called_this_vbl = 0; - } - if (scc_stat[1].state != 3) - { - scc_try_to_empty_writebuf(1, dcycs); - scc_stat[1].write_called_this_vbl = 0; - } + /* LLAP mode only transfers complete packets. Retain the data in the + transmit and receive buffers until the buffers contain one complete packet */ + if (scc_stat[0].state != 3) + { + scc_try_to_empty_writebuf(0, dcycs); + scc_stat[0].write_called_this_vbl = 0; + } + if (scc_stat[1].state != 3) + { + scc_try_to_empty_writebuf(1, dcycs); + scc_stat[1].write_called_this_vbl = 0; + } } -void -do_scc_event(int type, double dcycs) -{ - Scc *scc_ptr; - int port; +void do_scc_event(int type, double dcycs) { + Scc *scc_ptr; + int port; - port = type & 1; - type = (type >> 1); + port = type & 1; + type = (type >> 1); - scc_ptr = &(scc_stat[port]); - if(type == SCC_BR_EVENT) { - /* baud rate generator counted down to 0 */ - scc_ptr->br_event_pending = 0; - scc_set_zerocnt_int(port); - scc_maybe_br_event(port, dcycs); - } else if(type == SCC_TX_EVENT) { - scc_ptr->tx_event_pending = 0; - scc_ptr->tx_buf_empty = 1; - scc_handle_tx_event(port, dcycs); - } else if(type == SCC_RX_EVENT) { - scc_ptr->rx_event_pending = 0; - scc_maybe_rx_event(port, dcycs); - } else { - halt_printf("do_scc_event: %08x!\n", type); - } - return; + scc_ptr = &(scc_stat[port]); + if(type == SCC_BR_EVENT) { + /* baud rate generator counted down to 0 */ + scc_ptr->br_event_pending = 0; + scc_set_zerocnt_int(port); + scc_maybe_br_event(port, dcycs); + } else if(type == SCC_TX_EVENT) { + scc_ptr->tx_event_pending = 0; + scc_ptr->tx_buf_empty = 1; + scc_handle_tx_event(port, dcycs); + } else if(type == SCC_RX_EVENT) { + scc_ptr->rx_event_pending = 0; + scc_maybe_rx_event(port, dcycs); + } else { + halt_printf("do_scc_event: %08x!\n", type); + } + return; } -void -show_scc_state() -{ - Scc *scc_ptr; - int i, j; +void show_scc_state() { + Scc *scc_ptr; + int i, j; - for(i = 0; i < 2; i++) { - scc_ptr = &(scc_stat[i]); - printf("SCC port: %d\n", i); - for(j = 0; j < 16; j += 4) { - printf("Reg %2d-%2d: %02x %02x %02x %02x\n", j, j+3, - scc_ptr->reg[j], scc_ptr->reg[j+1], - scc_ptr->reg[j+2], scc_ptr->reg[j+3]); - } - printf("state: %d, accfd: %d, rdwrfd: %d, host:%p, host2:%p\n", - scc_ptr->state, scc_ptr->accfd, scc_ptr->rdwrfd, - scc_ptr->host_handle, scc_ptr->host_handle2); - printf("in_rdptr: %04x, in_wr:%04x, out_rd:%04x, out_wr:%04x\n", - scc_ptr->in_rdptr, scc_ptr->in_wrptr, - scc_ptr->out_rdptr, scc_ptr->out_wrptr); - printf("rx_queue_depth: %d, queue: %02x, %02x, %02x, %02x\n", - scc_ptr->rx_queue_depth, scc_ptr->rx_queue[0], - scc_ptr->rx_queue[1], scc_ptr->rx_queue[2], - scc_ptr->rx_queue[3]); - printf("want_ints: rx:%d, tx:%d, zc:%d\n", - scc_ptr->wantint_rx, scc_ptr->wantint_tx, - scc_ptr->wantint_zerocnt); - printf("ev_pendings: rx:%d, tx:%d, br:%d\n", - scc_ptr->rx_event_pending, - scc_ptr->tx_event_pending, - scc_ptr->br_event_pending); - printf("br_dcycs: %f, tx_dcycs: %f, rx_dcycs: %f\n", - scc_ptr->br_dcycs, scc_ptr->tx_dcycs, - scc_ptr->rx_dcycs); - printf("char_size: %d, baud_rate: %d, mode: %d\n", - scc_ptr->char_size, scc_ptr->baud_rate, - scc_ptr->mode); - printf("modem_dial_mode:%d, telnet_mode:%d iac:%d, " - "modem_cmd_len:%d\n", scc_ptr->modem_dial_or_acc_mode, - scc_ptr->telnet_mode, scc_ptr->telnet_iac, - scc_ptr->modem_cmd_len); - printf("telnet_loc_modes:%08x %08x, telnet_rem_motes:" - "%08x %08x\n", scc_ptr->telnet_local_mode[0], - scc_ptr->telnet_local_mode[1], - scc_ptr->telnet_remote_mode[0], - scc_ptr->telnet_remote_mode[1]); - printf("modem_mode:%08x plus_mode: %d, out_char_dcycs: %f\n", - scc_ptr->modem_mode, scc_ptr->modem_plus_mode, - scc_ptr->out_char_dcycs); - } + for(i = 0; i < 2; i++) { + scc_ptr = &(scc_stat[i]); + printf("SCC port: %d\n", i); + for(j = 0; j < 16; j += 4) { + printf("Reg %2d-%2d: %02x %02x %02x %02x\n", j, j+3, + scc_ptr->reg[j], scc_ptr->reg[j+1], + scc_ptr->reg[j+2], scc_ptr->reg[j+3]); + } + printf("state: %d, accfd: %d, rdwrfd: %d, host:%p, host2:%p\n", + scc_ptr->state, scc_ptr->accfd, scc_ptr->rdwrfd, + scc_ptr->host_handle, scc_ptr->host_handle2); + printf("in_rdptr: %04x, in_wr:%04x, out_rd:%04x, out_wr:%04x\n", + scc_ptr->in_rdptr, scc_ptr->in_wrptr, + scc_ptr->out_rdptr, scc_ptr->out_wrptr); + printf("rx_queue_depth: %d, queue: %02x, %02x, %02x, %02x\n", + scc_ptr->rx_queue_depth, scc_ptr->rx_queue[0], + scc_ptr->rx_queue[1], scc_ptr->rx_queue[2], + scc_ptr->rx_queue[3]); + printf("want_ints: rx:%d, tx:%d, zc:%d\n", + scc_ptr->wantint_rx, scc_ptr->wantint_tx, + scc_ptr->wantint_zerocnt); + printf("ev_pendings: rx:%d, tx:%d, br:%d\n", + scc_ptr->rx_event_pending, + scc_ptr->tx_event_pending, + scc_ptr->br_event_pending); + printf("br_dcycs: %f, tx_dcycs: %f, rx_dcycs: %f\n", + scc_ptr->br_dcycs, scc_ptr->tx_dcycs, + scc_ptr->rx_dcycs); + printf("char_size: %d, baud_rate: %d, mode: %d\n", + scc_ptr->char_size, scc_ptr->baud_rate, + scc_ptr->mode); + printf("modem_dial_mode:%d, telnet_mode:%d iac:%d, " + "modem_cmd_len:%d\n", scc_ptr->modem_dial_or_acc_mode, + scc_ptr->telnet_mode, scc_ptr->telnet_iac, + scc_ptr->modem_cmd_len); + printf("telnet_loc_modes:%08x %08x, telnet_rem_motes:" + "%08x %08x\n", scc_ptr->telnet_local_mode[0], + scc_ptr->telnet_local_mode[1], + scc_ptr->telnet_remote_mode[0], + scc_ptr->telnet_remote_mode[1]); + printf("modem_mode:%08x plus_mode: %d, out_char_dcycs: %f\n", + scc_ptr->modem_mode, scc_ptr->modem_plus_mode, + scc_ptr->out_char_dcycs); + } } -#define LEN_SCC_LOG 5000 +#define LEN_SCC_LOG 5000 STRUCT(Scc_log) { - int regnum; - word32 val; - double dcycs; + int regnum; + word32 val; + double dcycs; }; -Scc_log g_scc_log[LEN_SCC_LOG]; -int g_scc_log_pos = 0; +Scc_log g_scc_log[LEN_SCC_LOG]; +int g_scc_log_pos = 0; #define SCC_REGNUM(wr,port,reg) ((wr << 8) + (port << 4) + reg) -void -scc_log(int regnum, word32 val, double dcycs) -{ - int pos; +void scc_log(int regnum, word32 val, double dcycs) { + int pos; - pos = g_scc_log_pos; - g_scc_log[pos].regnum = regnum; - g_scc_log[pos].val = val; - g_scc_log[pos].dcycs = dcycs; - pos++; - if(pos >= LEN_SCC_LOG) { - pos = 0; - } - g_scc_log_pos = pos; + pos = g_scc_log_pos; + g_scc_log[pos].regnum = regnum; + g_scc_log[pos].val = val; + g_scc_log[pos].dcycs = dcycs; + pos++; + if(pos >= LEN_SCC_LOG) { + pos = 0; + } + g_scc_log_pos = pos; } -void -show_scc_log(void) -{ - double dcycs; - int regnum; - int pos; - int i; - char* name; +void show_scc_log(void) { + double dcycs; + int regnum; + int pos; + int i; + char* name; - pos = g_scc_log_pos; - dcycs = g_cur_dcycs; - printf("SCC log pos: %d, cur dcycs:%f\n", pos, dcycs); - for(i = 0; i < LEN_SCC_LOG; i++) { - pos--; - if(pos < 0) { - pos = LEN_SCC_LOG - 1; - } - regnum = g_scc_log[pos].regnum; - if (regnum >> 8) - name = wr_names[regnum & 0xf]; - else - name = rr_names[regnum & 0xf]; + pos = g_scc_log_pos; + dcycs = g_cur_dcycs; + printf("SCC log pos: %d, cur dcycs:%f\n", pos, dcycs); + for(i = 0; i < LEN_SCC_LOG; i++) { + pos--; + if(pos < 0) { + pos = LEN_SCC_LOG - 1; + } + regnum = g_scc_log[pos].regnum; + if (regnum >> 8) + name = wr_names[regnum & 0xf]; + else + name = rr_names[regnum & 0xf]; - printf("%d:%d:\tport:%d wr:%d reg: %d (%s)\t\tval:%02x \tat t:%f\n", - i, pos, (regnum >> 4) & 0xf, (regnum >> 8), - (regnum & 0xf), - name, - g_scc_log[pos].val, - g_scc_log[pos].dcycs /*- dcycs*/); - } + printf("%d:%d:\tport:%d wr:%d reg: %d (%s)\t\tval:%02x \tat t:%f\n", + i, pos, (regnum >> 4) & 0xf, (regnum >> 8), + (regnum & 0xf), + name, + g_scc_log[pos].val, + g_scc_log[pos].dcycs /*- dcycs*/); + } } -word16 scc_read_lad(int port) -{ - // The IIgs provides a "LocalTalk link activity detector (LAD)" through repurposing the - // MegaII mouse interface. Per the IIgs schematic, the MegaII mouse inputs connect via - // the MSEX and MSEY lines to the RX lines of the SCC between the SCC and the line drivers. - // So, if there's activity on the RX lines, the mouse counters increment. The firmware - // uses this for the "carrier sense" part of the CSMA/CA algorithm. Typical firmware usage - // is to (1) reset the mouse counter, (2) wait a bit, and (3) take action if some activity - // is present. The firmware does not appear to use the specific value of the LAD counter; - // rather, the firmware only considers "zero" and "not zero". - // - // Apple engineers invented the term LAD, and there are references to it in Gus. +word16 scc_read_lad(int port) { + // The IIgs provides a "LocalTalk link activity detector (LAD)" through repurposing the + // MegaII mouse interface. Per the IIgs schematic, the MegaII mouse inputs connect via + // the MSEX and MSEY lines to the RX lines of the SCC between the SCC and the line drivers. + // So, if there's activity on the RX lines, the mouse counters increment. The firmware + // uses this for the "carrier sense" part of the CSMA/CA algorithm. Typical firmware usage + // is to (1) reset the mouse counter, (2) wait a bit, and (3) take action if some activity + // is present. The firmware does not appear to use the specific value of the LAD counter; + // rather, the firmware only considers "zero" and "not zero". + // + // Apple engineers invented the term LAD, and there are references to it in Gus. - if (port != 0 && port != 1) - { - halt_printf("Invalid SCC port.\n"); - return 0; - } + if (port != 0 && port != 1) + { + halt_printf("Invalid SCC port.\n"); + return 0; + } - Scc* scc_ptr = &(scc_stat[port]); - if (g_c041_val & C041_EN_MOUSE) - { - unsigned int temp = scc_ptr->lad; - scc_ptr->lad = 0; - return temp; - } - else - return 0; + Scc* scc_ptr = &(scc_stat[port]); + if (g_c041_val & C041_EN_MOUSE) + { + unsigned int temp = scc_ptr->lad; + scc_ptr->lad = 0; + return temp; + } + else + return 0; } -word32 -scc_read_reg(int port, double dcycs) -{ - Scc *scc_ptr; - word32 ret; - int regnum; +word32 scc_read_reg(int port, double dcycs) { + Scc *scc_ptr; + word32 ret; + int regnum; - scc_ptr = &(scc_stat[port]); - scc_ptr->mode = 0; - regnum = scc_ptr->reg_ptr; + scc_ptr = &(scc_stat[port]); + scc_ptr->mode = 0; + regnum = scc_ptr->reg_ptr; - /* port 0 is channel A, port 1 is channel B */ - switch(regnum) { - case 0: - case 4: - ret = 0x20; - if (scc_ptr->eom) - ret |= 0x40; - if(scc_ptr->dcd) { - ret |= 0x08; - } - ret |= 0x8; /* HACK HACK */ - if(scc_ptr->rx_queue_depth) { - ret |= 0x01; - } - if(scc_ptr->tx_buf_empty) { - ret |= 0x04; - } - if(scc_ptr->br_is_zero) { - ret |= 0x02; - } - //printf("Read scc[%d] stat: %f : %02x\n", port, dcycs, ret); - break; - case 1: - case 5: - /* HACK: residue codes not right */ - ret = 0x07; /* all sent */ - if (scc_ptr->state == 3 && scc_ptr->sdlc_eof) - ret |= 0x80; - break; - case 2: - case 6: - if(port == 0) { - ret = scc_ptr->reg[2]; - } else { + /* port 0 is channel A, port 1 is channel B */ + switch(regnum) { + case 0: + case 4: + ret = 0x20; + if (scc_ptr->eom) + ret |= 0x40; + if(scc_ptr->dcd) { + ret |= 0x08; + } + ret |= 0x8; /* HACK HACK */ + if(scc_ptr->rx_queue_depth) { + ret |= 0x01; + } + if(scc_ptr->tx_buf_empty) { + ret |= 0x04; + } + if(scc_ptr->br_is_zero) { + ret |= 0x02; + } + //printf("Read scc[%d] stat: %f : %02x\n", port, dcycs, ret); + break; + case 1: + case 5: + /* HACK: residue codes not right */ + ret = 0x07; /* all sent */ + if (scc_ptr->state == 3 && scc_ptr->sdlc_eof) + ret |= 0x80; + break; + case 2: + case 6: + if(port == 0) { + ret = scc_ptr->reg[2]; + } else { - halt_printf("Read of RR2B...stopping\n"); - ret = 0; + halt_printf("Read of RR2B...stopping\n"); + ret = 0; #if 0 - ret = scc_stat[0].reg[2]; - wr9 = scc_stat[0].reg[9]; - for(i = 0; i < 8; i++) { - if(ZZZ){}; - } - if(wr9 & 0x10) { - /* wr9 status high */ - - } + ret = scc_stat[0].reg[2]; + wr9 = scc_stat[0].reg[9]; + for(i = 0; i < 8; i++) { + if(ZZZ) {}; + } + if(wr9 & 0x10) { + /* wr9 status high */ + + } #endif - } - break; - case 3: - case 7: - if(port == 0) { - // The interrupt pending register only exists in channel A. - ret = (scc_stat[0].irq_pending << 3) | scc_stat[1].irq_pending; - } else { - ret = 0; - } - break; - case 8: - ret = scc_read_data(port, dcycs); - break; - case 9: - case 13: - ret = scc_ptr->reg[13]; - break; - case 10: - case 14: - ret = 0; - break; - case 11: - case 15: - ret = scc_ptr->reg[15]; - break; - case 12: - ret = scc_ptr->reg[12]; - break; - default: - halt_printf("Tried reading c03%x with regnum: %d!\n", 8+port, - regnum); - ret = 0; - } + } + break; + case 3: + case 7: + if(port == 0) { + // The interrupt pending register only exists in channel A. + ret = (scc_stat[0].irq_pending << 3) | scc_stat[1].irq_pending; + } else { + ret = 0; + } + break; + case 8: + ret = scc_read_data(port, dcycs); + break; + case 9: + case 13: + ret = scc_ptr->reg[13]; + break; + case 10: + case 14: + ret = 0; + break; + case 11: + case 15: + ret = scc_ptr->reg[15]; + break; + case 12: + ret = scc_ptr->reg[12]; + break; + default: + halt_printf("Tried reading c03%x with regnum: %d!\n", 8+port, + regnum); + ret = 0; + } - scc_ptr->reg_ptr = 0; - scc_printf("Read c03%x, rr%d, ret: %02x\n", 8+port, regnum, ret); - //if(regnum != 0 && regnum != 3) { - scc_log(SCC_REGNUM(0,port,regnum), ret, dcycs); - //} + scc_ptr->reg_ptr = 0; + scc_printf("Read c03%x, rr%d, ret: %02x\n", 8+port, regnum, ret); + //if(regnum != 0 && regnum != 3) { + scc_log(SCC_REGNUM(0,port,regnum), ret, dcycs); + //} - return ret; + return ret; } -void -scc_write_reg(int port, word32 val, double dcycs) -{ - Scc *scc_ptr; - word32 old_val; - word32 changed_bits; - word32 irq_mask; - int regnum; - int mode; - int tmp1; +void scc_write_reg(int port, word32 val, double dcycs) { + Scc *scc_ptr; + word32 old_val; + word32 changed_bits; + word32 irq_mask; + int regnum; + int mode; + int tmp1; - scc_ptr = &(scc_stat[port]); - regnum = scc_ptr->reg_ptr & 0xf; - mode = scc_ptr->mode; + scc_ptr = &(scc_stat[port]); + regnum = scc_ptr->reg_ptr & 0xf; + mode = scc_ptr->mode; - // The SCC has more internal registers than memory locations mapped into the CPU's address space. - // To access alternate registers, the CPU writes a register selection code to WR0. The next write - // goes to the selected register. WR0 also contains several command and reset codes, and it is - // possible to write command, reset, and register selection in a single WR0 write. - if(mode == 0) { - // WR0 is selected, and this write goes to WR0. - // Extract the register selection code, which determines the next register access in conjunction with the "point high" command. - scc_ptr->reg_ptr = val & 0x07; - if (((val >> 3) & 0x07) == 0x01) - scc_ptr->reg_ptr |= 0x08; - - // But, this write goes to WR0. - regnum = 0; - if (scc_ptr->reg_ptr) - scc_ptr->mode = 1; - } else { - // Some other register is selected, but the next access goes to register 0. - scc_ptr->reg_ptr = 0; - scc_ptr->mode = 0; - } + // The SCC has more internal registers than memory locations mapped into the CPU's address space. + // To access alternate registers, the CPU writes a register selection code to WR0. The next write + // goes to the selected register. WR0 also contains several command and reset codes, and it is + // possible to write command, reset, and register selection in a single WR0 write. + if(mode == 0) { + // WR0 is selected, and this write goes to WR0. + // Extract the register selection code, which determines the next register access in conjunction with the "point high" command. + scc_ptr->reg_ptr = val & 0x07; + if (((val >> 3) & 0x07) == 0x01) + scc_ptr->reg_ptr |= 0x08; - if ((regnum != 0) || // accesses to registers other than WR0 - ((regnum == 0) && (val & 0xf8)) || // accesses to WR0 only for selecting a register - ((regnum == 0) && ((val & 0x38) == 0x80)) // access to WR0 with a point high register selection - ) - // To keep the log shorter and easier to read, omit register selection code write to WR0. Log everything else. - scc_log(SCC_REGNUM(1,port,regnum), val, dcycs); + // But, this write goes to WR0. + regnum = 0; + if (scc_ptr->reg_ptr) + scc_ptr->mode = 1; + } else { + // Some other register is selected, but the next access goes to register 0. + scc_ptr->reg_ptr = 0; + scc_ptr->mode = 0; + } - changed_bits = (scc_ptr->reg[regnum] ^ val) & 0xff; + if ((regnum != 0) || // accesses to registers other than WR0 + ((regnum == 0) && (val & 0xf8)) || // accesses to WR0 only for selecting a register + ((regnum == 0) && ((val & 0x38) == 0x80)) // access to WR0 with a point high register selection + ) + // To keep the log shorter and easier to read, omit register selection code write to WR0. Log everything else. + scc_log(SCC_REGNUM(1,port,regnum), val, dcycs); - /* Set reg reg */ - switch(regnum) { - case 0: /* wr0 */ - tmp1 = (val >> 3) & 0x7; - switch(tmp1) { - case 0x0: /* null code */ - break; - case 0x1: /* point high */ - break; - case 0x2: /* reset ext/status ints */ - /* should clear other ext ints */ - scc_clr_zerocnt_int(port); - break; - case 0x3: /* send abort (sdlc) */ - halt_printf("Wr c03%x to wr0 of %02x, bad cmd cd:%x!\n", - 8+port, val, tmp1); - scc_ptr->eom = 1; - break; - case 0x4: /* enable int on next rx char */ - scc_ptr->did_int_rx_first = 0; - break; - case 0x5: /* reset tx int pending */ - scc_clr_tx_int(port); - break; - case 0x6: /* reset rr1 bits */ - // Per Section 5.2.1 of the SCC User's Manual, issuing an Error Reset when - // a special condition exists (e.g. EOF) while using "interrupt on first RX" - // mode causes loss of the the data with the special condition from the receive - // FIFO. In some cases, the GS relies on this behavior to clear the final CRC - // byte from the RX FIFO. - // - // Based on experimentation, checking for an active first RX interrupt is incorrect. - // System 5 fails to operate correctly with this check. Anyway, skipping this check - // seems to correct operation, but more investigation is necessary. - if (scc_ptr->sdlc_eof == 1) /*&& (scc_ptr->did_int_rx_first == 1)*/ - { - // Remove and discard one byte (the one causing the current special condition) from the RX FIFO. - int depth = scc_ptr->rx_queue_depth; - if (depth != 0) { - for (int i = 1; i < depth; i++) { - scc_ptr->rx_queue[i - 1] = scc_ptr->rx_queue[i]; - } - scc_ptr->rx_queue_depth = depth - 1; - scc_maybe_rx_event(port, dcycs); - scc_maybe_rx_int(port, dcycs); - } - } + changed_bits = (scc_ptr->reg[regnum] ^ val) & 0xff; - // Reset emulated error bits. Note that we don't emulate all the bits. - scc_ptr->sdlc_eof = 0; - break; - case 0x7: /* reset highest pri int pending */ - irq_mask = g_irq_pending; - if(port == 0) { - /* Move SCC0 ints into SCC1 positions */ - irq_mask = irq_mask >> 3; - } - if(irq_mask & IRQ_PENDING_SCC1_RX) { - scc_clr_rx_int(port); - scc_ptr->irq_pending &= ~IRQ_PENDING_SCC1_RX; - } else if(irq_mask & IRQ_PENDING_SCC1_TX) { - scc_clr_tx_int(port); - scc_ptr->irq_pending &= ~IRQ_PENDING_SCC1_TX; - } else if(irq_mask & IRQ_PENDING_SCC1_ZEROCNT) { - scc_clr_zerocnt_int(port); - scc_ptr->irq_pending &= ~IRQ_PENDING_SCC1_ZEROCNT; - } - break; - default: - halt_printf("Wr c03%x to wr0 of %02x, bad cmd cd:%x!\n", - 8+port, val, tmp1); - } - tmp1 = (val >> 6) & 0x3; - switch(tmp1) { - case 0x0: /* null code */ - break; - case 0x1: /* reset rx crc */ - // Do nothing. Emulated packets never have CRC errors. - break; - case 0x2: /* reset tx crc */ - // Do nothing. Emulated packets never have CRC errors. - break; - case 0x3: /* reset tx underrun/eom latch */ - /* if no extern status pending, or being reset now */ - /* and tx disabled, ext int with tx underrun */ - /* ah, just do nothing */ - //if (!(scc_ptr->reg[5] & 0x08)) - // First, this command has no effect unless the transmitter is disabled. - //scc_ptr->eom = 0; - break; - } - return; - case 1: /* wr1 */ - /* proterm sets this == 0x10, which is int on all rx */ - scc_ptr->reg[regnum] = val; - return; - case 2: /* wr2 */ - /* All values do nothing, let 'em all through! */ - scc_ptr->reg[regnum] = val; - return; - case 3: /* wr3 */ - if((scc_ptr->state != 3) && ((val & 0x0e) != 0x0)) { - halt_printf("Wr c03%x to wr3 of %02x!\n", 8+port, val); - } - old_val = scc_ptr->reg[regnum]; - scc_ptr->reg[regnum] = val; + /* Set reg reg */ + switch(regnum) { + case 0: /* wr0 */ + tmp1 = (val >> 3) & 0x7; + switch(tmp1) { + case 0x0: /* null code */ + break; + case 0x1: /* point high */ + break; + case 0x2: /* reset ext/status ints */ + /* should clear other ext ints */ + scc_clr_zerocnt_int(port); + break; + case 0x3: /* send abort (sdlc) */ + halt_printf("Wr c03%x to wr0 of %02x, bad cmd cd:%x!\n", + 8+port, val, tmp1); + scc_ptr->eom = 1; + break; + case 0x4: /* enable int on next rx char */ + scc_ptr->did_int_rx_first = 0; + break; + case 0x5: /* reset tx int pending */ + scc_clr_tx_int(port); + break; + case 0x6: /* reset rr1 bits */ + // Per Section 5.2.1 of the SCC User's Manual, issuing an Error Reset when + // a special condition exists (e.g. EOF) while using "interrupt on first RX" + // mode causes loss of the the data with the special condition from the receive + // FIFO. In some cases, the GS relies on this behavior to clear the final CRC + // byte from the RX FIFO. + // + // Based on experimentation, checking for an active first RX interrupt is incorrect. + // System 5 fails to operate correctly with this check. Anyway, skipping this check + // seems to correct operation, but more investigation is necessary. + if (scc_ptr->sdlc_eof == 1) /*&& (scc_ptr->did_int_rx_first == 1)*/ + { + // Remove and discard one byte (the one causing the current special condition) from the RX FIFO. + int depth = scc_ptr->rx_queue_depth; + if (depth != 0) { + for (int i = 1; i < depth; i++) { + scc_ptr->rx_queue[i - 1] = scc_ptr->rx_queue[i]; + } + scc_ptr->rx_queue_depth = depth - 1; + scc_maybe_rx_event(port, dcycs); + scc_maybe_rx_int(port, dcycs); + } + } - if (!(old_val & 0x01) && (val & 0x01)) - { - // If the receiver transitions from disabled to enabled, try to pull data into the FIFO. - scc_try_fill_readbuf(port, dcycs); - scc_maybe_rx_event(port, dcycs); - } - - return; - case 4: /* wr4 */ - if((val & 0x30) != 0x00 && (val & 0x30) != 0x20) { - halt_printf("Wr c03%x to wr4 of %02x!\n", 8+port, val); - } + // Reset emulated error bits. Note that we don't emulate all the bits. + scc_ptr->sdlc_eof = 0; + break; + case 0x7: /* reset highest pri int pending */ + irq_mask = g_irq_pending; + if(port == 0) { + /* Move SCC0 ints into SCC1 positions */ + irq_mask = irq_mask >> 3; + } + if(irq_mask & IRQ_PENDING_SCC1_RX) { + scc_clr_rx_int(port); + scc_ptr->irq_pending &= ~IRQ_PENDING_SCC1_RX; + } else if(irq_mask & IRQ_PENDING_SCC1_TX) { + scc_clr_tx_int(port); + scc_ptr->irq_pending &= ~IRQ_PENDING_SCC1_TX; + } else if(irq_mask & IRQ_PENDING_SCC1_ZEROCNT) { + scc_clr_zerocnt_int(port); + scc_ptr->irq_pending &= ~IRQ_PENDING_SCC1_ZEROCNT; + } + break; + default: + halt_printf("Wr c03%x to wr0 of %02x, bad cmd cd:%x!\n", + 8+port, val, tmp1); + } + tmp1 = (val >> 6) & 0x3; + switch(tmp1) { + case 0x0: /* null code */ + break; + case 0x1: /* reset rx crc */ + // Do nothing. Emulated packets never have CRC errors. + break; + case 0x2: /* reset tx crc */ + // Do nothing. Emulated packets never have CRC errors. + break; + case 0x3: /* reset tx underrun/eom latch */ + /* if no extern status pending, or being reset now */ + /* and tx disabled, ext int with tx underrun */ + /* ah, just do nothing */ + //if (!(scc_ptr->reg[5] & 0x08)) + // First, this command has no effect unless the transmitter is disabled. + //scc_ptr->eom = 0; + break; + } + return; + case 1: /* wr1 */ + /* proterm sets this == 0x10, which is int on all rx */ + scc_ptr->reg[regnum] = val; + return; + case 2: /* wr2 */ + /* All values do nothing, let 'em all through! */ + scc_ptr->reg[regnum] = val; + return; + case 3: /* wr3 */ + if((scc_ptr->state != 3) && ((val & 0x0e) != 0x0)) { + halt_printf("Wr c03%x to wr3 of %02x!\n", 8+port, val); + } + old_val = scc_ptr->reg[regnum]; + scc_ptr->reg[regnum] = val; - if (((val >> 4) & 0x3) == 0x02 /* SDLC */ && - ((val >> 2) & 0x3) == 0x00 /* enable sync modes */) - { - if (g_appletalk_bridging) - { - // SDLC mode enabled. Redirect such data to the LocalTalk driver. - scc_ptr->state = 3; - scc_llap_init(); - printf("Enabled LocalTalk on port %d.\n", port); - } - else - printf("Attempted to enable LocalTalk on port %d but bridging is disabled.\n", port); - } + if (!(old_val & 0x01) && (val & 0x01)) + { + // If the receiver transitions from disabled to enabled, try to pull data into the FIFO. + scc_try_fill_readbuf(port, dcycs); + scc_maybe_rx_event(port, dcycs); + } - scc_ptr->reg[regnum] = val; - if(changed_bits) { - scc_regen_clocks(port); - } - return; - case 5: /* wr5 */ - if((val & 0x10) != 0x0) { - halt_printf("Wr c03%x to wr5 of %02x!\n", 8+port, val); - } + return; + case 4: /* wr4 */ + if((val & 0x30) != 0x00 && (val & 0x30) != 0x20) { + halt_printf("Wr c03%x to wr4 of %02x!\n", 8+port, val); + } - // Since we don't emulate the SDLC frame, ignore the CRC polynomial type (bit 2). - // Since the emulated link never has CRC errors, silently accept Tx CRC enable. - if (g_appletalk_bridging && scc_ptr->state == 3) - { - if ((scc_ptr->reg[regnum] & 0x08) && !(val & 0x08)) - { - // When the TX enable changes from enabled to disabled, the GS is about to finish a frame. - // The GS will wait a bit longer for the hardware to finish sending the abort sequence, but - // this is of little concern since we don't have a "real line". - scc_llap_empty_writebuf(port, dcycs); - //scc_ptr->eom = 1; - } - } + if (((val >> 4) & 0x3) == 0x02 /* SDLC */ && + ((val >> 2) & 0x3) == 0x00 /* enable sync modes */) + { + if (g_appletalk_bridging) + { + // SDLC mode enabled. Redirect such data to the LocalTalk driver. + scc_ptr->state = 3; + scc_llap_init(); + printf("Enabled LocalTalk on port %d.\n", port); + } + else + printf("Attempted to enable LocalTalk on port %d but bridging is disabled.\n", port); + } - scc_ptr->reg[regnum] = val; - if(changed_bits & 0x60) { - scc_regen_clocks(port); - } - return; - case 6: /* wr6 */ - if (scc_ptr->state == 3) { - // In SDLC mode (state 3), WR6 contains the node ID for hardware address filtering. - printf("Trying LocalTalk node ID %d.\n", val); - scc_llap_set_node(val); - } - else if(val != 0) { - halt_printf("Wr c03%x to wr6 of %02x!\n", 8+port, val); - } + scc_ptr->reg[regnum] = val; + if(changed_bits) { + scc_regen_clocks(port); + } + return; + case 5: /* wr5 */ + if((val & 0x10) != 0x0) { + halt_printf("Wr c03%x to wr5 of %02x!\n", 8+port, val); + } - scc_ptr->reg[regnum] = val; - return; - case 7: /* wr7 */ - if (((scc_ptr->state == 3) && (val != 0x7e)) || (scc_ptr->state != 3)) - // SDLC requires a sync character of 0x7e, per the SDLC spec. - halt_printf("Wr c03%x to wr7 of %02x!\n", 8+port, val); + // Since we don't emulate the SDLC frame, ignore the CRC polynomial type (bit 2). + // Since the emulated link never has CRC errors, silently accept Tx CRC enable. + if (g_appletalk_bridging && scc_ptr->state == 3) + { + if ((scc_ptr->reg[regnum] & 0x08) && !(val & 0x08)) + { + // When the TX enable changes from enabled to disabled, the GS is about to finish a frame. + // The GS will wait a bit longer for the hardware to finish sending the abort sequence, but + // this is of little concern since we don't have a "real line". + scc_llap_empty_writebuf(port, dcycs); + //scc_ptr->eom = 1; + } + } - scc_ptr->reg[regnum] = val; - return; - case 8: /* wr8 */ - scc_write_data(port, val, dcycs); - return; - case 9: /* wr9 */ - if((val & 0xc0)) { - if(val & 0x80) { - scc_reset_port(0); - } - if(val & 0x40) { - scc_reset_port(1); - } - if((val & 0xc0) == 0xc0) { - scc_hard_reset_port(0); - scc_hard_reset_port(1); - } - } - if((val & 0x35) != 0x00) { - printf("Write c03%x to wr9 of %02x!\n", 8+port, val); - halt_printf("val & 0x35: %02x\n", (val & 0x35)); - } - old_val = scc_stat[0].reg[9]; - scc_stat[0].reg[regnum] = val; - scc_evaluate_ints(0); - scc_evaluate_ints(1); - return; - case 10: /* wr10 */ - if(((val & 0xff) != 0x00) && - ((val & 0xe0) != 0xe0 && scc_ptr->state == 3) /* Allow FM0 */) { - printf("Wr c03%x to wr10 of %02x!\n", 8+port, val); - } - scc_ptr->reg[regnum] = val; - return; - case 11: /* wr11 */ - scc_ptr->reg[regnum] = val; - if(changed_bits) { - scc_regen_clocks(port); - } - return; - case 12: /* wr12 */ - scc_ptr->reg[regnum] = val; - if(changed_bits) { - scc_regen_clocks(port); - } - return; - case 13: /* wr13 */ - scc_ptr->reg[regnum] = val; - if(changed_bits) { - scc_regen_clocks(port); - } - return; - case 14: /* wr14 */ - old_val = scc_ptr->reg[regnum]; - val = val + (old_val & (~0xff)); - switch((val >> 5) & 0x7) { - case 0x0: - // Null command. - case 0x1: - // Enter search mode command. - case 0x2: - // Reset clock missing command - case 0x3: - // Disable PLL command. - break; - - case 0x4: /* DPLL source is BR gen */ - val |= SCC_R14_DPLL_SOURCE_BRG; - break; + scc_ptr->reg[regnum] = val; + if(changed_bits & 0x60) { + scc_regen_clocks(port); + } + return; + case 6: /* wr6 */ + if (scc_ptr->state == 3) { + // In SDLC mode (state 3), WR6 contains the node ID for hardware address filtering. + printf("Trying LocalTalk node ID %d.\n", val); + scc_llap_set_node(val); + } + else if(val != 0) { + halt_printf("Wr c03%x to wr6 of %02x!\n", 8+port, val); + } - case 0x6: - // Set FM mode. - // - // LocalTalk uses this mode. - // Ignore this command because we don't emulate line conding. - if (scc_ptr->state != 3) - halt_printf("Wr c03%x to wr14 of %02x, FM mode!\n", - 8+port, val); - val |= SCC_R14_FM_MODE; - break; + scc_ptr->reg[regnum] = val; + return; + case 7: /* wr7 */ + if (((scc_ptr->state == 3) && (val != 0x7e)) || (scc_ptr->state != 3)) + // SDLC requires a sync character of 0x7e, per the SDLC spec. + halt_printf("Wr c03%x to wr7 of %02x!\n", 8+port, val); - case 0x5: - // Set source = /RTxC. - case 0x7: - // Set NRZI mode. - default: - halt_printf("Wr c03%x to wr14 of %02x, bad dpll cd!\n", - 8+port, val); - } - if((val & 0x0c) != 0x0) { - halt_printf("Wr c03%x to wr14 of %02x!\n", 8+port, val); - } - scc_ptr->reg[regnum] = val; - if(changed_bits) { - scc_regen_clocks(port); - } - scc_maybe_br_event(port, dcycs); - return; - case 15: /* wr15 */ - /* ignore all accesses since IIgs self test messes with it */ - if((val & 0xff) != 0x0) { - scc_printf("Write c03%x to wr15 of %02x!\n", 8+port, - val); - } - if((scc_stat[0].reg[9] & 0x8) && (val != 0)) { - printf("Write wr15:%02x and master int en = 1!\n",val); - /* set_halt(1); */ - } - scc_ptr->reg[regnum] = val; - scc_maybe_br_event(port, dcycs); - scc_evaluate_ints(port); - return; - default: - halt_printf("Wr c03%x to wr%d of %02x!\n", 8+port, regnum, val); - return; - } + scc_ptr->reg[regnum] = val; + return; + case 8: /* wr8 */ + scc_write_data(port, val, dcycs); + return; + case 9: /* wr9 */ + if((val & 0xc0)) { + if(val & 0x80) { + scc_reset_port(0); + } + if(val & 0x40) { + scc_reset_port(1); + } + if((val & 0xc0) == 0xc0) { + scc_hard_reset_port(0); + scc_hard_reset_port(1); + } + } + if((val & 0x35) != 0x00) { + printf("Write c03%x to wr9 of %02x!\n", 8+port, val); + halt_printf("val & 0x35: %02x\n", (val & 0x35)); + } + old_val = scc_stat[0].reg[9]; + scc_stat[0].reg[regnum] = val; + scc_evaluate_ints(0); + scc_evaluate_ints(1); + return; + case 10: /* wr10 */ + if(((val & 0xff) != 0x00) && + ((val & 0xe0) != 0xe0 && scc_ptr->state == 3) /* Allow FM0 */) { + printf("Wr c03%x to wr10 of %02x!\n", 8+port, val); + } + scc_ptr->reg[regnum] = val; + return; + case 11: /* wr11 */ + scc_ptr->reg[regnum] = val; + if(changed_bits) { + scc_regen_clocks(port); + } + return; + case 12: /* wr12 */ + scc_ptr->reg[regnum] = val; + if(changed_bits) { + scc_regen_clocks(port); + } + return; + case 13: /* wr13 */ + scc_ptr->reg[regnum] = val; + if(changed_bits) { + scc_regen_clocks(port); + } + return; + case 14: /* wr14 */ + old_val = scc_ptr->reg[regnum]; + val = val + (old_val & (~0xff)); + switch((val >> 5) & 0x7) { + case 0x0: + // Null command. + case 0x1: + // Enter search mode command. + case 0x2: + // Reset clock missing command + case 0x3: + // Disable PLL command. + break; + + case 0x4: /* DPLL source is BR gen */ + val |= SCC_R14_DPLL_SOURCE_BRG; + break; + + case 0x6: + // Set FM mode. + // + // LocalTalk uses this mode. + // Ignore this command because we don't emulate line conding. + if (scc_ptr->state != 3) + halt_printf("Wr c03%x to wr14 of %02x, FM mode!\n", + 8+port, val); + val |= SCC_R14_FM_MODE; + break; + + case 0x5: + // Set source = /RTxC. + case 0x7: + // Set NRZI mode. + default: + halt_printf("Wr c03%x to wr14 of %02x, bad dpll cd!\n", + 8+port, val); + } + if((val & 0x0c) != 0x0) { + halt_printf("Wr c03%x to wr14 of %02x!\n", 8+port, val); + } + scc_ptr->reg[regnum] = val; + if(changed_bits) { + scc_regen_clocks(port); + } + scc_maybe_br_event(port, dcycs); + return; + case 15: /* wr15 */ + /* ignore all accesses since IIgs self test messes with it */ + if((val & 0xff) != 0x0) { + scc_printf("Write c03%x to wr15 of %02x!\n", 8+port, + val); + } + if((scc_stat[0].reg[9] & 0x8) && (val != 0)) { + printf("Write wr15:%02x and master int en = 1!\n",val); + /* set_halt(1); */ + } + scc_ptr->reg[regnum] = val; + scc_maybe_br_event(port, dcycs); + scc_evaluate_ints(port); + return; + default: + halt_printf("Wr c03%x to wr%d of %02x!\n", 8+port, regnum, val); + return; + } } -void -scc_maybe_br_event(int port, double dcycs) -{ - Scc *scc_ptr; - double br_dcycs; +void scc_maybe_br_event(int port, double dcycs) { + Scc *scc_ptr; + double br_dcycs; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - if(((scc_ptr->reg[14] & 0x01) == 0) || scc_ptr->br_event_pending) { - return; - } - /* also, if ext ints not enabled, don't do baud rate ints */ - if((scc_ptr->reg[15] & 0x02) == 0) { - return; - } + if(((scc_ptr->reg[14] & 0x01) == 0) || scc_ptr->br_event_pending) { + return; + } + /* also, if ext ints not enabled, don't do baud rate ints */ + if((scc_ptr->reg[15] & 0x02) == 0) { + return; + } - br_dcycs = scc_ptr->br_dcycs; - if(br_dcycs < 1.0) { - halt_printf("br_dcycs: %f!\n", br_dcycs); - } + br_dcycs = scc_ptr->br_dcycs; + if(br_dcycs < 1.0) { + halt_printf("br_dcycs: %f!\n", br_dcycs); + } - scc_ptr->br_event_pending = 1; - add_event_scc(dcycs + br_dcycs, SCC_MAKE_EVENT(port, SCC_BR_EVENT)); + scc_ptr->br_event_pending = 1; + add_event_scc(dcycs + br_dcycs, SCC_MAKE_EVENT(port, SCC_BR_EVENT)); } -void -scc_evaluate_ints(int port) -{ - Scc *scc_ptr; - word32 irq_add_mask, irq_remove_mask; - int mie; +void scc_evaluate_ints(int port) { + Scc *scc_ptr; + word32 irq_add_mask, irq_remove_mask; + int mie; - scc_ptr = &(scc_stat[port]); - mie = scc_stat[0].reg[9] & 0x8; /* Master int en */ + scc_ptr = &(scc_stat[port]); + mie = scc_stat[0].reg[9] & 0x8; /* Master int en */ - // The master interrupt enable (MIE) gates assertion of the interrupt line. - // Even if the MIE is disabled, the interrupt pending bits still reflect - // what interrupt would occur if MIE was enabled. Software could poll the - // pending bits, and AppleTalk does exactly this to detect the start of - // a packet. So, we must always calculate the pending interrupts. - irq_add_mask = 0; - irq_remove_mask = 0; - if(scc_ptr->wantint_rx) { - irq_add_mask |= IRQ_PENDING_SCC1_RX; - } else { - irq_remove_mask |= IRQ_PENDING_SCC1_RX; - } - if(scc_ptr->wantint_tx) { - irq_add_mask |= IRQ_PENDING_SCC1_TX; - } else { - irq_remove_mask |= IRQ_PENDING_SCC1_TX; - } - if(scc_ptr->wantint_zerocnt) { - irq_add_mask |= IRQ_PENDING_SCC1_ZEROCNT; - } else { - irq_remove_mask |= IRQ_PENDING_SCC1_ZEROCNT; - } - scc_stat[port].irq_pending &= ~irq_remove_mask; - scc_stat[port].irq_pending |= irq_add_mask; + // The master interrupt enable (MIE) gates assertion of the interrupt line. + // Even if the MIE is disabled, the interrupt pending bits still reflect + // what interrupt would occur if MIE was enabled. Software could poll the + // pending bits, and AppleTalk does exactly this to detect the start of + // a packet. So, we must always calculate the pending interrupts. + irq_add_mask = 0; + irq_remove_mask = 0; + if(scc_ptr->wantint_rx) { + irq_add_mask |= IRQ_PENDING_SCC1_RX; + } else { + irq_remove_mask |= IRQ_PENDING_SCC1_RX; + } + if(scc_ptr->wantint_tx) { + irq_add_mask |= IRQ_PENDING_SCC1_TX; + } else { + irq_remove_mask |= IRQ_PENDING_SCC1_TX; + } + if(scc_ptr->wantint_zerocnt) { + irq_add_mask |= IRQ_PENDING_SCC1_ZEROCNT; + } else { + irq_remove_mask |= IRQ_PENDING_SCC1_ZEROCNT; + } + scc_stat[port].irq_pending &= ~irq_remove_mask; + scc_stat[port].irq_pending |= irq_add_mask; - if(!mie) { - /* There can be no interrupts if MIE=0 */ - remove_irq(IRQ_PENDING_SCC1_RX | IRQ_PENDING_SCC1_TX | - IRQ_PENDING_SCC1_ZEROCNT | - IRQ_PENDING_SCC0_RX | IRQ_PENDING_SCC0_TX | - IRQ_PENDING_SCC0_ZEROCNT); - return; - } - if(port == 0) { - /* Port 1 is in bits 0-2 and port 0 is in bits 3-5 */ - irq_add_mask = irq_add_mask << 3; - irq_remove_mask = irq_remove_mask << 3; - } - if(irq_add_mask) { - add_irq(irq_add_mask); - } - if(irq_remove_mask) { - remove_irq(irq_remove_mask); - } + if(!mie) { + /* There can be no interrupts if MIE=0 */ + remove_irq(IRQ_PENDING_SCC1_RX | IRQ_PENDING_SCC1_TX | + IRQ_PENDING_SCC1_ZEROCNT | + IRQ_PENDING_SCC0_RX | IRQ_PENDING_SCC0_TX | + IRQ_PENDING_SCC0_ZEROCNT); + return; + } + if(port == 0) { + /* Port 1 is in bits 0-2 and port 0 is in bits 3-5 */ + irq_add_mask = irq_add_mask << 3; + irq_remove_mask = irq_remove_mask << 3; + } + if(irq_add_mask) { + add_irq(irq_add_mask); + } + if(irq_remove_mask) { + remove_irq(irq_remove_mask); + } } -void -scc_maybe_rx_event(int port, double dcycs) -{ - Scc *scc_ptr; - double rx_dcycs; - int in_rdptr, in_wrptr; - int depth; +void scc_maybe_rx_event(int port, double dcycs) { + Scc *scc_ptr; + double rx_dcycs; + int in_rdptr, in_wrptr; + int depth; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - if(scc_ptr->rx_event_pending) { - /* one pending already, wait for the event to arrive */ - return; - } + if(scc_ptr->rx_event_pending) { + /* one pending already, wait for the event to arrive */ + return; + } - if (!(scc_ptr->reg[3] & 0x01)) { - // If the receiver is disabled, don't transfer data into the RX FIFO. - return; - } + if (!(scc_ptr->reg[3] & 0x01)) { + // If the receiver is disabled, don't transfer data into the RX FIFO. + return; + } - in_rdptr = scc_ptr->in_rdptr; - in_wrptr = scc_ptr->in_wrptr; - depth = scc_ptr->rx_queue_depth; - if((in_rdptr == in_wrptr) || (depth >= 3)) { - /* no more chars or no more space, just get out */ - return; - } + in_rdptr = scc_ptr->in_rdptr; + in_wrptr = scc_ptr->in_wrptr; + depth = scc_ptr->rx_queue_depth; + if((in_rdptr == in_wrptr) || (depth >= 3)) { + /* no more chars or no more space, just get out */ + return; + } - if(depth < 0) { - depth = 0; - } + if(depth < 0) { + depth = 0; + } - /* pull char from in_rdptr into queue */ - scc_ptr->rx_queue[depth] = scc_ptr->in_buf[in_rdptr]; - scc_ptr->in_rdptr = (in_rdptr + 1) & (SCC_INBUF_SIZE - 1); - scc_ptr->rx_queue_depth = depth + 1; - scc_maybe_rx_int(port, dcycs); - rx_dcycs = scc_ptr->rx_dcycs; - scc_ptr->rx_event_pending = 1; - add_event_scc(dcycs + rx_dcycs, SCC_MAKE_EVENT(port, SCC_RX_EVENT)); + /* pull char from in_rdptr into queue */ + scc_ptr->rx_queue[depth] = scc_ptr->in_buf[in_rdptr]; + scc_ptr->in_rdptr = (in_rdptr + 1) & (SCC_INBUF_SIZE - 1); + scc_ptr->rx_queue_depth = depth + 1; + scc_maybe_rx_int(port, dcycs); + rx_dcycs = scc_ptr->rx_dcycs; + scc_ptr->rx_event_pending = 1; + add_event_scc(dcycs + rx_dcycs, SCC_MAKE_EVENT(port, SCC_RX_EVENT)); } -void -scc_maybe_rx_int(int port, double dcycs) -{ - Scc *scc_ptr; - int depth; - int rx_int_mode; +void scc_maybe_rx_int(int port, double dcycs) { + Scc *scc_ptr; + int depth; + int rx_int_mode; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - depth = scc_ptr->rx_queue_depth; - if(depth <= 0) { - /* no more chars, just get out */ - scc_clr_rx_int(port); - return; - } - rx_int_mode = (scc_ptr->reg[1] >> 3) & 0x3; - switch (rx_int_mode) - { - case 0: - break; - case 1: /* Rx Int On First Characters or Special Condition */ - // Based on experimentation, there's a delay in SDLC mode before the RX on first interrupt goes active. - // Most likely, this delay is due to the address matching requiring complete reception of the destination address field. - if (!scc_ptr->did_int_rx_first && ((scc_ptr->state != 3) || ((scc_ptr->state == 3) && (depth == 2)))) - { - scc_ptr->did_int_rx_first = 1; - scc_ptr->wantint_rx = 1; - } - break; - case 2: /* Int On All Rx Characters or Special Condition */ - scc_ptr->wantint_rx = 1; - break; - case 3: - halt_printf("Unsupported SCC RX interrupt mode 3 (Rx Int On Special Condition Only)."); - break; - } - scc_evaluate_ints(port); + depth = scc_ptr->rx_queue_depth; + if(depth <= 0) { + /* no more chars, just get out */ + scc_clr_rx_int(port); + return; + } + rx_int_mode = (scc_ptr->reg[1] >> 3) & 0x3; + switch (rx_int_mode) + { + case 0: + break; + case 1: /* Rx Int On First Characters or Special Condition */ + // Based on experimentation, there's a delay in SDLC mode before the RX on first interrupt goes active. + // Most likely, this delay is due to the address matching requiring complete reception of the destination address field. + if (!scc_ptr->did_int_rx_first && ((scc_ptr->state != 3) || ((scc_ptr->state == 3) && (depth == 2)))) + { + scc_ptr->did_int_rx_first = 1; + scc_ptr->wantint_rx = 1; + } + break; + case 2: /* Int On All Rx Characters or Special Condition */ + scc_ptr->wantint_rx = 1; + break; + case 3: + halt_printf("Unsupported SCC RX interrupt mode 3 (Rx Int On Special Condition Only)."); + break; + } + scc_evaluate_ints(port); } -void -scc_clr_rx_int(int port) -{ - scc_stat[port].wantint_rx = 0; - scc_evaluate_ints(port); +void scc_clr_rx_int(int port) { + scc_stat[port].wantint_rx = 0; + scc_evaluate_ints(port); } -void -scc_handle_tx_event(int port, double dcycs) -{ - Scc *scc_ptr; - int tx_int_mode; +void scc_handle_tx_event(int port, double dcycs) { + Scc *scc_ptr; + int tx_int_mode; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - /* nothing pending, see if ints on */ - tx_int_mode = (scc_ptr->reg[1] & 0x2); - if(tx_int_mode) { - scc_ptr->wantint_tx = 1; - } - scc_evaluate_ints(port); + /* nothing pending, see if ints on */ + tx_int_mode = (scc_ptr->reg[1] & 0x2); + if(tx_int_mode) { + scc_ptr->wantint_tx = 1; + } + scc_evaluate_ints(port); } -void -scc_maybe_tx_event(int port, double dcycs) -{ - Scc *scc_ptr; - double tx_dcycs; +void scc_maybe_tx_event(int port, double dcycs) { + Scc *scc_ptr; + double tx_dcycs; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - if(scc_ptr->tx_event_pending) { - /* one pending already, tx_buf is full */ - scc_ptr->tx_buf_empty = 0; - } else { - /* nothing pending, see ints on */ - scc_evaluate_ints(port); - tx_dcycs = scc_ptr->tx_dcycs; - scc_ptr->tx_event_pending = 1; - add_event_scc(dcycs + tx_dcycs, - SCC_MAKE_EVENT(port, SCC_TX_EVENT)); - } + if(scc_ptr->tx_event_pending) { + /* one pending already, tx_buf is full */ + scc_ptr->tx_buf_empty = 0; + } else { + /* nothing pending, see ints on */ + scc_evaluate_ints(port); + tx_dcycs = scc_ptr->tx_dcycs; + scc_ptr->tx_event_pending = 1; + add_event_scc(dcycs + tx_dcycs, + SCC_MAKE_EVENT(port, SCC_TX_EVENT)); + } } -void -scc_clr_tx_int(int port) -{ - scc_stat[port].wantint_tx = 0; - scc_evaluate_ints(port); +void scc_clr_tx_int(int port) { + scc_stat[port].wantint_tx = 0; + scc_evaluate_ints(port); } -void -scc_set_zerocnt_int(int port) -{ - Scc *scc_ptr; +void scc_set_zerocnt_int(int port) { + Scc *scc_ptr; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - if(scc_ptr->reg[15] & 0x2) { - scc_ptr->wantint_zerocnt = 1; - } - scc_evaluate_ints(port); + if(scc_ptr->reg[15] & 0x2) { + scc_ptr->wantint_zerocnt = 1; + } + scc_evaluate_ints(port); } -void -scc_clr_zerocnt_int(int port) -{ - scc_stat[port].wantint_zerocnt = 0; - scc_evaluate_ints(port); +void scc_clr_zerocnt_int(int port) { + scc_stat[port].wantint_zerocnt = 0; + scc_evaluate_ints(port); } -void -scc_add_to_readbuf(int port, word32 val, double dcycs) -{ - Scc *scc_ptr; - int in_wrptr; - int in_wrptr_next; - int in_rdptr; +void scc_add_to_readbuf(int port, word32 val, double dcycs) { + Scc *scc_ptr; + int in_wrptr; + int in_wrptr_next; + int in_rdptr; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - in_wrptr = scc_ptr->in_wrptr; - in_rdptr = scc_ptr->in_rdptr; - in_wrptr_next = (in_wrptr + 1) & (SCC_INBUF_SIZE - 1); - if(in_wrptr_next != in_rdptr) { - scc_ptr->in_buf[in_wrptr] = val; - scc_ptr->in_wrptr = in_wrptr_next; - scc_printf("scc in port[%d] add char 0x%02x, %d,%d != %d\n", - scc_ptr->port, val, - in_wrptr, in_wrptr_next, in_rdptr); - g_scc_overflow = 0; - } else { - if(g_scc_overflow == 0) { - g_code_yellow++; - printf("scc inbuf overflow port %d\n", port); - } - g_scc_overflow = 1; - } + in_wrptr = scc_ptr->in_wrptr; + in_rdptr = scc_ptr->in_rdptr; + in_wrptr_next = (in_wrptr + 1) & (SCC_INBUF_SIZE - 1); + if(in_wrptr_next != in_rdptr) { + scc_ptr->in_buf[in_wrptr] = val; + scc_ptr->in_wrptr = in_wrptr_next; + scc_printf("scc in port[%d] add char 0x%02x, %d,%d != %d\n", + scc_ptr->port, val, + in_wrptr, in_wrptr_next, in_rdptr); + g_scc_overflow = 0; + } else { + if(g_scc_overflow == 0) { + g_code_yellow++; + printf("scc inbuf overflow port %d\n", port); + } + g_scc_overflow = 1; + } - scc_maybe_rx_event(port, dcycs); + scc_maybe_rx_event(port, dcycs); } -void -scc_add_to_readbufv(int port, double dcycs, const char *fmt, ...) -{ - va_list ap; - char *bufptr; - int len, c; - int i; +void scc_add_to_readbufv(int port, double dcycs, const char *fmt, ...) { + va_list ap; + char *bufptr; + int len, c; + int i; - va_start(ap, fmt); - bufptr = (char*)malloc(4096); // OG cast added - bufptr[0] = 0; - vsnprintf(bufptr, 4090, fmt, ap); - len = strlen(bufptr); - for(i = 0; i < len; i++) { - c = bufptr[i]; - if(c == 0x0a) { - scc_add_to_readbuf(port, 0x0d, dcycs); - } - scc_add_to_readbuf(port, c, dcycs); - } - va_end(ap); + va_start(ap, fmt); + bufptr = (char*)malloc(4096); // OG cast added + bufptr[0] = 0; + vsnprintf(bufptr, 4090, fmt, ap); + len = strlen(bufptr); + for(i = 0; i < len; i++) { + c = bufptr[i]; + if(c == 0x0a) { + scc_add_to_readbuf(port, 0x0d, dcycs); + } + scc_add_to_readbuf(port, c, dcycs); + } + va_end(ap); } -void -scc_transmit(int port, word32 val, double dcycs) -{ - Scc *scc_ptr; - int out_wrptr; - int out_rdptr; +void scc_transmit(int port, word32 val, double dcycs) { + Scc *scc_ptr; + int out_wrptr; + int out_rdptr; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - /* See if port initialized, if not, do so now */ - if(scc_ptr->state == 0) { - scc_port_init(port); - } - if(scc_ptr->state < 0) { - /* No working serial port, just toss it and go */ - return; - } + /* See if port initialized, if not, do so now */ + if(scc_ptr->state == 0) { + scc_port_init(port); + } + if(scc_ptr->state < 0) { + /* No working serial port, just toss it and go */ + return; + } - if(!scc_ptr->tx_buf_empty) { - /* toss character! */ - printf("Tossing char\n"); - return; - } + if(!scc_ptr->tx_buf_empty) { + /* toss character! */ + printf("Tossing char\n"); + return; + } - out_wrptr = scc_ptr->out_wrptr; - out_rdptr = scc_ptr->out_rdptr; - if(scc_ptr->tx_dcycs < 1.0) { - if(out_wrptr != out_rdptr) { - /* do just one char, then get out */ - printf("tx_dcycs < 1\n"); - return; - } - } - if(g_serial_out_masking && - (scc_ptr->state != 3 /* never mask LLAP data */)) { - val = val & 0x7f; - } + out_wrptr = scc_ptr->out_wrptr; + out_rdptr = scc_ptr->out_rdptr; + if(scc_ptr->tx_dcycs < 1.0) { + if(out_wrptr != out_rdptr) { + /* do just one char, then get out */ + printf("tx_dcycs < 1\n"); + return; + } + } + if(g_serial_out_masking && + (scc_ptr->state != 3 /* never mask LLAP data */)) { + val = val & 0x7f; + } - scc_add_to_writebuf(port, val, dcycs); + scc_add_to_writebuf(port, val, dcycs); } -void -scc_add_to_writebuf(int port, word32 val, double dcycs) -{ - Scc *scc_ptr; - int out_wrptr; - int out_wrptr_next; - int out_rdptr; +void scc_add_to_writebuf(int port, word32 val, double dcycs) { + Scc *scc_ptr; + int out_wrptr; + int out_wrptr_next; + int out_rdptr; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - /* See if port initialized, if not, do so now */ - if(scc_ptr->state == 0) { - scc_port_init(port); - } - if(scc_ptr->state < 0) { - /* No working serial port, just toss it and go */ - return; - } + /* See if port initialized, if not, do so now */ + if(scc_ptr->state == 0) { + scc_port_init(port); + } + if(scc_ptr->state < 0) { + /* No working serial port, just toss it and go */ + return; + } - out_wrptr = scc_ptr->out_wrptr; - out_rdptr = scc_ptr->out_rdptr; + out_wrptr = scc_ptr->out_wrptr; + out_rdptr = scc_ptr->out_rdptr; - out_wrptr_next = (out_wrptr + 1) & (SCC_OUTBUF_SIZE - 1); - if(out_wrptr_next != out_rdptr) { - scc_ptr->out_buf[out_wrptr] = val; - scc_ptr->out_wrptr = out_wrptr_next; - scc_printf("scc wrbuf port %d had char 0x%02x added\n", - scc_ptr->port, val); - g_scc_overflow = 0; - } else { - if(g_scc_overflow == 0) { - g_code_yellow++; - printf("scc outbuf overflow port %d\n", port); - } - g_scc_overflow = 1; - } + out_wrptr_next = (out_wrptr + 1) & (SCC_OUTBUF_SIZE - 1); + if(out_wrptr_next != out_rdptr) { + scc_ptr->out_buf[out_wrptr] = val; + scc_ptr->out_wrptr = out_wrptr_next; + scc_printf("scc wrbuf port %d had char 0x%02x added\n", + scc_ptr->port, val); + g_scc_overflow = 0; + } else { + if(g_scc_overflow == 0) { + g_code_yellow++; + printf("scc outbuf overflow port %d\n", port); + } + g_scc_overflow = 1; + } } -word32 -scc_read_data(int port, double dcycs) -{ - Scc *scc_ptr; - word32 ret; - int depth; - int i; +word32 scc_read_data(int port, double dcycs) { + Scc *scc_ptr; + word32 ret; + int depth; + int i; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - scc_try_fill_readbuf(port, dcycs); + scc_try_fill_readbuf(port, dcycs); - depth = scc_ptr->rx_queue_depth; + depth = scc_ptr->rx_queue_depth; - ret = 0; - if(depth != 0) { - ret = scc_ptr->rx_queue[0]; - for(i = 1; i < depth; i++) { - scc_ptr->rx_queue[i-1] = scc_ptr->rx_queue[i]; - } - scc_ptr->rx_queue_depth = depth - 1; - scc_maybe_rx_event(port, dcycs); - scc_maybe_rx_int(port, dcycs); + ret = 0; + if(depth != 0) { + ret = scc_ptr->rx_queue[0]; + for(i = 1; i < depth; i++) { + scc_ptr->rx_queue[i-1] = scc_ptr->rx_queue[i]; + } + scc_ptr->rx_queue_depth = depth - 1; + scc_maybe_rx_event(port, dcycs); + scc_maybe_rx_int(port, dcycs); - int buffered_rx = scc_ptr->in_wrptr - scc_ptr->in_rdptr; - if(buffered_rx < 0) { - buffered_rx += SCC_INBUF_SIZE; - } - - int bytes_left = buffered_rx + scc_ptr->rx_queue_depth; - if (scc_ptr->state == 3 /* SDLC mode */ && bytes_left == 1) - { - // Flag an end of frame. - scc_ptr->sdlc_eof = 1; - } + int buffered_rx = scc_ptr->in_wrptr - scc_ptr->in_rdptr; + if(buffered_rx < 0) { + buffered_rx += SCC_INBUF_SIZE; + } - //printf("SCC read %04x: ret %02x, depth:%d, buffered: %d\n", 0xc03b - port, ret, scc_ptr->rx_queue_depth, buffered_rx); - } - - scc_printf("SCC read %04x: ret %02x, depth:%d\n", 0xc03b-port, ret, depth); - scc_log(SCC_REGNUM(0,port,8), ret, dcycs); + int bytes_left = buffered_rx + scc_ptr->rx_queue_depth; + if (scc_ptr->state == 3 /* SDLC mode */ && bytes_left == 1) + { + // Flag an end of frame. + scc_ptr->sdlc_eof = 1; + } - return ret; + //printf("SCC read %04x: ret %02x, depth:%d, buffered: %d\n", 0xc03b - port, ret, scc_ptr->rx_queue_depth, buffered_rx); + } + + scc_printf("SCC read %04x: ret %02x, depth:%d\n", 0xc03b-port, ret, depth); + scc_log(SCC_REGNUM(0,port,8), ret, dcycs); + + return ret; } -void -scc_write_data(int port, word32 val, double dcycs) -{ - Scc *scc_ptr; +void scc_write_data(int port, word32 val, double dcycs) { + Scc *scc_ptr; - scc_printf("SCC write %04x: %02x\n", 0xc03b-port, val); - scc_log(SCC_REGNUM(1,port,8), val, dcycs); + scc_printf("SCC write %04x: %02x\n", 0xc03b-port, val); + scc_log(SCC_REGNUM(1,port,8), val, dcycs); - scc_ptr = &(scc_stat[port]); - if(scc_ptr->reg[14] & 0x10) { - /* local loopback! */ - scc_add_to_readbuf(port, val, dcycs); - } else { - scc_transmit(port, val, dcycs); - } - if (scc_ptr->state != 3) { - // If we're doing LLAP, empty the writebuf at the end of the packet. - // Otherwise, empty as soon as possible. - scc_try_to_empty_writebuf(port, dcycs); - } + scc_ptr = &(scc_stat[port]); + if(scc_ptr->reg[14] & 0x10) { + /* local loopback! */ + scc_add_to_readbuf(port, val, dcycs); + } else { + scc_transmit(port, val, dcycs); + } + if (scc_ptr->state != 3) { + // If we're doing LLAP, empty the writebuf at the end of the packet. + // Otherwise, empty as soon as possible. + scc_try_to_empty_writebuf(port, dcycs); + } - scc_maybe_tx_event(port, dcycs); + scc_maybe_tx_event(port, dcycs); } diff --git a/src/scc_imagewriter.c b/src/scc_imagewriter.c index 7a04c4f..7ebca0e 100644 --- a/src/scc_imagewriter.c +++ b/src/scc_imagewriter.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /* This is an interface between the SCC emulation and the Virtual Imagewriter. */ @@ -25,122 +25,118 @@ word32 imagewriter_vbl_count = 0; int imagewriter_port_block = 0; int iw_scc_write = 0; -int scc_imagewriter_init(int port) -{ - Scc *scc_ptr; - scc_ptr = &(scc_stat[port]); - imagewriter_init(g_imagewriter_dpi,g_imagewriter_paper,g_imagewriter_banner,g_imagewriter_output,g_imagewriter_multipage); - scc_ptr->state = 4; - return 4; +int scc_imagewriter_init(int port) { + Scc *scc_ptr; + scc_ptr = &(scc_stat[port]); + imagewriter_init(g_imagewriter_dpi,g_imagewriter_paper,g_imagewriter_banner,g_imagewriter_output,g_imagewriter_multipage); + scc_ptr->state = 4; + return 4; } /** Transfer data from Imagewriter to the SCC **/ -void scc_imagewriter_fill_readbuf(int port, int space_left, double dcycs) -{ - if (iw_scc_write) - { - size_t bytes_read; - size_t i; - byte data[9]; - if (g_imagewriter == 1) - { - //Imagewriter LQ self ID string. Tell machine we have a color ribbon and sheet feeder installed. - data[0] = 0; //Start bit - data[1] ='L'; //Printer type is Imagewriter LQ - data[2] ='Q'; //(cont) - data[3] ='1'; //15 inch carriage width - data[4] ='C'; //Color ribbon installed - data[5] ='F'; //Sheet feeder installed, no envelope attachment - data[6] ='1'; //Number of sheet feeder bins - data[7] = 0x0D; //CR terminates string - data[8] = 0; //Stop bit - bytes_read = 9; - } - else - { - //Imagewriter II self ID string. Tell machine we have a color ribbon and sheet feeder installed. - data[0] = 0; //Start bit - data[1] ='I'; //Printer type is Imagewriter II - data[2] ='W'; //(cont) - data[3] ='1'; //10 inch carriage width - data[4] ='0'; //(cont) - data[5] ='C'; //Color ribbon installed - data[6] ='F'; //Sheet feeder installed - data[7] = 0; //Stop bit - bytes_read = 8; - } - for(i = 0; i < bytes_read; i++) { - scc_add_to_readbuf(port, data[i], dcycs); - } - iw_scc_write = 0; - } +void scc_imagewriter_fill_readbuf(int port, int space_left, double dcycs) { + if (iw_scc_write) + { + size_t bytes_read; + size_t i; + byte data[9]; + if (g_imagewriter == 1) + { + //Imagewriter LQ self ID string. Tell machine we have a color ribbon and sheet feeder installed. + data[0] = 0; //Start bit + data[1] ='L'; //Printer type is Imagewriter LQ + data[2] ='Q'; //(cont) + data[3] ='1'; //15 inch carriage width + data[4] ='C'; //Color ribbon installed + data[5] ='F'; //Sheet feeder installed, no envelope attachment + data[6] ='1'; //Number of sheet feeder bins + data[7] = 0x0D; //CR terminates string + data[8] = 0; //Stop bit + bytes_read = 9; + } + else + { + //Imagewriter II self ID string. Tell machine we have a color ribbon and sheet feeder installed. + data[0] = 0; //Start bit + data[1] ='I'; //Printer type is Imagewriter II + data[2] ='W'; //(cont) + data[3] ='1'; //10 inch carriage width + data[4] ='0'; //(cont) + data[5] ='C'; //Color ribbon installed + data[6] ='F'; //Sheet feeder installed + data[7] = 0; //Stop bit + bytes_read = 8; + } + for(i = 0; i < bytes_read; i++) { + scc_add_to_readbuf(port, data[i], dcycs); + } + iw_scc_write = 0; + } } /** Transfer data from the SCC to the Imagewriter. **/ -void scc_imagewriter_empty_writebuf(int port, double dcycs) -{ - Scc* scc_ptr; +void scc_imagewriter_empty_writebuf(int port, double dcycs) { + Scc* scc_ptr; - int rdptr; - int wrptr; - int len; - int done; - //int ret; - unsigned long bytes_written; + int rdptr; + int wrptr; + int len; + int done; + //int ret; + unsigned long bytes_written; - scc_ptr = &(scc_stat[port]); - done = 0; - while(!done) { - rdptr = scc_ptr->out_rdptr; - wrptr = scc_ptr->out_wrptr; - if(rdptr == wrptr) { - //printf("...rdptr == wrptr\n"); - done = 1; - break; - } - len = wrptr - rdptr; - if(len < 0) { - len = SCC_OUTBUF_SIZE - rdptr; - } - if(len > 32) { - len = 32; - } - if(len <= 0) { - done = 1; - break; - } - bytes_written = 1; - imagewriter_port_block = 1; - imagewriter_loop(scc_ptr->out_buf[rdptr]); - imagewriter_vbl_count = g_vbl_count+(g_imagewriter_timeout*60); - imagewriter_port_block = 0; - //printf("Write Imagewriter ret: %d, bytes_written:%d, len:%d\n", ret, - //(int)bytes_written, len); - - if (bytes_written == 0) { - done = 1; - break; - } else { - rdptr = rdptr + bytes_written; - if(rdptr >= SCC_OUTBUF_SIZE) { - rdptr = rdptr - SCC_OUTBUF_SIZE; - } - scc_ptr->out_rdptr = rdptr; - } - } + scc_ptr = &(scc_stat[port]); + done = 0; + while(!done) { + rdptr = scc_ptr->out_rdptr; + wrptr = scc_ptr->out_wrptr; + if(rdptr == wrptr) { + //printf("...rdptr == wrptr\n"); + done = 1; + break; + } + len = wrptr - rdptr; + if(len < 0) { + len = SCC_OUTBUF_SIZE - rdptr; + } + if(len > 32) { + len = 32; + } + if(len <= 0) { + done = 1; + break; + } + bytes_written = 1; + imagewriter_port_block = 1; + imagewriter_loop(scc_ptr->out_buf[rdptr]); + imagewriter_vbl_count = g_vbl_count+(g_imagewriter_timeout*60); + imagewriter_port_block = 0; + //printf("Write Imagewriter ret: %d, bytes_written:%d, len:%d\n", ret, + //(int)bytes_written, len); + + if (bytes_written == 0) { + done = 1; + break; + } else { + rdptr = rdptr + bytes_written; + if(rdptr >= SCC_OUTBUF_SIZE) { + rdptr = rdptr - SCC_OUTBUF_SIZE; + } + scc_ptr->out_rdptr = rdptr; + } + } } //This function handles the automatic timeout of the virtual printer if an //application doesn't send a form feed at the end of the page. It also //allows multipage mode Postscript and native printer documents to //print somewhat how a regular application would. -void imagewriter_update() -{ - if (imagewriter_port_block != 1 && imagewriter_vbl_count != 0 && g_vbl_count >= imagewriter_vbl_count) - { - printf("Calling imagewriter_update and flushing!\n"); - imagewriter_feed(); - imagewriter_vbl_count = 0; - } - return; +void imagewriter_update() { + if (imagewriter_port_block != 1 && imagewriter_vbl_count != 0 && g_vbl_count >= imagewriter_vbl_count) + { + printf("Calling imagewriter_update and flushing!\n"); + imagewriter_feed(); + imagewriter_vbl_count = 0; + } + return; } diff --git a/src/scc_llap.c b/src/scc_llap.c index 0a072e3..520b864 100644 --- a/src/scc_llap.c +++ b/src/scc_llap.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /* This is an interface between the SCC emulation and the LAP bridge. */ @@ -25,128 +25,118 @@ extern Scc scc_stat[2]; extern int g_config_gsplus_update_needed; static bool bridge_initialized = false; -void scc_llap_init() -{ - atbridge_set_diagnostics(g_appletalk_diagnostics); - bridge_initialized = atbridge_init(); - atbridge_set_net(g_appletalk_network_hint); +void scc_llap_init() { + atbridge_set_diagnostics(g_appletalk_diagnostics); + bridge_initialized = atbridge_init(); + atbridge_set_net(g_appletalk_network_hint); } -void scc_llap_set_node(byte val) -{ - atbridge_set_node(val); +void scc_llap_set_node(byte val) { + atbridge_set_node(val); } -void scc_llap_update() -{ - if (bridge_initialized) - { - atbridge_process(); +void scc_llap_update() { + if (bridge_initialized) + { + atbridge_process(); - // Save the AppleTalk network number. Since the network number does not - // change very often, this will slightly improve startup time. - if (g_appletalk_network_hint != atbridge_get_net()) - { - g_appletalk_network_hint = atbridge_get_net(); - g_config_gsplus_update_needed = 1; - } - } + // Save the AppleTalk network number. Since the network number does not + // change very often, this will slightly improve startup time. + if (g_appletalk_network_hint != atbridge_get_net()) + { + g_appletalk_network_hint = atbridge_get_net(); + g_config_gsplus_update_needed = 1; + } + } } /** Transfer one packet from the bridge to the SCC **/ -void scc_llap_fill_readbuf(int port, int space_left, double dcycs) -{ - atbridge_set_diagnostics(g_appletalk_diagnostics); +void scc_llap_fill_readbuf(int port, int space_left, double dcycs) { + atbridge_set_diagnostics(g_appletalk_diagnostics); - byte* data; + byte* data; - if (!bridge_initialized) - return; + if (!bridge_initialized) + return; - data = NULL; - size_t bytes_read; - size_t i; + data = NULL; + size_t bytes_read; + size_t i; - llap_dequeue_out(dcycs, &bytes_read, &data); + llap_dequeue_out(dcycs, &bytes_read, &data); - for(i = 0; i < bytes_read; i++) { - scc_add_to_readbuf(port, data[i], dcycs); - } + for(i = 0; i < bytes_read; i++) { + scc_add_to_readbuf(port, data[i], dcycs); + } - free(data); + free(data); - // Normally, the bridge updates from the 60 Hz loop, but that alone bottlenecks network throughput. - scc_llap_update(); + // Normally, the bridge updates from the 60 Hz loop, but that alone bottlenecks network throughput. + scc_llap_update(); } /** Transfer one packet from the SCC to the AppleTalk bridge. **/ -void scc_llap_empty_writebuf(int port, double dcycs) -{ - atbridge_set_diagnostics(g_appletalk_diagnostics); +void scc_llap_empty_writebuf(int port, double dcycs) { + atbridge_set_diagnostics(g_appletalk_diagnostics); - Scc* scc_ptr; + Scc* scc_ptr; - if (!bridge_initialized) - return; + if (!bridge_initialized) + return; - int rdptr; - int wrptr; - int len; + int rdptr; + int wrptr; + int len; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - // If there's data in the output buffer, send it. - rdptr = scc_ptr->out_rdptr; - wrptr = scc_ptr->out_wrptr; - if(rdptr == wrptr) - return; - - len = wrptr - rdptr; - if (len < 0) - { - // The data is not contiguous since it wraps around the end of the buffer. - // But, this should never happen since this function always empties the entire - // buffer, and the buffer is large enough to hold the maximum packet size. - halt_printf("SCC LLAP: Unexpected non-contiguous data. Dropping packet.\n"); - } - else - { - // The data is contiguous, so read the data directly from the buffer. - llap_enqueue_in(dcycs, len, &scc_ptr->out_buf[rdptr]); - } - - // Remove the sent data from the output buffer. Since the buffer contains - // one complete packet, always send all of the data. - scc_ptr->out_rdptr = 0; - scc_ptr->out_wrptr = 0; + // If there's data in the output buffer, send it. + rdptr = scc_ptr->out_rdptr; + wrptr = scc_ptr->out_wrptr; + if(rdptr == wrptr) + return; - // Latch EOM to indicate that the SCC has sent the message. - // The timing will be a bit off from the real hardware since we're not - // emulating the sending hardware timing and CRC generation. - scc_ptr->eom = 1; + len = wrptr - rdptr; + if (len < 0) + { + // The data is not contiguous since it wraps around the end of the buffer. + // But, this should never happen since this function always empties the entire + // buffer, and the buffer is large enough to hold the maximum packet size. + halt_printf("SCC LLAP: Unexpected non-contiguous data. Dropping packet.\n"); + } + else + { + // The data is contiguous, so read the data directly from the buffer. + llap_enqueue_in(dcycs, len, &scc_ptr->out_buf[rdptr]); + } - // Normally, the bridge updates from the 60 Hz loop, but that alone bottlenecks network throughput. - scc_llap_update(); + // Remove the sent data from the output buffer. Since the buffer contains + // one complete packet, always send all of the data. + scc_ptr->out_rdptr = 0; + scc_ptr->out_wrptr = 0; + + // Latch EOM to indicate that the SCC has sent the message. + // The timing will be a bit off from the real hardware since we're not + // emulating the sending hardware timing and CRC generation. + scc_ptr->eom = 1; + + // Normally, the bridge updates from the 60 Hz loop, but that alone bottlenecks network throughput. + scc_llap_update(); } #else -void scc_llap_init() -{ +void scc_llap_init() { } -void scc_llap_set_node(byte val) -{ +void scc_llap_set_node(byte val) { } -void scc_llap_update() -{ +void scc_llap_update() { } -void scc_llap_fill_readbuf(int port, int space_left, double dcycs) -{ +void scc_llap_fill_readbuf(int port, int space_left, double dcycs) { } -void scc_llap_empty_writebuf(int port, double dcycs) -{ +void scc_llap_empty_writebuf(int port, double dcycs) { } #endif diff --git a/src/scc_macdriver.c b/src/scc_macdriver.c index b3f9d5e..15eab92 100644 --- a/src/scc_macdriver.c +++ b/src/scc_macdriver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /* This file contains the Mac serial calls */ @@ -18,191 +18,183 @@ extern Scc scc_stat[2]; extern word32 g_c025_val; #ifdef MAC -int -scc_serial_mac_init(int port) -{ - char str_buf[1024]; - Scc *scc_ptr; - int state; - int fd; +int scc_serial_mac_init(int port) { + char str_buf[1024]; + Scc *scc_ptr; + int state; + int fd; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - scc_ptr->state = 0; /* mark as uninitialized */ + scc_ptr->state = 0; /* mark as uninitialized */ - /*sprintf(&str_buf[0], "/dev/tty.USA19QW11P1.1"); */ - sprintf(&str_buf[0], "/dev/tty.USA19H181P1.1"); - /* HACK: fix this... */ + /*sprintf(&str_buf[0], "/dev/tty.USA19QW11P1.1"); */ + sprintf(&str_buf[0], "/dev/tty.USA19H181P1.1"); + /* HACK: fix this... */ - fd = open(&str_buf[0], O_RDWR | O_NONBLOCK); + fd = open(&str_buf[0], O_RDWR | O_NONBLOCK); - scc_ptr->host_handle = (void *)fd; - scc_ptr->host_handle2 = 0; + scc_ptr->host_handle = (void *)fd; + scc_ptr->host_handle2 = 0; - printf("scc_serial_mac_init %d called, fd: %d\n", port, fd); + printf("scc_serial_mac_init %d called, fd: %d\n", port, fd); - if(fd < 0) { - scc_ptr->host_handle = (void *)-1; - return -1; - } + if(fd < 0) { + scc_ptr->host_handle = (void *)-1; + return -1; + } - scc_serial_mac_change_params(port); + scc_serial_mac_change_params(port); - state = 2; /* raw serial */ - scc_ptr->state = state; + state = 2; /* raw serial */ + scc_ptr->state = state; - return state; + return state; } -void -scc_serial_mac_change_params(int port) -{ - struct termios termios_buf; - Scc *scc_ptr; - int fd; - int csz; - int ret; +void scc_serial_mac_change_params(int port) { + struct termios termios_buf; + Scc *scc_ptr; + int fd; + int csz; + int ret; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - fd = (intptr_t)scc_ptr->host_handle; - printf("scc_serial_mac_change_parms port: %d, fd: %d\n", port, fd); - if(fd <= 0) { - return; - } + fd = (intptr_t)scc_ptr->host_handle; + printf("scc_serial_mac_change_parms port: %d, fd: %d\n", port, fd); + if(fd <= 0) { + return; + } - ret = tcgetattr(fd, &termios_buf); - if(ret != 0) { - printf("tcgetattr port%d ret: %d\n", port, ret); - } + ret = tcgetattr(fd, &termios_buf); + if(ret != 0) { + printf("tcgetattr port%d ret: %d\n", port, ret); + } #if 1 - printf("baudrate: %d, iflag:%x, oflag:%x, cflag:%x, lflag:%x\n", - (int)termios_buf.c_ispeed, (int)termios_buf.c_iflag, - (int)termios_buf.c_oflag, (int)termios_buf.c_cflag, - (int)termios_buf.c_lflag); + printf("baudrate: %d, iflag:%x, oflag:%x, cflag:%x, lflag:%x\n", + (int)termios_buf.c_ispeed, (int)termios_buf.c_iflag, + (int)termios_buf.c_oflag, (int)termios_buf.c_cflag, + (int)termios_buf.c_lflag); #endif - memset(&termios_buf, 0, sizeof(struct termios)); - cfmakeraw(&termios_buf); - cfsetspeed(&termios_buf, scc_ptr->baud_rate); + memset(&termios_buf, 0, sizeof(struct termios)); + cfmakeraw(&termios_buf); + cfsetspeed(&termios_buf, scc_ptr->baud_rate); - csz = scc_ptr->char_size; - termios_buf.c_cflag = CREAD | CLOCAL; - termios_buf.c_cflag |= (csz == 5) ? CS5 : - (csz == 6) ? CS6 : - (csz == 7) ? CS7 : - CS8; - switch((scc_ptr->reg[4] >> 2) & 0x3) { - case 2: // 1.5 stop bits - termios_buf.c_cflag |= CSTOPB; /* no 1.5 stop bit setting.*/ - break; - case 3: // 2 stop bits - termios_buf.c_cflag |= CSTOPB; - break; - } + csz = scc_ptr->char_size; + termios_buf.c_cflag = CREAD | CLOCAL; + termios_buf.c_cflag |= (csz == 5) ? CS5 : + (csz == 6) ? CS6 : + (csz == 7) ? CS7 : + CS8; + switch((scc_ptr->reg[4] >> 2) & 0x3) { + case 2: // 1.5 stop bits + termios_buf.c_cflag |= CSTOPB; /* no 1.5 stop bit setting.*/ + break; + case 3: // 2 stop bits + termios_buf.c_cflag |= CSTOPB; + break; + } - switch((scc_ptr->reg[4]) & 0x3) { - case 1: // Odd parity - termios_buf.c_cflag |= (PARENB | PARODD); - break; - case 3: // Even parity - termios_buf.c_cflag |= PARENB; - break; - } + switch((scc_ptr->reg[4]) & 0x3) { + case 1: // Odd parity + termios_buf.c_cflag |= (PARENB | PARODD); + break; + case 3: // Even parity + termios_buf.c_cflag |= PARENB; + break; + } - /* always enabled DTR and RTS control */ - termios_buf.c_cflag |= CDTR_IFLOW | CRTS_IFLOW; + /* always enabled DTR and RTS control */ + termios_buf.c_cflag |= CDTR_IFLOW | CRTS_IFLOW; - printf("fd: %d, baudrate: %d, iflag:%x, oflag:%x, cflag:%x, lflag:%x\n", - fd, (int)termios_buf.c_ispeed, (int)termios_buf.c_iflag, - (int)termios_buf.c_oflag, (int)termios_buf.c_cflag, - (int)termios_buf.c_lflag); - ret = tcsetattr(fd, TCSANOW, &termios_buf); - if(ret != 0) { - printf("tcsetattr ret: %d\n", ret); - } + printf("fd: %d, baudrate: %d, iflag:%x, oflag:%x, cflag:%x, lflag:%x\n", + fd, (int)termios_buf.c_ispeed, (int)termios_buf.c_iflag, + (int)termios_buf.c_oflag, (int)termios_buf.c_cflag, + (int)termios_buf.c_lflag); + ret = tcsetattr(fd, TCSANOW, &termios_buf); + if(ret != 0) { + printf("tcsetattr ret: %d\n", ret); + } } -void -scc_serial_mac_fill_readbuf(int port, int space_left, double dcycs) -{ - byte tmp_buf[256]; - Scc *scc_ptr; - int fd; - int i; - int ret; +void scc_serial_mac_fill_readbuf(int port, int space_left, double dcycs) { + byte tmp_buf[256]; + Scc *scc_ptr; + int fd; + int i; + int ret; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - fd = (intptr_t)scc_ptr->host_handle; - if(fd <= 0) { - return; - } + fd = (intptr_t)scc_ptr->host_handle; + if(fd <= 0) { + return; + } - /* Try reading some bytes */ - space_left = MIN(space_left, 256); - ret = read(fd, tmp_buf, space_left); + /* Try reading some bytes */ + space_left = MIN(space_left, 256); + ret = read(fd, tmp_buf, space_left); + + if(ret > 0) { + for(i = 0; i < ret; i++) { + scc_add_to_readbuf(port, tmp_buf[i], dcycs); + } + } - if(ret > 0) { - for(i = 0; i < ret; i++) { - scc_add_to_readbuf(port, tmp_buf[i], dcycs); - } - } - } -void -scc_serial_mac_empty_writebuf(int port) -{ - Scc *scc_ptr; - int fd; - int rdptr; - int wrptr; - int done; - int ret; - int len; +void scc_serial_mac_empty_writebuf(int port) { + Scc *scc_ptr; + int fd; + int rdptr; + int wrptr; + int done; + int ret; + int len; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - fd = (intptr_t)scc_ptr->host_handle; - if(fd <= 0) { - return; - } + fd = (intptr_t)scc_ptr->host_handle; + if(fd <= 0) { + return; + } - /* Try writing some bytes */ - done = 0; - while(!done) { - rdptr = scc_ptr->out_rdptr; - wrptr = scc_ptr->out_wrptr; - if(rdptr == wrptr) { - //printf("...rdptr == wrptr\n"); - done = 1; - break; - } - len = wrptr - rdptr; - if(len < 0) { - len = SCC_OUTBUF_SIZE - rdptr; - } - if(len > 32) { - len = 32; - } - if(len <= 0) { - done = 1; - break; - } - ret = write(fd, &(scc_ptr->out_buf[rdptr]), len); + /* Try writing some bytes */ + done = 0; + while(!done) { + rdptr = scc_ptr->out_rdptr; + wrptr = scc_ptr->out_wrptr; + if(rdptr == wrptr) { + //printf("...rdptr == wrptr\n"); + done = 1; + break; + } + len = wrptr - rdptr; + if(len < 0) { + len = SCC_OUTBUF_SIZE - rdptr; + } + if(len > 32) { + len = 32; + } + if(len <= 0) { + done = 1; + break; + } + ret = write(fd, &(scc_ptr->out_buf[rdptr]), len); - if(ret <= 0) { - done = 1; - break; - } else { - rdptr = rdptr + ret; - if(rdptr >= SCC_OUTBUF_SIZE) { - rdptr = rdptr - SCC_OUTBUF_SIZE; - } - scc_ptr->out_rdptr = rdptr; - } - } + if(ret <= 0) { + done = 1; + break; + } else { + rdptr = rdptr + ret; + if(rdptr >= SCC_OUTBUF_SIZE) { + rdptr = rdptr - SCC_OUTBUF_SIZE; + } + scc_ptr->out_rdptr = rdptr; + } + } } -#endif /* MAC */ +#endif /* MAC */ diff --git a/src/scc_socket_driver.c b/src/scc_socket_driver.c index 141e9ad..bf43bfd 100644 --- a/src/scc_socket_driver.c +++ b/src/scc_socket_driver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /* This file contains the socket calls */ @@ -11,7 +11,7 @@ #include "scc.h" #include #include -#ifndef UNDER_CE //OG +#ifndef UNDER_CE //OG #include #endif #ifdef __CYGWIN__ @@ -34,1134 +34,1096 @@ typedef unsigned short USHORT; /* outgoing socket. Any hang-up causes the socket to be closed and it will */ /* then re-open on a subsequent call to scc_socket_open */ -void -scc_socket_init(int port) -{ - Scc *scc_ptr; +void scc_socket_init(int port) { + Scc *scc_ptr; #ifdef _WIN32 - WSADATA wsadata; - int ret; + WSADATA wsadata; + int ret; - if(g_wsastartup_called == 0) { - ret = WSAStartup(MAKEWORD(2,0), &wsadata); - printf("WSAStartup ret: %d\n", ret); - g_wsastartup_called = 1; - } + if(g_wsastartup_called == 0) { + ret = WSAStartup(MAKEWORD(2,0), &wsadata); + printf("WSAStartup ret: %d\n", ret); + g_wsastartup_called = 1; + } #endif - scc_ptr = &(scc_stat[port]); - scc_ptr->state = 1; /* successful socket */ - scc_ptr->sockfd = -1; /* Indicate no socket open yet */ - scc_ptr->accfd = -1; /* Indicate no socket open yet */ - scc_ptr->rdwrfd = -1; /* Indicate no socket open yet */ - scc_ptr->socket_state = -2; /* 0 means talk to "modem" */ - /* 1 connected */ - scc_ptr->socket_num_rings = 0; - scc_ptr->socket_last_ring_dcycs = 0; - scc_ptr->dcd = 0; /* 0 means no carrier */ - scc_ptr->modem_s0_val = 0; - scc_ptr->host_aux1 = sizeof(struct sockaddr_in); - scc_ptr->host_handle = malloc(scc_ptr->host_aux1); - /* Real init will be done when bytes need to be read/write from skt */ + scc_ptr = &(scc_stat[port]); + scc_ptr->state = 1; /* successful socket */ + scc_ptr->sockfd = -1; /* Indicate no socket open yet */ + scc_ptr->accfd = -1; /* Indicate no socket open yet */ + scc_ptr->rdwrfd = -1; /* Indicate no socket open yet */ + scc_ptr->socket_state = -2; /* 0 means talk to "modem" */ + /* 1 connected */ + scc_ptr->socket_num_rings = 0; + scc_ptr->socket_last_ring_dcycs = 0; + scc_ptr->dcd = 0; /* 0 means no carrier */ + scc_ptr->modem_s0_val = 0; + scc_ptr->host_aux1 = sizeof(struct sockaddr_in); + scc_ptr->host_handle = malloc(scc_ptr->host_aux1); + /* Real init will be done when bytes need to be read/write from skt */ } -static int -scc_socket_close_handle(SOCKET sockfd) -{ - if (sockfd != -1) - { +static int scc_socket_close_handle(SOCKET sockfd) { + if (sockfd != -1) + { #if defined(_WIN32) - return closesocket(sockfd); // NW: a Windows socket handle is not a file descriptor + return closesocket(sockfd); // NW: a Windows socket handle is not a file descriptor #else - return close(sockfd); + return close(sockfd); #endif - } - return 0; + } + return 0; } -void -scc_socket_maybe_open_incoming(int port, double dcycs) -{ - Scc *scc_ptr; - struct sockaddr_in sa_in; - int on; - int ret; - SOCKET sockfd; - int inc; +void scc_socket_maybe_open_incoming(int port, double dcycs) { + Scc *scc_ptr; + struct sockaddr_in sa_in; + int on; + int ret; + SOCKET sockfd; + int inc; - inc = 0; + inc = 0; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - if(scc_ptr->sockfd != -1) { - /* it's already open, get out */ - return; - } - if(scc_ptr->socket_state < 0) { - /* not initialized; ok, since we need to listen */ - //return; - } + if(scc_ptr->sockfd != -1) { + /* it's already open, get out */ + return; + } + if(scc_ptr->socket_state < 0) { + /* not initialized; ok, since we need to listen */ + //return; + } - printf("scc socket close being called from scc_socket_maybe_open_incoming\n"); - scc_socket_close(port, 0, dcycs); + printf("scc socket close being called from scc_socket_maybe_open_incoming\n"); + scc_socket_close(port, 0, dcycs); - scc_ptr->socket_state = 0; - scc_ptr->socket_num_rings = 0; + scc_ptr->socket_state = 0; + scc_ptr->socket_num_rings = 0; - memset(scc_ptr->host_handle, 0, scc_ptr->host_aux1); + memset(scc_ptr->host_handle, 0, scc_ptr->host_aux1); - while(1) { - sockfd = socket(AF_INET, SOCK_STREAM, 0); - printf("sockfd ret: %d\n", sockfd); - if(sockfd == -1) { - printf("socket ret: %d, errno: %d\n", sockfd, errno); - scc_socket_close(port, 0, dcycs); - scc_ptr->socket_state = -1; - return; - } - /* printf("socket ret: %d\n", sockfd); */ + while(1) { + sockfd = socket(AF_INET, SOCK_STREAM, 0); + printf("sockfd ret: %d\n", sockfd); + if(sockfd == -1) { + printf("socket ret: %d, errno: %d\n", sockfd, errno); + scc_socket_close(port, 0, dcycs); + scc_ptr->socket_state = -1; + return; + } + /* printf("socket ret: %d\n", sockfd); */ - on = 1; - ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, - (char *)&on, sizeof(on)); - if(ret < 0) { - printf("setsockopt REUSEADDR ret: %d, err:%d\n", - ret, errno); - scc_socket_close(port, 0, dcycs); - scc_ptr->socket_state = -1; - return; - } + on = 1; + ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, + (char *)&on, sizeof(on)); + if(ret < 0) { + printf("setsockopt REUSEADDR ret: %d, err:%d\n", + ret, errno); + scc_socket_close(port, 0, dcycs); + scc_ptr->socket_state = -1; + return; + } - memset(&sa_in, 0, sizeof(sa_in)); - sa_in.sin_family = AF_INET; - sa_in.sin_port = htons(6501 + port + inc); - sa_in.sin_addr.s_addr = htonl(INADDR_ANY); + memset(&sa_in, 0, sizeof(sa_in)); + sa_in.sin_family = AF_INET; + sa_in.sin_port = htons(6501 + port + inc); + sa_in.sin_addr.s_addr = htonl(INADDR_ANY); - ret = bind(sockfd, (struct sockaddr *)&sa_in, sizeof(sa_in)); + ret = bind(sockfd, (struct sockaddr *)&sa_in, sizeof(sa_in)); - if(ret >= 0) { - ret = listen(sockfd, 1); - break; - } - /* else ret to bind was < 0 */ - printf("bind ret: %d, errno: %d\n", ret, errno); - inc++; - scc_socket_close_handle(sockfd); - printf("Trying next port: %d\n", 6501 + port + inc); - if(inc >= 10) { - printf("Too many retries, quitting\n"); - scc_socket_close(port, 0, dcycs); - scc_ptr->socket_state = -1; - return; - } - } + if(ret >= 0) { + ret = listen(sockfd, 1); + break; + } + /* else ret to bind was < 0 */ + printf("bind ret: %d, errno: %d\n", ret, errno); + inc++; + scc_socket_close_handle(sockfd); + printf("Trying next port: %d\n", 6501 + port + inc); + if(inc >= 10) { + printf("Too many retries, quitting\n"); + scc_socket_close(port, 0, dcycs); + scc_ptr->socket_state = -1; + return; + } + } - printf("SCC port %d is at unix port %d\n", port, 6501 + port + inc); + printf("SCC port %d is at unix port %d\n", port, 6501 + port + inc); - scc_ptr->sockfd = sockfd; - scc_ptr->state = 1; /* successful socket */ + scc_ptr->sockfd = sockfd; + scc_ptr->state = 1; /* successful socket */ - scc_socket_make_nonblock(port, dcycs); + scc_socket_make_nonblock(port, dcycs); } -void -scc_socket_open_outgoing(int port, double dcycs) -{ - Scc *scc_ptr; - struct sockaddr_in sa_in; - struct hostent *hostentptr; - int on; - int ret; - SOCKET sockfd; - USHORT port_number = 23; +void scc_socket_open_outgoing(int port, double dcycs) { + Scc *scc_ptr; + struct sockaddr_in sa_in; + struct hostent *hostentptr; + int on; + int ret; + SOCKET sockfd; + USHORT port_number = 23; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - printf("scc socket close being called from scc_socket_open_outgoing\n"); - scc_socket_close(port, 0, dcycs); + printf("scc socket close being called from scc_socket_open_outgoing\n"); + scc_socket_close(port, 0, dcycs); - scc_ptr->socket_state = 0; + scc_ptr->socket_state = 0; - memset(scc_ptr->host_handle, 0, scc_ptr->host_aux1); + memset(scc_ptr->host_handle, 0, scc_ptr->host_aux1); - sockfd = socket(AF_INET, SOCK_STREAM, 0); - printf("sockfd ret: %d\n", sockfd); - if(sockfd == -1) { - printf("socket ret: %d, errno: %d\n", sockfd, errno); - scc_socket_close(port, 1, dcycs); - return; - } - /* printf("socket ret: %d\n", sockfd); */ + sockfd = socket(AF_INET, SOCK_STREAM, 0); + printf("sockfd ret: %d\n", sockfd); + if(sockfd == -1) { + printf("socket ret: %d, errno: %d\n", sockfd, errno); + scc_socket_close(port, 1, dcycs); + return; + } + /* printf("socket ret: %d\n", sockfd); */ - on = 1; - ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, - (char *)&on, sizeof(on)); - if(ret < 0) { - printf("setsockopt REUSEADDR ret: %d, err:%d\n", - ret, errno); - scc_socket_close(port, 1, dcycs); - return; - } + on = 1; + ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, + (char *)&on, sizeof(on)); + if(ret < 0) { + printf("setsockopt REUSEADDR ret: %d, err:%d\n", + ret, errno); + scc_socket_close(port, 1, dcycs); + return; + } - memset(&sa_in, 0, sizeof(sa_in)); - sa_in.sin_family = AF_INET; - /* ARO: inspect the ATDT command to see if there is a decimal port number declared & if so, use it */ - /* Format: ATDT, */ - /* Example ATDT192.168.1.21,4001 */ - char *comma_ptr = strchr((const char *)&scc_ptr->modem_cmd_str[0], ','); - if (comma_ptr != NULL) { - long custom_port = strtol(comma_ptr + 1, NULL, 10); - *comma_ptr = '\0'; /* null terminate the hostname string at the position of the comma */ - if (custom_port >= 1 && custom_port <= 65535) { - port_number = (USHORT)custom_port; - } else { - printf("Specified port out of range: %ld\n", custom_port); - scc_socket_close_handle(sockfd); - scc_socket_close(port, 1, dcycs); - return; - } - } - sa_in.sin_port = htons(port_number); - hostentptr = gethostbyname((const char*)&scc_ptr->modem_cmd_str[0]); // OG Added Cast - if(hostentptr == 0) { + memset(&sa_in, 0, sizeof(sa_in)); + sa_in.sin_family = AF_INET; + /* ARO: inspect the ATDT command to see if there is a decimal port number declared & if so, use it */ + /* Format: ATDT, */ + /* Example ATDT192.168.1.21,4001 */ + char *comma_ptr = strchr((const char *)&scc_ptr->modem_cmd_str[0], ','); + if (comma_ptr != NULL) { + long custom_port = strtol(comma_ptr + 1, NULL, 10); + *comma_ptr = '\0'; /* null terminate the hostname string at the position of the comma */ + if (custom_port >= 1 && custom_port <= 65535) { + port_number = (USHORT)custom_port; + } else { + printf("Specified port out of range: %ld\n", custom_port); + scc_socket_close_handle(sockfd); + scc_socket_close(port, 1, dcycs); + return; + } + } + sa_in.sin_port = htons(port_number); + hostentptr = gethostbyname((const char*)&scc_ptr->modem_cmd_str[0]); // OG Added Cast + if(hostentptr == 0) { #if defined(_WIN32) - fatal_printf("Lookup host %s failed\n", &scc_ptr->modem_cmd_str[0]); + fatal_printf("Lookup host %s failed\n", &scc_ptr->modem_cmd_str[0]); #else - fatal_printf("Lookup host %s failed, herrno: %d\n", &scc_ptr->modem_cmd_str[0], h_errno); + fatal_printf("Lookup host %s failed, herrno: %d\n", &scc_ptr->modem_cmd_str[0], h_errno); #endif - scc_socket_close_handle(sockfd); - scc_socket_close(port, 1, dcycs); - x_show_alert(0, 0); - return; - } - memcpy(&sa_in.sin_addr.s_addr, hostentptr->h_addr, - hostentptr->h_length); - /* The above copies the 32-bit internet address into */ - /* sin_addr.s_addr. It's in correct network format */ + scc_socket_close_handle(sockfd); + scc_socket_close(port, 1, dcycs); + x_show_alert(0, 0); + return; + } + memcpy(&sa_in.sin_addr.s_addr, hostentptr->h_addr, + hostentptr->h_length); + /* The above copies the 32-bit internet address into */ + /* sin_addr.s_addr. It's in correct network format */ - ret = connect(sockfd, (struct sockaddr *)&sa_in, sizeof(sa_in)); - if(ret < 0) { - printf("connect ret: %d, errno: %d\n", ret, errno); - scc_socket_close_handle(sockfd); - scc_socket_close(port, 1, dcycs); - return; - } - scc_socket_modem_connect(port, dcycs); - scc_ptr->dcd = 1; /* carrier on */ - scc_ptr->socket_state = 1; /* talk to socket */ - scc_ptr->socket_num_rings = 0; + ret = connect(sockfd, (struct sockaddr *)&sa_in, sizeof(sa_in)); + if(ret < 0) { + printf("connect ret: %d, errno: %d\n", ret, errno); + scc_socket_close_handle(sockfd); + scc_socket_close(port, 1, dcycs); + return; + } + scc_socket_modem_connect(port, dcycs); + scc_ptr->dcd = 1; /* carrier on */ + scc_ptr->socket_state = 1; /* talk to socket */ + scc_ptr->socket_num_rings = 0; - printf("SCC port %d is now outgoing to %s on TCP port %d\n", port, - &scc_ptr->modem_cmd_str[0], port_number); + printf("SCC port %d is now outgoing to %s on TCP port %d\n", port, + &scc_ptr->modem_cmd_str[0], port_number); - scc_ptr->sockfd = sockfd; - scc_ptr->state = 1; /* successful socket */ + scc_ptr->sockfd = sockfd; + scc_ptr->state = 1; /* successful socket */ - scc_socket_make_nonblock(port, dcycs); - scc_ptr->rdwrfd = scc_ptr->sockfd; + scc_socket_make_nonblock(port, dcycs); + scc_ptr->rdwrfd = scc_ptr->sockfd; } -void -scc_socket_make_nonblock(int port, double dcycs) -{ - Scc *scc_ptr; - SOCKET sockfd; - int ret; +void scc_socket_make_nonblock(int port, double dcycs) { + Scc *scc_ptr; + SOCKET sockfd; + int ret; #if defined(_WIN32) - u_long flags; + u_long flags; #else - int flags; + int flags; #endif - scc_ptr = &(scc_stat[port]); - sockfd = scc_ptr->sockfd; + scc_ptr = &(scc_stat[port]); + sockfd = scc_ptr->sockfd; #if defined(_WIN32) - flags = 1; - ret = ioctlsocket(sockfd, FIONBIO, &flags); - if(ret != 0) { - printf("ioctlsocket ret: %d\n", ret); - } + flags = 1; + ret = ioctlsocket(sockfd, FIONBIO, &flags); + if(ret != 0) { + printf("ioctlsocket ret: %d\n", ret); + } #else - flags = fcntl(sockfd, F_GETFL, 0); - if(flags == -1) { - printf("fcntl GETFL ret: %d, errno: %d\n", flags, errno); - scc_socket_close(port, 1, dcycs); - scc_ptr->socket_state = -1; - return; - } - ret = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); - if(ret == -1) { - printf("fcntl SETFL ret: %d, errno: %d\n", ret, errno); - scc_socket_close(port, 1, dcycs); - scc_ptr->socket_state = -1; - return; - } + flags = fcntl(sockfd, F_GETFL, 0); + if(flags == -1) { + printf("fcntl GETFL ret: %d, errno: %d\n", flags, errno); + scc_socket_close(port, 1, dcycs); + scc_ptr->socket_state = -1; + return; + } + ret = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); + if(ret == -1) { + printf("fcntl SETFL ret: %d, errno: %d\n", ret, errno); + scc_socket_close(port, 1, dcycs); + scc_ptr->socket_state = -1; + return; + } #endif } -void -scc_socket_change_params(int port) -{ +void scc_socket_change_params(int port) { } -void -scc_socket_close(int port, int full_close, double dcycs) -{ - Scc *scc_ptr; - int rdwrfd; - SOCKET sockfd; - int i; +void scc_socket_close(int port, int full_close, double dcycs) { + Scc *scc_ptr; + int rdwrfd; + SOCKET sockfd; + int i; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - printf("In scc_socket_close, %d, %d, %f\n", port, full_close, dcycs); + printf("In scc_socket_close, %d, %d, %f\n", port, full_close, dcycs); - rdwrfd = scc_ptr->rdwrfd; - if(rdwrfd != -1) { - printf("socket_close: rdwrfd=%d, closing\n", rdwrfd); - scc_socket_close_handle(rdwrfd); - } - sockfd = scc_ptr->sockfd; - if(sockfd != -1) { - printf("socket_close: sockfd=%d, closing\n", sockfd); - scc_socket_close_handle(sockfd); - } + rdwrfd = scc_ptr->rdwrfd; + if(rdwrfd != -1) { + printf("socket_close: rdwrfd=%d, closing\n", rdwrfd); + scc_socket_close_handle(rdwrfd); + } + sockfd = scc_ptr->sockfd; + if(sockfd != -1) { + printf("socket_close: sockfd=%d, closing\n", sockfd); + scc_socket_close_handle(sockfd); + } - scc_ptr->modem_cmd_len = 0; - scc_ptr->telnet_mode = 0; - scc_ptr->telnet_iac = 0; - for(i = 0; i < 2; i++) { - scc_ptr->telnet_local_mode[i] = 0; - scc_ptr->telnet_remote_mode[i] = 0; - scc_ptr->telnet_reqwill_mode[i] = 0; - scc_ptr->telnet_reqdo_mode[i] = 0; - } - scc_ptr->rdwrfd = -1; - scc_ptr->sockfd = -1; - scc_ptr->dcd = 0; - scc_ptr->socket_num_rings = 0; + scc_ptr->modem_cmd_len = 0; + scc_ptr->telnet_mode = 0; + scc_ptr->telnet_iac = 0; + for(i = 0; i < 2; i++) { + scc_ptr->telnet_local_mode[i] = 0; + scc_ptr->telnet_remote_mode[i] = 0; + scc_ptr->telnet_reqwill_mode[i] = 0; + scc_ptr->telnet_reqdo_mode[i] = 0; + } + scc_ptr->rdwrfd = -1; + scc_ptr->sockfd = -1; + scc_ptr->dcd = 0; + scc_ptr->socket_num_rings = 0; - if(!full_close) { - return; - } + if(!full_close) { + return; + } - scc_socket_modem_hangup(port, dcycs); + scc_socket_modem_hangup(port, dcycs); - /* and go back to modem mode */ - scc_ptr->socket_state = 0; - if(g_serial_modem[port]) { - scc_ptr->modem_dial_or_acc_mode = 0; - } else { - scc_ptr->modem_dial_or_acc_mode = 2; /* always accept */ - } + /* and go back to modem mode */ + scc_ptr->socket_state = 0; + if(g_serial_modem[port]) { + scc_ptr->modem_dial_or_acc_mode = 0; + } else { + scc_ptr->modem_dial_or_acc_mode = 2; /* always accept */ + } } -void -scc_accept_socket(int port, double dcycs) -{ +void scc_accept_socket(int port, double dcycs) { #ifdef SCC_SOCKETS - Scc *scc_ptr; - int flags; - int num_rings; - int rdwrfd; - int ret; + Scc *scc_ptr; + int flags; + int num_rings; + int rdwrfd; + int ret; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - if(scc_ptr->sockfd == -1) { - printf("in accept_socket, call socket_open\n"); - scc_socket_maybe_open_incoming(port, dcycs); - } - if(scc_ptr->sockfd == -1) { - return; /* just give up */ - } - if(scc_ptr->rdwrfd == -1) { - rdwrfd = accept(scc_ptr->sockfd, (struct sockaddr*)scc_ptr->host_handle, - (socklen_t*)&(scc_ptr->host_aux1)); - if(rdwrfd < 0) { - return; - } + if(scc_ptr->sockfd == -1) { + printf("in accept_socket, call socket_open\n"); + scc_socket_maybe_open_incoming(port, dcycs); + } + if(scc_ptr->sockfd == -1) { + return; /* just give up */ + } + if(scc_ptr->rdwrfd == -1) { + rdwrfd = accept(scc_ptr->sockfd, (struct sockaddr*)scc_ptr->host_handle, + (socklen_t*)&(scc_ptr->host_aux1)); + if(rdwrfd < 0) { + return; + } - flags = 0; - ret = 0; + flags = 0; + ret = 0; #if !defined(_WIN32) - /* For Linux, we need to set O_NONBLOCK on the rdwrfd */ - flags = fcntl(rdwrfd, F_GETFL, 0); - if(flags == -1) { - printf("fcntl GETFL ret: %d, errno: %d\n", flags,errno); - return; - } - ret = fcntl(rdwrfd, F_SETFL, flags | O_NONBLOCK); - if(ret == -1) { - printf("fcntl SETFL ret: %d, errno: %d\n", ret, errno); - return; - } + /* For Linux, we need to set O_NONBLOCK on the rdwrfd */ + flags = fcntl(rdwrfd, F_GETFL, 0); + if(flags == -1) { + printf("fcntl GETFL ret: %d, errno: %d\n", flags,errno); + return; + } + ret = fcntl(rdwrfd, F_SETFL, flags | O_NONBLOCK); + if(ret == -1) { + printf("fcntl SETFL ret: %d, errno: %d\n", ret, errno); + return; + } #endif - scc_ptr->rdwrfd = rdwrfd; - printf("Set port[%d].rdwrfd = %d\n", port, rdwrfd); + scc_ptr->rdwrfd = rdwrfd; + printf("Set port[%d].rdwrfd = %d\n", port, rdwrfd); - num_rings = 4; - if(scc_ptr->modem_s0_val > 0) { - num_rings = scc_ptr->modem_s0_val; - } - scc_ptr->socket_num_rings = num_rings; - scc_ptr->socket_last_ring_dcycs = 0; /* do ring now*/ - scc_socket_modem_do_ring(port, dcycs); + num_rings = 4; + if(scc_ptr->modem_s0_val > 0) { + num_rings = scc_ptr->modem_s0_val; + } + scc_ptr->socket_num_rings = num_rings; + scc_ptr->socket_last_ring_dcycs = 0; /* do ring now*/ + scc_socket_modem_do_ring(port, dcycs); - /* and send some telnet codes */ - scc_ptr->telnet_reqwill_mode[0] = 0xa; /* 3=GO_AH and 1=ECHO */ - scc_ptr->telnet_reqdo_mode[0] = 0xa; /* 3=GO_AH and 1=ECHO */ + /* and send some telnet codes */ + scc_ptr->telnet_reqwill_mode[0] = 0xa; /* 3=GO_AH and 1=ECHO */ + scc_ptr->telnet_reqdo_mode[0] = 0xa; /* 3=GO_AH and 1=ECHO */ #if 0 - scc_ptr->telnet_reqdo_mode[1] = 0x4; /* 34=LINEMODE */ + scc_ptr->telnet_reqdo_mode[1] = 0x4; /* 34=LINEMODE */ #endif - } + } #endif } -void -scc_socket_telnet_reqs(int port, double dcycs) -{ - Scc *scc_ptr; - word32 mask, willmask, domask; - int i, j; +void scc_socket_telnet_reqs(int port, double dcycs) { + Scc *scc_ptr; + word32 mask, willmask, domask; + int i, j; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - for(i = 0; i < 64; i++) { - j = i >> 5; - mask = 1 << (i & 31); - willmask = scc_ptr->telnet_reqwill_mode[j]; - if(willmask & mask) { - scc_add_to_writebuf(port, 0xff, dcycs); - scc_add_to_writebuf(port, 0xfb, dcycs); /* WILL */ - scc_add_to_writebuf(port, i, dcycs); - } - domask = scc_ptr->telnet_reqdo_mode[j]; - if(domask & mask) { - scc_add_to_writebuf(port, 0xff, dcycs); - scc_add_to_writebuf(port, 0xfd, dcycs); /* DO */ - scc_add_to_writebuf(port, i, dcycs); - } - } + for(i = 0; i < 64; i++) { + j = i >> 5; + mask = 1 << (i & 31); + willmask = scc_ptr->telnet_reqwill_mode[j]; + if(willmask & mask) { + scc_add_to_writebuf(port, 0xff, dcycs); + scc_add_to_writebuf(port, 0xfb, dcycs); /* WILL */ + scc_add_to_writebuf(port, i, dcycs); + } + domask = scc_ptr->telnet_reqdo_mode[j]; + if(domask & mask) { + scc_add_to_writebuf(port, 0xff, dcycs); + scc_add_to_writebuf(port, 0xfd, dcycs); /* DO */ + scc_add_to_writebuf(port, i, dcycs); + } + } } -void -scc_socket_fill_readbuf(int port, int space_left, double dcycs) -{ +void scc_socket_fill_readbuf(int port, int space_left, double dcycs) { #ifdef SCC_SOCKETS - byte tmp_buf[256]; - Scc *scc_ptr; - int rdwrfd; - int ret; - int i; + byte tmp_buf[256]; + Scc *scc_ptr; + int rdwrfd; + int ret; + int i; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - scc_accept_socket(port, dcycs); - scc_socket_modem_do_ring(port, dcycs); + scc_accept_socket(port, dcycs); + scc_socket_modem_do_ring(port, dcycs); - if(scc_ptr->socket_state == 0 && g_serial_modem[port]) { - /* Just get out, this is modem mode */ - /* The transmit function stuffs any bytes in receive buf */ - return; - } - rdwrfd = scc_ptr->rdwrfd; - if(rdwrfd < 0) { - return; /* just get out */ - } + if(scc_ptr->socket_state == 0 && g_serial_modem[port]) { + /* Just get out, this is modem mode */ + /* The transmit function stuffs any bytes in receive buf */ + return; + } + rdwrfd = scc_ptr->rdwrfd; + if(rdwrfd < 0) { + return; /* just get out */ + } - /* Try reading some bytes */ - space_left = MIN(space_left, 256); - ret = recv(rdwrfd, (char*)tmp_buf, space_left, 0); // OG Added cast - if(ret > 0) { - for(i = 0; i < ret; i++) { - if(tmp_buf[i] == 0) { - /* Skip null chars */ - continue; - } - scc_socket_recvd_char(port, tmp_buf[i], dcycs); - } - } else if(ret == 0) { - /* assume socket close */ - printf("recv got 0 from rdwrfd=%d, closing\n", rdwrfd); - scc_socket_close(port, 1, dcycs); - } + /* Try reading some bytes */ + space_left = MIN(space_left, 256); + ret = recv(rdwrfd, (char*)tmp_buf, space_left, 0); // OG Added cast + if(ret > 0) { + for(i = 0; i < ret; i++) { + if(tmp_buf[i] == 0) { + /* Skip null chars */ + continue; + } + scc_socket_recvd_char(port, tmp_buf[i], dcycs); + } + } else if(ret == 0) { + /* assume socket close */ + printf("recv got 0 from rdwrfd=%d, closing\n", rdwrfd); + scc_socket_close(port, 1, dcycs); + } #endif } int g_scc_dbg_print_cnt = 50; -void -scc_socket_recvd_char(int port, int c, double dcycs) -{ - Scc *scc_ptr; - word32 locmask, remmask, mask; - word32 reqwillmask, reqdomask; - int telnet_mode, telnet_iac; - int eff_c, cpos; - int reply; +void scc_socket_recvd_char(int port, int c, double dcycs) { + Scc *scc_ptr; + word32 locmask, remmask, mask; + word32 reqwillmask, reqdomask; + int telnet_mode, telnet_iac; + int eff_c, cpos; + int reply; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - scc_socket_maybe_open_incoming(port, dcycs); + scc_socket_maybe_open_incoming(port, dcycs); - telnet_mode = scc_ptr->telnet_mode; - telnet_iac = scc_ptr->telnet_iac; - if(c >= 0xf0 || telnet_mode || telnet_iac) { - g_scc_dbg_print_cnt = 50; - } + telnet_mode = scc_ptr->telnet_mode; + telnet_iac = scc_ptr->telnet_iac; + if(c >= 0xf0 || telnet_mode || telnet_iac) { + g_scc_dbg_print_cnt = 50; + } #if 0 - if(g_scc_dbg_print_cnt) { - printf("Recv: %02x mode: %d\n", c, telnet_mode); - g_scc_dbg_print_cnt--; - } + if(g_scc_dbg_print_cnt) { + printf("Recv: %02x mode: %d\n", c, telnet_mode); + g_scc_dbg_print_cnt--; + } #endif - eff_c = c; - if(telnet_iac == 0) { - if(c == 0xff) { - scc_ptr->telnet_iac = 0xff; - return; /* and just get out */ - } - } else { - /* last char was 0xff, see if this is 0xff */ - if(c != 0xff) { - /* this is some kind of command */ - eff_c = eff_c | 0x100; /* indicate prev char IAC */ - } - } - scc_ptr->telnet_iac = 0; + eff_c = c; + if(telnet_iac == 0) { + if(c == 0xff) { + scc_ptr->telnet_iac = 0xff; + return; /* and just get out */ + } + } else { + /* last char was 0xff, see if this is 0xff */ + if(c != 0xff) { + /* this is some kind of command */ + eff_c = eff_c | 0x100; /* indicate prev char IAC */ + } + } + scc_ptr->telnet_iac = 0; - mask = 1 << (c & 31); - cpos = (c >> 5) & 1; - locmask = scc_ptr->telnet_local_mode[cpos] & mask; - remmask = scc_ptr->telnet_remote_mode[cpos] & mask; - reqwillmask = scc_ptr->telnet_reqwill_mode[cpos] & mask; - reqdomask = scc_ptr->telnet_reqdo_mode[cpos] & mask; - switch(telnet_mode) { - case 0: /* just passing through bytes */ - switch(eff_c) { - case 0x1fe: /* DON'T */ - case 0x1fd: /* DO */ - case 0x1fc: /* WON'T */ - case 0x1fb: /* WILL */ - case 0x1fa: /* SB */ - telnet_mode = c; - break; - default: - if(eff_c < 0x100) { - scc_add_to_readbuf(port, c, dcycs); - } - break; - } - break; - case 3: /* LINEMODE SB SLC, octet 0 */ - if(eff_c == 0x1f0) { - /* SE, the end */ - telnet_mode = 0; - } - printf("LINEMODE SLC octet 0: %02x\n", c); - telnet_mode = 4; - break; - case 4: /* LINEMODE SB SLC, octet 1 */ - printf("LINEMODE SLC octet 1: %02x\n", c); - telnet_mode = 5; - if(eff_c == 0x1f0) { - /* SE, the end */ - printf("Got SE at octet 1...\n"); - telnet_mode = 0; - } - break; - case 5: /* LINEMODE SB SLC, octet 2 */ - printf("LINEMODE SLC octet 2: %02x\n", c); - telnet_mode = 3; - if(eff_c == 0x1f0) { - /* SE, the end */ - printf("Got SE at octet 2...\n"); - telnet_mode = 0; - } - break; - case 34: /* LINEMODE SB beginning */ - switch(c) { - case 3: /* SLC */ - telnet_mode = 3; - break; - default: - telnet_mode = 0xee; /* go to SB eat */ - break; - } - break; - case 0xfa: /* in 0xfa = SB mode, eat up subcommands */ - switch(c) { - case 34: /* LINEMODE */ - telnet_mode = 34; - break; - default: - telnet_mode = 0xee; /* SB eat mode */ - break; - } - break; - case 0xee: /* in SB eat mode */ - if(eff_c == 0x1f0) { /* SE, end of sub-command */ - telnet_mode = 0; - } else { - /* just stay in eat mode */ - } - break; - case 0xfe: /* previous char was "DON'T" */ - if(locmask && (reqwillmask == 0)) { - /* it's a mode change */ - /* always OK to turn off a mode that we had on */ - scc_add_to_writebuf(port, 0xff, dcycs); - scc_add_to_writebuf(port, 0xfc, dcycs); /* WON'T */ - scc_add_to_writebuf(port, c, dcycs); - } - scc_ptr->telnet_local_mode[cpos] &= ~mask; - scc_ptr->telnet_reqwill_mode[cpos] &= ~mask; - telnet_mode = 0; - break; - case 0xfd: /* previous char was "DO" */ - reply = 0xfc; - if(locmask == 0 && (reqwillmask == 0)) { - /* it's a mode change, send some response */ - reply = 0xfc; /* nack it with WON'T */ - if(c == 0x03 || c == 0x01) { - reply = 0xfb; /* Ack with WILL */ - } - scc_add_to_writebuf(port, 0xff, dcycs); - scc_add_to_writebuf(port, reply, dcycs); - scc_add_to_writebuf(port, c, dcycs); - } - if(reqwillmask || (reply == 0xfb)) { - scc_ptr->telnet_local_mode[cpos] |= mask; - } - scc_ptr->telnet_reqwill_mode[cpos] &= ~mask; - telnet_mode = 0; - break; - case 0xfc: /* previous char was "WON'T" */ - if(remmask && (reqdomask == 0)) { - /* it's a mode change, ack with DON'T */ - scc_add_to_writebuf(port, 0xff, dcycs); - scc_add_to_writebuf(port, 0xfe, dcycs); /* DON'T */ - scc_add_to_writebuf(port, c, dcycs); - } - scc_ptr->telnet_remote_mode[cpos] &= ~mask; - scc_ptr->telnet_reqdo_mode[cpos] &= ~mask; - telnet_mode = 0; - break; - case 0xfb: /* previous char was "WILL" */ - reply = 0xfe; /* nack it with DON'T */ - if(remmask == 0 && (reqdomask == 0)) { - /* it's a mode change, send some response */ - if(c == 0x03 || c == 0x01) { - reply = 0xfd; /* Ack with DO */ - } - scc_add_to_writebuf(port, 0xff, dcycs); - scc_add_to_writebuf(port, reply, dcycs); - scc_add_to_writebuf(port, c, dcycs); - } - if(reqdomask || (reply == 0xfd)) { - scc_ptr->telnet_remote_mode[cpos] |= mask; - } - scc_ptr->telnet_reqdo_mode[cpos] &= ~mask; - telnet_mode = 0; - break; - default: - telnet_mode = 0; - break; - } - scc_ptr->telnet_mode = telnet_mode; + mask = 1 << (c & 31); + cpos = (c >> 5) & 1; + locmask = scc_ptr->telnet_local_mode[cpos] & mask; + remmask = scc_ptr->telnet_remote_mode[cpos] & mask; + reqwillmask = scc_ptr->telnet_reqwill_mode[cpos] & mask; + reqdomask = scc_ptr->telnet_reqdo_mode[cpos] & mask; + switch(telnet_mode) { + case 0: /* just passing through bytes */ + switch(eff_c) { + case 0x1fe: /* DON'T */ + case 0x1fd: /* DO */ + case 0x1fc: /* WON'T */ + case 0x1fb: /* WILL */ + case 0x1fa: /* SB */ + telnet_mode = c; + break; + default: + if(eff_c < 0x100) { + scc_add_to_readbuf(port, c, dcycs); + } + break; + } + break; + case 3: /* LINEMODE SB SLC, octet 0 */ + if(eff_c == 0x1f0) { + /* SE, the end */ + telnet_mode = 0; + } + printf("LINEMODE SLC octet 0: %02x\n", c); + telnet_mode = 4; + break; + case 4: /* LINEMODE SB SLC, octet 1 */ + printf("LINEMODE SLC octet 1: %02x\n", c); + telnet_mode = 5; + if(eff_c == 0x1f0) { + /* SE, the end */ + printf("Got SE at octet 1...\n"); + telnet_mode = 0; + } + break; + case 5: /* LINEMODE SB SLC, octet 2 */ + printf("LINEMODE SLC octet 2: %02x\n", c); + telnet_mode = 3; + if(eff_c == 0x1f0) { + /* SE, the end */ + printf("Got SE at octet 2...\n"); + telnet_mode = 0; + } + break; + case 34: /* LINEMODE SB beginning */ + switch(c) { + case 3: /* SLC */ + telnet_mode = 3; + break; + default: + telnet_mode = 0xee; /* go to SB eat */ + break; + } + break; + case 0xfa: /* in 0xfa = SB mode, eat up subcommands */ + switch(c) { + case 34: /* LINEMODE */ + telnet_mode = 34; + break; + default: + telnet_mode = 0xee; /* SB eat mode */ + break; + } + break; + case 0xee: /* in SB eat mode */ + if(eff_c == 0x1f0) { /* SE, end of sub-command */ + telnet_mode = 0; + } else { + /* just stay in eat mode */ + } + break; + case 0xfe: /* previous char was "DON'T" */ + if(locmask && (reqwillmask == 0)) { + /* it's a mode change */ + /* always OK to turn off a mode that we had on */ + scc_add_to_writebuf(port, 0xff, dcycs); + scc_add_to_writebuf(port, 0xfc, dcycs); /* WON'T */ + scc_add_to_writebuf(port, c, dcycs); + } + scc_ptr->telnet_local_mode[cpos] &= ~mask; + scc_ptr->telnet_reqwill_mode[cpos] &= ~mask; + telnet_mode = 0; + break; + case 0xfd: /* previous char was "DO" */ + reply = 0xfc; + if(locmask == 0 && (reqwillmask == 0)) { + /* it's a mode change, send some response */ + reply = 0xfc; /* nack it with WON'T */ + if(c == 0x03 || c == 0x01) { + reply = 0xfb; /* Ack with WILL */ + } + scc_add_to_writebuf(port, 0xff, dcycs); + scc_add_to_writebuf(port, reply, dcycs); + scc_add_to_writebuf(port, c, dcycs); + } + if(reqwillmask || (reply == 0xfb)) { + scc_ptr->telnet_local_mode[cpos] |= mask; + } + scc_ptr->telnet_reqwill_mode[cpos] &= ~mask; + telnet_mode = 0; + break; + case 0xfc: /* previous char was "WON'T" */ + if(remmask && (reqdomask == 0)) { + /* it's a mode change, ack with DON'T */ + scc_add_to_writebuf(port, 0xff, dcycs); + scc_add_to_writebuf(port, 0xfe, dcycs); /* DON'T */ + scc_add_to_writebuf(port, c, dcycs); + } + scc_ptr->telnet_remote_mode[cpos] &= ~mask; + scc_ptr->telnet_reqdo_mode[cpos] &= ~mask; + telnet_mode = 0; + break; + case 0xfb: /* previous char was "WILL" */ + reply = 0xfe; /* nack it with DON'T */ + if(remmask == 0 && (reqdomask == 0)) { + /* it's a mode change, send some response */ + if(c == 0x03 || c == 0x01) { + reply = 0xfd; /* Ack with DO */ + } + scc_add_to_writebuf(port, 0xff, dcycs); + scc_add_to_writebuf(port, reply, dcycs); + scc_add_to_writebuf(port, c, dcycs); + } + if(reqdomask || (reply == 0xfd)) { + scc_ptr->telnet_remote_mode[cpos] |= mask; + } + scc_ptr->telnet_reqdo_mode[cpos] &= ~mask; + telnet_mode = 0; + break; + default: + telnet_mode = 0; + break; + } + scc_ptr->telnet_mode = telnet_mode; } -void -scc_socket_empty_writebuf(int port, double dcycs) -{ +void scc_socket_empty_writebuf(int port, double dcycs) { #ifdef SCC_SOCKETS # if !defined(_WIN32) - struct sigaction newact, oldact; + struct sigaction newact, oldact; # endif - Scc *scc_ptr; - double diff_dcycs; - int plus_mode; - int rdptr; - int wrptr; - int rdwrfd; - int done; - int ret; - int len; - int c; - int i; + Scc *scc_ptr; + double diff_dcycs; + int plus_mode; + int rdptr; + int wrptr; + int rdwrfd; + int done; + int ret; + int len; + int c; + int i; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - /* See if +++ done and we should go to command mode */ - diff_dcycs = dcycs - scc_ptr->out_char_dcycs; - if((diff_dcycs > 900.0*1000) && (scc_ptr->modem_plus_mode == 3) && - (scc_ptr->socket_state >= 1) && - (g_serial_modem[port] != 0)) { - scc_ptr->socket_state = 0; /* go modem mode, stay connect*/ - scc_ptr->modem_plus_mode = 0; - scc_socket_send_modem_code(port, 0, dcycs); - } + /* See if +++ done and we should go to command mode */ + diff_dcycs = dcycs - scc_ptr->out_char_dcycs; + if((diff_dcycs > 900.0*1000) && (scc_ptr->modem_plus_mode == 3) && + (scc_ptr->socket_state >= 1) && + (g_serial_modem[port] != 0)) { + scc_ptr->socket_state = 0; /* go modem mode, stay connect*/ + scc_ptr->modem_plus_mode = 0; + scc_socket_send_modem_code(port, 0, dcycs); + } - /* Try writing some bytes */ - done = 0; - while(!done) { - rdptr = scc_ptr->out_rdptr; - wrptr = scc_ptr->out_wrptr; - if(rdptr == wrptr) { - done = 1; - break; - } - rdwrfd = scc_ptr->rdwrfd; - len = wrptr - rdptr; - if(len < 0) { - len = SCC_OUTBUF_SIZE - rdptr; - } - if(len > 32) { - len = 32; - } - if(len <= 0) { - done = 1; - break; - } + /* Try writing some bytes */ + done = 0; + while(!done) { + rdptr = scc_ptr->out_rdptr; + wrptr = scc_ptr->out_wrptr; + if(rdptr == wrptr) { + done = 1; + break; + } + rdwrfd = scc_ptr->rdwrfd; + len = wrptr - rdptr; + if(len < 0) { + len = SCC_OUTBUF_SIZE - rdptr; + } + if(len > 32) { + len = 32; + } + if(len <= 0) { + done = 1; + break; + } - if(scc_ptr->socket_state < 1 && g_serial_modem[port]) { - len = 1; - scc_socket_modem_write(port, scc_ptr->out_buf[rdptr], - dcycs); - ret = 1; - } else { - if(rdwrfd == -1) { - if(g_serial_modem[port]) { - printf("socket_state: %d, ser_mod: %d, " - "rdwrfd: %d\n", - scc_ptr->socket_state, - g_serial_modem[port], rdwrfd); - } - scc_ptr->socket_state = 0; - scc_socket_maybe_open_incoming(port, dcycs); - return; - } - for(i = 0; i < len; i++) { - c = scc_ptr->out_buf[rdptr + i]; - plus_mode = scc_ptr->modem_plus_mode; - diff_dcycs = dcycs - scc_ptr->out_char_dcycs; - if(c == '+' && plus_mode == 0) { - if(diff_dcycs > 500*1000) { - scc_ptr->modem_plus_mode = 1; - } - } else if(c == '+') { - if(diff_dcycs < 800.0*1000) { - plus_mode++; - scc_ptr->modem_plus_mode = - plus_mode; - } - } else { - scc_ptr->modem_plus_mode = 0; - } - scc_ptr->out_char_dcycs = dcycs; - } + if(scc_ptr->socket_state < 1 && g_serial_modem[port]) { + len = 1; + scc_socket_modem_write(port, scc_ptr->out_buf[rdptr], + dcycs); + ret = 1; + } else { + if(rdwrfd == -1) { + if(g_serial_modem[port]) { + printf("socket_state: %d, ser_mod: %d, " + "rdwrfd: %d\n", + scc_ptr->socket_state, + g_serial_modem[port], rdwrfd); + } + scc_ptr->socket_state = 0; + scc_socket_maybe_open_incoming(port, dcycs); + return; + } + for(i = 0; i < len; i++) { + c = scc_ptr->out_buf[rdptr + i]; + plus_mode = scc_ptr->modem_plus_mode; + diff_dcycs = dcycs - scc_ptr->out_char_dcycs; + if(c == '+' && plus_mode == 0) { + if(diff_dcycs > 500*1000) { + scc_ptr->modem_plus_mode = 1; + } + } else if(c == '+') { + if(diff_dcycs < 800.0*1000) { + plus_mode++; + scc_ptr->modem_plus_mode = + plus_mode; + } + } else { + scc_ptr->modem_plus_mode = 0; + } + scc_ptr->out_char_dcycs = dcycs; + } #if defined(_WIN32) - ret = send(rdwrfd, (const char*)&(scc_ptr->out_buf[rdptr]), len, 0); // OG Added Cast + ret = send(rdwrfd, (const char*)&(scc_ptr->out_buf[rdptr]), len, 0); // OG Added Cast # else - /* ignore SIGPIPE around writes to the socket, so we */ - /* can catch a closed socket and prepare to accept */ - /* a new connection. Otherwise, SIGPIPE kills GSport */ - sigemptyset(&newact.sa_mask); - newact.sa_handler = SIG_IGN; - newact.sa_flags = 0; - sigaction(SIGPIPE, &newact, &oldact); + /* ignore SIGPIPE around writes to the socket, so we */ + /* can catch a closed socket and prepare to accept */ + /* a new connection. Otherwise, SIGPIPE kills GSport */ + sigemptyset(&newact.sa_mask); + newact.sa_handler = SIG_IGN; + newact.sa_flags = 0; + sigaction(SIGPIPE, &newact, &oldact); - ret = send(rdwrfd, &(scc_ptr->out_buf[rdptr]), len, 0); + ret = send(rdwrfd, &(scc_ptr->out_buf[rdptr]), len, 0); - sigaction(SIGPIPE, &oldact, 0); - /* restore previous SIGPIPE behavior */ -# endif /* WIN32 */ + sigaction(SIGPIPE, &oldact, 0); + /* restore previous SIGPIPE behavior */ +# endif /* WIN32 */ #if 0 - printf("sock output: %02x\n", scc_ptr->out_buf[rdptr]); + printf("sock output: %02x\n", scc_ptr->out_buf[rdptr]); #endif - } + } - if(ret == 0) { - done = 1; /* give up for now */ - break; - } else if(ret < 0) { - /* assume socket is dead */ - printf("socket write failed, resuming modem mode\n"); - scc_socket_close(port, 1, dcycs); - done = 1; - break; - } else { - rdptr = rdptr + ret; - if(rdptr >= SCC_OUTBUF_SIZE) { - rdptr = rdptr - SCC_OUTBUF_SIZE; - } - scc_ptr->out_rdptr = rdptr; - } - } + if(ret == 0) { + done = 1; /* give up for now */ + break; + } else if(ret < 0) { + /* assume socket is dead */ + printf("socket write failed, resuming modem mode\n"); + scc_socket_close(port, 1, dcycs); + done = 1; + break; + } else { + rdptr = rdptr + ret; + if(rdptr >= SCC_OUTBUF_SIZE) { + rdptr = rdptr - SCC_OUTBUF_SIZE; + } + scc_ptr->out_rdptr = rdptr; + } + } #endif } -void -scc_socket_modem_write(int port, int c, double dcycs) -{ - Scc *scc_ptr; - char *str; - word32 modem_mode; - int do_echo; - int got_at; - int len; +void scc_socket_modem_write(int port, int c, double dcycs) { + Scc *scc_ptr; + char *str; + word32 modem_mode; + int do_echo; + int got_at; + int len; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - if(scc_ptr->sockfd == -1) { - scc_ptr->socket_state = 0; - scc_socket_maybe_open_incoming(port, dcycs); - } + if(scc_ptr->sockfd == -1) { + scc_ptr->socket_state = 0; + scc_socket_maybe_open_incoming(port, dcycs); + } - modem_mode = scc_ptr->modem_mode; - str = (char*)&(scc_ptr->modem_cmd_str[0]); // OG Added Cast + modem_mode = scc_ptr->modem_mode; + str = (char*)&(scc_ptr->modem_cmd_str[0]); // OG Added Cast #if 0 - printf("M: %02x\n", c); + printf("M: %02x\n", c); #endif - do_echo = ((modem_mode & SCCMODEM_NOECHO) == 0); - len = scc_ptr->modem_cmd_len; - got_at = 0; - if(len >= 2 && str[0] == 'a' && str[1] == 't') { - /* we've got an 'at', do not back up past it */ - got_at = 1; - } - if(c == 0x0d) { - if(do_echo) { - scc_add_to_readbuf(port, c, dcycs); /* echo cr */ - scc_add_to_readbuf(port, 0x0a, dcycs); /* echo lf */ - } - do_echo = 0; /* already did the echo */ - scc_socket_do_cmd_str(port, dcycs); - scc_ptr->modem_cmd_len = 0; - len = 0; - str[0] = 0; - } else if(c == 0x08) { - if(len <= 0) { - do_echo = 0; /* do not go past left margin */ - } else if(len == 2 && got_at) { - do_echo = 0; /* do not erase "AT" */ - } else { - /* erase a character */ - len--; - str[len] = 0; - } - } else if(c < 0x20) { - /* ignore all control characters, don't echo */ - /* includes line feeds and nulls */ - do_echo = 0; - } else { - /* other characters */ - if(len < SCC_MODEM_MAX_CMD_STR) { - str[len] = tolower(c); - str[len+1] = 0; - len++; - } - } - scc_ptr->modem_cmd_len = len; - if(do_echo) { - scc_add_to_readbuf(port, c, dcycs); /* echo */ - } + do_echo = ((modem_mode & SCCMODEM_NOECHO) == 0); + len = scc_ptr->modem_cmd_len; + got_at = 0; + if(len >= 2 && str[0] == 'a' && str[1] == 't') { + /* we've got an 'at', do not back up past it */ + got_at = 1; + } + if(c == 0x0d) { + if(do_echo) { + scc_add_to_readbuf(port, c, dcycs); /* echo cr */ + scc_add_to_readbuf(port, 0x0a, dcycs); /* echo lf */ + } + do_echo = 0; /* already did the echo */ + scc_socket_do_cmd_str(port, dcycs); + scc_ptr->modem_cmd_len = 0; + len = 0; + str[0] = 0; + } else if(c == 0x08) { + if(len <= 0) { + do_echo = 0; /* do not go past left margin */ + } else if(len == 2 && got_at) { + do_echo = 0; /* do not erase "AT" */ + } else { + /* erase a character */ + len--; + str[len] = 0; + } + } else if(c < 0x20) { + /* ignore all control characters, don't echo */ + /* includes line feeds and nulls */ + do_echo = 0; + } else { + /* other characters */ + if(len < SCC_MODEM_MAX_CMD_STR) { + str[len] = tolower(c); + str[len+1] = 0; + len++; + } + } + scc_ptr->modem_cmd_len = len; + if(do_echo) { + scc_add_to_readbuf(port, c, dcycs); /* echo */ + } } -void -scc_socket_do_cmd_str(int port, double dcycs) -{ - Scc *scc_ptr; - char *str; - int pos, len; - int ret_val; - int reg, reg_val; - int was_amp; - int c; - int i; +void scc_socket_do_cmd_str(int port, double dcycs) { + Scc *scc_ptr; + char *str; + int pos, len; + int ret_val; + int reg, reg_val; + int was_amp; + int c; + int i; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - str = (char*)&(scc_ptr->modem_cmd_str[0]); // OG Added cast - printf("Got modem string :%s:=%02x %02x %02x\n", str, str[0], str[1], - str[2]); + str = (char*)&(scc_ptr->modem_cmd_str[0]); // OG Added cast + printf("Got modem string :%s:=%02x %02x %02x\n", str, str[0], str[1], + str[2]); - len = scc_ptr->modem_cmd_len; - str[len] = 0; - str[len+1] = 0; - str[len+2] = 0; - pos = -1; - if(len < 2) { - /* just ignore it */ - return; - } - if(str[0] != 'a' || str[1] != 't') { - return; - } + len = scc_ptr->modem_cmd_len; + str[len] = 0; + str[len+1] = 0; + str[len+2] = 0; + pos = -1; + if(len < 2) { + /* just ignore it */ + return; + } + if(str[0] != 'a' || str[1] != 't') { + return; + } - /* Some AT command received--make sure socket 6501/6502 is open */ - printf("Some AT command received, sockfd=%d\n", scc_ptr->sockfd); + /* Some AT command received--make sure socket 6501/6502 is open */ + printf("Some AT command received, sockfd=%d\n", scc_ptr->sockfd); - pos = 2 - 1; - ret_val = 0; /* "OK" */ - was_amp = 0; - while(++pos < len) { - c = str[pos] + was_amp; - was_amp = 0; - switch(c) { - case '&': /* at& */ - was_amp = 0x100; - break; - case 'z': /* atz */ - scc_ptr->modem_mode = 0; - scc_ptr->modem_s0_val = 0; - pos = len; /* ignore any other commands */ - break; - case 'e': /* ate = echo */ - c = str[pos+1]; - if(c == '1') { - scc_ptr->modem_mode &= ~SCCMODEM_NOECHO; - pos++; - } else { - scc_ptr->modem_mode |= SCCMODEM_NOECHO; - pos++; - } - break; - case 'v': /* atv = verbose */ - c = str[pos+1]; - if(c == '1') { - scc_ptr->modem_mode &= ~SCCMODEM_NOVERBOSE; - pos++; - } else { - scc_ptr->modem_mode |= SCCMODEM_NOVERBOSE; - pos++; - } - break; - case 'o': /* ato = go online */ - printf("ato\n"); - if(scc_ptr->dcd && (scc_ptr->rdwrfd != -1) && - (scc_ptr->socket_state == 0)) { - printf("Going back online\n"); - scc_ptr->socket_state = 1; - scc_socket_modem_connect(port, dcycs); - ret_val = -1; - } - break; - case 'h': /* ath = hang up */ - printf("ath, hanging up\n"); - scc_socket_close(port, (scc_ptr->rdwrfd != -1), dcycs); - /* scc_socket_maybe_open_incoming(port, dcycs); */ - /* reopen listen */ - break; - case 'a': /* ata */ - printf("Doing ATA\n"); - scc_socket_do_answer(port, dcycs); - ret_val = -1; - break; - case 'd': /* atd */ - pos++; - c = str[pos]; - if(c == 't' || c == 'p') { - /* skip tone or pulse */ - pos++; - } - /* see if it is 111 */ - if(strcmp(&str[pos], "111") == 0) { - /* Do PPP! */ - } else { - /* get string to connect to */ - /* Shift string so hostname moves to str[0] */ - for(i = 0; i < len; i++) { - str[i] = str[pos]; - if(pos >= len) { - break; - } - pos++; - } + pos = 2 - 1; + ret_val = 0; /* "OK" */ + was_amp = 0; + while(++pos < len) { + c = str[pos] + was_amp; + was_amp = 0; + switch(c) { + case '&': /* at& */ + was_amp = 0x100; + break; + case 'z': /* atz */ + scc_ptr->modem_mode = 0; + scc_ptr->modem_s0_val = 0; + pos = len; /* ignore any other commands */ + break; + case 'e': /* ate = echo */ + c = str[pos+1]; + if(c == '1') { + scc_ptr->modem_mode &= ~SCCMODEM_NOECHO; + pos++; + } else { + scc_ptr->modem_mode |= SCCMODEM_NOECHO; + pos++; + } + break; + case 'v': /* atv = verbose */ + c = str[pos+1]; + if(c == '1') { + scc_ptr->modem_mode &= ~SCCMODEM_NOVERBOSE; + pos++; + } else { + scc_ptr->modem_mode |= SCCMODEM_NOVERBOSE; + pos++; + } + break; + case 'o': /* ato = go online */ + printf("ato\n"); + if(scc_ptr->dcd && (scc_ptr->rdwrfd != -1) && + (scc_ptr->socket_state == 0)) { + printf("Going back online\n"); + scc_ptr->socket_state = 1; + scc_socket_modem_connect(port, dcycs); + ret_val = -1; + } + break; + case 'h': /* ath = hang up */ + printf("ath, hanging up\n"); + scc_socket_close(port, (scc_ptr->rdwrfd != -1), dcycs); + /* scc_socket_maybe_open_incoming(port, dcycs); */ + /* reopen listen */ + break; + case 'a': /* ata */ + printf("Doing ATA\n"); + scc_socket_do_answer(port, dcycs); + ret_val = -1; + break; + case 'd': /* atd */ + pos++; + c = str[pos]; + if(c == 't' || c == 'p') { + /* skip tone or pulse */ + pos++; + } + /* see if it is 111 */ + if(strcmp(&str[pos], "111") == 0) { + /* Do PPP! */ + } else { + /* get string to connect to */ + /* Shift string so hostname moves to str[0] */ + for(i = 0; i < len; i++) { + str[i] = str[pos]; + if(pos >= len) { + break; + } + pos++; + } - } - scc_ptr->modem_dial_or_acc_mode = 1; - scc_socket_open_outgoing(port, dcycs); - ret_val = -1; - pos = len; /* always eat rest of the line */ - break; - case 's': /* atsnn=yy */ - pos++; - reg = 0; - while(1) { - c = str[pos]; - if(c < '0' || c > '9') { - break; - } - reg = (reg * 10) + c - '0'; - pos++; - } - if(c == '?') { - /* display S-register */ - if(reg == 0) { - scc_add_to_readbufv(port, dcycs, - "S0=%d\n", - scc_ptr->modem_s0_val); - } - break; - } - if(c != '=') { - break; - } - pos++; - reg_val = 0; - while(1) { - c = str[pos]; - if(c < '0' || c >'9') { - break; - } - reg_val = (reg_val * 10) + c - '0'; - pos++; - } - printf("ats%d = %d\n", reg, reg_val); - if(reg == 0) { - scc_ptr->modem_s0_val = reg_val; - } - pos--; - break; - default: - /* some command--peek into next chars to finish it */ - while(1) { - c = str[pos+1]; - if(c >= '0' && c <= '9') { - /* eat numbers */ - pos++; - continue; - } - if(c == '=') { - /* eat this as well */ - pos++; - continue; - } - /* else get out */ - break; - } - } - } + } + scc_ptr->modem_dial_or_acc_mode = 1; + scc_socket_open_outgoing(port, dcycs); + ret_val = -1; + pos = len; /* always eat rest of the line */ + break; + case 's': /* atsnn=yy */ + pos++; + reg = 0; + while(1) { + c = str[pos]; + if(c < '0' || c > '9') { + break; + } + reg = (reg * 10) + c - '0'; + pos++; + } + if(c == '?') { + /* display S-register */ + if(reg == 0) { + scc_add_to_readbufv(port, dcycs, + "S0=%d\n", + scc_ptr->modem_s0_val); + } + break; + } + if(c != '=') { + break; + } + pos++; + reg_val = 0; + while(1) { + c = str[pos]; + if(c < '0' || c >'9') { + break; + } + reg_val = (reg_val * 10) + c - '0'; + pos++; + } + printf("ats%d = %d\n", reg, reg_val); + if(reg == 0) { + scc_ptr->modem_s0_val = reg_val; + } + pos--; + break; + default: + /* some command--peek into next chars to finish it */ + while(1) { + c = str[pos+1]; + if(c >= '0' && c <= '9') { + /* eat numbers */ + pos++; + continue; + } + if(c == '=') { + /* eat this as well */ + pos++; + continue; + } + /* else get out */ + break; + } + } + } - if(ret_val >= 0) { - scc_socket_send_modem_code(port, ret_val, dcycs); - } + if(ret_val >= 0) { + scc_socket_send_modem_code(port, ret_val, dcycs); + } } -void -scc_socket_send_modem_code(int port, int code, double dcycs) -{ - Scc *scc_ptr; - char *str; - word32 modem_mode; +void scc_socket_send_modem_code(int port, int code, double dcycs) { + Scc *scc_ptr; + char *str; + word32 modem_mode; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - switch(code) { - case 0: str = "OK"; break; - case 1: str = "CONNECT"; break; - case 2: str = "RING"; break; - case 3: str = "NO CARRIER"; break; - case 4: str = "ERROR"; break; - case 5: str = "CONNECT 1200"; break; - case 13: str = "CONNECT 9600"; break; - case 16: str = "CONNECT 19200"; break; - case 25: str = "CONNECT 14400"; break; - case 85: str = "CONNECT 19200"; break; - default: - str = "ERROR"; - } + switch(code) { + case 0: str = "OK"; break; + case 1: str = "CONNECT"; break; + case 2: str = "RING"; break; + case 3: str = "NO CARRIER"; break; + case 4: str = "ERROR"; break; + case 5: str = "CONNECT 1200"; break; + case 13: str = "CONNECT 9600"; break; + case 16: str = "CONNECT 19200"; break; + case 25: str = "CONNECT 14400"; break; + case 85: str = "CONNECT 19200"; break; + default: + str = "ERROR"; + } - printf("Sending modem code %d = %s\n", code, str); + printf("Sending modem code %d = %s\n", code, str); - modem_mode = scc_ptr->modem_mode; - if(modem_mode & SCCMODEM_NOVERBOSE) { - /* just the number */ - scc_add_to_readbufv(port, dcycs, "%d", code); - scc_add_to_readbuf(port, 0x0d, dcycs); - } else { - scc_add_to_readbufv(port, dcycs, "%s\n", str); - } + modem_mode = scc_ptr->modem_mode; + if(modem_mode & SCCMODEM_NOVERBOSE) { + /* just the number */ + scc_add_to_readbufv(port, dcycs, "%d", code); + scc_add_to_readbuf(port, 0x0d, dcycs); + } else { + scc_add_to_readbufv(port, dcycs, "%s\n", str); + } } -void -scc_socket_modem_hangup(int port, double dcycs) -{ - scc_socket_send_modem_code(port, 3, dcycs); +void scc_socket_modem_hangup(int port, double dcycs) { + scc_socket_send_modem_code(port, 3, dcycs); } -void -scc_socket_modem_connect(int port, double dcycs) -{ - /* decide which code to send. Default to 1 if needed */ - scc_socket_send_modem_code(port, 13, dcycs); /*13=9600*/ +void scc_socket_modem_connect(int port, double dcycs) { + /* decide which code to send. Default to 1 if needed */ + scc_socket_send_modem_code(port, 13, dcycs); /*13=9600*/ } -void -scc_socket_modem_do_ring(int port, double dcycs) -{ - Scc *scc_ptr; - double diff_dcycs; - int num_rings; +void scc_socket_modem_do_ring(int port, double dcycs) { + Scc *scc_ptr; + double diff_dcycs; + int num_rings; - scc_ptr = &(scc_stat[port]); - num_rings = scc_ptr->socket_num_rings; - if(num_rings > 0 && scc_ptr->socket_state == 0) { - num_rings--; - diff_dcycs = dcycs - scc_ptr->socket_last_ring_dcycs; - if(diff_dcycs < 2.0*1000*1000 && g_serial_modem[port]) { - return; /* nothing more to do */ - } - printf("In modem_do_ring, ringing at %f\n", dcycs); - if(g_serial_modem[port]) { - scc_socket_send_modem_code(port, 2, dcycs); /* RING */ - } else { - num_rings = 0; - } - scc_ptr->socket_num_rings = num_rings; - scc_ptr->socket_last_ring_dcycs = (int)dcycs; - if(num_rings <= 0) { - /* decide on answering */ - if(scc_ptr->modem_s0_val || (g_serial_modem[port]==0)) { - scc_socket_do_answer(port, dcycs); - } else { - printf("No answer, closing socket\n"); - scc_socket_close(port, 0, dcycs); - } - } - } + scc_ptr = &(scc_stat[port]); + num_rings = scc_ptr->socket_num_rings; + if(num_rings > 0 && scc_ptr->socket_state == 0) { + num_rings--; + diff_dcycs = dcycs - scc_ptr->socket_last_ring_dcycs; + if(diff_dcycs < 2.0*1000*1000 && g_serial_modem[port]) { + return; /* nothing more to do */ + } + printf("In modem_do_ring, ringing at %f\n", dcycs); + if(g_serial_modem[port]) { + scc_socket_send_modem_code(port, 2, dcycs); /* RING */ + } else { + num_rings = 0; + } + scc_ptr->socket_num_rings = num_rings; + scc_ptr->socket_last_ring_dcycs = (int)dcycs; + if(num_rings <= 0) { + /* decide on answering */ + if(scc_ptr->modem_s0_val || (g_serial_modem[port]==0)) { + scc_socket_do_answer(port, dcycs); + } else { + printf("No answer, closing socket\n"); + scc_socket_close(port, 0, dcycs); + } + } + } } -void -scc_socket_do_answer(int port, double dcycs) -{ - Scc *scc_ptr; +void scc_socket_do_answer(int port, double dcycs) { + Scc *scc_ptr; - scc_ptr = &(scc_stat[port]); - scc_ptr->modem_dial_or_acc_mode = 2; - scc_accept_socket(port, dcycs); - if(scc_ptr->rdwrfd == -1) { - printf("Answer when rdwrfd=-1, closing\n"); - scc_socket_close(port, 1, dcycs); - /* send NO CARRIER message */ - } else { - scc_ptr->socket_state = 1; - scc_socket_telnet_reqs(port, dcycs); - printf("Send telnet reqs, rdwrfd=%d\n", scc_ptr->rdwrfd); - if(g_serial_modem[port]) { - scc_socket_modem_connect(port, dcycs); - } - scc_ptr->dcd = 1; /* carrier on */ - scc_ptr->socket_state = 1; /* talk to socket */ - scc_ptr->socket_num_rings = 0; - } + scc_ptr = &(scc_stat[port]); + scc_ptr->modem_dial_or_acc_mode = 2; + scc_accept_socket(port, dcycs); + if(scc_ptr->rdwrfd == -1) { + printf("Answer when rdwrfd=-1, closing\n"); + scc_socket_close(port, 1, dcycs); + /* send NO CARRIER message */ + } else { + scc_ptr->socket_state = 1; + scc_socket_telnet_reqs(port, dcycs); + printf("Send telnet reqs, rdwrfd=%d\n", scc_ptr->rdwrfd); + if(g_serial_modem[port]) { + scc_socket_modem_connect(port, dcycs); + } + scc_ptr->dcd = 1; /* carrier on */ + scc_ptr->socket_state = 1; /* talk to socket */ + scc_ptr->socket_num_rings = 0; + } } diff --git a/src/scc_windriver.c b/src/scc_windriver.c index 6de835c..8fecd17 100644 --- a/src/scc_windriver.c +++ b/src/scc_windriver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ /* This file contains the Win32 COM1/COM2 calls */ @@ -23,238 +23,230 @@ extern Scc scc_stat[2]; extern word32 g_c025_val; #ifdef _WIN32 -int -scc_serial_win_init(int port) -{ - COMMTIMEOUTS commtimeouts; - TCHAR str_buf[8]; - Scc *scc_ptr; - HANDLE host_handle; - int state; - int ret; +int scc_serial_win_init(int port) { + COMMTIMEOUTS commtimeouts; + TCHAR str_buf[8]; + Scc *scc_ptr; + HANDLE host_handle; + int state; + int ret; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - scc_ptr->state = 0; /* mark as failed */ + scc_ptr->state = 0; /* mark as failed */ #ifdef UNICODE - wsprintf(&str_buf[0], _T("COM%d"), port+1); + wsprintf(&str_buf[0], _T("COM%d"), port+1); #else - sprintf(&str_buf[0], "COM%d", port+1); + sprintf(&str_buf[0], "COM%d", port+1); #endif - host_handle = CreateFile(&str_buf[0], GENERIC_READ | GENERIC_WRITE, - 0, NULL, OPEN_EXISTING, 0, NULL); + host_handle = CreateFile(&str_buf[0], GENERIC_READ | GENERIC_WRITE, + 0, NULL, OPEN_EXISTING, 0, NULL); - scc_ptr->host_handle = host_handle; - scc_ptr->host_handle2 = malloc(sizeof(DCB)); + scc_ptr->host_handle = host_handle; + scc_ptr->host_handle2 = malloc(sizeof(DCB)); - printf("scc_socket_init %d called, host_handle: %p\n", port, - host_handle); + printf("scc_socket_init %d called, host_handle: %p\n", port, + host_handle); - if(host_handle == INVALID_HANDLE_VALUE) { - scc_ptr->host_handle = 0; - return 0; - } + if(host_handle == INVALID_HANDLE_VALUE) { + scc_ptr->host_handle = 0; + return 0; + } - scc_serial_win_change_params(port); + scc_serial_win_change_params(port); - commtimeouts.ReadIntervalTimeout = MAXDWORD; - commtimeouts.ReadTotalTimeoutMultiplier = 0; - commtimeouts.ReadTotalTimeoutConstant = 0; - commtimeouts.WriteTotalTimeoutMultiplier = 0; - commtimeouts.WriteTotalTimeoutConstant = 10; - ret = SetCommTimeouts(host_handle, &commtimeouts); - if(ret == 0) { - printf("setcommtimeout ret: %d\n", ret); - } + commtimeouts.ReadIntervalTimeout = MAXDWORD; + commtimeouts.ReadTotalTimeoutMultiplier = 0; + commtimeouts.ReadTotalTimeoutConstant = 0; + commtimeouts.WriteTotalTimeoutMultiplier = 0; + commtimeouts.WriteTotalTimeoutConstant = 10; + ret = SetCommTimeouts(host_handle, &commtimeouts); + if(ret == 0) { + printf("setcommtimeout ret: %d\n", ret); + } - state = 2; /* raw serial */ - scc_ptr->state = state; + state = 2; /* raw serial */ + scc_ptr->state = state; - return state; + return state; } -void -scc_serial_win_change_params(int port) -{ - DCB *dcbptr; - HANDLE host_handle; - Scc *scc_ptr; - int ret; +void scc_serial_win_change_params(int port) { + DCB *dcbptr; + HANDLE host_handle; + Scc *scc_ptr; + int ret; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - host_handle = scc_ptr->host_handle; - dcbptr = (DCB*)scc_ptr->host_handle2; // OG Added cast - if(host_handle == 0) { - return; - } + host_handle = scc_ptr->host_handle; + dcbptr = (DCB*)scc_ptr->host_handle2; // OG Added cast + if(host_handle == 0) { + return; + } - ret = GetCommState(host_handle, dcbptr); - if(ret == 0) { - printf("getcomm port%d ret: %d\n", port, ret); - } + ret = GetCommState(host_handle, dcbptr); + if(ret == 0) { + printf("getcomm port%d ret: %d\n", port, ret); + } #if 1 - printf("dcb.baudrate: %d, bytesize:%d, stops:%d, parity:%d\n", - (int)dcbptr->BaudRate, (int)dcbptr->ByteSize, - (int)dcbptr->StopBits, (int)dcbptr->Parity); - printf("dcb.binary: %d, ctsflow: %d, dsrflow: %d, dtr: %d, dsr: %d\n", - (int)dcbptr->fBinary, - (int)dcbptr->fOutxCtsFlow, - (int)dcbptr->fOutxDsrFlow, - (int)dcbptr->fDtrControl, - (int)dcbptr->fDsrSensitivity); - printf("dcb.txonxoff:%d, outx:%d, inx: %d, null: %d, rts: %d\n", - (int)dcbptr->fTXContinueOnXoff, - (int)dcbptr->fOutX, - (int)dcbptr->fInX, - (int)dcbptr->fNull, - (int)dcbptr->fRtsControl); - printf("dcb.fAbortOnErr:%d, fParity:%d\n", (int)dcbptr->fAbortOnError, - (int)dcbptr->fParity); + printf("dcb.baudrate: %d, bytesize:%d, stops:%d, parity:%d\n", + (int)dcbptr->BaudRate, (int)dcbptr->ByteSize, + (int)dcbptr->StopBits, (int)dcbptr->Parity); + printf("dcb.binary: %d, ctsflow: %d, dsrflow: %d, dtr: %d, dsr: %d\n", + (int)dcbptr->fBinary, + (int)dcbptr->fOutxCtsFlow, + (int)dcbptr->fOutxDsrFlow, + (int)dcbptr->fDtrControl, + (int)dcbptr->fDsrSensitivity); + printf("dcb.txonxoff:%d, outx:%d, inx: %d, null: %d, rts: %d\n", + (int)dcbptr->fTXContinueOnXoff, + (int)dcbptr->fOutX, + (int)dcbptr->fInX, + (int)dcbptr->fNull, + (int)dcbptr->fRtsControl); + printf("dcb.fAbortOnErr:%d, fParity:%d\n", (int)dcbptr->fAbortOnError, + (int)dcbptr->fParity); #endif - dcbptr->fAbortOnError = 0; + dcbptr->fAbortOnError = 0; - dcbptr->BaudRate = scc_ptr->baud_rate; - dcbptr->ByteSize = scc_ptr->char_size; - dcbptr->StopBits = ONESTOPBIT; - switch((scc_ptr->reg[4] >> 2) & 0x3) { - case 2: // 1.5 stop bits - dcbptr->StopBits = ONE5STOPBITS; - break; - case 3: // 2 stop bits - dcbptr->StopBits = TWOSTOPBITS; - break; - } + dcbptr->BaudRate = scc_ptr->baud_rate; + dcbptr->ByteSize = scc_ptr->char_size; + dcbptr->StopBits = ONESTOPBIT; + switch((scc_ptr->reg[4] >> 2) & 0x3) { + case 2: // 1.5 stop bits + dcbptr->StopBits = ONE5STOPBITS; + break; + case 3: // 2 stop bits + dcbptr->StopBits = TWOSTOPBITS; + break; + } - dcbptr->Parity = NOPARITY; - switch((scc_ptr->reg[4]) & 0x3) { - case 1: // Odd parity - dcbptr->Parity = ODDPARITY; - break; - case 3: // Even parity - dcbptr->Parity = EVENPARITY; - break; - } + dcbptr->Parity = NOPARITY; + switch((scc_ptr->reg[4]) & 0x3) { + case 1: // Odd parity + dcbptr->Parity = ODDPARITY; + break; + case 3: // Even parity + dcbptr->Parity = EVENPARITY; + break; + } - dcbptr->fNull = 0; - dcbptr->fDtrControl = DTR_CONTROL_ENABLE; - dcbptr->fDsrSensitivity = 0; - dcbptr->fOutxCtsFlow = 0; - dcbptr->fOutxDsrFlow = 0; - dcbptr->fParity = 0; - dcbptr->fInX = 0; - dcbptr->fOutX = 0; - dcbptr->fRtsControl = RTS_CONTROL_ENABLE; + dcbptr->fNull = 0; + dcbptr->fDtrControl = DTR_CONTROL_ENABLE; + dcbptr->fDsrSensitivity = 0; + dcbptr->fOutxCtsFlow = 0; + dcbptr->fOutxDsrFlow = 0; + dcbptr->fParity = 0; + dcbptr->fInX = 0; + dcbptr->fOutX = 0; + dcbptr->fRtsControl = RTS_CONTROL_ENABLE; - ret = SetCommState(host_handle, dcbptr); - if(ret == 0) { - printf("SetCommState ret: %d, new baud: %d\n", ret, - (int)dcbptr->BaudRate); - } + ret = SetCommState(host_handle, dcbptr); + if(ret == 0) { + printf("SetCommState ret: %d, new baud: %d\n", ret, + (int)dcbptr->BaudRate); + } } -void -scc_serial_win_fill_readbuf(int port, int space_left, double dcycs) -{ - byte tmp_buf[256]; - Scc *scc_ptr; - HANDLE host_handle; - DWORD bytes_read; - DWORD i; - int ret; +void scc_serial_win_fill_readbuf(int port, int space_left, double dcycs) { + byte tmp_buf[256]; + Scc *scc_ptr; + HANDLE host_handle; + DWORD bytes_read; + DWORD i; + int ret; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - host_handle = scc_ptr->host_handle; - if(host_handle == 0) { - return; - } + host_handle = scc_ptr->host_handle; + if(host_handle == 0) { + return; + } - /* Try reading some bytes */ - space_left = MIN(256, space_left); - ret = ReadFile(host_handle, tmp_buf, space_left, &bytes_read, NULL); + /* Try reading some bytes */ + space_left = MIN(256, space_left); + ret = ReadFile(host_handle, tmp_buf, space_left, &bytes_read, NULL); - if(ret == 0) { - printf("ReadFile ret 0\n"); - } + if(ret == 0) { + printf("ReadFile ret 0\n"); + } + + if(ret && (bytes_read > 0)) { + for(i = 0; i < bytes_read; i++) { + scc_add_to_readbuf(port, tmp_buf[i], dcycs); + } + } - if(ret && (bytes_read > 0)) { - for(i = 0; i < bytes_read; i++) { - scc_add_to_readbuf(port, tmp_buf[i], dcycs); - } - } - } -void -scc_serial_win_empty_writebuf(int port) -{ - Scc *scc_ptr; - HANDLE host_handle; - int rdptr; - int wrptr; - int done; - word32 err_code; - DWORD bytes_written; - int ret; - int len; +void scc_serial_win_empty_writebuf(int port) { + Scc *scc_ptr; + HANDLE host_handle; + int rdptr; + int wrptr; + int done; + word32 err_code; + DWORD bytes_written; + int ret; + int len; - scc_ptr = &(scc_stat[port]); + scc_ptr = &(scc_stat[port]); - //printf("win_empty_writebuf, host_handle: %d\n", scc_ptr->host_handle); - host_handle = scc_ptr->host_handle; - if(host_handle == 0) { - return; - } + //printf("win_empty_writebuf, host_handle: %d\n", scc_ptr->host_handle); + host_handle = scc_ptr->host_handle; + if(host_handle == 0) { + return; + } - /* Try writing some bytes */ - done = 0; - while(!done) { - rdptr = scc_ptr->out_rdptr; - wrptr = scc_ptr->out_wrptr; - if(rdptr == wrptr) { - //printf("...rdptr == wrptr\n"); - done = 1; - break; - } - len = wrptr - rdptr; - if(len < 0) { - len = SCC_OUTBUF_SIZE - rdptr; - } - if(len > 32) { - len = 32; - } - if(len <= 0) { - done = 1; - break; - } - bytes_written = 1; - ret = WriteFile(host_handle, &(scc_ptr->out_buf[rdptr]), len, - &bytes_written, NULL); - printf("WriteFile ret: %d, bytes_written:%d, len:%d\n", ret, - (int)bytes_written, len); + /* Try writing some bytes */ + done = 0; + while(!done) { + rdptr = scc_ptr->out_rdptr; + wrptr = scc_ptr->out_wrptr; + if(rdptr == wrptr) { + //printf("...rdptr == wrptr\n"); + done = 1; + break; + } + len = wrptr - rdptr; + if(len < 0) { + len = SCC_OUTBUF_SIZE - rdptr; + } + if(len > 32) { + len = 32; + } + if(len <= 0) { + done = 1; + break; + } + bytes_written = 1; + ret = WriteFile(host_handle, &(scc_ptr->out_buf[rdptr]), len, + &bytes_written, NULL); + printf("WriteFile ret: %d, bytes_written:%d, len:%d\n", ret, + (int)bytes_written, len); - err_code = (word32)-1; - if(ret == 0) { - err_code = (word32)GetLastError(); - printf("WriteFile ret:0, err_code: %08x\n", err_code); - } + err_code = (word32)-1; + if(ret == 0) { + err_code = (word32)GetLastError(); + printf("WriteFile ret:0, err_code: %08x\n", err_code); + } - if(ret == 0 || (bytes_written == 0)) { - done = 1; - break; - } else { - rdptr = rdptr + bytes_written; - if(rdptr >= SCC_OUTBUF_SIZE) { - rdptr = rdptr - SCC_OUTBUF_SIZE; - } - scc_ptr->out_rdptr = rdptr; - } - } + if(ret == 0 || (bytes_written == 0)) { + done = 1; + break; + } else { + rdptr = rdptr + bytes_written; + if(rdptr >= SCC_OUTBUF_SIZE) { + rdptr = rdptr - SCC_OUTBUF_SIZE; + } + scc_ptr->out_rdptr = rdptr; + } + } } #endif diff --git a/src/sdl2_driver.c b/src/sdl2_driver.c index 679e37a..2d04f84 100644 --- a/src/sdl2_driver.c +++ b/src/sdl2_driver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ // fps shiz unsigned int lastTime = 0, currentTime, frames; @@ -25,16 +25,16 @@ unsigned int lastTime = 0, currentTime, frames; #endif // BITMASKS -#define ShiftMask 1 -#define ControlMask 4 -#define LockMask 2 +#define ShiftMask 1 +#define ControlMask 4 +#define LockMask 2 int g_use_shmem = 0; int g_num_check_input_calls = 0; int g_check_input_flush_rate = 2; -int g_win_status_debug = 0; // Current visibility of status lines. -int g_win_status_debug_request = 0; // Desired visibility of status lines. +int g_win_status_debug = 0; // Current visibility of status lines. +int g_win_status_debug_request = 0; // Desired visibility of status lines. int g_screen_mdepth = 0; int kb_shift_control_state = 0; @@ -67,7 +67,7 @@ extern word32 g_full_refresh_needed; extern word32 g_palette_8to1624[256]; extern word32 g_a2palette_8to1624[256]; extern Kimage g_mainwin_kimage; -extern const char g_gsplus_version_str[]; // version string for title bar +extern const char g_gsplus_version_str[]; // version string for title bar SDL_Window *window; // Declare a pointer SDL_Renderer *renderer; @@ -76,124 +76,123 @@ SDL_Texture *overlay_texture; // This is used for scanline simulation. Could be Uint32 *overlay_pixels; static char *g_clipboard = NULL; // clipboard variables -static size_t g_clipboard_pos = 0; +static size_t g_clipboard_pos = 0; void dev_video_init_sdl(); void handle_sdl_key_event(SDL_Event event); void check_input_events_sdl(); int handle_sdl_mouse_motion_event(SDL_Event event); -int g_num_a2_keycodes = 0; +int g_num_a2_keycodes = 0; int a2_key_to_sdlkeycode[][3] = { - { 0x35, SDLK_ESCAPE,0 }, - { 0x7a, SDLK_F1, 0 }, - { 0x78, SDLK_F2, 0 }, - { 0x63, SDLK_F3, 0 }, - { 0x76, SDLK_F4, 0 }, - { 0x60, SDLK_F5, 0 }, - { 0x61, SDLK_F6, 0 }, - { 0x62, SDLK_F7, 0 }, - { 0x64, SDLK_F8, 0 }, - { 0x65, SDLK_F9, 0 }, - { 0x6d, SDLK_F10, 0 }, - { 0x67, SDLK_F11, 0 }, - { 0x6f, SDLK_F12, 0 }, - { 0x69, SDLK_F13, 0 }, - { 0x6b, SDLK_F14, 0 }, - { 0x71, SDLK_F15, 0 }, + { 0x35, SDLK_ESCAPE,0 }, + { 0x7a, SDLK_F1, 0 }, + { 0x78, SDLK_F2, 0 }, + { 0x63, SDLK_F3, 0 }, + { 0x76, SDLK_F4, 0 }, + { 0x60, SDLK_F5, 0 }, + { 0x61, SDLK_F6, 0 }, + { 0x62, SDLK_F7, 0 }, + { 0x64, SDLK_F8, 0 }, + { 0x65, SDLK_F9, 0 }, + { 0x6d, SDLK_F10, 0 }, + { 0x67, SDLK_F11, 0 }, + { 0x6f, SDLK_F12, 0 }, + { 0x69, SDLK_F13, 0 }, + { 0x6b, SDLK_F14, 0 }, + { 0x71, SDLK_F15, 0 }, { 0x7f, SDLK_PAUSE, 0 }, - { 0x32, '`', '~' }, /* Key number 18? */ - { 0x12, '1', '!' }, - { 0x13, '2', '@' }, - { 0x14, '3', '#' }, - { 0x15, '4', '$' }, - { 0x17, '5', '%' }, - { 0x16, '6', '^' }, - { 0x1a, '7', '&' }, - { 0x1c, '8', '*' }, - { 0x19, '9', '(' }, - { 0x1d, '0', ')' }, - { 0x1b, '-', '_' }, - { 0x18, '=', '+' }, - { 0x33, SDLK_BACKSPACE, 0 }, - { 0x72, SDLK_INSERT, 0 }, /* Help? XK_Help */ + { 0x32, '`', '~' }, /* Key number 18? */ + { 0x12, '1', '!' }, + { 0x13, '2', '@' }, + { 0x14, '3', '#' }, + { 0x15, '4', '$' }, + { 0x17, '5', '%' }, + { 0x16, '6', '^' }, + { 0x1a, '7', '&' }, + { 0x1c, '8', '*' }, + { 0x19, '9', '(' }, + { 0x1d, '0', ')' }, + { 0x1b, '-', '_' }, + { 0x18, '=', '+' }, + { 0x33, SDLK_BACKSPACE, 0 }, + { 0x72, SDLK_INSERT, 0 }, /* Help? XK_Help */ /* { 0x73, XK_Home, 0 }, alias XK_Home to be XK_KP_Equal! */ - { 0x74, SDLK_PAGEUP, 0 }, - { 0x47, SDLK_NUMLOCKCLEAR, 0 }, /* Clear, XK_Clear */ - { 0x51, SDLK_KP_EQUALS, 0 }, /* Note XK_Home alias! XK_Home */ - { 0x4b, SDLK_KP_DIVIDE, 0 }, - { 0x43, SDLK_KP_MULTIPLY, 0 }, - { 0x30, SDLK_TAB, 0 }, - { 0x0c, 'q', 'Q' }, - { 0x0d, 'w', 'W' }, - { 0x0e, 'e', 'E' }, - { 0x0f, 'r', 'R' }, - { 0x11, 't', 'T' }, - { 0x10, 'y', 'Y' }, - { 0x20, 'u', 'U' }, - { 0x22, 'i', 'I' }, - { 0x1f, 'o', 'O' }, - { 0x23, 'p', 'P' }, - { 0x21, '[', '{' }, - { 0x1e, ']', '}' }, - { 0x2a, 0x5c, '|' }, /* backslash, bar */ - { 0x75, SDLK_DELETE, 0 }, - { 0x77, SDLK_END, 0 }, - { 0x79, SDLK_PAGEDOWN, 0 }, - { 0x59, SDLK_KP_7, SDLK_HOME }, - { 0x5b, SDLK_KP_8, SDLK_UP }, - { 0x5c, SDLK_KP_9, SDLK_PAGEUP }, - { 0x4e, SDLK_KP_MINUS, 0 }, - { 0x39, SDLK_CAPSLOCK, 0 }, - { 0x00, 'a', 'A' }, - { 0x01, 's', 'S' }, - { 0x02, 'd', 'D' }, - { 0x03, 'f', 'F' }, - { 0x05, 'g', 'G' }, - { 0x04, 'h', 'H' }, - { 0x26, 'j', 'J' }, - { 0x28, 'k', 'K' }, - { 0x25, 'l', 'L' }, - { 0x29, ';', ':' }, - { 0x27, 0x27, '"' }, /* single quote */ - { 0x24, SDLK_RETURN, 0 }, - { 0x56, SDLK_KP_4, SDLK_LEFT}, - { 0x57, SDLK_KP_5, 0 }, - { 0x58, SDLK_KP_6, SDLK_RIGHT }, - { 0x45, SDLK_KP_PLUS, 0 }, - { 0x38, SDLK_LSHIFT, SDLK_RSHIFT }, - { 0x06, 'z', 'Z' }, - { 0x07, 'x', 'X' }, - { 0x08, 'c', 'C' }, - { 0x09, 'v', 'V' }, - { 0x0b, 'b', 'B' }, - { 0x2d, 'n', 'N' }, - { 0x2e, 'm', 'M' }, - { 0x2b, ',', '<' }, - { 0x2f, '.', '>' }, - { 0x2c, '/', '?' }, - { 0x3e, SDLK_UP, 0 }, - { 0x53, SDLK_KP_1, 0 }, - { 0x54, SDLK_KP_2, SDLK_DOWN }, - { 0x55, SDLK_KP_3, SDLK_PAGEDOWN }, - { 0x36, SDLK_RCTRL, SDLK_LCTRL }, - { 0x3a, SDLK_LALT, SDLK_RALT }, /* Option */ - { 0x37, SDLK_LGUI, SDLK_RGUI }, /* Command */ - { 0x31, ' ', 0 }, - { 0x3b, SDLK_LEFT, 0 }, - { 0x3d, SDLK_DOWN, 0 }, - { 0x3c, SDLK_RIGHT, 0 }, - { 0x52, SDLK_KP_0, 0 }, - { 0x41, SDLK_KP_PERIOD, 0 }, - { 0x4c, SDLK_KP_ENTER, 0 }, + { 0x74, SDLK_PAGEUP, 0 }, + { 0x47, SDLK_NUMLOCKCLEAR, 0 }, /* Clear, XK_Clear */ + { 0x51, SDLK_KP_EQUALS, 0 }, /* Note XK_Home alias! XK_Home */ + { 0x4b, SDLK_KP_DIVIDE, 0 }, + { 0x43, SDLK_KP_MULTIPLY, 0 }, + { 0x30, SDLK_TAB, 0 }, + { 0x0c, 'q', 'Q' }, + { 0x0d, 'w', 'W' }, + { 0x0e, 'e', 'E' }, + { 0x0f, 'r', 'R' }, + { 0x11, 't', 'T' }, + { 0x10, 'y', 'Y' }, + { 0x20, 'u', 'U' }, + { 0x22, 'i', 'I' }, + { 0x1f, 'o', 'O' }, + { 0x23, 'p', 'P' }, + { 0x21, '[', '{' }, + { 0x1e, ']', '}' }, + { 0x2a, 0x5c, '|' }, /* backslash, bar */ + { 0x75, SDLK_DELETE, 0 }, + { 0x77, SDLK_END, 0 }, + { 0x79, SDLK_PAGEDOWN, 0 }, + { 0x59, SDLK_KP_7, SDLK_HOME }, + { 0x5b, SDLK_KP_8, SDLK_UP }, + { 0x5c, SDLK_KP_9, SDLK_PAGEUP }, + { 0x4e, SDLK_KP_MINUS, 0 }, + { 0x39, SDLK_CAPSLOCK, 0 }, + { 0x00, 'a', 'A' }, + { 0x01, 's', 'S' }, + { 0x02, 'd', 'D' }, + { 0x03, 'f', 'F' }, + { 0x05, 'g', 'G' }, + { 0x04, 'h', 'H' }, + { 0x26, 'j', 'J' }, + { 0x28, 'k', 'K' }, + { 0x25, 'l', 'L' }, + { 0x29, ';', ':' }, + { 0x27, 0x27, '"' }, /* single quote */ + { 0x24, SDLK_RETURN, 0 }, + { 0x56, SDLK_KP_4, SDLK_LEFT}, + { 0x57, SDLK_KP_5, 0 }, + { 0x58, SDLK_KP_6, SDLK_RIGHT }, + { 0x45, SDLK_KP_PLUS, 0 }, + { 0x38, SDLK_LSHIFT, SDLK_RSHIFT }, + { 0x06, 'z', 'Z' }, + { 0x07, 'x', 'X' }, + { 0x08, 'c', 'C' }, + { 0x09, 'v', 'V' }, + { 0x0b, 'b', 'B' }, + { 0x2d, 'n', 'N' }, + { 0x2e, 'm', 'M' }, + { 0x2b, ',', '<' }, + { 0x2f, '.', '>' }, + { 0x2c, '/', '?' }, + { 0x3e, SDLK_UP, 0 }, + { 0x53, SDLK_KP_1, 0 }, + { 0x54, SDLK_KP_2, SDLK_DOWN }, + { 0x55, SDLK_KP_3, SDLK_PAGEDOWN }, + { 0x36, SDLK_RCTRL, SDLK_LCTRL }, + { 0x3a, SDLK_LALT, SDLK_RALT }, /* Option */ + { 0x37, SDLK_LGUI, SDLK_RGUI }, /* Command */ + { 0x31, ' ', 0 }, + { 0x3b, SDLK_LEFT, 0 }, + { 0x3d, SDLK_DOWN, 0 }, + { 0x3c, SDLK_RIGHT, 0 }, + { 0x52, SDLK_KP_0, 0 }, + { 0x41, SDLK_KP_PERIOD, 0 }, + { 0x4c, SDLK_KP_ENTER, 0 }, { -1, -1, -1 } }; -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { return gsplusmain(argc, argv); } @@ -225,12 +224,12 @@ SDL_bool IsFullScreen(SDL_Window *win) { void dev_video_init() { - word32 lores_col; + word32 lores_col; // build keycode map ?? g_num_a2_keycodes = 0; - int i; - int keycode; + int i; + int keycode; for(i = 0; i < 0x7f; i++) { keycode = a2_key_to_sdlkeycode[i][0]; @@ -310,15 +309,14 @@ void dev_video_init_sdl() { window = SDL_CreateWindow( - window_title, // window title (GSport vX.X) - // SDL_WINDOWPOS_UNDEFINED, // initial x position - // SDL_WINDOWPOS_UNDEFINED, // initial y position - startx, starty, + window_title, // window title (GSport vX.X) + startx, + starty, BASE_WINDOW_WIDTH, // width, in pixels X_A2_WINDOW_HEIGHT, // height, in pixels SDL_WINDOW_OPENGL // flags - see below | more_flags - ); + ); // Check that the window was successfully created @@ -340,16 +338,16 @@ void dev_video_init_sdl() { SDL_RenderSetLogicalSize(renderer, BASE_WINDOW_WIDTH, X_A2_WINDOW_HEIGHT); texture = SDL_CreateTexture(renderer, - SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STREAMING, - BASE_WINDOW_WIDTH, X_A2_WINDOW_HEIGHT); + SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + BASE_WINDOW_WIDTH, X_A2_WINDOW_HEIGHT); // The window is open: could enter program loop here (see SDL_PollEvent()) //overlay test overlay_texture = SDL_CreateTexture(renderer, - SDL_PIXELFORMAT_ARGB8888, - SDL_TEXTUREACCESS_STREAMING, - BASE_WINDOW_WIDTH, - X_A2_WINDOW_HEIGHT); + SDL_PIXELFORMAT_ARGB8888, + SDL_TEXTUREACCESS_STREAMING, + BASE_WINDOW_WIDTH, + X_A2_WINDOW_HEIGHT); SDL_SetTextureBlendMode(overlay_texture, SDL_BLENDMODE_BLEND); overlay_pixels = malloc(BASE_WINDOW_WIDTH*X_A2_WINDOW_HEIGHT*sizeof(Uint32)); @@ -384,15 +382,6 @@ void dev_video_init_sdl() { // Copy a rect to our SDL window void sdl_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, int width, int height) { - // FPS shiz - currentTime = SDL_GetTicks(); - if (currentTime > lastTime + 1000) { - printf("FPS: %d\n", frames); - frames = 0; - lastTime = currentTime; - } - frames++; - byte *src_ptr; int pixel_size = 4; @@ -436,9 +425,9 @@ void set_refresh_needed() { void x_get_kimage(Kimage *kimage_ptr) { byte *data; - int width; - int height; - int depth; + int width; + int height; + int depth; width = kimage_ptr->width_req; height = kimage_ptr->height; @@ -455,7 +444,7 @@ void check_input_events() { void check_input_events_sdl() { - int motion = 0; + int motion = 0; SDL_Event event; while (SDL_PollEvent(&event)) { @@ -463,7 +452,7 @@ void check_input_events_sdl() { if (event.type == SDL_WINDOWEVENT) { set_refresh_needed(); } - switch( event.type ){ + switch( event.type ) { case SDL_KEYDOWN: case SDL_KEYUP: handle_sdl_key_event(event); @@ -486,7 +475,7 @@ void check_input_events_sdl() { cfg_inspect_maybe_insert_file(file, 0); SDL_free(file); } - break; + break; default: break; } @@ -495,7 +484,7 @@ void check_input_events_sdl() { int sdl_keysym_to_a2code(int keysym, int is_up) { - int i; + int i; if(keysym == 0) { return -1; @@ -526,7 +515,7 @@ int sdl_keysym_to_a2code(int keysym, int is_up) { /* Look up Apple 2 keycode */ for(i = g_num_a2_keycodes - 1; i >= 0; i--) { if((keysym == a2_key_to_sdlkeycode[i][1]) || - (keysym == a2_key_to_sdlkeycode[i][2])) { + (keysym == a2_key_to_sdlkeycode[i][2])) { return a2_key_to_sdlkeycode[i][0]; } } @@ -536,9 +525,9 @@ int sdl_keysym_to_a2code(int keysym, int is_up) { void handle_sdl_key_event(SDL_Event event) { - int state_xor; + int state_xor; int state = 0; - int is_up; + int is_up; int mod = event.key.keysym.mod; @@ -548,11 +537,11 @@ void handle_sdl_key_event(SDL_Event event) { // when mod key is first press, comes as event, otherwise just a modifier if( mod & KMOD_LCTRL || mod & KMOD_RCTRL || - event.type == (SDL_KEYDOWN && (event.key.keysym.sym == SDLK_LCTRL || event.key.keysym.sym == SDLK_RCTRL))) { - state = state | ControlMask; + event.type == (SDL_KEYDOWN && (event.key.keysym.sym == SDLK_LCTRL || event.key.keysym.sym == SDLK_RCTRL))) { + state = state | ControlMask; } if( (mod & KMOD_LSHIFT) || (mod & KMOD_RSHIFT) || - event.type == (SDL_KEYDOWN && (event.key.keysym.sym == SDLK_LSHIFT || event.key.keysym.sym == SDLK_RSHIFT))) { + event.type == (SDL_KEYDOWN && (event.key.keysym.sym == SDLK_LSHIFT || event.key.keysym.sym == SDLK_RSHIFT))) { state = state | ShiftMask; } if( mod & KMOD_CAPS) { @@ -581,7 +570,7 @@ void handle_sdl_key_event(SDL_Event event) { if (event.type == SDL_KEYUP) { is_up = 1; } - switch( event.key.keysym.sym ){ + switch( event.key.keysym.sym ) { case SDLK_F11: if (kb_shift_control_state & ShiftMask) { // SHIFT+F11 if (!is_up) { @@ -623,7 +612,7 @@ int handle_sdl_mouse_motion_event(SDL_Event event) { x = event.motion.x - BASE_MARGIN_LEFT; y = event.motion.y - BASE_MARGIN_TOP; if (event.type == SDL_MOUSEBUTTONUP) { - return update_mouse(x, y, 0 , event.motion.state &7 ); + return update_mouse(x, y, 0, event.motion.state &7 ); } else { return update_mouse(x, y, event.motion.state, event.motion.state &7 ); } @@ -708,13 +697,13 @@ void x_take_screenshot() { SDL_FreeSurface(sshot); SDL_Surface *s = SDL_CreateRGBSurface(0, BASE_WINDOW_WIDTH, X_A2_WINDOW_HEIGHT, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); - if (s) { - SDL_Surface * image = SDL_LoadBMP("screenshot.bmp"); - IMG_SavePNG(image, screenshot_filename); - SDL_FreeSurface(image); - } - SDL_FreeSurface(s); + if (s) { + SDL_Surface * image = SDL_LoadBMP("screenshot.bmp"); + IMG_SavePNG(image, screenshot_filename); + SDL_FreeSurface(image); } + SDL_FreeSurface(s); +} void clipboard_paste(void) { @@ -741,7 +730,7 @@ int clipboard_get_char(void) { char c; if (!g_clipboard) - return 0; + return 0; /* skip utf-8 characters. */ do { @@ -750,7 +739,7 @@ int clipboard_get_char(void) { /* windows -- skip the \n in \r\n. */ if (c == '\r' && g_clipboard[g_clipboard_pos] == '\n') - g_clipboard_pos++; + g_clipboard_pos++; /* everybody else -- convert \n to \r */ if (c == '\n') c = '\r'; @@ -788,7 +777,7 @@ void x_auto_repeat_off(int must) { } void x_release_kimage(Kimage* kimage_ptr) { } // OG Addding ratio int x_calc_ratio(float x,float y) { return 1; } -void x_set_mask_and_shift(word32 x_mask, word32 *mask_ptr, int *shift_left_ptr, int *shift_right_ptr) { return; } +void x_set_mask_and_shift(word32 x_mask, word32 *mask_ptr, int *shift_left_ptr, int *shift_right_ptr) { return; } void x_update_color(int col_num, int red, int green, int blue, word32 rgb) { } void x_update_physical_colormap() { } void show_xcolor_array() { } diff --git a/src/sdl2snd_driver.c b/src/sdl2snd_driver.c index e3cb138..4293904 100644 --- a/src/sdl2snd_driver.c +++ b/src/sdl2snd_driver.c @@ -1,18 +1,18 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "SDL.h" #include "defc.h" #include "glog.h" #include "sound.h" #include -extern word32 *g_sound_shm_addr; -extern int g_sound_shm_pos; -extern int g_audio_enable; +extern word32 *g_sound_shm_addr; +extern int g_sound_shm_pos; +extern int g_audio_enable; extern int g_preferred_rate; static byte *playbuf = 0; @@ -29,187 +29,179 @@ static int g_zeroes_seen; SDL_AudioDeviceID dev = 0; -void sdlsnd_init(word32 *shmaddr) -{ +void sdlsnd_init(word32 *shmaddr) { if (SDL_Init(SDL_INIT_AUDIO) < 0) { - glog("Could not initialize SDL2 audio"); - g_audio_enable = 0; - } else { - glog("SDL2 audio initialized"); - } + glog("Could not initialize SDL2 audio"); + g_audio_enable = 0; + } else { + glog("SDL2 audio initialized"); + } child_sound_loop(-1, -1, shmaddr); return; } -void -sound_write_sdl(int real_samps, int size) -{ +void sound_write_sdl(int real_samps, int size) { #ifdef HAVE_SDL - int shm_read; + int shm_read; - if (real_samps) { - shm_read = (g_sound_shm_pos - size + SOUND_SHM_SAMP_SIZE)%SOUND_SHM_SAMP_SIZE; - SDL_LockAudioDevice(dev); - while(size > 0) { - if(g_playbuf_buffered >= snd_buf) { - printf("sound_write_sdl failed @%d, %d buffered, %d samples skipped\n",snd_write,g_playbuf_buffered, size); - shm_read += size; - shm_read %= SOUND_SHM_SAMP_SIZE; - size = 0; - } else { - ((word32*)playbuf)[snd_write/SAMPLE_CHAN_SIZE] = g_sound_shm_addr[shm_read]; - shm_read++; - if (shm_read >= SOUND_SHM_SAMP_SIZE) - shm_read = 0; - snd_write += SAMPLE_CHAN_SIZE; - if (snd_write >= snd_buf) - snd_write = 0; - size--; - g_playbuf_buffered += SAMPLE_CHAN_SIZE; - } - } + if (real_samps) { + shm_read = (g_sound_shm_pos - size + SOUND_SHM_SAMP_SIZE)%SOUND_SHM_SAMP_SIZE; + SDL_LockAudioDevice(dev); + while(size > 0) { + if(g_playbuf_buffered >= snd_buf) { + printf("sound_write_sdl failed @%d, %d buffered, %d samples skipped\n",snd_write,g_playbuf_buffered, size); + shm_read += size; + shm_read %= SOUND_SHM_SAMP_SIZE; + size = 0; + } else { + ((word32*)playbuf)[snd_write/SAMPLE_CHAN_SIZE] = g_sound_shm_addr[shm_read]; + shm_read++; + if (shm_read >= SOUND_SHM_SAMP_SIZE) + shm_read = 0; + snd_write += SAMPLE_CHAN_SIZE; + if (snd_write >= snd_buf) + snd_write = 0; + size--; + g_playbuf_buffered += SAMPLE_CHAN_SIZE; + } + } - assert((snd_buf+snd_write - snd_read)%snd_buf == g_playbuf_buffered%snd_buf); - assert(g_sound_shm_pos == shm_read); - SDL_UnlockAudioDevice(dev); - } - if(g_sound_paused && (g_playbuf_buffered > 0)) { - glogf("Unpausing sound, %d buffered",g_playbuf_buffered); - g_sound_paused = 0; - SDL_PauseAudioDevice(dev, 0); - } - if(!g_sound_paused && (g_playbuf_buffered <= 0)) { - glog("Pausing sound"); - g_sound_paused = 1; - SDL_PauseAudioDevice(dev, 1); - } + assert((snd_buf+snd_write - snd_read)%snd_buf == g_playbuf_buffered%snd_buf); + assert(g_sound_shm_pos == shm_read); + SDL_UnlockAudioDevice(dev); + } + if(g_sound_paused && (g_playbuf_buffered > 0)) { + glogf("Unpausing sound, %d buffered",g_playbuf_buffered); + g_sound_paused = 0; + SDL_PauseAudioDevice(dev, 0); + } + if(!g_sound_paused && (g_playbuf_buffered <= 0)) { + glog("Pausing sound"); + g_sound_paused = 1; + SDL_PauseAudioDevice(dev, 1); + } #endif } #ifdef HAVE_SDL /* Callback for sound */ -static void _snd_callback(void* userdata, Uint8 *stream, int len) -{ - int i; - /* Slurp off the play buffer */ - assert((snd_buf+snd_write - snd_read)%snd_buf == g_playbuf_buffered%snd_buf); - /*printf("slurp %d, %d buffered\n",len, g_playbuf_buffered);*/ - for(i = 0; i < len; ++i) { - if(g_playbuf_buffered <= 0) { - stream[i] = 0; - } else { - stream[i] = playbuf[snd_read++]; - if(snd_read == snd_buf) - snd_read = 0; - g_playbuf_buffered--; - } - } -#if 0 - if (g_playbuf_buffered <= 0) { - printf("snd_callback: buffer empty, Pausing sound\n"); - g_sound_paused = 1; - SDL_PauseAudio(1); - } -#endif - //printf("end slurp %d, %d buffered\n",len, g_playbuf_buffered); -} -#endif - - - - -long -sound_init_device_sdl() -{ -#ifdef HAVE_SDL - long rate; - SDL_AudioSpec wanted; - - if (SDL_Init(SDL_INIT_AUDIO) < 0) { - glogf("SDL2 Couldn't init SDL_INIT_AUDIO: %s!", SDL_GetError()); - return 0; - } - - /* Set the desired format */ - wanted.freq = g_preferred_rate; - wanted.format = AUDIO_S16SYS; - wanted.channels = NUM_CHANNELS; - wanted.samples = 512; - wanted.callback = _snd_callback; - wanted.userdata = NULL; - - /* Open audio, and get the real spec */ - dev = SDL_OpenAudioDevice(NULL, 0, &wanted, &spec, 0); - if (dev == 0) { - glogf("SDL2 Couldn't open audio: %s!", SDL_GetError()); - SDL_QuitSubSystem(SDL_INIT_AUDIO); - return 0; - +static void _snd_callback(void* userdata, Uint8 *stream, int len) { + int i; + /* Slurp off the play buffer */ + assert((snd_buf+snd_write - snd_read)%snd_buf == g_playbuf_buffered%snd_buf); + /*printf("slurp %d, %d buffered\n",len, g_playbuf_buffered);*/ + for(i = 0; i < len; ++i) { + if(g_playbuf_buffered <= 0) { + stream[i] = 0; } else { - glogf("SDL2 opened audio device: %d", dev); + stream[i] = playbuf[snd_read++]; + if(snd_read == snd_buf) + snd_read = 0; + g_playbuf_buffered--; } - - /* Check everything */ - if(spec.channels != wanted.channels) { - glogf("SDL2 Warning, couldn't get stereo audio format!"); - //goto snd_error; - } - if(spec.format != wanted.format) { - glog("SDL2 Warning, couldn't get a supported audio format!"); - glogf("SDL2 wanted %X, got %X",wanted.format,spec.format); - //goto snd_error; - } - if(spec.freq != wanted.freq) { - glogf("SDL2 wanted rate = %d, got rate = %d", wanted.freq, spec.freq); - } - /* Set things as they really are */ - rate = spec.freq; - - snd_buf = SOUND_SHM_SAMP_SIZE*SAMPLE_CHAN_SIZE; - playbuf = (byte*) malloc(snd_buf); - if (!playbuf) - goto snd_error; - g_playbuf_buffered = 0; - - glogf("SDL2 sound shared memory size=%d", SOUND_SHM_SAMP_SIZE * SAMPLE_CHAN_SIZE); - - g_sound_shm_addr = malloc(SOUND_SHM_SAMP_SIZE * SAMPLE_CHAN_SIZE); - memset(g_sound_shm_addr,0,SOUND_SHM_SAMP_SIZE * SAMPLE_CHAN_SIZE); - - /* It's all good! */ - g_zeroes_buffered = 0; - g_zeroes_seen = 0; - /* Let's start playing sound */ - g_sound_paused = 0; - SDL_PauseAudioDevice(dev, 0); - - set_audio_rate(rate); - return rate; - - snd_error: - /* Oops! Something bad happened, cleanup. */ - SDL_CloseAudioDevice(dev); - SDL_QuitSubSystem(SDL_INIT_AUDIO); - if(playbuf) - free((void*)playbuf); - playbuf = 0; - snd_buf = 0; - return 0; -#else - return 0; + } +#if 0 + if (g_playbuf_buffered <= 0) { + printf("snd_callback: buffer empty, Pausing sound\n"); + g_sound_paused = 1; + SDL_PauseAudio(1); + } #endif + //printf("end slurp %d, %d buffered\n",len, g_playbuf_buffered); } +#endif -void -sound_shutdown_sdl() -{ + + + +long sound_init_device_sdl() { #ifdef HAVE_SDL - SDL_CloseAudioDevice(dev); - if(playbuf) - free((void*)playbuf); - playbuf = 0; + long rate; + SDL_AudioSpec wanted; + + if (SDL_Init(SDL_INIT_AUDIO) < 0) { + glogf("SDL2 Couldn't init SDL_INIT_AUDIO: %s!", SDL_GetError()); + return 0; + } + + /* Set the desired format */ + wanted.freq = g_preferred_rate; + wanted.format = AUDIO_S16SYS; + wanted.channels = NUM_CHANNELS; + wanted.samples = 512; + wanted.callback = _snd_callback; + wanted.userdata = NULL; + + /* Open audio, and get the real spec */ + dev = SDL_OpenAudioDevice(NULL, 0, &wanted, &spec, 0); + if (dev == 0) { + glogf("SDL2 Couldn't open audio: %s!", SDL_GetError()); + SDL_QuitSubSystem(SDL_INIT_AUDIO); + return 0; + + } else { + glogf("SDL2 opened audio device: %d", dev); + } + + /* Check everything */ + if(spec.channels != wanted.channels) { + glogf("SDL2 Warning, couldn't get stereo audio format!"); + //goto snd_error; + } + if(spec.format != wanted.format) { + glog("SDL2 Warning, couldn't get a supported audio format!"); + glogf("SDL2 wanted %X, got %X",wanted.format,spec.format); + //goto snd_error; + } + if(spec.freq != wanted.freq) { + glogf("SDL2 wanted rate = %d, got rate = %d", wanted.freq, spec.freq); + } + /* Set things as they really are */ + rate = spec.freq; + + snd_buf = SOUND_SHM_SAMP_SIZE*SAMPLE_CHAN_SIZE; + playbuf = (byte*) malloc(snd_buf); + if (!playbuf) + goto snd_error; + g_playbuf_buffered = 0; + + glogf("SDL2 sound shared memory size=%d", SOUND_SHM_SAMP_SIZE * SAMPLE_CHAN_SIZE); + + g_sound_shm_addr = malloc(SOUND_SHM_SAMP_SIZE * SAMPLE_CHAN_SIZE); + memset(g_sound_shm_addr,0,SOUND_SHM_SAMP_SIZE * SAMPLE_CHAN_SIZE); + + /* It's all good! */ + g_zeroes_buffered = 0; + g_zeroes_seen = 0; + /* Let's start playing sound */ + g_sound_paused = 0; + SDL_PauseAudioDevice(dev, 0); + + set_audio_rate(rate); + return rate; + +snd_error: + /* Oops! Something bad happened, cleanup. */ + SDL_CloseAudioDevice(dev); + SDL_QuitSubSystem(SDL_INIT_AUDIO); + if(playbuf) + free((void*)playbuf); + playbuf = 0; + snd_buf = 0; + return 0; +#else + return 0; +#endif +} + +void sound_shutdown_sdl() { +#ifdef HAVE_SDL + SDL_CloseAudioDevice(dev); + if(playbuf) + free((void*)playbuf); + playbuf = 0; #endif } diff --git a/src/sdlsnd_driver.c b/src/sdlsnd_driver.c index c67b65b..a0803d0 100644 --- a/src/sdlsnd_driver.c +++ b/src/sdlsnd_driver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include #include "defc.h" @@ -18,7 +18,7 @@ extern int g_audio_rate; unsigned int __stdcall child_sound_loop_win32(void *param); void check_wave_error(int res, char *str); -#define NUM_WAVE_HEADERS 8 +#define NUM_WAVE_HEADERS 8 //HWAVEOUT g_wave_handle = NULL; // OG Default value must be set //WAVEHDR g_wavehdr[NUM_WAVE_HEADERS]; @@ -27,7 +27,7 @@ extern int g_audio_enable; extern word32 *g_sound_shm_addr; extern int g_preferred_rate; -int g_sdlsnd_buflen = 0x1000; +int g_sdlsnd_buflen = 0x1000; word32 *bptr = NULL; int g_sdlsnd_write_idx; int g_sdlsnd_read_idx; @@ -55,13 +55,12 @@ static void sound_write_sdl(int real_samps, int size); #endif -void sdlsnd_init(word32 *shmaddr) -{ +void sdlsnd_init(word32 *shmaddr) { printf("sdlsnd_init\n"); if (SDL_Init(SDL_INIT_AUDIO) < 0) { - printf("Cannot initialize SDL audio\n"); - g_audio_enable = 0; - } + printf("Cannot initialize SDL audio\n"); + g_audio_enable = 0; + } child_sound_loop(-1, -1, shmaddr); return; @@ -70,17 +69,15 @@ void sdlsnd_init(word32 *shmaddr) -void -win32snd_init(word32 *shmaddr) -{ - printf("win32snd_init\n"); - child_sound_loop(-1, -1, shmaddr); +void win32snd_init(word32 *shmaddr) { + printf("win32snd_init\n"); + child_sound_loop(-1, -1, shmaddr); - return; + return; } -void sdl_send_audio(word32 *ptr, int size, int real_samps) { +void sdl_send_audio(word32 *ptr, int size, int real_samps) { /* code */ //printf(" sdl_s_a %d\t 0x%08x ",size, &ptr); @@ -103,16 +100,16 @@ void handle_sdl_snd(void *userdata, Uint8 *stream, int len) { /* Only play if we have data left */ /* if ( g_playbuf_buffered == 0) { return; - } -*/ + } + */ for(int i = 0; i < len; ++i) { if(g_playbuf_buffered <= 0) { - stream[i] = 0; + stream[i] = 0; } else { - stream[i] = bptr[g_sdlsnd_read_idx++]; - if(g_sdlsnd_read_idx == g_sdlsnd_buflen) - g_sdlsnd_read_idx = 0; - g_playbuf_buffered--; + stream[i] = bptr[g_sdlsnd_read_idx++]; + if(g_sdlsnd_read_idx == g_sdlsnd_buflen) + g_sdlsnd_read_idx = 0; + g_playbuf_buffered--; } } return; @@ -128,15 +125,15 @@ void handle_sdl_snd(void *userdata, Uint8 *stream, int len) { g_playbuf_buffered -= len; } else { /* - int top_len = 0; - top_len = g_sdlsnd_buflen - g_sdlsnd_read_idx; - SDL_memcpy (stream, &bptr[g_sdlsnd_read_idx], top_len); - g_sdlsnd_read_idx = 0; - g_playbuf_buffered -= top_len; - // SDL_memcpy (stream+top_len, bptr[g_sdlsnd_read_idx], len-top_len); - g_sdlsnd_read_idx += len-top_len; - g_playbuf_buffered -= len-top_len; - */ + int top_len = 0; + top_len = g_sdlsnd_buflen - g_sdlsnd_read_idx; + SDL_memcpy (stream, &bptr[g_sdlsnd_read_idx], top_len); + g_sdlsnd_read_idx = 0; + g_playbuf_buffered -= top_len; + // SDL_memcpy (stream+top_len, bptr[g_sdlsnd_read_idx], len-top_len); + g_sdlsnd_read_idx += len-top_len; + g_playbuf_buffered -= len-top_len; + */ } //SDL_MixAudio(stream, pointer, len, SDL_MIX_MAXVOLUME); @@ -144,9 +141,7 @@ void handle_sdl_snd(void *userdata, Uint8 *stream, int len) { } -void -child_sound_init_sdl() -{ +void child_sound_init_sdl() { printf("child_sound_init_sdl"); SDL_memset(&want, 0, sizeof(want)); // or SDL_zero(want) @@ -158,11 +153,11 @@ child_sound_init_sdl() dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, SDL_AUDIO_ALLOW_FORMAT_CHANGE); if (dev == 0) { - printf("Failed to open audio: %s\n", SDL_GetError()); + printf("Failed to open audio: %s\n", SDL_GetError()); } else { - if (have.format != want.format) { // we let this one thing change. - printf("We didn't get Float32 audio format.\n"); - } + if (have.format != want.format) { // we let this one thing change. + printf("We didn't get Float32 audio format.\n"); + } } @@ -185,16 +180,15 @@ child_sound_init_sdl() SDL_PauseAudioDevice(dev, 0); // start audio playing. - g_audio_rate = have.freq; + g_audio_rate = have.freq; printf("g_audio_rate: %d\n", g_audio_rate); - set_audio_rate(g_audio_rate); // let kegs simulator know the rate + set_audio_rate(g_audio_rate); // let kegs simulator know the rate } -void -sdlsnd_shutdown() { +void sdlsnd_shutdown() { //SDL_Delay(5000); // let the audio callback play some sound for 5 seconds. SDL_CloseAudioDevice(dev); printf("sdlsnd_shutdown"); diff --git a/src/sim65816.c b/src/sim65816.c index fefa9cd..5162313 100644 --- a/src/sim65816.c +++ b/src/sim65816.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include #include "defc.h" @@ -25,17 +25,17 @@ extern char g_config_gsplus_screenshot_dir[]; #endif #if defined (_WIN32) || defined(__CYGWIN__) && !defined(WIN_SDL) - #define WIN32_LEAN_AND_MEAN /* Tell windows we want less header gunk */ - #define STRICT /* Tell Windows we want compile type checks */ - #include /* Need a definition for LPTSTR in CYGWIN */ - extern void get_cwd(LPTSTR buffer, int size); + #define WIN32_LEAN_AND_MEAN /* Tell windows we want less header gunk */ + #define STRICT /* Tell Windows we want compile type checks */ + #include /* Need a definition for LPTSTR in CYGWIN */ +extern void get_cwd(LPTSTR buffer, int size); #endif -#define PC_LOG_LEN (8*1024) +#define PC_LOG_LEN (8*1024) -int g_speed_fast ; // OG Expose fast parameter -int g_initialized = 0; // OG To know if the emulator has finalized its initialization -int g_accept_events = 0; // OG To know if the emulator is ready to accept external events +int g_speed_fast; // OG Expose fast parameter +int g_initialized = 0; // OG To know if the emulator has finalized its initialization +int g_accept_events = 0; // OG To know if the emulator is ready to accept external events char g_argv0_path[256] = "./"; const char *g_gsplus_default_paths[] = { // probably overkill on the paths @@ -51,19 +51,20 @@ const char *g_gsplus_default_paths[] = { // probably overkill on the paths "/usr/share/gsplus/", "/var/lib/", "${0}/", - 0 }; + 0 +}; -#define MAX_EVENTS 64 +#define MAX_EVENTS 64 /* All EV_* must be less than 256, since upper bits reserved for other use */ /* e.g., DOC_INT uses upper bits to encode oscillator */ -#define EV_60HZ 1 -#define EV_STOP 2 -#define EV_SCAN_INT 3 -#define EV_DOC_INT 4 -#define EV_VBL_INT 5 -#define EV_SCC 6 -#define EV_VID_UPD 7 +#define EV_60HZ 1 +#define EV_STOP 2 +#define EV_SCAN_INT 3 +#define EV_DOC_INT 4 +#define EV_VBL_INT 5 +#define EV_SCC 6 +#define EV_VID_UPD 7 extern int g_stepping; extern int g_dbg_step; @@ -112,8 +113,8 @@ extern int g_dbg_enable_port; /* display parameters */ char g_display_env[512]; -int g_force_depth = -1; -int g_screen_depth = 8; +int g_force_depth = -1; +int g_screen_depth = 8; int g_scanline_simulator = 0; extern int g_screen_redraw_skip_amt; @@ -122,22 +123,22 @@ extern int g_use_bw_hires; void U_STACK_TRACE(); -double g_fcycles_stop = 0.0; -int halt_sim = 0; -int enter_debug = 0; -int g_rom_version = -1; +double g_fcycles_stop = 0.0; +int halt_sim = 0; +int enter_debug = 0; +int g_rom_version = -1; -int g_user_halt_bad = 0; -int g_halt_on_bad_read = 0; -int g_ignore_bad_acc = 1; -int g_ignore_halts = 1; +int g_user_halt_bad = 0; +int g_halt_on_bad_read = 0; +int g_ignore_bad_acc = 1; +int g_ignore_halts = 1; -int g_code_red = 0; -int g_code_yellow = 0; -int g_serial_type[2]; -int g_iw2_emul = 0; -int g_serial_out_masking = 0; -int g_serial_modem[2] = { 0, 1 }; +int g_code_red = 0; +int g_code_yellow = 0; +int g_serial_type[2]; +int g_iw2_emul = 0; +int g_serial_out_masking = 0; +int g_serial_modem[2] = { 0, 1 }; int g_ethernet = 0; int g_ethernet_interface = 0; int g_parallel = 0; @@ -164,39 +165,39 @@ char* g_imagewriter_prop_font = "lib/letgothl.ttf"; int g_imagewriter_paper = 0; int g_imagewriter_banner = 0; -int g_config_iwm_vbl_count = 0; -const char g_gsplus_version_str[] = "0.14rc"; -int g_pause=0; // OG Added pause +int g_config_iwm_vbl_count = 0; +const char g_gsplus_version_str[] = "0.14rc"; +int g_pause=0; // OG Added pause -#define START_DCYCS (0.0) +#define START_DCYCS (0.0) -double g_last_vbl_dcycs = START_DCYCS; -double g_cur_dcycs = START_DCYCS; +double g_last_vbl_dcycs = START_DCYCS; +double g_cur_dcycs = START_DCYCS; -double g_last_vbl_dadjcycs = 0.0; -double g_dadjcycs = 0.0; +double g_last_vbl_dadjcycs = 0.0; +double g_dadjcycs = 0.0; -int g_wait_pending = 0; -int g_stp_pending = 0; +int g_wait_pending = 0; +int g_stp_pending = 0; extern int g_irq_pending; -int g_num_irq = 0; -int g_num_brk = 0; -int g_num_cop = 0; -int g_num_enter_engine = 0; -int g_io_amt = 0; -int g_engine_action = 0; -int g_engine_halt_event = 0; -int g_engine_scan_int = 0; -int g_engine_doc_int = 0; +int g_num_irq = 0; +int g_num_brk = 0; +int g_num_cop = 0; +int g_num_enter_engine = 0; +int g_io_amt = 0; +int g_engine_action = 0; +int g_engine_halt_event = 0; +int g_engine_scan_int = 0; +int g_engine_doc_int = 0; -int g_testing = 0; +int g_testing = 0; -#define MAX_FATAL_LOGS 20 +#define MAX_FATAL_LOGS 20 -int g_debug_file_fd = -1; -int g_fatal_log = -1; +int g_debug_file_fd = -1; +int g_fatal_log = -1; char *g_fatal_log_strs[MAX_FATAL_LOGS]; word32 stop_run_at; @@ -210,9 +211,9 @@ word32 g_natcycs_lastvbl = 0; int Verbose = 0; int Halt_on = 0; -word32 g_mem_size_base = 256*1024; /* size of motherboard memory */ -word32 g_mem_size_exp = 8*1024*1024; /* size of expansion RAM card */ -word32 g_mem_size_total = 256*1024; /* Total contiguous RAM from 0 */ +word32 g_mem_size_base = 256*1024; /* size of motherboard memory */ +word32 g_mem_size_exp = 8*1024*1024; /* size of expansion RAM card */ +word32 g_mem_size_total = 256*1024; /* Total contiguous RAM from 0 */ extern word32 slow_mem_changed[]; @@ -229,16 +230,16 @@ byte *g_dummy_memory1_ptr_allocated = 0; byte *g_rom_fc_ff_ptr_allocated = 0; byte *g_rom_cards_ptr_allocated = 0; -void *g_memory_alloc_ptr = 0; /* for freeing memory area */ +void *g_memory_alloc_ptr = 0; /* for freeing memory area */ Page_info page_info_rd_wr[2*65536 + PAGE_INFO_PAD_SIZE]; Pc_log g_pc_log_array[PC_LOG_LEN + 2]; Data_log g_data_log_array[PC_LOG_LEN + 2]; -Pc_log *g_log_pc_ptr = &(g_pc_log_array[0]); -Pc_log *g_log_pc_start_ptr = &(g_pc_log_array[0]); -Pc_log *g_log_pc_end_ptr = &(g_pc_log_array[PC_LOG_LEN]); +Pc_log *g_log_pc_ptr = &(g_pc_log_array[0]); +Pc_log *g_log_pc_start_ptr = &(g_pc_log_array[0]); +Pc_log *g_log_pc_end_ptr = &(g_pc_log_array[PC_LOG_LEN]); Data_log *g_log_data_ptr = &(g_data_log_array[0]); Data_log *g_log_data_start_ptr = &(g_data_log_array[0]); @@ -298,585 +299,573 @@ void sim65816_initglobals() { Verbose = 0; Halt_on = 0; - g_mem_size_base = 256*1024; /* size of motherboard memory */ - g_mem_size_exp = 8*1024*1024; /* size of expansion RAM card */ - g_mem_size_total = 256*1024; /* Total contiguous RAM from 0 */ + g_mem_size_base = 256*1024; /* size of motherboard memory */ + g_mem_size_exp = 8*1024*1024; /* size of expansion RAM card */ + g_mem_size_total = 256*1024; /* Total contiguous RAM from 0 */ } void show_pc_log() { - FILE *pcfile; - Pc_log *log_pc_ptr; - Data_log *log_data_ptr; - double dcycs; - double start_dcycs; - word32 instr; - word32 psr; - word32 acc, xreg, yreg; - word32 stack, direct; - word32 dbank; - word32 kpc; - int data_wrap; - int accsize, xsize; - int num; - int i; + FILE *pcfile; + Pc_log *log_pc_ptr; + Data_log *log_data_ptr; + double dcycs; + double start_dcycs; + word32 instr; + word32 psr; + word32 acc, xreg, yreg; + word32 stack, direct; + word32 dbank; + word32 kpc; + int data_wrap; + int accsize, xsize; + int num; + int i; - pcfile = fopen("pc_log_out", "w"); - if(pcfile == 0) { - fprintf(stderr,"fopen failed...errno: %d\n", errno); - exit(2); - } + pcfile = fopen("pc_log_out", "w"); + if(pcfile == 0) { + fprintf(stderr,"fopen failed...errno: %d\n", errno); + exit(2); + } - log_pc_ptr = g_log_pc_ptr; - log_data_ptr = g_log_data_ptr; + log_pc_ptr = g_log_pc_ptr; + log_data_ptr = g_log_data_ptr; #if 0 - fprintf(pcfile, "current pc_log_ptr: %p, start: %p, end: %p\n", - log_pc_ptr, log_pc_start_ptr, log_pc_end_ptr); + fprintf(pcfile, "current pc_log_ptr: %p, start: %p, end: %p\n", + log_pc_ptr, log_pc_start_ptr, log_pc_end_ptr); #endif - start_dcycs = log_pc_ptr->dcycs; - dcycs = start_dcycs; + start_dcycs = log_pc_ptr->dcycs; + dcycs = start_dcycs; - data_wrap = 0; - /* find first data entry */ - while(data_wrap < 2 && (log_data_ptr->dcycs < dcycs)) { - log_data_ptr++; - if(log_data_ptr >= g_log_data_end_ptr) { - log_data_ptr = g_log_data_start_ptr; - data_wrap++; - } - } - fprintf(pcfile, "start_dcycs: %9.2f\n", start_dcycs); + data_wrap = 0; + /* find first data entry */ + while(data_wrap < 2 && (log_data_ptr->dcycs < dcycs)) { + log_data_ptr++; + if(log_data_ptr >= g_log_data_end_ptr) { + log_data_ptr = g_log_data_start_ptr; + data_wrap++; + } + } + fprintf(pcfile, "start_dcycs: %9.2f\n", start_dcycs); - for(i = 0; i < PC_LOG_LEN; i++) { - dcycs = log_pc_ptr->dcycs; - while((data_wrap < 2) && (log_data_ptr->dcycs <= dcycs) && - (log_data_ptr->dcycs >= start_dcycs)) { - fprintf(pcfile, "DATA set %06x = %06x (%d) %9.2f\n", - log_data_ptr->addr, log_data_ptr->val, - log_data_ptr->size, - log_data_ptr->dcycs - start_dcycs); - log_data_ptr++; - if(log_data_ptr >= g_log_data_end_ptr) { - log_data_ptr = g_log_data_start_ptr; - data_wrap++; - } - } - dbank = (log_pc_ptr->dbank_kpc >> 24) & 0xff; - kpc = log_pc_ptr->dbank_kpc & 0xffffff; - instr = log_pc_ptr->instr; - psr = (log_pc_ptr->psr_acc >> 16) & 0xffff;; - acc = log_pc_ptr->psr_acc & 0xffff;; - xreg = (log_pc_ptr->xreg_yreg >> 16) & 0xffff;; - yreg = log_pc_ptr->xreg_yreg & 0xffff;; - stack = (log_pc_ptr->stack_direct >> 16) & 0xffff;; - direct = log_pc_ptr->stack_direct & 0xffff;; + for(i = 0; i < PC_LOG_LEN; i++) { + dcycs = log_pc_ptr->dcycs; + while((data_wrap < 2) && (log_data_ptr->dcycs <= dcycs) && + (log_data_ptr->dcycs >= start_dcycs)) { + fprintf(pcfile, "DATA set %06x = %06x (%d) %9.2f\n", + log_data_ptr->addr, log_data_ptr->val, + log_data_ptr->size, + log_data_ptr->dcycs - start_dcycs); + log_data_ptr++; + if(log_data_ptr >= g_log_data_end_ptr) { + log_data_ptr = g_log_data_start_ptr; + data_wrap++; + } + } + dbank = (log_pc_ptr->dbank_kpc >> 24) & 0xff; + kpc = log_pc_ptr->dbank_kpc & 0xffffff; + instr = log_pc_ptr->instr; + psr = (log_pc_ptr->psr_acc >> 16) & 0xffff;; + acc = log_pc_ptr->psr_acc & 0xffff;; + xreg = (log_pc_ptr->xreg_yreg >> 16) & 0xffff;; + yreg = log_pc_ptr->xreg_yreg & 0xffff;; + stack = (log_pc_ptr->stack_direct >> 16) & 0xffff;; + direct = log_pc_ptr->stack_direct & 0xffff;; - num = log_pc_ptr - g_log_pc_start_ptr; + num = log_pc_ptr - g_log_pc_start_ptr; - accsize = 2; - xsize = 2; - if(psr & 0x20) { - accsize = 1; - } - if(psr & 0x10) { - xsize = 1; - } + accsize = 2; + xsize = 2; + if(psr & 0x20) { + accsize = 1; + } + if(psr & 0x10) { + xsize = 1; + } - fprintf(pcfile, "%04x: A:%04x X:%04x Y:%04x P:%03x " - "S:%04x D:%04x B:%02x %9.2f ", i, - acc, xreg, yreg, psr, stack, direct, dbank, - (dcycs-start_dcycs)); + fprintf(pcfile, "%04x: A:%04x X:%04x Y:%04x P:%03x " + "S:%04x D:%04x B:%02x %9.2f ", i, + acc, xreg, yreg, psr, stack, direct, dbank, + (dcycs-start_dcycs)); - do_dis(pcfile, kpc, accsize, xsize, 1, instr); - log_pc_ptr++; - if(log_pc_ptr >= g_log_pc_end_ptr) { - log_pc_ptr = g_log_pc_start_ptr; - } - } + do_dis(pcfile, kpc, accsize, xsize, 1, instr); + log_pc_ptr++; + if(log_pc_ptr >= g_log_pc_end_ptr) { + log_pc_ptr = g_log_pc_start_ptr; + } + } - fclose(pcfile); + fclose(pcfile); } -#define TOOLBOX_LOG_LEN 64 +#define TOOLBOX_LOG_LEN 64 int g_toolbox_log_pos = 0; word32 g_toolbox_log_array[TOOLBOX_LOG_LEN][8]; word32 toolbox_debug_4byte(word32 addr) { - word32 part1, part2; + word32 part1, part2; - /* If addr looks safe, use it */ - if(addr > 0xbffc) { - return (word32)-1; - } + /* If addr looks safe, use it */ + if(addr > 0xbffc) { + return (word32)-1; + } - part1 = get_memory16_c(addr, 0); - part1 = (part1 >> 8) + ((part1 & 0xff) << 8); - part2 = get_memory16_c(addr+2, 0); - part2 = (part2 >> 8) + ((part2 & 0xff) << 8); + part1 = get_memory16_c(addr, 0); + part1 = (part1 >> 8) + ((part1 & 0xff) << 8); + part2 = get_memory16_c(addr+2, 0); + part2 = (part2 >> 8) + ((part2 & 0xff) << 8); - return (part1 << 16) + part2; + return (part1 << 16) + part2; } void toolbox_debug_c(word32 xreg, word32 stack, double *cyc_ptr) { - int pos; + int pos; - pos = g_toolbox_log_pos; + pos = g_toolbox_log_pos; - stack += 9; - g_toolbox_log_array[pos][0] = (word32)(g_last_vbl_dcycs + *cyc_ptr); - g_toolbox_log_array[pos][1] = stack+1; - g_toolbox_log_array[pos][2] = xreg; - g_toolbox_log_array[pos][3] = toolbox_debug_4byte(stack+1); - g_toolbox_log_array[pos][4] = toolbox_debug_4byte(stack+5); - g_toolbox_log_array[pos][5] = toolbox_debug_4byte(stack+9); - g_toolbox_log_array[pos][6] = toolbox_debug_4byte(stack+13); - g_toolbox_log_array[pos][7] = toolbox_debug_4byte(stack+17); + stack += 9; + g_toolbox_log_array[pos][0] = (word32)(g_last_vbl_dcycs + *cyc_ptr); + g_toolbox_log_array[pos][1] = stack+1; + g_toolbox_log_array[pos][2] = xreg; + g_toolbox_log_array[pos][3] = toolbox_debug_4byte(stack+1); + g_toolbox_log_array[pos][4] = toolbox_debug_4byte(stack+5); + g_toolbox_log_array[pos][5] = toolbox_debug_4byte(stack+9); + g_toolbox_log_array[pos][6] = toolbox_debug_4byte(stack+13); + g_toolbox_log_array[pos][7] = toolbox_debug_4byte(stack+17); - pos++; - if(pos >= TOOLBOX_LOG_LEN) { - pos = 0; - } + pos++; + if(pos >= TOOLBOX_LOG_LEN) { + pos = 0; + } - g_toolbox_log_pos = pos; + g_toolbox_log_pos = pos; } void show_toolbox_log() { - int pos; - int i; + int pos; + int i; - pos = g_toolbox_log_pos; + pos = g_toolbox_log_pos; - for(i = TOOLBOX_LOG_LEN - 1; i >= 0; i--) { - printf("%2d:%2d: %08x %06x %04x: %08x %08x %08x %08x %08x\n", - i, pos, - g_toolbox_log_array[pos][0], - g_toolbox_log_array[pos][1], - g_toolbox_log_array[pos][2], - g_toolbox_log_array[pos][3], - g_toolbox_log_array[pos][4], - g_toolbox_log_array[pos][5], - g_toolbox_log_array[pos][6], - g_toolbox_log_array[pos][7]); - pos++; - if(pos >= TOOLBOX_LOG_LEN) { - pos = 0; - } - } + for(i = TOOLBOX_LOG_LEN - 1; i >= 0; i--) { + printf("%2d:%2d: %08x %06x %04x: %08x %08x %08x %08x %08x\n", + i, pos, + g_toolbox_log_array[pos][0], + g_toolbox_log_array[pos][1], + g_toolbox_log_array[pos][2], + g_toolbox_log_array[pos][3], + g_toolbox_log_array[pos][4], + g_toolbox_log_array[pos][5], + g_toolbox_log_array[pos][6], + g_toolbox_log_array[pos][7]); + pos++; + if(pos >= TOOLBOX_LOG_LEN) { + pos = 0; + } + } } #if 0 /* get_memory_c is not used, get_memory_asm is, but this does what the */ /* assembly language would do */ -word32 -get_memory_c(word32 loc, int diff_cycles) -{ - byte *addr; - word32 result; - int index; +word32 get_memory_c(word32 loc, int diff_cycles) { + byte *addr; + word32 result; + int index; #ifdef CHECK_BREAKPOINTS - check_breakpoints_c(loc); + check_breakpoints_c(loc); #endif - index = loc >> 8; - result = page_info[index].rd; - if(result & BANK_IO_BIT) { - return get_memory_io(loc, diff_cycles); - } + index = loc >> 8; + result = page_info[index].rd; + if(result & BANK_IO_BIT) { + return get_memory_io(loc, diff_cycles); + } - addr = (byte *)((result & 0xffffff00) + (loc & 0xff)); + addr = (byte *)((result & 0xffffff00) + (loc & 0xff)); - return *addr; + return *addr; } #endif word32 get_memory_io(word32 loc, double *cyc_ptr) { - int tmp; + int tmp; - if(loc > 0xffffff) { - halt_printf("get_memory_io:%08x out of range==halt!\n", loc); - return 0; - } - tmp = loc & 0xfef000; - if(tmp == 0xc000 || tmp == 0xe0c000) { - return(io_read(loc & 0xfff, cyc_ptr)); - } + if(loc > 0xffffff) { + halt_printf("get_memory_io:%08x out of range==halt!\n", loc); + return 0; + } + tmp = loc & 0xfef000; + if(tmp == 0xc000 || tmp == 0xe0c000) { + return(io_read(loc & 0xfff, cyc_ptr)); + } - /* Else it's an illegal addr...skip if memory sizing */ - if(loc >= g_mem_size_total) { - if((loc & 0xfffe) == 0) { + /* Else it's an illegal addr...skip if memory sizing */ + if(loc >= g_mem_size_total) { + if((loc & 0xfffe) == 0) { #if 0 - printf("get_io assuming mem sizing, not halting\n"); + printf("get_io assuming mem sizing, not halting\n"); #endif - return 0; - } - } + return 0; + } + } - /* Skip reads to f80000 and f00000, just return 0 */ - if((loc & 0xf70000) == 0xf00000) { - return 0; - } + /* Skip reads to f80000 and f00000, just return 0 */ + if((loc & 0xf70000) == 0xf00000) { + return 0; + } - if((loc & 0xff0000) == 0xef0000) { - /* DOC RAM */ - return (doc_ram[loc & 0xffff]); - } + if((loc & 0xff0000) == 0xef0000) { + /* DOC RAM */ + return (doc_ram[loc & 0xffff]); + } - g_code_yellow++; - if(g_ignore_bad_acc && !g_user_halt_bad) { - /* print no message, just get out. User doesn't want */ - /* to be bothered by buggy programs */ - return 0; - } + g_code_yellow++; + if(g_ignore_bad_acc && !g_user_halt_bad) { + /* print no message, just get out. User doesn't want */ + /* to be bothered by buggy programs */ + return 0; + } - printf("get_memory_io for addr: %06x\n", loc); - printf("stat for addr: %06x = %p\n", loc, - GET_PAGE_INFO_RD((loc >> 8) & 0xffff)); - set_halt(g_halt_on_bad_read | g_user_halt_bad); + printf("get_memory_io for addr: %06x\n", loc); + printf("stat for addr: %06x = %p\n", loc, + GET_PAGE_INFO_RD((loc >> 8) & 0xffff)); + set_halt(g_halt_on_bad_read | g_user_halt_bad); - return 0; + return 0; } #if 0 -word32 -get_memory16_pieces(word32 loc, int diff_cycles) -{ - return(get_memory_c(loc, diff_cycles) + - (get_memory_c(loc+1, diff_cycles) << 8)); +word32 get_memory16_pieces(word32 loc, int diff_cycles) { + return(get_memory_c(loc, diff_cycles) + + (get_memory_c(loc+1, diff_cycles) << 8)); } -word32 -get_memory24(word32 loc, int diff_cycles) -{ - return(get_memory_c(loc, diff_cycles) + - (get_memory_c(loc+1, diff_cycles) << 8) + - (get_memory_c(loc+2, diff_cycles) << 16)); +word32 get_memory24(word32 loc, int diff_cycles) { + return(get_memory_c(loc, diff_cycles) + + (get_memory_c(loc+1, diff_cycles) << 8) + + (get_memory_c(loc+2, diff_cycles) << 16)); } #endif #if 0 -void -set_memory(word32 loc, int val, int diff_cycles) -{ - byte *ptr; - word32 new_addr; - word32 tmp; - word32 or_val; - int or_pos; - int old_slow_val; +void set_memory(word32 loc, int val, int diff_cycles) { + byte *ptr; + word32 new_addr; + word32 tmp; + word32 or_val; + int or_pos; + int old_slow_val; #ifdef CHECK_BREAKPOINTS - check_breakpoints_c(loc); + check_breakpoints_c(loc); #endif - tmp = GET_PAGE_INFO_WR((loc>>8) & 0xffff); - if(tmp & BANK_IO) { - set_memory_io(loc, val, diff_cycles); - return; - } + tmp = GET_PAGE_INFO_WR((loc>>8) & 0xffff); + if(tmp & BANK_IO) { + set_memory_io(loc, val, diff_cycles); + return; + } - if((loc & 0xfef000) == 0xe0c000) { - printf("set_memory_special: non-io for addr %08x, %02x, %d\n", - loc, val, diff_cycles); - halt_printf("tmp: %08x\n", tmp); - } + if((loc & 0xfef000) == 0xe0c000) { + printf("set_memory_special: non-io for addr %08x, %02x, %d\n", + loc, val, diff_cycles); + halt_printf("tmp: %08x\n", tmp); + } - ptr = (byte *)(tmp & (~0xff)); + ptr = (byte *)(tmp & (~0xff)); - new_addr = loc & 0xffff; - old_slow_val = val; + new_addr = loc & 0xffff; + old_slow_val = val; - if(tmp & BANK_SHADOW) { - old_slow_val = g_slow_memory_ptr[new_addr]; - } else if(tmp & BANK_SHADOW2) { - new_addr += 0x10000; - old_slow_val = g_slow_memory_ptr[new_addr]; - } + if(tmp & BANK_SHADOW) { + old_slow_val = g_slow_memory_ptr[new_addr]; + } else if(tmp & BANK_SHADOW2) { + new_addr += 0x10000; + old_slow_val = g_slow_memory_ptr[new_addr]; + } - if(old_slow_val != val) { - g_slow_memory_ptr[new_addr] = val; - or_pos = (new_addr >> SHIFT_PER_CHANGE) & 0x1f; - or_val = DEP1(1, or_pos, 0); - if((new_addr >> CHANGE_SHIFT) >= SLOW_MEM_CH_SIZE) { - printf("new_addr: %08x\n", new_addr); - exit(12); - } - slow_mem_changed[(new_addr & 0xffff) >> CHANGE_SHIFT] |= or_val; - } + if(old_slow_val != val) { + g_slow_memory_ptr[new_addr] = val; + or_pos = (new_addr >> SHIFT_PER_CHANGE) & 0x1f; + or_val = DEP1(1, or_pos, 0); + if((new_addr >> CHANGE_SHIFT) >= SLOW_MEM_CH_SIZE) { + printf("new_addr: %08x\n", new_addr); + exit(12); + } + slow_mem_changed[(new_addr & 0xffff) >> CHANGE_SHIFT] |= or_val; + } - ptr[loc & 0xff] = val; + ptr[loc & 0xff] = val; } #endif void set_memory_io(word32 loc, int val, double *cyc_ptr) { - word32 tmp; - tmp = loc & 0xfef000; - if(tmp == 0xc000 || tmp == 0xe0c000) { - io_write(loc, val, cyc_ptr); - return; - } + word32 tmp; + tmp = loc & 0xfef000; + if(tmp == 0xc000 || tmp == 0xe0c000) { + io_write(loc, val, cyc_ptr); + return; + } - /* Else it's an illegal addr */ - if(loc >= g_mem_size_total) { - if((loc & 0xfffe) == 0) { + /* Else it's an illegal addr */ + if(loc >= g_mem_size_total) { + if((loc & 0xfffe) == 0) { #if 0 - printf("set_io assuming mem sizing, not halting\n"); + printf("set_io assuming mem sizing, not halting\n"); #endif - return; - } - } + return; + } + } - /* ignore writes to ROM */ - if((loc & 0xfc0000) == 0xfc0000) { - return; - } + /* ignore writes to ROM */ + if((loc & 0xfc0000) == 0xfc0000) { + return; + } - if((loc & 0xff0000) == 0xef0000) { - /* DOC RAM */ - doc_ram[loc & 0xffff] = val; - return; - } + if((loc & 0xff0000) == 0xef0000) { + /* DOC RAM */ + doc_ram[loc & 0xffff] = val; + return; + } - if(g_ignore_bad_acc && !g_user_halt_bad) { - /* print no message, just get out. User doesn't want */ - /* to be bothered by buggy programs */ - return; - } + if(g_ignore_bad_acc && !g_user_halt_bad) { + /* print no message, just get out. User doesn't want */ + /* to be bothered by buggy programs */ + return; + } - if((loc & 0xffc000) == 0x00c000) { - printf("set_memory %06x = %02x, warning\n", loc, val); - return; - } + if((loc & 0xffc000) == 0x00c000) { + printf("set_memory %06x = %02x, warning\n", loc, val); + return; + } - halt_printf("set_memory %06x = %02x, stopping\n", loc, val); + halt_printf("set_memory %06x = %02x, stopping\n", loc, val); - return; + return; } #if 0 -void -check_breakpoints_c(word32 loc) -{ - int index; - int count; - int i; +void check_breakpoints_c(word32 loc) { + int index; + int count; + int i; - index = (loc & (MAX_BP_INDEX-1)); - count = breakpoints[index].count; - if(count) { - for(i = 0; i < count; i++) { - if(loc == breakpoints[index].addrs[i]) { - halt_printf("Write hit breakpoint %d!\n", i); - } - } - } + index = (loc & (MAX_BP_INDEX-1)); + count = breakpoints[index].count; + if(count) { + for(i = 0; i < count; i++) { + if(loc == breakpoints[index].addrs[i]) { + halt_printf("Write hit breakpoint %d!\n", i); + } + } + } } #endif -void -show_regs_act(Engine_reg *eptr) -{ - int tmp_acc, tmp_x, tmp_y, tmp_psw; - int kpc; - int direct_page, dbank; - int stack; +void show_regs_act(Engine_reg *eptr) { + int tmp_acc, tmp_x, tmp_y, tmp_psw; + int kpc; + int direct_page, dbank; + int stack; - kpc = eptr->kpc; - tmp_acc = eptr->acc; - direct_page = eptr->direct; - dbank = eptr->dbank; - stack = eptr->stack; + kpc = eptr->kpc; + tmp_acc = eptr->acc; + direct_page = eptr->direct; + dbank = eptr->dbank; + stack = eptr->stack; - tmp_x = eptr->xreg; - tmp_y = eptr->yreg; + tmp_x = eptr->xreg; + tmp_y = eptr->yreg; - tmp_psw = eptr->psr; + tmp_psw = eptr->psr; - printf(" PC=%02x.%04x A=%04x X=%04x Y=%04x P=%03x", - kpc>>16, kpc & 0xffff ,tmp_acc,tmp_x,tmp_y,tmp_psw); - printf(" S=%04x D=%04x B=%02x,cyc:%.3f\n", stack, direct_page, - dbank, g_cur_dcycs); + printf(" PC=%02x.%04x A=%04x X=%04x Y=%04x P=%03x", + kpc>>16, kpc & 0xffff,tmp_acc,tmp_x,tmp_y,tmp_psw); + printf(" S=%04x D=%04x B=%02x,cyc:%.3f\n", stack, direct_page, + dbank, g_cur_dcycs); } void show_regs() { - show_regs_act(&engine); + show_regs_act(&engine); } //OG for regular exit, use quitEmulator() void quitEmulator() { - printf("set_halt(HALT_WANTTOQUIT)\n"); - set_halt(HALT_WANTTOQUIT); + printf("set_halt(HALT_WANTTOQUIT)\n"); + set_halt(HALT_WANTTOQUIT); } //OG change exit to fatal_exit() #ifndef ACTIVEGS - // use standard exit function - #define fatalExit exit +// use standard exit function + #define fatalExit exit #else - extern void fatalExit(int); +extern void fatalExit(int); #endif void my_exit(int ret) { - end_screen(); - imagewriter_close(); - printer_close(); - glogf("Exiting (ret=%d)",ret); - fatalExit(ret); + end_screen(); + imagewriter_close(); + printer_close(); + glogf("Exiting (ret=%d)",ret); + fatalExit(ret); } void do_reset() { - // OG Cleared remaining IRQS on RESET - extern int g_irq_pending; - extern int g_scan_int_events ; - extern int g_c023_val; + // OG Cleared remaining IRQS on RESET + extern int g_irq_pending; + extern int g_scan_int_events; + extern int g_c023_val; - g_c068_statereg = 0x08 + 0x04 + 0x01; /* rdrom, lcbank2, intcx */ - g_c035_shadow_reg = 0; + g_c068_statereg = 0x08 + 0x04 + 0x01; /* rdrom, lcbank2, intcx */ + g_c035_shadow_reg = 0; - g_c08x_wrdefram = 1; - g_c02d_int_crom = 0; - g_c023_val = 0; - g_c041_val = 0; + g_c08x_wrdefram = 1; + g_c02d_int_crom = 0; + g_c023_val = 0; + g_c041_val = 0; - engine.psr = (engine.psr | 0x134) & ~(0x08); - engine.stack = 0x100 + (engine.stack & 0xff); - engine.dbank = 0; - engine.direct = 0; - engine.xreg &= 0xff; - engine.yreg &= 0xff; - g_wait_pending = 0; - g_stp_pending = 0; + engine.psr = (engine.psr | 0x134) & ~(0x08); + engine.stack = 0x100 + (engine.stack & 0xff); + engine.dbank = 0; + engine.direct = 0; + engine.xreg &= 0xff; + engine.yreg &= 0xff; + g_wait_pending = 0; + g_stp_pending = 0; - video_reset(); - adb_reset(); - iwm_reset(); - scc_reset(); - sound_reset(g_cur_dcycs); - setup_pageinfo(); - change_display_mode(g_cur_dcycs); + video_reset(); + adb_reset(); + iwm_reset(); + scc_reset(); + sound_reset(g_cur_dcycs); + setup_pageinfo(); + change_display_mode(g_cur_dcycs); - g_irq_pending = 0; + g_irq_pending = 0; - engine.kpc = get_memory16_c(0x00fffc, 0); + engine.kpc = get_memory16_c(0x00fffc, 0); - g_stepping = 0; + g_stepping = 0; g_dbg_step = 0; - if (g_irq_pending) - halt_printf("*** irq remainings...\n"); + if (g_irq_pending) + halt_printf("*** irq remainings...\n"); } -#define CHECK(start, var, value, var1, var2) \ - var2 = PTR2WORD(&(var)); \ - var1 = PTR2WORD((start)); \ - if((var2 - var1) != value) { \ - printf("CHECK: " #var " is 0x%x, but " #value " is 0x%x\n", \ - (var2 - var1), value); \ - exit(5); \ - } +#define CHECK(start, var, value, var1, var2) \ + var2 = PTR2WORD(&(var)); \ + var1 = PTR2WORD((start)); \ + if((var2 - var1) != value) { \ + printf("CHECK: " #var " is 0x%x, but " #value " is 0x%x\n", \ + (var2 - var1), value); \ + exit(5); \ + } void check_engine_asm_defines() { - Fplus fplus; - Fplus *fplusptr; - Pc_log pclog; - Pc_log *pcptr; - Engine_reg ereg; - Engine_reg *eptr; - word32 val1; - word32 val2; + Fplus fplus; + Fplus *fplusptr; + Pc_log pclog; + Pc_log *pcptr; + Engine_reg ereg; + Engine_reg *eptr; + word32 val1; + word32 val2; - eptr = &ereg; - CHECK(eptr, eptr->fcycles, ENGINE_FCYCLES, val1, val2); - CHECK(eptr, eptr->fplus_ptr, ENGINE_FPLUS_PTR, val1, val2); - CHECK(eptr, eptr->acc, ENGINE_REG_ACC, val1, val2); - CHECK(eptr, eptr->xreg, ENGINE_REG_XREG, val1, val2); - CHECK(eptr, eptr->yreg, ENGINE_REG_YREG, val1, val2); - CHECK(eptr, eptr->stack, ENGINE_REG_STACK, val1, val2); - CHECK(eptr, eptr->dbank, ENGINE_REG_DBANK, val1, val2); - CHECK(eptr, eptr->direct, ENGINE_REG_DIRECT, val1, val2); - CHECK(eptr, eptr->psr, ENGINE_REG_PSR, val1, val2); - CHECK(eptr, eptr->kpc, ENGINE_REG_KPC, val1, val2); + eptr = &ereg; + CHECK(eptr, eptr->fcycles, ENGINE_FCYCLES, val1, val2); + CHECK(eptr, eptr->fplus_ptr, ENGINE_FPLUS_PTR, val1, val2); + CHECK(eptr, eptr->acc, ENGINE_REG_ACC, val1, val2); + CHECK(eptr, eptr->xreg, ENGINE_REG_XREG, val1, val2); + CHECK(eptr, eptr->yreg, ENGINE_REG_YREG, val1, val2); + CHECK(eptr, eptr->stack, ENGINE_REG_STACK, val1, val2); + CHECK(eptr, eptr->dbank, ENGINE_REG_DBANK, val1, val2); + CHECK(eptr, eptr->direct, ENGINE_REG_DIRECT, val1, val2); + CHECK(eptr, eptr->psr, ENGINE_REG_PSR, val1, val2); + CHECK(eptr, eptr->kpc, ENGINE_REG_KPC, val1, val2); - pcptr = &pclog; - CHECK(pcptr, pcptr->dbank_kpc, LOG_PC_DBANK_KPC, val1, val2); - CHECK(pcptr, pcptr->instr, LOG_PC_INSTR, val1, val2); - CHECK(pcptr, pcptr->psr_acc, LOG_PC_PSR_ACC, val1, val2); - CHECK(pcptr, pcptr->xreg_yreg, LOG_PC_XREG_YREG, val1, val2); - CHECK(pcptr, pcptr->stack_direct, LOG_PC_STACK_DIRECT, val1, val2); - if(LOG_PC_SIZE != sizeof(pclog)) { - printf("LOG_PC_SIZE: %d != sizeof=%d\n", LOG_PC_SIZE, - (int)sizeof(pclog)); - exit(2); - } + pcptr = &pclog; + CHECK(pcptr, pcptr->dbank_kpc, LOG_PC_DBANK_KPC, val1, val2); + CHECK(pcptr, pcptr->instr, LOG_PC_INSTR, val1, val2); + CHECK(pcptr, pcptr->psr_acc, LOG_PC_PSR_ACC, val1, val2); + CHECK(pcptr, pcptr->xreg_yreg, LOG_PC_XREG_YREG, val1, val2); + CHECK(pcptr, pcptr->stack_direct, LOG_PC_STACK_DIRECT, val1, val2); + if(LOG_PC_SIZE != sizeof(pclog)) { + printf("LOG_PC_SIZE: %d != sizeof=%d\n", LOG_PC_SIZE, + (int)sizeof(pclog)); + exit(2); + } - fplusptr = &fplus; - CHECK(fplusptr, fplusptr->plus_1, FPLUS_PLUS_1, val1, val2); - CHECK(fplusptr, fplusptr->plus_2, FPLUS_PLUS_2, val1, val2); - CHECK(fplusptr, fplusptr->plus_3, FPLUS_PLUS_3, val1, val2); - CHECK(fplusptr, fplusptr->plus_x_minus_1, FPLUS_PLUS_X_M1, val1, val2); + fplusptr = &fplus; + CHECK(fplusptr, fplusptr->plus_1, FPLUS_PLUS_1, val1, val2); + CHECK(fplusptr, fplusptr->plus_2, FPLUS_PLUS_2, val1, val2); + CHECK(fplusptr, fplusptr->plus_3, FPLUS_PLUS_3, val1, val2); + CHECK(fplusptr, fplusptr->plus_x_minus_1, FPLUS_PLUS_X_M1, val1, val2); } byte * memalloc_align(int size, int skip_amt, void **alloc_ptr) { - byte *bptr; - word32 addr; - word32 offset; + byte *bptr; + word32 addr; + word32 offset; - skip_amt = MAX(256, skip_amt); - bptr = (byte*)calloc(size + skip_amt + 256, 1); // OG Added cast - if(alloc_ptr) { - /* Save allocation address */ - *alloc_ptr = bptr; - } + skip_amt = MAX(256, skip_amt); + bptr = (byte*)calloc(size + skip_amt + 256, 1); // OG Added cast + if(alloc_ptr) { + /* Save allocation address */ + *alloc_ptr = bptr; + } - addr = PTR2WORD(bptr) & 0xff; + addr = PTR2WORD(bptr) & 0xff; - /* must align bptr to be 256-byte aligned */ - /* this code should work even if ptrs are > 32 bits */ + /* must align bptr to be 256-byte aligned */ + /* this code should work even if ptrs are > 32 bits */ - offset = ((addr + skip_amt - 1) & (~0xff)) - addr; + offset = ((addr + skip_amt - 1) & (~0xff)) - addr; - return (bptr + offset); + return (bptr + offset); } void memory_ptr_init() { - word32 mem_size; + word32 mem_size; - /* This routine may be called several times--each time the ROM file */ - /* changes this will be called */ - mem_size = MIN(0xdf0000, g_mem_size_base + g_mem_size_exp); - g_mem_size_total = mem_size; + /* This routine may be called several times--each time the ROM file */ + /* changes this will be called */ + mem_size = MIN(0xdf0000, g_mem_size_base + g_mem_size_exp); + g_mem_size_total = mem_size; - // OG using memory_ptr_shut() instead - memory_ptr_shut(); - /* - if(g_memory_alloc_ptr) { - free(g_memory_alloc_ptr); - g_memory_alloc_ptr = 0; - } - */ - g_memory_ptr = memalloc_align(mem_size, 256, &g_memory_alloc_ptr); + // OG using memory_ptr_shut() instead + memory_ptr_shut(); + /* + if(g_memory_alloc_ptr) { + free(g_memory_alloc_ptr); + g_memory_alloc_ptr = 0; + } + */ + g_memory_ptr = memalloc_align(mem_size, 256, &g_memory_alloc_ptr); - glogf("RAM size is %d bytes (%.2fMB)", mem_size, (double)mem_size/(1024.0*1024.0)); + glogf("RAM size is %d bytes (%.2fMB)", mem_size, (double)mem_size/(1024.0*1024.0)); } // OG Added memory_ptr_shut void memory_ptr_shut() { - if(g_memory_alloc_ptr) - { - free(g_memory_alloc_ptr); - g_memory_alloc_ptr = 0; - } - g_memory_ptr = 0; + if(g_memory_alloc_ptr) + { + free(g_memory_alloc_ptr); + g_memory_alloc_ptr = 0; + } + g_memory_ptr = 0; } @@ -892,19 +881,19 @@ void banner() { } int gsplusmain(int argc, char **argv) { - int diff; - int skip_amt; - int tmp1; - int i; - char *final_arg = 0; + int diff; + int skip_amt; + int tmp1; + int i; + char *final_arg = 0; // just for fun banner(); - // OG Restoring globals - sim65816_initglobals(); - moremem_init(); + // OG Restoring globals + sim65816_initglobals(); + moremem_init(); tmp1 = parse_cli_options(argc, argv); @@ -915,264 +904,258 @@ int gsplusmain(int argc, char **argv) { - check_engine_asm_defines(); - fixed_memory_ptrs_init(); + check_engine_asm_defines(); + fixed_memory_ptrs_init(); - if(sizeof(word32) != 4) { - printf("sizeof(word32) = %d, must be 4!\n", - (int)sizeof(word32)); - exit(1); - } + if(sizeof(word32) != 4) { + printf("sizeof(word32) = %d, must be 4!\n", + (int)sizeof(word32)); + exit(1); + } - if(!g_engine_c_mode) { - diff = &defs_instr_end_8 - &defs_instr_start_8; - if(diff != 1) { - printf("defs_instr_end_8 - start is %d\n",diff); - exit(1); - } + if(!g_engine_c_mode) { + diff = &defs_instr_end_8 - &defs_instr_start_8; + if(diff != 1) { + printf("defs_instr_end_8 - start is %d\n",diff); + exit(1); + } - diff = &defs_instr_end_16 - &defs_instr_start_16; - if(diff != 1) { - printf("defs_instr_end_16 - start is %d\n", diff); - exit(1); - } + diff = &defs_instr_end_16 - &defs_instr_start_16; + if(diff != 1) { + printf("defs_instr_end_16 - start is %d\n", diff); + exit(1); + } - diff = &op_routs_end - &op_routs_start; - if(diff != 1) { - printf("op_routs_end - start is %d\n", diff); - exit(1); - } - } + diff = &op_routs_end - &op_routs_start; + if(diff != 1) { + printf("op_routs_end - start is %d\n", diff); + exit(1); + } + } - iwm_init(); + iwm_init(); debug_init(); // enable socket debugger if configured - config_init(); - // If the final argument was not a switch, then treat it like a disk image filename to insert - if (final_arg) { - // ...and flag it to boot - cfg_inspect_maybe_insert_file(final_arg, 1); - } - printer_init(g_printer_dpi,85,110,g_printer_output,g_printer_multipage != 0); - //If ethernet is enabled in config.gsport, let's initialize it + config_init(); + // If the final argument was not a switch, then treat it like a disk image filename to insert + if (final_arg) { + // ...and flag it to boot + cfg_inspect_maybe_insert_file(final_arg, 1); + } + printer_init(g_printer_dpi,85,110,g_printer_output,g_printer_multipage != 0); + //If ethernet is enabled in config.gsport, let's initialize it #ifdef HAVE_TFE - if (g_ethernet == 1) - { - int i = 0; - char *ppname = NULL; - char *ppdes = NULL; - if (tfe_enumadapter_open()) - { - //Loop through the available adapters until we reach the interface number specified in config.gsport - while(tfe_enumadapter(&ppname,&ppdes)) - { - if (i == g_ethernet_interface) break; - i++; - } - tfe_enumadapter_close(); - printf("Using host ethernet interface: %s\nUthernet support is ON.\n",ppdes); - } - else - { - printf("No ethernet host adapters found. Do you have PCap installed/enabled?\nUthernet support is OFF.\n"); - } - set_tfe_interface(ppname); //Connect the emulated ethernet device with the selected host adapter - lib_free(ppname); - lib_free(ppdes); - tfe_init(); - } + if (g_ethernet == 1) + { + int i = 0; + char *ppname = NULL; + char *ppdes = NULL; + if (tfe_enumadapter_open()) + { + //Loop through the available adapters until we reach the interface number specified in config.gsport + while(tfe_enumadapter(&ppname,&ppdes)) + { + if (i == g_ethernet_interface) break; + i++; + } + tfe_enumadapter_close(); + printf("Using host ethernet interface: %s\nUthernet support is ON.\n",ppdes); + } + else + { + printf("No ethernet host adapters found. Do you have PCap installed/enabled?\nUthernet support is OFF.\n"); + } + set_tfe_interface(ppname); //Connect the emulated ethernet device with the selected host adapter + lib_free(ppname); + lib_free(ppdes); + tfe_init(); + } #endif - load_roms_init_memory(); + load_roms_init_memory(); - init_reg(); - clear_halt(); + init_reg(); + clear_halt(); - initialize_events(); + initialize_events(); - video_init(); + video_init(); - sound_init(); - scc_init(); - adb_init(); - joystick_init(); - if(g_rom_version >= 3) { - g_c036_val_speed |= 0x40; /* set power-on bit */ - } + sound_init(); + scc_init(); + adb_init(); + joystick_init(); + if(g_rom_version >= 3) { + g_c036_val_speed |= 0x40; /* set power-on bit */ + } - do_reset(); - g_stepping = 0; + do_reset(); + g_stepping = 0; g_dbg_step = 0; - // OG Notify emulator has been initialized and ready to accept external events - g_initialized = 1; - g_accept_events = 1; + // OG Notify emulator has been initialized and ready to accept external events + g_initialized = 1; + g_accept_events = 1; // Call one of two main run_prog() routines if (g_dbg_enable_port) { do_go_debug(); } else { - do_go(); + do_go(); /* If we get here, we hit a breakpoint, call debug intfc */ - do_debug_intfc(); + do_debug_intfc(); } - // OG Notify emulator is being closed, and cannot accept events anymore - g_accept_events = 0; + // OG Notify emulator is being closed, and cannot accept events anymore + g_accept_events = 0; - sound_shutdown(); + sound_shutdown(); - // OG Cleaning up - adb_shut(); - iwm_shut(); - fixed_memory_ptrs_shut(); - load_roms_shut_memory(); - clear_fatal_logs(); + // OG Cleaning up + adb_shut(); + iwm_shut(); + fixed_memory_ptrs_shut(); + load_roms_shut_memory(); + clear_fatal_logs(); - // OG Not needed anymore : the emulator will quit gently - //my_exit(0); - end_screen(); + // OG Not needed anymore : the emulator will quit gently + //my_exit(0); + end_screen(); - return 0; + return 0; } -void -load_roms_init_memory() -{ - config_load_roms(); - memory_ptr_init(); - clk_setup_bram_version(); /* Must be after config_load_roms */ - if(g_rom_version >= 3) { - g_c036_val_speed |= 0x40; /* set power-on bit */ - } else { - g_c036_val_speed &= (~0x40); /* clear the bit */ - } - do_reset(); +void load_roms_init_memory() { + config_load_roms(); + memory_ptr_init(); + clk_setup_bram_version(); /* Must be after config_load_roms */ + if(g_rom_version >= 3) { + g_c036_val_speed |= 0x40; /* set power-on bit */ + } else { + g_c036_val_speed &= (~0x40); /* clear the bit */ + } + do_reset(); - /* if user booted ROM 01, switches to ROM 03, then switches back */ - /* to ROM 01, then the reset routines call to Tool $0102 looks */ - /* at uninitialized $e1/15fe and if it is negative it will JMP */ - /* through $e1/1688 which ROM 03 left pointing to fc/0199 */ - /* So set e1/15fe = 0 */ - set_memory16_c(0xe115fe, 0, 0); + /* if user booted ROM 01, switches to ROM 03, then switches back */ + /* to ROM 01, then the reset routines call to Tool $0102 looks */ + /* at uninitialized $e1/15fe and if it is negative it will JMP */ + /* through $e1/1688 which ROM 03 left pointing to fc/0199 */ + /* So set e1/15fe = 0 */ + set_memory16_c(0xe115fe, 0, 0); } // OG Added load_roms_shut_memory -void load_roms_shut_memory() -{ - memory_ptr_shut(); +void load_roms_shut_memory() { + memory_ptr_shut(); } #ifndef ACTIVEGS -void -gsport_expand_path(char *out_ptr, const char *in_ptr, int maxlen) -{ - char name_buf[256]; - char *tmp_ptr; - int name_len; - int in_char; - int state; +void gsport_expand_path(char *out_ptr, const char *in_ptr, int maxlen) { + char name_buf[256]; + char *tmp_ptr; + int name_len; + int in_char; + int state; - out_ptr[0] = 0; + out_ptr[0] = 0; - name_len = 0; - state = 0; + name_len = 0; + state = 0; - /* See if in_ptr has ${} notation, replace with getenv or argv0 */ - while(maxlen > 0) { - in_char = *in_ptr++; - *out_ptr++ = in_char; - maxlen--; - if(state == 0) { - /* No $ seen yet, look for it */ - if(in_char == '$') { - state = 1; - } - } else if(state == 1) { - /* See if next char is '{' (dummy }) */ - if(in_char == '{') { /* add dummy } */ - state = 2; - name_len = 0; - out_ptr -= 2; - } else { - state = 0; - } - } else if(state == 2) { - /* fill name_buf ... dummy '{' */ - out_ptr--; - if(in_char == '}') { - name_buf[name_len] = 0; + /* See if in_ptr has ${} notation, replace with getenv or argv0 */ + while(maxlen > 0) { + in_char = *in_ptr++; + *out_ptr++ = in_char; + maxlen--; + if(state == 0) { + /* No $ seen yet, look for it */ + if(in_char == '$') { + state = 1; + } + } else if(state == 1) { + /* See if next char is '{' (dummy }) */ + if(in_char == '{') { /* add dummy } */ + state = 2; + name_len = 0; + out_ptr -= 2; + } else { + state = 0; + } + } else if(state == 2) { + /* fill name_buf ... dummy '{' */ + out_ptr--; + if(in_char == '}') { + name_buf[name_len] = 0; - /* got token, now look it up */ - tmp_ptr = ""; - if(!strncmp("0", name_buf, 128)) { - /* Replace ${0} with g_argv0_path */ - tmp_ptr = &(g_argv0_path[0]); + /* got token, now look it up */ + tmp_ptr = ""; + if(!strncmp("0", name_buf, 128)) { + /* Replace ${0} with g_argv0_path */ + tmp_ptr = &(g_argv0_path[0]); #if defined (_WIN32) || defined(__CYGWIN__) && !defined(WIN_SDL) - } else if(!strncmp("PWD", name_buf, 128)) { - /* Replace ${PWD} with cwd in Windows */ - get_cwd(out_ptr,128); - tmp_ptr = out_ptr; + } else if(!strncmp("PWD", name_buf, 128)) { + /* Replace ${PWD} with cwd in Windows */ + get_cwd(out_ptr,128); + tmp_ptr = out_ptr; #endif - } else { - tmp_ptr = getenv(name_buf); - if(tmp_ptr == 0) { - tmp_ptr = ""; - } - } - strncpy(out_ptr, tmp_ptr, maxlen); - out_ptr += strlen(tmp_ptr); - maxlen -= strlen(tmp_ptr); - state = 0; - } else { - name_buf[name_len++] = in_char; - } - } - if(in_char == 0) { - /* make sure its null terminated */ - *out_ptr++ = 0; - break; - } - } + } else { + tmp_ptr = getenv(name_buf); + if(tmp_ptr == 0) { + tmp_ptr = ""; + } + } + strncpy(out_ptr, tmp_ptr, maxlen); + out_ptr += strlen(tmp_ptr); + maxlen -= strlen(tmp_ptr); + state = 0; + } else { + name_buf[name_len++] = in_char; + } + } + if(in_char == 0) { + /* make sure its null terminated */ + *out_ptr++ = 0; + break; + } + } } -void setup_gsplus_file(char *outname, int maxlen, int ok_if_missing, int can_create_file, const char **name_ptr) -{ - char local_path[256]; - struct stat stat_buf; - const char **path_ptr; - const char **cur_name_ptr, **save_path_ptr; - int ret; +void setup_gsplus_file(char *outname, int maxlen, int ok_if_missing, int can_create_file, const char **name_ptr) { + char local_path[256]; + struct stat stat_buf; + const char **path_ptr; + const char **cur_name_ptr, **save_path_ptr; + int ret; - outname[0] = 0; + outname[0] = 0; - path_ptr = &g_gsplus_default_paths[0]; + path_ptr = &g_gsplus_default_paths[0]; - save_path_ptr = path_ptr; - while(*path_ptr) { - gsport_expand_path(&(local_path[0]), *path_ptr, 250); - cur_name_ptr = name_ptr; - while(*cur_name_ptr) { - strcpy(outname, &(local_path[0])); - strncat(outname, *cur_name_ptr, 255-strlen(outname)); - if(!ok_if_missing) { // ?? - glogf("Trying config file '%s'", outname); - } - ret = stat(outname, &stat_buf); - if(ret == 0) { - return; - } - cur_name_ptr++; - } - path_ptr++; - } + save_path_ptr = path_ptr; + while(*path_ptr) { + gsport_expand_path(&(local_path[0]), *path_ptr, 250); + cur_name_ptr = name_ptr; + while(*cur_name_ptr) { + strcpy(outname, &(local_path[0])); + strncat(outname, *cur_name_ptr, 255-strlen(outname)); + if(!ok_if_missing) { // ?? + glogf("Trying config file '%s'", outname); + } + ret = stat(outname, &stat_buf); + if(ret == 0) { + return; + } + cur_name_ptr++; + } + path_ptr++; + } - outname[0] = 0; - if(ok_if_missing > 0) { - return; - } + outname[0] = 0; + if(ok_if_missing > 0) { + return; + } glogf("Could not find required file \"%s\" !!!", *name_ptr); // this check is crap @@ -1185,7 +1168,7 @@ void setup_gsplus_file(char *outname, int maxlen, int ok_if_missing, int can_cre x_show_alert(0, "Missing Apple IIgs ROM file.\nHit F4 to configure ROM location or refer to documentation."); } - if(can_create_file) { + if(can_create_file) { // If we didn't find a file, pick a place to put it. // Default is the current working directory. // This is often wrong in OS X. /Applications/GSplus.app is @@ -1202,23 +1185,23 @@ void setup_gsplus_file(char *outname, int maxlen, int ok_if_missing, int can_cre glogf("Trying to create config (%s)", "${HOME}/.config.gsp"); gsport_expand_path(&(local_path[0]), "${HOME}/.config.gsp", 250); - strcpy(outname, &(local_path[0])); - // Ask user if it's OK to create the file (or just create it) - x_dialog_create_gsport_conf(*name_ptr); - can_create_file = 0; + strcpy(outname, &(local_path[0])); + // Ask user if it's OK to create the file (or just create it) + x_dialog_create_gsport_conf(*name_ptr); + can_create_file = 0; - // But clear out the fatal_printfs first - clear_fatal_logs(); - setup_gsplus_file(outname, maxlen, ok_if_missing, can_create_file, name_ptr); - // It's one-level of recursion--it cannot loop since we - // clear can_create_file. - // If it returns, then there was succes and we should get out - return; - } else if(ok_if_missing) { - return; - } + // But clear out the fatal_printfs first + clear_fatal_logs(); + setup_gsplus_file(outname, maxlen, ok_if_missing, can_create_file, name_ptr); + // It's one-level of recursion--it cannot loop since we + // clear can_create_file. + // If it returns, then there was succes and we should get out + return; + } else if(ok_if_missing) { + return; + } - my_exit(2); + my_exit(2); } #endif @@ -1227,669 +1210,631 @@ Event g_event_list[MAX_EVENTS]; Event g_event_free; Event g_event_start; -void -initialize_events() -{ - int i; +void initialize_events() { + int i; - for(i = 1; i < MAX_EVENTS; i++) { - g_event_list[i-1].next = &g_event_list[i]; - } - g_event_free.next = &g_event_list[0]; - g_event_list[MAX_EVENTS-1].next = 0; + for(i = 1; i < MAX_EVENTS; i++) { + g_event_list[i-1].next = &g_event_list[i]; + } + g_event_free.next = &g_event_list[0]; + g_event_list[MAX_EVENTS-1].next = 0; - g_event_start.next = 0; - g_event_start.dcycs = 0.0; + g_event_start.next = 0; + g_event_start.dcycs = 0.0; - add_event_entry(DCYCS_IN_16MS, EV_60HZ); + add_event_entry(DCYCS_IN_16MS, EV_60HZ); } -void -check_for_one_event_type(int type) -{ - Event *ptr; - int count; - int depth; +void check_for_one_event_type(int type) { + Event *ptr; + int count; + int depth; - count = 0; - depth = 0; - ptr = g_event_start.next; - while(ptr != 0) { - depth++; - if(ptr->type == type) { - count++; - if(count != 1) { - halt_printf("in check_for_1, type %d found at " - "depth: %d, count: %d, at %f\n", - type, depth, count, ptr->dcycs); - } - } - ptr = ptr->next; - } + count = 0; + depth = 0; + ptr = g_event_start.next; + while(ptr != 0) { + depth++; + if(ptr->type == type) { + count++; + if(count != 1) { + halt_printf("in check_for_1, type %d found at " + "depth: %d, count: %d, at %f\n", + type, depth, count, ptr->dcycs); + } + } + ptr = ptr->next; + } } -void -add_event_entry(double dcycs, int type) -{ - Event *this_event; - Event *ptr, *prev_ptr; - int tmp_type; - int done; +void add_event_entry(double dcycs, int type) { + Event *this_event; + Event *ptr, *prev_ptr; + int tmp_type; + int done; - this_event = g_event_free.next; - if(this_event == 0) { - halt_printf("Out of queue entries!\n"); - show_all_events(); - return; - } - g_event_free.next = this_event->next; + this_event = g_event_free.next; + if(this_event == 0) { + halt_printf("Out of queue entries!\n"); + show_all_events(); + return; + } + g_event_free.next = this_event->next; - this_event->type = type; + this_event->type = type; - tmp_type = type & 0xff; - if((dcycs < 0.0) || (dcycs > (g_cur_dcycs + 50*1000*1000.0)) || - ((dcycs < g_cur_dcycs) && (tmp_type != EV_SCAN_INT))) { - halt_printf("add_event: dcycs: %f, type:%05x, cur_dcycs: %f!\n", - dcycs, type, g_cur_dcycs); - dcycs = g_cur_dcycs + 1000.0; - } + tmp_type = type & 0xff; + if((dcycs < 0.0) || (dcycs > (g_cur_dcycs + 50*1000*1000.0)) || + ((dcycs < g_cur_dcycs) && (tmp_type != EV_SCAN_INT))) { + halt_printf("add_event: dcycs: %f, type:%05x, cur_dcycs: %f!\n", + dcycs, type, g_cur_dcycs); + dcycs = g_cur_dcycs + 1000.0; + } - ptr = g_event_start.next; - if(ptr && (dcycs < ptr->dcycs)) { - /* create event before next expected event */ - /* do this by setting HALT_EVENT */ - set_halt(HALT_EVENT); - } + ptr = g_event_start.next; + if(ptr && (dcycs < ptr->dcycs)) { + /* create event before next expected event */ + /* do this by setting HALT_EVENT */ + set_halt(HALT_EVENT); + } - prev_ptr = &g_event_start; - ptr = g_event_start.next; + prev_ptr = &g_event_start; + ptr = g_event_start.next; - done = 0; - while(!done) { - if(ptr == 0) { - this_event->next = ptr; - this_event->dcycs = dcycs; - prev_ptr->next = this_event; - return; - } else { - if(ptr->dcycs < dcycs) { - /* step across this guy */ - prev_ptr = ptr; - ptr = ptr->next; - } else { - /* go in front of this guy */ - this_event->dcycs = dcycs; - this_event->next = ptr; - prev_ptr->next = this_event; - return; - } - } - } + done = 0; + while(!done) { + if(ptr == 0) { + this_event->next = ptr; + this_event->dcycs = dcycs; + prev_ptr->next = this_event; + return; + } else { + if(ptr->dcycs < dcycs) { + /* step across this guy */ + prev_ptr = ptr; + ptr = ptr->next; + } else { + /* go in front of this guy */ + this_event->dcycs = dcycs; + this_event->next = ptr; + prev_ptr->next = this_event; + return; + } + } + } } extern int g_doc_saved_ctl; -double -remove_event_entry(int type) -{ - Event *ptr, *prev_ptr; - Event *next_ptr; +double remove_event_entry(int type) { + Event *ptr, *prev_ptr; + Event *next_ptr; - ptr = g_event_start.next; - prev_ptr = &g_event_start; + ptr = g_event_start.next; + prev_ptr = &g_event_start; - while(ptr != 0) { - if((ptr->type & 0xffff) == type) { - /* got it, remove it */ - next_ptr = ptr->next; - prev_ptr->next = next_ptr; + while(ptr != 0) { + if((ptr->type & 0xffff) == type) { + /* got it, remove it */ + next_ptr = ptr->next; + prev_ptr->next = next_ptr; - /* Add ptr to free list */ - ptr->next = g_event_free.next; - g_event_free.next = ptr; + /* Add ptr to free list */ + ptr->next = g_event_free.next; + g_event_free.next = ptr; - return ptr->dcycs; - } - prev_ptr = ptr; - ptr = ptr->next; - } + return ptr->dcycs; + } + prev_ptr = ptr; + ptr = ptr->next; + } - halt_printf("remove event_entry: %08x, but not found!\n", type); - if((type & 0xff) == EV_DOC_INT) { - printf("DOC, g_doc_saved_ctl = %02x\n", g_doc_saved_ctl); - } + halt_printf("remove event_entry: %08x, but not found!\n", type); + if((type & 0xff) == EV_DOC_INT) { + printf("DOC, g_doc_saved_ctl = %02x\n", g_doc_saved_ctl); + } #ifdef HPUX - U_STACK_TRACE(); + U_STACK_TRACE(); #endif - show_all_events(); + show_all_events(); - return 0.0; + return 0.0; } -void -add_event_stop(double dcycs) -{ - add_event_entry(dcycs, EV_STOP); +void add_event_stop(double dcycs) { + add_event_entry(dcycs, EV_STOP); } -void -add_event_doc(double dcycs, int osc) -{ - if(dcycs < g_cur_dcycs) { - dcycs = g_cur_dcycs; +void add_event_doc(double dcycs, int osc) { + if(dcycs < g_cur_dcycs) { + dcycs = g_cur_dcycs; #if 0 - halt_printf("add_event_doc: dcycs: %f, cur_dcycs: %f\n", - dcycs, g_cur_dcycs); + halt_printf("add_event_doc: dcycs: %f, cur_dcycs: %f\n", + dcycs, g_cur_dcycs); #endif - } + } - add_event_entry(dcycs, EV_DOC_INT + (osc << 8)); + add_event_entry(dcycs, EV_DOC_INT + (osc << 8)); } -void -add_event_scc(double dcycs, int type) -{ - if(dcycs < g_cur_dcycs) { - dcycs = g_cur_dcycs; - } +void add_event_scc(double dcycs, int type) { + if(dcycs < g_cur_dcycs) { + dcycs = g_cur_dcycs; + } - add_event_entry(dcycs, EV_SCC + (type << 8)); + add_event_entry(dcycs, EV_SCC + (type << 8)); } -void -add_event_vbl() -{ - double dcycs; +void add_event_vbl() { + double dcycs; - dcycs = g_last_vbl_dcycs + (DCYCS_IN_16MS * (192.0/262.0)); - add_event_entry(dcycs, EV_VBL_INT); + dcycs = g_last_vbl_dcycs + (DCYCS_IN_16MS * (192.0/262.0)); + add_event_entry(dcycs, EV_VBL_INT); } -void -add_event_vid_upd(int line) -{ - double dcycs; +void add_event_vid_upd(int line) { + double dcycs; - dcycs = g_last_vbl_dcycs + ((DCYCS_IN_16MS * line) / 262.0); - add_event_entry(dcycs, EV_VID_UPD + (line << 8)); + dcycs = g_last_vbl_dcycs + ((DCYCS_IN_16MS * line) / 262.0); + add_event_entry(dcycs, EV_VID_UPD + (line << 8)); } -double -remove_event_doc(int osc) -{ - return remove_event_entry(EV_DOC_INT + (osc << 8)); +double remove_event_doc(int osc) { + return remove_event_entry(EV_DOC_INT + (osc << 8)); } -double -remove_event_scc(int type) -{ - return remove_event_entry(EV_SCC + (type << 8)); +double remove_event_scc(int type) { + return remove_event_entry(EV_SCC + (type << 8)); } -void -show_all_events() -{ - Event *ptr; - int count; - double dcycs; +void show_all_events() { + Event *ptr; + int count; + double dcycs; - count = 0; - ptr = g_event_start.next; - while(ptr != 0) { - dcycs = ptr->dcycs; - printf("Event: %02x: type: %05x, dcycs: %f (%f)\n", - count, ptr->type, dcycs, dcycs - g_cur_dcycs); - ptr = ptr->next; - count++; - } + count = 0; + ptr = g_event_start.next; + while(ptr != 0) { + dcycs = ptr->dcycs; + printf("Event: %02x: type: %05x, dcycs: %f (%f)\n", + count, ptr->type, dcycs, dcycs - g_cur_dcycs); + ptr = ptr->next; + count++; + } } -word32 g_vbl_count = 0; -int g_vbl_index_count = 0; -double dtime_array[60]; -double g_dadjcycs_array[60]; -double g_dtime_diff3_array[60]; -double g_dtime_this_vbl_array[60]; -double g_dtime_exp_array[60]; -double g_dtime_pmhz_array[60]; -double g_dtime_eff_pmhz_array[60]; -int g_limit_speed = 2; -double sim_time[60]; -double g_sim_sum = 0.0; +word32 g_vbl_count = 0; +int g_vbl_index_count = 0; +double dtime_array[60]; +double g_dadjcycs_array[60]; +double g_dtime_diff3_array[60]; +double g_dtime_this_vbl_array[60]; +double g_dtime_exp_array[60]; +double g_dtime_pmhz_array[60]; +double g_dtime_eff_pmhz_array[60]; +int g_limit_speed = 2; +double sim_time[60]; +double g_sim_sum = 0.0; -double g_cur_sim_dtime = 0.0; -double g_projected_pmhz = 1.0; -double g_zip_pmhz = 8.0; -double g_sim_mhz = 100.0; -int g_line_ref_amt = 1; -int g_video_line_update_interval = 0; +double g_cur_sim_dtime = 0.0; +double g_projected_pmhz = 1.0; +double g_zip_pmhz = 8.0; +double g_sim_mhz = 100.0; +int g_line_ref_amt = 1; +int g_video_line_update_interval = 0; -Fplus g_recip_projected_pmhz_slow; -Fplus g_recip_projected_pmhz_fast; -Fplus g_recip_projected_pmhz_zip; -Fplus g_recip_projected_pmhz_unl; +Fplus g_recip_projected_pmhz_slow; +Fplus g_recip_projected_pmhz_fast; +Fplus g_recip_projected_pmhz_zip; +Fplus g_recip_projected_pmhz_unl; -void -show_pmhz() -{ - printf("Pmhz: %f, c036:%02x, limit: %d\n", - g_projected_pmhz, g_c036_val_speed, g_limit_speed); +void show_pmhz() { + printf("Pmhz: %f, c036:%02x, limit: %d\n", + g_projected_pmhz, g_c036_val_speed, g_limit_speed); } -void -setup_zip_speeds() -{ - double frecip; - double fmhz; - int mult; +void setup_zip_speeds() { + double frecip; + double fmhz; + int mult; - mult = 16 - ((g_zipgs_reg_c05a >> 4) & 0xf); - // 16 = full speed, 1 = 1/16th speed - fmhz = (8.0 * mult) / 16.0; + mult = 16 - ((g_zipgs_reg_c05a >> 4) & 0xf); + // 16 = full speed, 1 = 1/16th speed + fmhz = (8.0 * mult) / 16.0; #if 0 - if(mult == 16) { - /* increase full speed by 19% to make zipgs freq measuring */ - /* programs work correctly */ - fmhz = fmhz * 1.19; - } + if(mult == 16) { + /* increase full speed by 19% to make zipgs freq measuring */ + /* programs work correctly */ + fmhz = fmhz * 1.19; + } #endif - frecip = 1.0 / fmhz; - g_zip_pmhz = fmhz; - g_recip_projected_pmhz_zip.plus_1 = frecip; - g_recip_projected_pmhz_zip.plus_2 = 2.0 * frecip; - g_recip_projected_pmhz_zip.plus_3 = 3.0 * frecip; - if(frecip >= 0.5) { - g_recip_projected_pmhz_zip.plus_x_minus_1 = 1.01; - } else { - g_recip_projected_pmhz_zip.plus_x_minus_1 = 1.01 - frecip; - } + frecip = 1.0 / fmhz; + g_zip_pmhz = fmhz; + g_recip_projected_pmhz_zip.plus_1 = frecip; + g_recip_projected_pmhz_zip.plus_2 = 2.0 * frecip; + g_recip_projected_pmhz_zip.plus_3 = 3.0 * frecip; + if(frecip >= 0.5) { + g_recip_projected_pmhz_zip.plus_x_minus_1 = 1.01; + } else { + g_recip_projected_pmhz_zip.plus_x_minus_1 = 1.01 - frecip; + } } -void -run_prog() -{ - Fplus *fplus_ptr; - Event *this_event; - Event *db1; - double dcycs; - double now_dtime; - double prev_dtime; - double prerun_fcycles; - double fspeed_mult; - double fcycles_stop; - word32 ret; - word32 zip_speed_0tof, zip_speed_0tof_new; - int zip_en, zip_follow_cps; - int type; - int motor_on; - int iwm_1; - int iwm_25; - int limit_speed; - int apple35_sel; - int fast, zip_speed, faster_than_28, unl_speed; - int this_type; +void run_prog() { + Fplus *fplus_ptr; + Event *this_event; + Event *db1; + double dcycs; + double now_dtime; + double prev_dtime; + double prerun_fcycles; + double fspeed_mult; + double fcycles_stop; + word32 ret; + word32 zip_speed_0tof, zip_speed_0tof_new; + int zip_en, zip_follow_cps; + int type; + int motor_on; + int iwm_1; + int iwm_25; + int limit_speed; + int apple35_sel; + int fast, zip_speed, faster_than_28, unl_speed; + int this_type; - fflush(stdout); + fflush(stdout); - g_cur_sim_dtime = 0.0; + g_cur_sim_dtime = 0.0; - g_recip_projected_pmhz_slow.plus_1 = 1.0; - g_recip_projected_pmhz_slow.plus_2 = 2.0; - g_recip_projected_pmhz_slow.plus_3 = 3.0; - g_recip_projected_pmhz_slow.plus_x_minus_1 = 0.9; + g_recip_projected_pmhz_slow.plus_1 = 1.0; + g_recip_projected_pmhz_slow.plus_2 = 2.0; + g_recip_projected_pmhz_slow.plus_3 = 3.0; + g_recip_projected_pmhz_slow.plus_x_minus_1 = 0.9; - g_recip_projected_pmhz_fast.plus_1 = (1.0 / 2.5); - g_recip_projected_pmhz_fast.plus_2 = (2.0 / 2.5); - g_recip_projected_pmhz_fast.plus_3 = (3.0 / 2.5); - g_recip_projected_pmhz_fast.plus_x_minus_1 = (1.98 - (1.0/2.5)); + g_recip_projected_pmhz_fast.plus_1 = (1.0 / 2.5); + g_recip_projected_pmhz_fast.plus_2 = (2.0 / 2.5); + g_recip_projected_pmhz_fast.plus_3 = (3.0 / 2.5); + g_recip_projected_pmhz_fast.plus_x_minus_1 = (1.98 - (1.0/2.5)); - zip_speed_0tof = g_zipgs_reg_c05a & 0xf0; - setup_zip_speeds(); + zip_speed_0tof = g_zipgs_reg_c05a & 0xf0; + setup_zip_speeds(); - if(engine.fplus_ptr == 0) { - g_recip_projected_pmhz_unl = g_recip_projected_pmhz_slow; - } + if(engine.fplus_ptr == 0) { + g_recip_projected_pmhz_unl = g_recip_projected_pmhz_slow; + } - while(1) { - fflush(stdout); + while(1) { + fflush(stdout); // OG Disabling control panel #ifndef ACTIVEGS - if(g_config_control_panel) { - config_control_panel(); - } + if(g_config_control_panel) { + config_control_panel(); + } #endif - if(g_irq_pending && !(engine.psr & 0x4)) { - irq_printf("taking an irq!\n"); - take_irq(0); - /* Interrupt! */ - } + if(g_irq_pending && !(engine.psr & 0x4)) { + irq_printf("taking an irq!\n"); + take_irq(0); + /* Interrupt! */ + } - motor_on = g_iwm_motor_on; - limit_speed = g_limit_speed; - apple35_sel = g_c031_disk35 & 0x40; - zip_en = ((g_zipgs_reg_c05b & 0x10) == 0); - zip_follow_cps = ((g_zipgs_reg_c059 & 0x8) != 0); - zip_speed_0tof_new = g_zipgs_reg_c05a & 0xf0; - fast = (g_c036_val_speed & 0x80) || (zip_en && !zip_follow_cps); - // OG Make fast parameter public - g_speed_fast = fast; - if(zip_speed_0tof_new != zip_speed_0tof) { - zip_speed_0tof = zip_speed_0tof_new; - setup_zip_speeds(); - } + motor_on = g_iwm_motor_on; + limit_speed = g_limit_speed; + apple35_sel = g_c031_disk35 & 0x40; + zip_en = ((g_zipgs_reg_c05b & 0x10) == 0); + zip_follow_cps = ((g_zipgs_reg_c059 & 0x8) != 0); + zip_speed_0tof_new = g_zipgs_reg_c05a & 0xf0; + fast = (g_c036_val_speed & 0x80) || (zip_en && !zip_follow_cps); + // OG Make fast parameter public + g_speed_fast = fast; + if(zip_speed_0tof_new != zip_speed_0tof) { + zip_speed_0tof = zip_speed_0tof_new; + setup_zip_speeds(); + } - iwm_1 = motor_on && !apple35_sel && - (g_c036_val_speed & 0x4) && - (g_slow_525_emul_wr || !g_fast_disk_emul); - iwm_25 = (motor_on && apple35_sel) && !g_fast_disk_emul; - faster_than_28 = fast && (!iwm_1 && !iwm_25) && zip_en && - ((limit_speed == 0) || (limit_speed == 3)); - zip_speed = faster_than_28 && - ((zip_speed_0tof != 0) || (limit_speed == 3) || - (g_zipgs_unlock >= 4) ); + iwm_1 = motor_on && !apple35_sel && + (g_c036_val_speed & 0x4) && + (g_slow_525_emul_wr || !g_fast_disk_emul); + iwm_25 = (motor_on && apple35_sel) && !g_fast_disk_emul; + faster_than_28 = fast && (!iwm_1 && !iwm_25) && zip_en && + ((limit_speed == 0) || (limit_speed == 3)); + zip_speed = faster_than_28 && + ((zip_speed_0tof != 0) || (limit_speed == 3) || + (g_zipgs_unlock >= 4) ); - // OG unlimited speed should not be affected by zip. - // unl_speed = faster_than_28 && !zip_speed; - unl_speed = (limit_speed == 0) && faster_than_28; + // OG unlimited speed should not be affected by zip. + // unl_speed = faster_than_28 && !zip_speed; + unl_speed = (limit_speed == 0) && faster_than_28; - if(unl_speed) { - /* use unlimited speed */ - fspeed_mult = g_projected_pmhz; - fplus_ptr = &g_recip_projected_pmhz_unl; - } else if(zip_speed) { - fspeed_mult = g_zip_pmhz; - fplus_ptr = &g_recip_projected_pmhz_zip; - } else if(fast && !iwm_1 && !(limit_speed == 1)) { - fspeed_mult = 2.5; - fplus_ptr = &g_recip_projected_pmhz_fast; - } else { - /* else run slow */ - fspeed_mult = 1.0; - fplus_ptr = &g_recip_projected_pmhz_slow; - } + if(unl_speed) { + /* use unlimited speed */ + fspeed_mult = g_projected_pmhz; + fplus_ptr = &g_recip_projected_pmhz_unl; + } else if(zip_speed) { + fspeed_mult = g_zip_pmhz; + fplus_ptr = &g_recip_projected_pmhz_zip; + } else if(fast && !iwm_1 && !(limit_speed == 1)) { + fspeed_mult = 2.5; + fplus_ptr = &g_recip_projected_pmhz_fast; + } else { + /* else run slow */ + fspeed_mult = 1.0; + fplus_ptr = &g_recip_projected_pmhz_slow; + } - engine.fplus_ptr = fplus_ptr; + engine.fplus_ptr = fplus_ptr; - this_type = g_event_start.next->type; + this_type = g_event_start.next->type; - prerun_fcycles = g_cur_dcycs - g_last_vbl_dcycs; - engine.fcycles = prerun_fcycles; - fcycles_stop = (g_event_start.next->dcycs - g_last_vbl_dcycs) + - 0.001; - if(g_stepping || g_dbg_step < 0) { - fcycles_stop = prerun_fcycles; - } - g_fcycles_stop = fcycles_stop; + prerun_fcycles = g_cur_dcycs - g_last_vbl_dcycs; + engine.fcycles = prerun_fcycles; + fcycles_stop = (g_event_start.next->dcycs - g_last_vbl_dcycs) + + 0.001; + if(g_stepping || g_dbg_step < 0) { + fcycles_stop = prerun_fcycles; + } + g_fcycles_stop = fcycles_stop; #if 0 - printf("Enter engine, fcycs: %f, stop: %f\n", - prerun_fcycles, fcycles_stop); - printf("g_cur_dcycs: %f, last_vbl_dcyc: %f\n", g_cur_dcycs, - g_last_vbl_dcycs); + printf("Enter engine, fcycs: %f, stop: %f\n", + prerun_fcycles, fcycles_stop); + printf("g_cur_dcycs: %f, last_vbl_dcyc: %f\n", g_cur_dcycs, + g_last_vbl_dcycs); #endif - g_num_enter_engine++; - prev_dtime = get_dtime(); + g_num_enter_engine++; + prev_dtime = get_dtime(); - ret = enter_engine(&engine); + ret = enter_engine(&engine); - now_dtime = get_dtime(); + now_dtime = get_dtime(); - g_cur_sim_dtime += (now_dtime - prev_dtime); + g_cur_sim_dtime += (now_dtime - prev_dtime); - dcycs = g_last_vbl_dcycs + (double)(engine.fcycles); + dcycs = g_last_vbl_dcycs + (double)(engine.fcycles); - g_dadjcycs += (engine.fcycles - prerun_fcycles) * - fspeed_mult; + g_dadjcycs += (engine.fcycles - prerun_fcycles) * + fspeed_mult; #if 0 - printf("...back, engine.fcycles: %f, dcycs: %f\n", - (double)engine.fcycles, dcycs); + printf("...back, engine.fcycles: %f, dcycs: %f\n", + (double)engine.fcycles, dcycs); #endif - g_cur_dcycs = dcycs; + g_cur_dcycs = dcycs; - if(ret != 0) { - g_engine_action++; - handle_action(ret); - } + if(ret != 0) { + g_engine_action++; + handle_action(ret); + } - if(halt_sim == HALT_EVENT) { - g_engine_halt_event++; - /* if we needed to stop to check for interrupts, */ - /* clear halt */ - halt_sim = 0; - } + if(halt_sim == HALT_EVENT) { + g_engine_halt_event++; + /* if we needed to stop to check for interrupts, */ + /* clear halt */ + halt_sim = 0; + } #if 0 - if(!g_testing && run_cycles < -2000000) { - halt_printf("run_cycles: %d, cycles: %d\n", run_cycles, - cycles); - printf("this_type: %05x\n", this_type); - printf("duff_cycles: %d\n", duff_cycles); - printf("start.next->rel_time: %d, type: %05x\n", - g_event_start.next->rel_time, - g_event_start.next->type); - } + if(!g_testing && run_cycles < -2000000) { + halt_printf("run_cycles: %d, cycles: %d\n", run_cycles, + cycles); + printf("this_type: %05x\n", this_type); + printf("duff_cycles: %d\n", duff_cycles); + printf("start.next->rel_time: %d, type: %05x\n", + g_event_start.next->rel_time, + g_event_start.next->type); + } #endif - this_event = g_event_start.next; - while(dcycs >= this_event->dcycs) { + this_event = g_event_start.next; + while(dcycs >= this_event->dcycs) { if(halt_sim != 0 && halt_sim != HALT_EVENT) { - break; - } - if(g_stepping || g_dbg_step != 0) { - printf("HIT STEPPING BREAK!\n"); - break; + break; } - /* Pop this guy off of the queue */ - g_event_start.next = this_event->next; + if(g_stepping || g_dbg_step != 0) { + printf("HIT STEPPING BREAK!\n"); + break; + } + /* Pop this guy off of the queue */ + g_event_start.next = this_event->next; - type = this_event->type; - this_event->next = g_event_free.next; - g_event_free.next = this_event; - switch(type & 0xff) { - case EV_60HZ: - update_60hz(dcycs, now_dtime); - debug_server_poll(); - if (debug_events_waiting() > 0) { - debug_handle_event(); - } - break; - case EV_STOP: - printf("type: EV_STOP\n"); - printf("next: %p, dcycs: %f\n", - g_event_start.next, dcycs); - db1 = g_event_start.next; - halt_printf("next.dcycs: %f\n", db1->dcycs); - break; - case EV_SCAN_INT: - g_engine_scan_int++; - irq_printf("type: scan int\n"); - do_scan_int(dcycs, type >> 8); - break; - case EV_DOC_INT: - g_engine_doc_int++; - doc_handle_event(type >> 8, dcycs); - break; - case EV_VBL_INT: - do_vbl_int(); - break; - case EV_SCC: - do_scc_event(type >> 8, dcycs); - break; - case EV_VID_UPD: - video_update_event_line(type >> 8); - break; - default: - printf("Unknown event: %d!\n", type); - exit(3); - } + type = this_event->type; + this_event->next = g_event_free.next; + g_event_free.next = this_event; + switch(type & 0xff) { + case EV_60HZ: + update_60hz(dcycs, now_dtime); + debug_server_poll(); + if (debug_events_waiting() > 0) { + debug_handle_event(); + } + break; + case EV_STOP: + printf("type: EV_STOP\n"); + printf("next: %p, dcycs: %f\n", + g_event_start.next, dcycs); + db1 = g_event_start.next; + halt_printf("next.dcycs: %f\n", db1->dcycs); + break; + case EV_SCAN_INT: + g_engine_scan_int++; + irq_printf("type: scan int\n"); + do_scan_int(dcycs, type >> 8); + break; + case EV_DOC_INT: + g_engine_doc_int++; + doc_handle_event(type >> 8, dcycs); + break; + case EV_VBL_INT: + do_vbl_int(); + break; + case EV_SCC: + do_scc_event(type >> 8, dcycs); + break; + case EV_VID_UPD: + video_update_event_line(type >> 8); + break; + default: + printf("Unknown event: %d!\n", type); + exit(3); + } - this_event = g_event_start.next; + this_event = g_event_start.next; - } + } - if(g_event_start.next == 0) { - halt_printf("ERROR...run_prog, event_start.n=0!\n"); - } + if(g_event_start.next == 0) { + halt_printf("ERROR...run_prog, event_start.n=0!\n"); + } #if 0 - if(!g_testing && g_event_start.next->rel_time > 2000000) { - printf("Z:start.next->rel_time: %d, duff_cycles: %d\n", - g_event_start.next->rel_time, duff_cycles); - halt_printf("Zrun_cycles:%d, cycles:%d\n", run_cycles, - cycles); + if(!g_testing && g_event_start.next->rel_time > 2000000) { + printf("Z:start.next->rel_time: %d, duff_cycles: %d\n", + g_event_start.next->rel_time, duff_cycles); + halt_printf("Zrun_cycles:%d, cycles:%d\n", run_cycles, + cycles); - show_all_events(); - } + show_all_events(); + } #endif - if(halt_sim != 0 && halt_sim != HALT_EVENT) { - break; - } - if(g_stepping || g_dbg_step != 0) { - break; - } - } + if(halt_sim != 0 && halt_sim != HALT_EVENT) { + break; + } + if(g_stepping || g_dbg_step != 0) { + break; + } + } - if(!g_testing) { - printf("leaving run_prog, halt_sim:%d\n", halt_sim); - } + if(!g_testing) { + printf("leaving run_prog, halt_sim:%d\n", halt_sim); + } - x_auto_repeat_on(0); + x_auto_repeat_on(0); } -void -add_irq(word32 irq_mask) -{ - if(g_irq_pending & irq_mask) { - /* Already requested, just get out */ - return; - } - g_irq_pending |= irq_mask; - set_halt(HALT_EVENT); +void add_irq(word32 irq_mask) { + if(g_irq_pending & irq_mask) { + /* Already requested, just get out */ + return; + } + g_irq_pending |= irq_mask; + set_halt(HALT_EVENT); } -void -remove_irq(word32 irq_mask) -{ - g_irq_pending = g_irq_pending & (~irq_mask); +void remove_irq(word32 irq_mask) { + g_irq_pending = g_irq_pending & (~irq_mask); } -void -take_irq(int is_it_brk) -{ - word32 new_kpc; - word32 va; +void take_irq(int is_it_brk) { + word32 new_kpc; + word32 va; - irq_printf("Taking irq, at: %02x/%04x, psw: %02x, dcycs: %f\n", - engine.kpc>>16, engine.kpc & 0xffff, engine.psr, - g_cur_dcycs); + irq_printf("Taking irq, at: %02x/%04x, psw: %02x, dcycs: %f\n", + engine.kpc>>16, engine.kpc & 0xffff, engine.psr, + g_cur_dcycs); - g_num_irq++; - if(g_wait_pending) { - /* step over WAI instruction */ - engine.kpc++; - g_wait_pending = 0; - } + g_num_irq++; + if(g_wait_pending) { + /* step over WAI instruction */ + engine.kpc++; + g_wait_pending = 0; + } - if(engine.psr & 0x100) { - /* Emulation */ - set_memory_c(engine.stack, (engine.kpc >> 8) & 0xff, 0); - engine.stack = ((engine.stack -1) & 0xff) + 0x100; + if(engine.psr & 0x100) { + /* Emulation */ + set_memory_c(engine.stack, (engine.kpc >> 8) & 0xff, 0); + engine.stack = ((engine.stack -1) & 0xff) + 0x100; - set_memory_c(engine.stack, engine.kpc & 0xff, 0); - engine.stack = ((engine.stack -1) & 0xff) + 0x100; + set_memory_c(engine.stack, engine.kpc & 0xff, 0); + engine.stack = ((engine.stack -1) & 0xff) + 0x100; - set_memory_c(engine.stack, - (engine.psr & 0xef)|(is_it_brk<<4),0); - /* Clear B bit in psr on stack */ - engine.stack = ((engine.stack -1) & 0xff) + 0x100; + set_memory_c(engine.stack, + (engine.psr & 0xef)|(is_it_brk<<4),0); + /* Clear B bit in psr on stack */ + engine.stack = ((engine.stack -1) & 0xff) + 0x100; - va = 0xfffffe; - if(g_c035_shadow_reg & 0x40) { - /* I/O shadowing off...use ram locs */ - va = 0x00fffe; - } + va = 0xfffffe; + if(g_c035_shadow_reg & 0x40) { + /* I/O shadowing off...use ram locs */ + va = 0x00fffe; + } - } else { - /* native */ - set_memory_c(engine.stack, (engine.kpc >> 16) & 0xff, 0); - engine.stack = ((engine.stack -1) & 0xffff); + } else { + /* native */ + set_memory_c(engine.stack, (engine.kpc >> 16) & 0xff, 0); + engine.stack = ((engine.stack -1) & 0xffff); - set_memory_c(engine.stack, (engine.kpc >> 8) & 0xff, 0); - engine.stack = ((engine.stack -1) & 0xffff); + set_memory_c(engine.stack, (engine.kpc >> 8) & 0xff, 0); + engine.stack = ((engine.stack -1) & 0xffff); - set_memory_c(engine.stack, engine.kpc & 0xff, 0); - engine.stack = ((engine.stack -1) & 0xffff); + set_memory_c(engine.stack, engine.kpc & 0xff, 0); + engine.stack = ((engine.stack -1) & 0xffff); - set_memory_c(engine.stack, engine.psr & 0xff, 0); - engine.stack = ((engine.stack -1) & 0xffff); + set_memory_c(engine.stack, engine.psr & 0xff, 0); + engine.stack = ((engine.stack -1) & 0xffff); - if(is_it_brk) { - /* break */ - va = 0xffffe6; - if(g_c035_shadow_reg & 0x40) { - va = 0xffe6; - } - } else { - /* irq */ - va = 0xffffee; - if(g_c035_shadow_reg & 0x40) { - va = 0xffee; - } - } + if(is_it_brk) { + /* break */ + va = 0xffffe6; + if(g_c035_shadow_reg & 0x40) { + va = 0xffe6; + } + } else { + /* irq */ + va = 0xffffee; + if(g_c035_shadow_reg & 0x40) { + va = 0xffee; + } + } - } + } - new_kpc = get_memory_c(va, 0); - new_kpc = new_kpc + (get_memory_c(va+1, 0) << 8); + new_kpc = get_memory_c(va, 0); + new_kpc = new_kpc + (get_memory_c(va+1, 0) << 8); - engine.psr = ((engine.psr & 0x1f3) | 0x4); + engine.psr = ((engine.psr & 0x1f3) | 0x4); - engine.kpc = new_kpc; - HALT_ON(HALT_ON_IRQ, "Halting on IRQ\n"); + engine.kpc = new_kpc; + HALT_ON(HALT_ON_IRQ, "Halting on IRQ\n"); } -double g_dtime_last_vbl = 0.0; -double g_dtime_expected = (1.0/60.0); +double g_dtime_last_vbl = 0.0; +double g_dtime_expected = (1.0/60.0); int g_scan_int_events = 0; -void -show_dtime_array() -{ - double dfirst_time; - double first_total_cycs; - int i; - int pos; +void show_dtime_array() { + double dfirst_time; + double first_total_cycs; + int i; + int pos; - dfirst_time = 0.0; - first_total_cycs = 0.0; + dfirst_time = 0.0; + first_total_cycs = 0.0; - for(i = 0; i < 60; i++) { - pos = (g_vbl_index_count + i) % 60; - printf("%2d:%2d dt:%.5f adjc:%9.1f this_vbl:%.6f " - "exp:%.5f p:%2.2f ep:%2.2f\n", - i, pos, - dtime_array[pos] - dfirst_time, - g_dadjcycs_array[pos] - first_total_cycs, - g_dtime_this_vbl_array[pos], - g_dtime_exp_array[pos] - dfirst_time, - g_dtime_pmhz_array[pos], - g_dtime_eff_pmhz_array[pos]); - dfirst_time = dtime_array[pos]; - first_total_cycs = g_dadjcycs_array[pos]; - } + for(i = 0; i < 60; i++) { + pos = (g_vbl_index_count + i) % 60; + printf("%2d:%2d dt:%.5f adjc:%9.1f this_vbl:%.6f " + "exp:%.5f p:%2.2f ep:%2.2f\n", + i, pos, + dtime_array[pos] - dfirst_time, + g_dadjcycs_array[pos] - first_total_cycs, + g_dtime_this_vbl_array[pos], + g_dtime_exp_array[pos] - dfirst_time, + g_dtime_pmhz_array[pos], + g_dtime_eff_pmhz_array[pos]); + dfirst_time = dtime_array[pos]; + first_total_cycs = g_dadjcycs_array[pos]; + } } extern word32 g_cycs_in_40col; @@ -1919,723 +1864,681 @@ extern int g_a2vid_palette; extern int g_status_refresh_needed; -void -update_60hz(double dcycs, double dtime_now) -{ - register word32 end_time; - char status_buf[1024]; - char sim_mhz_buf[128]; - char total_mhz_buf[128]; - char *sim_mhz_ptr, *total_mhz_ptr; - char *code_str1, *code_str2, *sp_str; - double eff_pmhz; - double planned_dcycs; - double predicted_pmhz; - double recip_predicted_pmhz; - double dtime_this_vbl_sim; - double dtime_diff_1sec; - double dratio; - double dtime_till_expected; - double dtime_diff; - double dtime_this_vbl; - double dadjcycs_this_vbl; - double dadj_cycles_1sec; - double dtmp1, dtmp2, dtmp3, dtmp4, dtmp5; - double dnatcycs_1sec; - int tmp; - int doit_3_persec; - int cur_vbl_index; - int prev_vbl_index; +void update_60hz(double dcycs, double dtime_now) { + register word32 end_time; + char status_buf[1024]; + char sim_mhz_buf[128]; + char total_mhz_buf[128]; + char *sim_mhz_ptr, *total_mhz_ptr; + char *code_str1, *code_str2, *sp_str; + double eff_pmhz; + double planned_dcycs; + double predicted_pmhz; + double recip_predicted_pmhz; + double dtime_this_vbl_sim; + double dtime_diff_1sec; + double dratio; + double dtime_till_expected; + double dtime_diff; + double dtime_this_vbl; + double dadjcycs_this_vbl; + double dadj_cycles_1sec; + double dtmp1, dtmp2, dtmp3, dtmp4, dtmp5; + double dnatcycs_1sec; + int tmp; + int doit_3_persec; + int cur_vbl_index; + int prev_vbl_index; - g_vbl_count++; + g_vbl_count++; - /* NOTE: this event is defined to occur before line 0 */ - /* It's actually happening at the start of the border for line (-1) */ - /* All other timings should be adjusted for this */ + /* NOTE: this event is defined to occur before line 0 */ + /* It's actually happening at the start of the border for line (-1) */ + /* All other timings should be adjusted for this */ - irq_printf("vbl_60hz: vbl: %d, dcycs: %f, last_vbl_dcycs: %f\n", - g_vbl_count, dcycs, g_last_vbl_dcycs); + irq_printf("vbl_60hz: vbl: %d, dcycs: %f, last_vbl_dcycs: %f\n", + g_vbl_count, dcycs, g_last_vbl_dcycs); - planned_dcycs = DCYCS_IN_16MS; + planned_dcycs = DCYCS_IN_16MS; - g_last_vbl_dcycs = g_last_vbl_dcycs + planned_dcycs; + g_last_vbl_dcycs = g_last_vbl_dcycs + planned_dcycs; - add_event_entry(g_last_vbl_dcycs + planned_dcycs, EV_60HZ); - check_for_one_event_type(EV_60HZ); + add_event_entry(g_last_vbl_dcycs + planned_dcycs, EV_60HZ); + check_for_one_event_type(EV_60HZ); - cur_vbl_index = g_vbl_index_count; + cur_vbl_index = g_vbl_index_count; - /* figure out dtime spent running SIM, not all the overhead */ - dtime_this_vbl_sim = g_cur_sim_dtime; - g_cur_sim_dtime = 0.0; - g_sim_sum = g_sim_sum - sim_time[cur_vbl_index] + dtime_this_vbl_sim; - sim_time[cur_vbl_index] = dtime_this_vbl_sim; + /* figure out dtime spent running SIM, not all the overhead */ + dtime_this_vbl_sim = g_cur_sim_dtime; + g_cur_sim_dtime = 0.0; + g_sim_sum = g_sim_sum - sim_time[cur_vbl_index] + dtime_this_vbl_sim; + sim_time[cur_vbl_index] = dtime_this_vbl_sim; - dadj_cycles_1sec = g_dadjcycs - g_dadjcycs_array[cur_vbl_index]; + dadj_cycles_1sec = g_dadjcycs - g_dadjcycs_array[cur_vbl_index]; - /* dtime_diff_1sec is dtime total spent over the last 60 ticks */ - dtime_diff_1sec = dtime_now - dtime_array[cur_vbl_index]; + /* dtime_diff_1sec is dtime total spent over the last 60 ticks */ + dtime_diff_1sec = dtime_now - dtime_array[cur_vbl_index]; - dtime_array[cur_vbl_index] = dtime_now; - g_dadjcycs_array[cur_vbl_index] = g_dadjcycs; + dtime_array[cur_vbl_index] = dtime_now; + g_dadjcycs_array[cur_vbl_index] = g_dadjcycs; - prev_vbl_index = cur_vbl_index; - cur_vbl_index = prev_vbl_index + 1; - if(cur_vbl_index >= 60) { - cur_vbl_index = 0; - } - g_vbl_index_count = cur_vbl_index; + prev_vbl_index = cur_vbl_index; + cur_vbl_index = prev_vbl_index + 1; + if(cur_vbl_index >= 60) { + cur_vbl_index = 0; + } + g_vbl_index_count = cur_vbl_index; - GET_ITIMER(end_time); - g_dnatcycs_1sec += (double)(end_time - g_natcycs_lastvbl); - g_natcycs_lastvbl = end_time; + GET_ITIMER(end_time); + g_dnatcycs_1sec += (double)(end_time - g_natcycs_lastvbl); + g_natcycs_lastvbl = end_time; - if(prev_vbl_index == 0) { - if(g_sim_sum < (1.0/250.0)) { - sim_mhz_ptr = "???"; - g_sim_mhz = 250.0; - } else { - g_sim_mhz = (dadj_cycles_1sec / g_sim_sum) / - (1000.0*1000.0); - sprintf(sim_mhz_buf, "%6.2f", g_sim_mhz); - sim_mhz_ptr = sim_mhz_buf; - } - if(dtime_diff_1sec < (1.0/250.0)) { - total_mhz_ptr = "???"; - } else { - sprintf(total_mhz_buf, "%6.2f", - (dadj_cycles_1sec / dtime_diff_1sec) / - (1000000.0)); - total_mhz_ptr = total_mhz_buf; - } + if(prev_vbl_index == 0) { + if(g_sim_sum < (1.0/250.0)) { + sim_mhz_ptr = "???"; + g_sim_mhz = 250.0; + } else { + g_sim_mhz = (dadj_cycles_1sec / g_sim_sum) / + (1000.0*1000.0); + sprintf(sim_mhz_buf, "%6.2f", g_sim_mhz); + sim_mhz_ptr = sim_mhz_buf; + } + if(dtime_diff_1sec < (1.0/250.0)) { + total_mhz_ptr = "???"; + } else { + sprintf(total_mhz_buf, "%6.2f", + (dadj_cycles_1sec / dtime_diff_1sec) / + (1000000.0)); + total_mhz_ptr = total_mhz_buf; + } - switch(g_limit_speed) { - case 1: sp_str = "1Mhz"; break; - case 2: sp_str = "2.8Mhz"; break; - case 3: sp_str = "8.0Mhz"; break; - default: sp_str = "Unlimited"; break; - } + switch(g_limit_speed) { + case 1: sp_str = "1Mhz"; break; + case 2: sp_str = "2.8Mhz"; break; + case 3: sp_str = "8.0Mhz"; break; + default: sp_str = "Unlimited"; break; + } // OG Pass speed info to the control (ActiveX specific) #ifdef ACTIVEGS - { - extern void updateInfo(const char* target,const char *speed); - updateInfo(sp_str,total_mhz_ptr); - } + { + extern void updateInfo(const char* target,const char *speed); + updateInfo(sp_str,total_mhz_ptr); + } #endif - sprintf(status_buf, "dcycs:%9.1f sim MHz:%s " - "Eff MHz:%s, sec:%1.3f vol:%02x pal:%x, Limit:%s", - dcycs/(1000.0*1000.0), sim_mhz_ptr, total_mhz_ptr, - dtime_diff_1sec, g_doc_vol, g_a2vid_palette, - sp_str); - video_update_status_line(0, status_buf); + sprintf(status_buf, "dcycs:%9.1f sim MHz:%s " + "Eff MHz:%s, sec:%1.3f vol:%02x pal:%x, Limit:%s", + dcycs/(1000.0*1000.0), sim_mhz_ptr, total_mhz_ptr, + dtime_diff_1sec, g_doc_vol, g_a2vid_palette, + sp_str); + video_update_status_line(0, status_buf); - if(g_video_line_update_interval == 0) { - if(g_sim_mhz > 12.0) { - /* just set video line_ref_amt to 1 */ - g_line_ref_amt = 1; - } else if(g_line_ref_amt == 1 && g_sim_mhz < 4.0) { - g_line_ref_amt = 8; - } - } else { - g_line_ref_amt = g_video_line_update_interval; - } + if(g_video_line_update_interval == 0) { + if(g_sim_mhz > 12.0) { + /* just set video line_ref_amt to 1 */ + g_line_ref_amt = 1; + } else if(g_line_ref_amt == 1 && g_sim_mhz < 4.0) { + g_line_ref_amt = 8; + } + } else { + g_line_ref_amt = g_video_line_update_interval; + } - if(g_dnatcycs_1sec < (1000.0*1000.0)) { - /* make it so large that all %'s become 0 */ - g_dnatcycs_1sec = 800.0*1000.0*1000.0*1000.0; - } - dnatcycs_1sec = g_dnatcycs_1sec / 100.0; /* eff mult by 100 */ + if(g_dnatcycs_1sec < (1000.0*1000.0)) { + /* make it so large that all %'s become 0 */ + g_dnatcycs_1sec = 800.0*1000.0*1000.0*1000.0; + } + dnatcycs_1sec = g_dnatcycs_1sec / 100.0; /* eff mult by 100 */ - dtmp2 = (double)(g_cycs_in_check_input) / dnatcycs_1sec; - dtmp3 = (double)(g_cycs_in_refresh_line) / dnatcycs_1sec; - dtmp4 = (double)(g_cycs_in_refresh_ximage) / dnatcycs_1sec; - sprintf(status_buf, "xfer:%08x, %5.1f ref_amt:%d " - "ch_in:%4.1f%% ref_l:%4.1f%% ref_x:%4.1f%%", - g_refresh_bytes_xfer, g_dnatcycs_1sec/(1000.0*1000.0), - g_line_ref_amt, dtmp2, dtmp3, dtmp4); - video_update_status_line(1, status_buf); + dtmp2 = (double)(g_cycs_in_check_input) / dnatcycs_1sec; + dtmp3 = (double)(g_cycs_in_refresh_line) / dnatcycs_1sec; + dtmp4 = (double)(g_cycs_in_refresh_ximage) / dnatcycs_1sec; + sprintf(status_buf, "xfer:%08x, %5.1f ref_amt:%d " + "ch_in:%4.1f%% ref_l:%4.1f%% ref_x:%4.1f%%", + g_refresh_bytes_xfer, g_dnatcycs_1sec/(1000.0*1000.0), + g_line_ref_amt, dtmp2, dtmp3, dtmp4); + video_update_status_line(1, status_buf); - sprintf(status_buf, "Ints:%3d I/O:%4dK BRK:%3d COP:%2d " - "Eng:%3d act:%3d hev:%3d esi:%3d edi:%3d", - g_num_irq, g_io_amt>>10, g_num_brk, g_num_cop, - g_num_enter_engine, g_engine_action, - g_engine_halt_event, g_engine_scan_int, - g_engine_doc_int); - video_update_status_line(2, status_buf); + sprintf(status_buf, "Ints:%3d I/O:%4dK BRK:%3d COP:%2d " + "Eng:%3d act:%3d hev:%3d esi:%3d edi:%3d", + g_num_irq, g_io_amt>>10, g_num_brk, g_num_cop, + g_num_enter_engine, g_engine_action, + g_engine_halt_event, g_engine_scan_int, + g_engine_doc_int); + video_update_status_line(2, status_buf); - dtmp1 = (double)(g_cycs_in_sound1) / dnatcycs_1sec; - dtmp2 = (double)(g_cycs_in_sound2) / dnatcycs_1sec; - dtmp3 = (double)(g_cycs_in_sound3) / dnatcycs_1sec; - dtmp4 = (double)(g_cycs_in_start_sound) / dnatcycs_1sec; - dtmp5 = (double)(g_cycs_in_est_sound) / dnatcycs_1sec; - sprintf(status_buf, "snd1:%4.1f%%, 2:%4.1f%%, " - "3:%4.1f%%, st:%4.1f%% est:%4.1f%% %4.2f", - dtmp1, dtmp2, dtmp3, dtmp4, dtmp5, g_fvoices); - video_update_status_line(3, status_buf); + dtmp1 = (double)(g_cycs_in_sound1) / dnatcycs_1sec; + dtmp2 = (double)(g_cycs_in_sound2) / dnatcycs_1sec; + dtmp3 = (double)(g_cycs_in_sound3) / dnatcycs_1sec; + dtmp4 = (double)(g_cycs_in_start_sound) / dnatcycs_1sec; + dtmp5 = (double)(g_cycs_in_est_sound) / dnatcycs_1sec; + sprintf(status_buf, "snd1:%4.1f%%, 2:%4.1f%%, " + "3:%4.1f%%, st:%4.1f%% est:%4.1f%% %4.2f", + dtmp1, dtmp2, dtmp3, dtmp4, dtmp5, g_fvoices); + video_update_status_line(3, status_buf); - code_str1 = ""; - code_str2 = ""; - if(g_code_yellow) { - code_str1 = "Code: Yellow"; - code_str2 = "Emulated system state suspect, save work"; - } - if(g_code_red) { - code_str1 = "Code: RED"; - code_str2 = "Emulated system state probably corrupt"; - } - sprintf(status_buf, "snd_plays:%4d, doc_ev:%4d, st_snd:%4d " - "snd_parms: %4d %s", - g_num_snd_plays, g_num_doc_events, g_num_start_sounds, - g_num_recalc_snd_parms, code_str1); - video_update_status_line(4, status_buf); + code_str1 = ""; + code_str2 = ""; + if(g_code_yellow) { + code_str1 = "Code: Yellow"; + code_str2 = "Emulated system state suspect, save work"; + } + if(g_code_red) { + code_str1 = "Code: RED"; + code_str2 = "Emulated system state probably corrupt"; + } + sprintf(status_buf, "snd_plays:%4d, doc_ev:%4d, st_snd:%4d " + "snd_parms: %4d %s", + g_num_snd_plays, g_num_doc_events, g_num_start_sounds, + g_num_recalc_snd_parms, code_str1); + video_update_status_line(4, status_buf); - draw_iwm_status(5, status_buf); + draw_iwm_status(5, status_buf); - sprintf(status_buf, "GSplus v%-6s " - "Press F4 for Config Menu %s", - g_gsplus_version_str, code_str2); - video_update_status_line(6, status_buf); + sprintf(status_buf, "GSplus v%-6s " + "Press F4 for Config Menu %s", + g_gsplus_version_str, code_str2); + video_update_status_line(6, status_buf); - g_status_refresh_needed = 1; + g_status_refresh_needed = 1; - g_num_irq = 0; - g_num_brk = 0; - g_num_cop = 0; - g_num_enter_engine = 0; - g_io_amt = 0; - g_engine_action = 0; - g_engine_halt_event = 0; - g_engine_scan_int = 0; - g_engine_doc_int = 0; + g_num_irq = 0; + g_num_brk = 0; + g_num_cop = 0; + g_num_enter_engine = 0; + g_io_amt = 0; + g_engine_action = 0; + g_engine_halt_event = 0; + g_engine_scan_int = 0; + g_engine_doc_int = 0; - g_cycs_in_40col = 0; - g_cycs_in_xredraw = 0; - g_cycs_in_check_input = 0; - g_cycs_in_refresh_line = 0; - g_cycs_in_refresh_ximage = 0; - g_cycs_in_io_read = 0; - g_cycs_in_sound1 = 0; - g_cycs_in_sound2 = 0; - g_cycs_in_sound3 = 0; - g_cycs_in_sound4 = 0; - g_cycs_in_start_sound = 0; - g_cycs_in_est_sound = 0; - g_dnatcycs_1sec = 0.0; - g_refresh_bytes_xfer = 0; + g_cycs_in_40col = 0; + g_cycs_in_xredraw = 0; + g_cycs_in_check_input = 0; + g_cycs_in_refresh_line = 0; + g_cycs_in_refresh_ximage = 0; + g_cycs_in_io_read = 0; + g_cycs_in_sound1 = 0; + g_cycs_in_sound2 = 0; + g_cycs_in_sound3 = 0; + g_cycs_in_sound4 = 0; + g_cycs_in_start_sound = 0; + g_cycs_in_est_sound = 0; + g_dnatcycs_1sec = 0.0; + g_refresh_bytes_xfer = 0; - g_num_snd_plays = 0; - g_num_doc_events = 0; - g_num_start_sounds = 0; - g_num_scan_osc = 0; - g_num_recalc_snd_parms = 0; + g_num_snd_plays = 0; + g_num_doc_events = 0; + g_num_start_sounds = 0; + g_num_scan_osc = 0; + g_num_recalc_snd_parms = 0; - g_fvoices = (float)0.0; - } + g_fvoices = (float)0.0; + } - dtime_this_vbl = dtime_now - g_dtime_last_vbl; - if(dtime_this_vbl < 0.001) { - dtime_this_vbl = 0.001; - } + dtime_this_vbl = dtime_now - g_dtime_last_vbl; + if(dtime_this_vbl < 0.001) { + dtime_this_vbl = 0.001; + } - g_dtime_last_vbl = dtime_now; + g_dtime_last_vbl = dtime_now; - dadjcycs_this_vbl = g_dadjcycs - g_last_vbl_dadjcycs; - g_last_vbl_dadjcycs = g_dadjcycs; + dadjcycs_this_vbl = g_dadjcycs - g_last_vbl_dadjcycs; + g_last_vbl_dadjcycs = g_dadjcycs; - g_dtime_expected += (1.0/60.0); + g_dtime_expected += (1.0/60.0); - eff_pmhz = ((dadjcycs_this_vbl) / (dtime_this_vbl)) / - DCYCS_1_MHZ; + eff_pmhz = ((dadjcycs_this_vbl) / (dtime_this_vbl)) / + DCYCS_1_MHZ; - /* using eff_pmhz, predict how many cycles can be run by */ - /* g_dtime_expected */ + /* using eff_pmhz, predict how many cycles can be run by */ + /* g_dtime_expected */ - dtime_till_expected = g_dtime_expected - dtime_now; + dtime_till_expected = g_dtime_expected - dtime_now; - dratio = 60.0 * dtime_till_expected; + dratio = 60.0 * dtime_till_expected; - predicted_pmhz = eff_pmhz * dratio; + predicted_pmhz = eff_pmhz * dratio; - if(! (predicted_pmhz < (1.4 * g_projected_pmhz))) { - predicted_pmhz = 1.4 * g_projected_pmhz; - } + if(!(predicted_pmhz < (1.4 * g_projected_pmhz))) { + predicted_pmhz = 1.4 * g_projected_pmhz; + } - if(! (predicted_pmhz > (0.7 * g_projected_pmhz))) { - predicted_pmhz = 0.7 * g_projected_pmhz; - } + if(!(predicted_pmhz > (0.7 * g_projected_pmhz))) { + predicted_pmhz = 0.7 * g_projected_pmhz; + } - if(!(predicted_pmhz >= 1.0)) { - irq_printf("predicted: %f, setting to 1.0\n", predicted_pmhz); - predicted_pmhz = 1.0; - } + if(!(predicted_pmhz >= 1.0)) { + irq_printf("predicted: %f, setting to 1.0\n", predicted_pmhz); + predicted_pmhz = 1.0; + } - if(!(predicted_pmhz < 250.0)) { - irq_printf("predicted: %f, setting to 250.0\n", predicted_pmhz); - predicted_pmhz = 250.0; - } + if(!(predicted_pmhz < 250.0)) { + irq_printf("predicted: %f, setting to 250.0\n", predicted_pmhz); + predicted_pmhz = 250.0; + } - recip_predicted_pmhz = 1.0/predicted_pmhz; - g_projected_pmhz = predicted_pmhz; + recip_predicted_pmhz = 1.0/predicted_pmhz; + g_projected_pmhz = predicted_pmhz; - g_recip_projected_pmhz_unl.plus_1 = 1.0*recip_predicted_pmhz; - g_recip_projected_pmhz_unl.plus_2 = 2.0*recip_predicted_pmhz; - g_recip_projected_pmhz_unl.plus_3 = 3.0*recip_predicted_pmhz; - g_recip_projected_pmhz_unl.plus_x_minus_1 = 1.01 - recip_predicted_pmhz; + g_recip_projected_pmhz_unl.plus_1 = 1.0*recip_predicted_pmhz; + g_recip_projected_pmhz_unl.plus_2 = 2.0*recip_predicted_pmhz; + g_recip_projected_pmhz_unl.plus_3 = 3.0*recip_predicted_pmhz; + g_recip_projected_pmhz_unl.plus_x_minus_1 = 1.01 - recip_predicted_pmhz; - if(dtime_till_expected < -0.125) { - /* If we were way off, get back on track */ - /* this happens because our sim took much longer than */ - /* expected, so we're going to skip some VBL */ - irq_printf("adj1: dtexp:%f, dt_new:%f\n", - g_dtime_expected, dtime_now); + if(dtime_till_expected < -0.125) { + /* If we were way off, get back on track */ + /* this happens because our sim took much longer than */ + /* expected, so we're going to skip some VBL */ + irq_printf("adj1: dtexp:%f, dt_new:%f\n", + g_dtime_expected, dtime_now); - dtime_diff = -dtime_till_expected; + dtime_diff = -dtime_till_expected; - irq_printf("dtime_till_exp: %f, dtime_diff: %f, dcycs: %f\n", - dtime_till_expected, dtime_diff, dcycs); + irq_printf("dtime_till_exp: %f, dtime_diff: %f, dcycs: %f\n", + dtime_till_expected, dtime_diff, dcycs); - g_dtime_expected += dtime_diff; - } + g_dtime_expected += dtime_diff; + } - if(dtime_till_expected > (3/60.0)) { - /* we're running fast, usleep */ - micro_sleep(dtime_till_expected - (1/60.0)); - } + if(dtime_till_expected > (3/60.0)) { + /* we're running fast, usleep */ + micro_sleep(dtime_till_expected - (1/60.0)); + } - g_dtime_this_vbl_array[prev_vbl_index] = dtime_this_vbl; - g_dtime_exp_array[prev_vbl_index] = g_dtime_expected; - g_dtime_pmhz_array[prev_vbl_index] = predicted_pmhz; - g_dtime_eff_pmhz_array[prev_vbl_index] = eff_pmhz; + g_dtime_this_vbl_array[prev_vbl_index] = dtime_this_vbl; + g_dtime_exp_array[prev_vbl_index] = g_dtime_expected; + g_dtime_pmhz_array[prev_vbl_index] = predicted_pmhz; + g_dtime_eff_pmhz_array[prev_vbl_index] = eff_pmhz; - if(g_c041_val & C041_EN_VBL_INTS) { - add_event_vbl(); - } + if(g_c041_val & C041_EN_VBL_INTS) { + add_event_vbl(); + } - g_25sec_cntr++; - if(g_25sec_cntr >= 16) { - g_25sec_cntr = 0; - if(g_c041_val & C041_EN_25SEC_INTS) { - add_irq(IRQ_PENDING_C046_25SEC); - g_c046_val |= 0x10; - irq_printf("Setting c046 .25 sec int, g_irq_pend:%d\n", - g_irq_pending); - } - } + g_25sec_cntr++; + if(g_25sec_cntr >= 16) { + g_25sec_cntr = 0; + if(g_c041_val & C041_EN_25SEC_INTS) { + add_irq(IRQ_PENDING_C046_25SEC); + g_c046_val |= 0x10; + irq_printf("Setting c046 .25 sec int, g_irq_pend:%d\n", + g_irq_pending); + } + } - g_1sec_cntr++; - if(g_1sec_cntr >= 60) { - g_1sec_cntr = 0; - tmp = g_c023_val; - tmp |= 0x40; /* set 1sec int */ - if(tmp & 0x04) { - tmp |= 0x80; - add_irq(IRQ_PENDING_C023_1SEC); - irq_printf("Setting c023 to %02x irq_pend: %d\n", - tmp, g_irq_pending); - } - g_c023_val = tmp; - } + g_1sec_cntr++; + if(g_1sec_cntr >= 60) { + g_1sec_cntr = 0; + tmp = g_c023_val; + tmp |= 0x40; /* set 1sec int */ + if(tmp & 0x04) { + tmp |= 0x80; + add_irq(IRQ_PENDING_C023_1SEC); + irq_printf("Setting c023 to %02x irq_pend: %d\n", + tmp, g_irq_pending); + } + g_c023_val = tmp; + } - if(!g_scan_int_events) { - check_scan_line_int(dcycs, 0); - } + if(!g_scan_int_events) { + check_scan_line_int(dcycs, 0); + } - doit_3_persec = 0; - if(g_config_iwm_vbl_count > 0) { - g_config_iwm_vbl_count--; - } else { - g_config_iwm_vbl_count = 20; - doit_3_persec = 1; - } + doit_3_persec = 0; + if(g_config_iwm_vbl_count > 0) { + g_config_iwm_vbl_count--; + } else { + g_config_iwm_vbl_count = 20; + doit_3_persec = 1; + } - iwm_vbl_update(doit_3_persec); + iwm_vbl_update(doit_3_persec); // OG Disabling config update #ifndef ACTIVEGS - config_vbl_update(doit_3_persec); + config_vbl_update(doit_3_persec); #else // OG Added disk update - { - extern void checkImages(); - checkImages(); - } + { + extern void checkImages(); + checkImages(); + } #endif - video_update(); - sound_update(dcycs); - clock_update(); - scc_update(dcycs); - //Check and see if virtual printer timeout has been reached. - if (g_printer_timeout) - { - printer_update(); - } - if (g_imagewriter_timeout) - { - imagewriter_update(); - } - paddle_update_buttons(); + video_update(); + sound_update(dcycs); + clock_update(); + scc_update(dcycs); + //Check and see if virtual printer timeout has been reached. + if (g_printer_timeout) + { + printer_update(); + } + if (g_imagewriter_timeout) + { + imagewriter_update(); + } + paddle_update_buttons(); } -void -do_vbl_int() -{ - if(g_c041_val & C041_EN_VBL_INTS) { - g_c046_val |= 0x08; - add_irq(IRQ_PENDING_C046_VBL); - irq_printf("Setting c046 vbl_int_status to 1, irq_pend: %d\n", - g_irq_pending); - } +void do_vbl_int() { + if(g_c041_val & C041_EN_VBL_INTS) { + g_c046_val |= 0x08; + add_irq(IRQ_PENDING_C046_VBL); + irq_printf("Setting c046 vbl_int_status to 1, irq_pend: %d\n", + g_irq_pending); + } } -void -do_scan_int(double dcycs, int line) -{ - int c023_val; - g_scan_int_events = 0; +void do_scan_int(double dcycs, int line) { + int c023_val; + g_scan_int_events = 0; - c023_val = g_c023_val; - if(c023_val & 0x20) { - halt_printf("c023 scan_int and another on line %03x\n", line); - } + c023_val = g_c023_val; + if(c023_val & 0x20) { + halt_printf("c023 scan_int and another on line %03x\n", line); + } - /* make sure scan int is still enabled for this line */ - if((g_slow_memory_ptr[0x19d00 + line] & 0x40) && - (g_cur_a2_stat & ALL_STAT_SUPER_HIRES)) { - /* valid interrupt, do it */ - c023_val |= 0xa0; /* vgc_int and scan_int */ - if(c023_val & 0x02) { - add_irq(IRQ_PENDING_C023_SCAN); - irq_printf("Setting c023 to %02x, irq_pend: %d\n", - c023_val, g_irq_pending); - } - g_c023_val = c023_val; - HALT_ON(HALT_ON_SCAN_INT, "In do_scan_int\n"); - } else { - /* scan int bit cleared on scan line control byte */ - /* look for next line, if any */ - check_scan_line_int(dcycs, line+1); - } + /* make sure scan int is still enabled for this line */ + if((g_slow_memory_ptr[0x19d00 + line] & 0x40) && + (g_cur_a2_stat & ALL_STAT_SUPER_HIRES)) { + /* valid interrupt, do it */ + c023_val |= 0xa0; /* vgc_int and scan_int */ + if(c023_val & 0x02) { + add_irq(IRQ_PENDING_C023_SCAN); + irq_printf("Setting c023 to %02x, irq_pend: %d\n", + c023_val, g_irq_pending); + } + g_c023_val = c023_val; + HALT_ON(HALT_ON_SCAN_INT, "In do_scan_int\n"); + } else { + /* scan int bit cleared on scan line control byte */ + /* look for next line, if any */ + check_scan_line_int(dcycs, line+1); + } } -void -check_scan_line_int(double dcycs, int cur_video_line) -{ - int delay; - int start; - int line; - int i; - /* Called during VBL interrupt phase */ +void check_scan_line_int(double dcycs, int cur_video_line) { + int delay; + int start; + int line; + int i; + /* Called during VBL interrupt phase */ - if(!(g_cur_a2_stat & ALL_STAT_SUPER_HIRES)) { - return; - } + if(!(g_cur_a2_stat & ALL_STAT_SUPER_HIRES)) { + return; + } - if(g_c023_val & 0x20) { - /* don't check for any more */ - return; - } + if(g_c023_val & 0x20) { + /* don't check for any more */ + return; + } - start = cur_video_line; - if(start < 0) { - halt_printf("check_scan_line_int: cur_video_line: %d\n", - cur_video_line); - start = 0; - } + start = cur_video_line; + if(start < 0) { + halt_printf("check_scan_line_int: cur_video_line: %d\n", + cur_video_line); + start = 0; + } - for(line = start; line < 200; line++) { - i = line; + for(line = start; line < 200; line++) { + i = line; - if(i < 0 || i >= 200) { - halt_printf("check_new_scan_int:i:%d, line:%d, st:%d\n", - i, line, start); - i = 0; - } - if(g_slow_memory_ptr[0x19d00+i] & 0x40) { - irq_printf("Adding scan_int for line %d\n", i); - delay = (int)( (DCYCS_IN_16MS/262.0) * ((double)line) ); - add_event_entry(g_last_vbl_dcycs + delay, EV_SCAN_INT + - (line << 8)); - g_scan_int_events = 1; - check_for_one_event_type(EV_SCAN_INT); - break; - } - } + if(i < 0 || i >= 200) { + halt_printf("check_new_scan_int:i:%d, line:%d, st:%d\n", + i, line, start); + i = 0; + } + if(g_slow_memory_ptr[0x19d00+i] & 0x40) { + irq_printf("Adding scan_int for line %d\n", i); + delay = (int)( (DCYCS_IN_16MS/262.0) * ((double)line) ); + add_event_entry(g_last_vbl_dcycs + delay, EV_SCAN_INT + + (line << 8)); + g_scan_int_events = 1; + check_for_one_event_type(EV_SCAN_INT); + break; + } + } } -void -check_for_new_scan_int(double dcycs) -{ - int cur_video_line; +void check_for_new_scan_int(double dcycs) { + int cur_video_line; - cur_video_line = get_lines_since_vbl(dcycs) >> 8; + cur_video_line = get_lines_since_vbl(dcycs) >> 8; - check_scan_line_int(dcycs, cur_video_line); + check_scan_line_int(dcycs, cur_video_line); } -void -init_reg() -{ - engine.acc = 0; - engine.xreg = 0; - engine.yreg = 0; - engine.stack = 0x1ff; - engine.direct = 0; - engine.psr = 0x134; - engine.fplus_ptr = 0; +void init_reg() { + engine.acc = 0; + engine.xreg = 0; + engine.yreg = 0; + engine.stack = 0x1ff; + engine.direct = 0; + engine.psr = 0x134; + engine.fplus_ptr = 0; } -void -handle_action(word32 ret) -{ - int type; +void handle_action(word32 ret) { + int type; - type = EXTRU(ret,3,4); - switch(type) { - case RET_BREAK: - do_break(ret & 0xff); - break; - case RET_COP: - do_cop(ret & 0xff); - break; + type = EXTRU(ret,3,4); + switch(type) { + case RET_BREAK: + do_break(ret & 0xff); + break; + case RET_COP: + do_cop(ret & 0xff); + break; #if 0 - case RET_MVN: - do_mvn(ret & 0xffff); - break; + case RET_MVN: + do_mvn(ret & 0xffff); + break; #endif - case RET_C700: - do_c700(ret); - break; - case RET_C70A: - do_c70a(ret); - break; - case RET_C70D: - do_c70d(ret); - break; + case RET_C700: + do_c700(ret); + break; + case RET_C70A: + do_c70a(ret); + break; + case RET_C70D: + do_c70d(ret); + break; #if 0 - case RET_ADD_DEC_8: - do_add_dec_8(ret); - break; - case RET_ADD_DEC_16: - do_add_dec_16(ret); - break; + case RET_ADD_DEC_8: + do_add_dec_8(ret); + break; + case RET_ADD_DEC_16: + do_add_dec_16(ret); + break; #endif - case RET_IRQ: - irq_printf("Special fast IRQ response. irq_pending: %x\n", - g_irq_pending); - break; - case RET_WDM: - do_wdm(ret & 0xff); - break; - case RET_STP: - do_stp(); - break; - default: - halt_printf("Unknown special action: %08x!\n", ret); - } + case RET_IRQ: + irq_printf("Special fast IRQ response. irq_pending: %x\n", + g_irq_pending); + break; + case RET_WDM: + do_wdm(ret & 0xff); + break; + case RET_STP: + do_stp(); + break; + default: + halt_printf("Unknown special action: %08x!\n", ret); + } } #if 0 -void -do_add_dec_8(word32 ret) -{ - halt_printf("do_add_dec_8 called, ret: %08x\n", ret); +void do_add_dec_8(word32 ret) { + halt_printf("do_add_dec_8 called, ret: %08x\n", ret); } -void -do_add_dec_16(word32 ret) -{ - halt_printf("do_add_dec_16 called, ret: %08x\n", ret); +void do_add_dec_16(word32 ret) { + halt_printf("do_add_dec_16 called, ret: %08x\n", ret); } #endif -void -do_break(word32 ret) -{ - if(!g_testing) { - printf("I think I got a break, second byte: %02x!\n", ret); - printf("kpc: %06x\n", engine.kpc); - } +void do_break(word32 ret) { + if(!g_testing) { + printf("I think I got a break, second byte: %02x!\n", ret); + printf("kpc: %06x\n", engine.kpc); + } - halt_printf("do_break, kpc: %06x\n", engine.kpc); - enter_debug = 1; + halt_printf("do_break, kpc: %06x\n", engine.kpc); + enter_debug = 1; } -void -do_cop(word32 ret) -{ - halt_printf("COP instr %02x!\n", ret); - fflush(stdout); +void do_cop(word32 ret) { + halt_printf("COP instr %02x!\n", ret); + fflush(stdout); } #if 0 -void -do_mvn(word32 banks) -{ - int src_bank, dest_bank; - int dest, src; - int num; - int i; - int val; +void do_mvn(word32 banks) { + int src_bank, dest_bank; + int dest, src; + int num; + int i; + int val; - halt_printf("In MVN...just quitting\n"); - return; - printf("MVN instr with %04x, cycles: %08x\n", banks, engine.cycles); - src_bank = banks >> 8; - dest_bank = banks & 0xff; - printf("psr: %03x\n", engine.psr); - if((engine.psr & 0x30) != 0) { - halt_printf("MVN in non-native mode unimplemented!\n"); - } + halt_printf("In MVN...just quitting\n"); + return; + printf("MVN instr with %04x, cycles: %08x\n", banks, engine.cycles); + src_bank = banks >> 8; + dest_bank = banks & 0xff; + printf("psr: %03x\n", engine.psr); + if((engine.psr & 0x30) != 0) { + halt_printf("MVN in non-native mode unimplemented!\n"); + } - dest = dest_bank << 16 | engine.yreg; - src = src_bank << 16 | engine.xreg; - num = engine.acc; - printf("Moving %08x+1 bytes from %08x to %08x\n", num, src, dest); + dest = dest_bank << 16 | engine.yreg; + src = src_bank << 16 | engine.xreg; + num = engine.acc; + printf("Moving %08x+1 bytes from %08x to %08x\n", num, src, dest); - for(i = 0; i <= num; i++) { - val = get_memory_c(src, 0); - set_memory_c(dest, val, 0); - src = (src_bank << 16) | ((src + 1) & 0xffff); - dest = (dest_bank << 16) | ((dest + 1) & 0xffff); - } - engine.dbank = dest_bank; - engine.acc = 0xffff; - engine.yreg = dest & 0xffff; - engine.xreg = src & 0xffff; - engine.kpc = (engine.kpc + 3); - printf("move done. db: %02x, acc: %04x, y: %04x, x: %04x, num: %08x\n", - engine.dbank, engine.acc, engine.yreg, engine.xreg, num); + for(i = 0; i <= num; i++) { + val = get_memory_c(src, 0); + set_memory_c(dest, val, 0); + src = (src_bank << 16) | ((src + 1) & 0xffff); + dest = (dest_bank << 16) | ((dest + 1) & 0xffff); + } + engine.dbank = dest_bank; + engine.acc = 0xffff; + engine.yreg = dest & 0xffff; + engine.xreg = src & 0xffff; + engine.kpc = (engine.kpc + 3); + printf("move done. db: %02x, acc: %04x, y: %04x, x: %04x, num: %08x\n", + engine.dbank, engine.acc, engine.yreg, engine.xreg, num); } #endif extern void host_fst(void); -void -do_wdm(word32 arg) -{ - switch(arg) { - case 0x8d: /* Bouncin Ferno does WDM 8d */ - break; +void do_wdm(word32 arg) { + switch(arg) { + case 0x8d: /* Bouncin Ferno does WDM 8d */ + break; - case 0xff: /* fst */ - host_fst(); - break; + case 0xff: /* fst */ + host_fst(); + break; - default: - halt_printf("do_wdm: %02x! pc = $%06x\n", arg, engine.kpc - 2); - } + default: + halt_printf("do_wdm: %02x! pc = $%06x\n", arg, engine.kpc - 2); + } } -void -do_wai() -{ - halt_printf("do_wai!\n"); +void do_wai() { + halt_printf("do_wai!\n"); } -void -do_stp() -{ - if(!g_stp_pending) { - g_stp_pending = 1; - halt_printf("Hit STP instruction at: %06x, press RESET to " - "continue\n", engine.kpc); - } +void do_stp() { + if(!g_stp_pending) { + g_stp_pending = 1; + halt_printf("Hit STP instruction at: %06x, press RESET to " + "continue\n", engine.kpc); + } } -void -size_fail(int val, word32 v1, word32 v2) -{ - halt_printf("Size failure, val: %08x, %08x %08x\n", val, v1, v2); +void size_fail(int val, word32 v1, word32 v2) { + halt_printf("Size failure, val: %08x, %08x %08x\n", val, v1, v2); } -int -gsplus_vprintf(const char *fmt, va_list ap) -{ - char *bufptr, *buf2ptr; - int len; - int ret; +int gsplus_vprintf(const char *fmt, va_list ap) { + char *bufptr, *buf2ptr; + int len; + int ret; - bufptr = (char*)malloc(4096); // OG Added Cast - ret = vsnprintf(bufptr, 4090, fmt, ap); + bufptr = (char*)malloc(4096); // OG Added Cast + ret = vsnprintf(bufptr, 4090, fmt, ap); - // OG Display warning - printf("Warning:%s",bufptr); + // OG Display warning + printf("Warning:%s",bufptr); - len = strlen(bufptr); - if(g_fatal_log >= 0 && g_fatal_log < MAX_FATAL_LOGS) { - buf2ptr = (char*)malloc(len+1); // OG Added Cast - memcpy(buf2ptr, bufptr, len+1); - g_fatal_log_strs[g_fatal_log++] = buf2ptr; - } - must_write(1, bufptr, len); - if(g_debug_file_fd >= 0) { - must_write(g_debug_file_fd, bufptr, len); - } - free(bufptr); + len = strlen(bufptr); + if(g_fatal_log >= 0 && g_fatal_log < MAX_FATAL_LOGS) { + buf2ptr = (char*)malloc(len+1); // OG Added Cast + memcpy(buf2ptr, bufptr, len+1); + g_fatal_log_strs[g_fatal_log++] = buf2ptr; + } + must_write(1, bufptr, len); + if(g_debug_file_fd >= 0) { + must_write(g_debug_file_fd, bufptr, len); + } + free(bufptr); - return ret; + return ret; } -int -fatal_printf(const char *fmt, ...) -{ - va_list ap; - int ret; +int fatal_printf(const char *fmt, ...) { + va_list ap; + int ret; - va_start(ap, fmt); + va_start(ap, fmt); - if(g_fatal_log < 0) { - g_fatal_log = 0; - } - ret = gsplus_vprintf(fmt, ap); - va_end(ap); + if(g_fatal_log < 0) { + g_fatal_log = 0; + } + ret = gsplus_vprintf(fmt, ap); + va_end(ap); - return ret; + return ret; } -void -must_write(int fd, char *bufptr, int len) -{ - int ret; - while(len > 0) { - ret = write(fd, bufptr, len); - if(ret >= 0) { - len -= ret; - bufptr += ret; - } else if(errno != EAGAIN && errno != EINTR) { - return; // just get out - } - } +void must_write(int fd, char *bufptr, int len) { + int ret; + while(len > 0) { + ret = write(fd, bufptr, len); + if(ret >= 0) { + len -= ret; + bufptr += ret; + } else if(errno != EAGAIN && errno != EINTR) { + return; // just get out + } + } } -void -clear_fatal_logs() -{ - int i; +void clear_fatal_logs() { + int i; - for(i = 0; i < g_fatal_log; i++) { - free(g_fatal_log_strs[i]); - g_fatal_log_strs[i] = 0; - } - g_fatal_log = -1; + for(i = 0; i < g_fatal_log; i++) { + free(g_fatal_log_strs[i]); + g_fatal_log_strs[i] = 0; + } + g_fatal_log = -1; } -char * -gsplus_malloc_str(char *in_str) -{ - char *str; - int len; +char *gsplus_malloc_str(char *in_str) { + char *str; + int len; - len = strlen(in_str) + 1; - str = (char*)malloc(len); // OG Added cast - memcpy(str, in_str, len); + len = strlen(in_str) + 1; + str = (char*)malloc(len); // OG Added cast + memcpy(str, in_str, len); - return str; + return str; } diff --git a/src/smartport.c b/src/smartport.c index 730e62a..0dc4cf5 100644 --- a/src/smartport.c +++ b/src/smartport.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include "glog.h" @@ -20,727 +20,713 @@ extern Engine_reg engine; extern Iwm iwm; -#define LEN_SMPT_LOG 16 +#define LEN_SMPT_LOG 16 STRUCT(Smpt_log) { - word32 start_addr; - int cmd; - int rts_addr; - int cmd_list; - int extras; - int unit; - int buf; - int blk; + word32 start_addr; + int cmd; + int rts_addr; + int cmd_list; + int extras; + int unit; + int buf; + int blk; }; Smpt_log g_smpt_log[LEN_SMPT_LOG]; -int g_smpt_log_pos = 0; +int g_smpt_log_pos = 0; -void -smartport_error(void) -{ - int pos; - int i; +void smartport_error(void) { + int pos; + int i; - pos = g_smpt_log_pos; - printf("Smartport log pos: %d\n", pos); - for(i = 0; i < LEN_SMPT_LOG; i++) { - pos--; - if(pos < 0) { - pos = LEN_SMPT_LOG - 1; - } - printf("%d:%d: t:%04x, cmd:%02x, rts:%04x, " - "cmd_l:%04x, x:%d, unit:%d, buf:%04x, blk:%04x\n", - i, pos, - g_smpt_log[pos].start_addr, - g_smpt_log[pos].cmd, - g_smpt_log[pos].rts_addr, - g_smpt_log[pos].cmd_list, - g_smpt_log[pos].extras, - g_smpt_log[pos].unit, - g_smpt_log[pos].buf, - g_smpt_log[pos].blk); - } + pos = g_smpt_log_pos; + printf("Smartport log pos: %d\n", pos); + for(i = 0; i < LEN_SMPT_LOG; i++) { + pos--; + if(pos < 0) { + pos = LEN_SMPT_LOG - 1; + } + printf("%d:%d: t:%04x, cmd:%02x, rts:%04x, " + "cmd_l:%04x, x:%d, unit:%d, buf:%04x, blk:%04x\n", + i, pos, + g_smpt_log[pos].start_addr, + g_smpt_log[pos].cmd, + g_smpt_log[pos].rts_addr, + g_smpt_log[pos].cmd_list, + g_smpt_log[pos].extras, + g_smpt_log[pos].unit, + g_smpt_log[pos].buf, + g_smpt_log[pos].blk); + } } -void -smartport_log(word32 start_addr, int cmd, int rts_addr, int cmd_list) -{ - int pos; +void smartport_log(word32 start_addr, int cmd, int rts_addr, int cmd_list) { + int pos; - pos = g_smpt_log_pos; - if(start_addr != 0) { - g_smpt_log[pos].start_addr = start_addr; - g_smpt_log[pos].cmd = cmd; - g_smpt_log[pos].rts_addr = rts_addr; - g_smpt_log[pos].cmd_list = cmd_list; - g_smpt_log[pos].extras = 0; - g_smpt_log[pos].unit = 0; - g_smpt_log[pos].buf = 0; - g_smpt_log[pos].blk = 0; - } else { - pos--; - if(pos < 0) { - pos = LEN_SMPT_LOG - 1; - } - g_smpt_log[pos].extras = 1; - g_smpt_log[pos].unit = cmd; - g_smpt_log[pos].buf = rts_addr; - g_smpt_log[pos].blk = cmd_list; - } - pos++; - if(pos >= LEN_SMPT_LOG) { - pos = 0; - } - g_smpt_log_pos = pos; + pos = g_smpt_log_pos; + if(start_addr != 0) { + g_smpt_log[pos].start_addr = start_addr; + g_smpt_log[pos].cmd = cmd; + g_smpt_log[pos].rts_addr = rts_addr; + g_smpt_log[pos].cmd_list = cmd_list; + g_smpt_log[pos].extras = 0; + g_smpt_log[pos].unit = 0; + g_smpt_log[pos].buf = 0; + g_smpt_log[pos].blk = 0; + } else { + pos--; + if(pos < 0) { + pos = LEN_SMPT_LOG - 1; + } + g_smpt_log[pos].extras = 1; + g_smpt_log[pos].unit = cmd; + g_smpt_log[pos].buf = rts_addr; + g_smpt_log[pos].blk = cmd_list; + } + pos++; + if(pos >= LEN_SMPT_LOG) { + pos = 0; + } + g_smpt_log_pos = pos; } -void -do_c70d(word32 arg0) -{ - int cmd; - int cmd_list_lo, cmd_list_mid, cmd_list_hi; - int rts_lo, rts_hi; - word32 rts_addr; - word32 cmd_list; - int unit; - int param_cnt; - int status_ptr_lo, status_ptr_mid, status_ptr_hi; - int buf_ptr_lo, buf_ptr_hi; - int buf_ptr; - int block_lo, block_mid, block_hi; - int block; - word32 status_ptr; - int status_code; - int ctl_ptr_lo, ctl_ptr_hi; - int ctl_ptr; - int ctl_code; - int mask; - int stat_val; - int size; - int ret; - int ext; - int i; +void do_c70d(word32 arg0) { + int cmd; + int cmd_list_lo, cmd_list_mid, cmd_list_hi; + int rts_lo, rts_hi; + word32 rts_addr; + word32 cmd_list; + int unit; + int param_cnt; + int status_ptr_lo, status_ptr_mid, status_ptr_hi; + int buf_ptr_lo, buf_ptr_hi; + int buf_ptr; + int block_lo, block_mid, block_hi; + int block; + word32 status_ptr; + int status_code; + int ctl_ptr_lo, ctl_ptr_hi; + int ctl_ptr; + int ctl_code; + int mask; + int stat_val; + int size; + int ret; + int ext; + int i; - set_memory_c(0x7f8, 0xc7, 0); + set_memory_c(0x7f8, 0xc7, 0); - if((engine.psr & 0x100) == 0) { - disk_printf("c70d called in native mode!\n"); - if((engine.psr & 0x30) != 0x30) { - halt_printf("c70d called native, psr: %03x!\n", - engine.psr); - } - } + if((engine.psr & 0x100) == 0) { + disk_printf("c70d called in native mode!\n"); + if((engine.psr & 0x30) != 0x30) { + halt_printf("c70d called native, psr: %03x!\n", + engine.psr); + } + } - engine.stack = ((engine.stack + 1) & 0xff) + 0x100; - rts_lo = get_memory_c(engine.stack, 0); - engine.stack = ((engine.stack + 1) & 0xff) + 0x100; - rts_hi = get_memory_c(engine.stack, 0); - rts_addr = (rts_lo + (256*rts_hi) + 1) & 0xffff; - disk_printf("rts_addr: %04x\n", rts_addr); + engine.stack = ((engine.stack + 1) & 0xff) + 0x100; + rts_lo = get_memory_c(engine.stack, 0); + engine.stack = ((engine.stack + 1) & 0xff) + 0x100; + rts_hi = get_memory_c(engine.stack, 0); + rts_addr = (rts_lo + (256*rts_hi) + 1) & 0xffff; + disk_printf("rts_addr: %04x\n", rts_addr); - cmd = get_memory_c(rts_addr, 0); - cmd_list_lo = get_memory_c((rts_addr + 1) & 0xffff, 0); - cmd_list_mid = get_memory_c((rts_addr + 2) & 0xffff, 0); - cmd_list_hi = 0; - mask = 0xffff; - if(cmd & 0x40) { - /* extended */ - mask = 0xffffff; - cmd_list_hi = get_memory_c((rts_addr + 3) & 0xffff, 0); - } + cmd = get_memory_c(rts_addr, 0); + cmd_list_lo = get_memory_c((rts_addr + 1) & 0xffff, 0); + cmd_list_mid = get_memory_c((rts_addr + 2) & 0xffff, 0); + cmd_list_hi = 0; + mask = 0xffff; + if(cmd & 0x40) { + /* extended */ + mask = 0xffffff; + cmd_list_hi = get_memory_c((rts_addr + 3) & 0xffff, 0); + } - cmd_list = cmd_list_lo + (256*cmd_list_mid) + (65536*cmd_list_hi); + cmd_list = cmd_list_lo + (256*cmd_list_mid) + (65536*cmd_list_hi); - disk_printf("cmd: %02x, cmd_list: %06x\n", cmd, cmd_list); - param_cnt = get_memory_c(cmd_list, 0); + disk_printf("cmd: %02x, cmd_list: %06x\n", cmd, cmd_list); + param_cnt = get_memory_c(cmd_list, 0); - ext = 0; - if(cmd & 0x40) { - ext = 2; - } + ext = 0; + if(cmd & 0x40) { + ext = 2; + } - smartport_log(0xc70d, cmd, rts_addr, cmd_list); + smartport_log(0xc70d, cmd, rts_addr, cmd_list); - switch(cmd & 0x3f) { - case 0x00: /* Status == 0x00 and 0x40 */ - if(param_cnt != 3) { - disk_printf("param_cnt %d is != 3!\n", param_cnt); - exit(8); - } - unit = get_memory_c((cmd_list+1) & mask, 0); - status_ptr_lo = get_memory_c((cmd_list+2) & mask, 0); - status_ptr_mid = get_memory_c((cmd_list+3) & mask, 0); - status_ptr_hi = 0; - if(cmd & 0x40) { - status_ptr_hi = get_memory_c((cmd_list+4) & mask, 0); - } + switch(cmd & 0x3f) { + case 0x00: /* Status == 0x00 and 0x40 */ + if(param_cnt != 3) { + disk_printf("param_cnt %d is != 3!\n", param_cnt); + exit(8); + } + unit = get_memory_c((cmd_list+1) & mask, 0); + status_ptr_lo = get_memory_c((cmd_list+2) & mask, 0); + status_ptr_mid = get_memory_c((cmd_list+3) & mask, 0); + status_ptr_hi = 0; + if(cmd & 0x40) { + status_ptr_hi = get_memory_c((cmd_list+4) & mask, 0); + } - status_ptr = status_ptr_lo + (256*status_ptr_mid) + - (65536*status_ptr_hi); - if(cmd & 0x40) { - status_code = get_memory_c((cmd_list+6) & mask, 0); - } else { - status_code = get_memory_c((cmd_list+4) & mask, 0); - } + status_ptr = status_ptr_lo + (256*status_ptr_mid) + + (65536*status_ptr_hi); + if(cmd & 0x40) { + status_code = get_memory_c((cmd_list+6) & mask, 0); + } else { + status_code = get_memory_c((cmd_list+4) & mask, 0); + } - smartport_log(0, unit, status_ptr, status_code); + smartport_log(0, unit, status_ptr, status_code); - disk_printf("unit: %02x, status_ptr: %06x, code: %02x\n", - unit, status_ptr, status_code); - if(unit == 0 && status_code == 0) { - /* Smartport driver status */ - /* see technotes/smpt/tn-smpt-002 */ - set_memory_c(status_ptr, g_highest_smartport_unit+1, 0); - set_memory_c(status_ptr+1, 0xff, 0); /* interrupt stat*/ - set_memory16_c(status_ptr+2, 0x0002, 0); /* vendor id */ - set_memory16_c(status_ptr+4, 0x1000, 0); /* version */ - set_memory16_c(status_ptr+6, 0x0000, 0); + disk_printf("unit: %02x, status_ptr: %06x, code: %02x\n", + unit, status_ptr, status_code); + if(unit == 0 && status_code == 0) { + /* Smartport driver status */ + /* see technotes/smpt/tn-smpt-002 */ + set_memory_c(status_ptr, g_highest_smartport_unit+1, 0); + set_memory_c(status_ptr+1, 0xff, 0); /* interrupt stat*/ + set_memory16_c(status_ptr+2, 0x0002, 0); /* vendor id */ + set_memory16_c(status_ptr+4, 0x1000, 0); /* version */ + set_memory16_c(status_ptr+6, 0x0000, 0); - engine.xreg = 8; - engine.yreg = 0; - engine.acc &= 0xff00; - engine.psr &= ~1; - engine.kpc = (rts_addr + 3 + ext) & mask; - return; - } else if(unit > 0 && status_code == 0) { - /* status for unit x */ - if(unit > MAX_C7_DISKS || (!iwm.smartport[unit-1].file)){ - stat_val = 0x80; - size = 0; - } else { - stat_val = 0xf8; - size = iwm.smartport[unit-1].image_size; - size = (size+511) / 512; - } - set_memory_c(status_ptr, stat_val, 0); - set_memory24_c(status_ptr +1, size, 0); - engine.xreg = 4; - if(cmd & 0x40) { - set_memory_c(status_ptr + 4, - (size >> 16) & 0xff, 0); - engine.xreg = 5; - } - engine.yreg = 0; - engine.acc &= 0xff00; - engine.psr &= ~1; - engine.kpc = (rts_addr + 3 + ext) & mask; + engine.xreg = 8; + engine.yreg = 0; + engine.acc &= 0xff00; + engine.psr &= ~1; + engine.kpc = (rts_addr + 3 + ext) & mask; + return; + } else if(unit > 0 && status_code == 0) { + /* status for unit x */ + if(unit > MAX_C7_DISKS || (!iwm.smartport[unit-1].file)) { + stat_val = 0x80; + size = 0; + } else { + stat_val = 0xf8; + size = iwm.smartport[unit-1].image_size; + size = (size+511) / 512; + } + set_memory_c(status_ptr, stat_val, 0); + set_memory24_c(status_ptr +1, size, 0); + engine.xreg = 4; + if(cmd & 0x40) { + set_memory_c(status_ptr + 4, + (size >> 16) & 0xff, 0); + engine.xreg = 5; + } + engine.yreg = 0; + engine.acc &= 0xff00; + engine.psr &= ~1; + engine.kpc = (rts_addr + 3 + ext) & mask; - disk_printf("just finished unit %d, stat 0\n", unit); - return; - } else if(status_code == 3) { - if(unit > MAX_C7_DISKS || (!iwm.smartport[unit-1].file)){ - stat_val = 0x80; - size = 0; - } else { - stat_val = 0xf8; - size = iwm.smartport[unit-1].image_size; - size = (size+511) / 512; - } - if(cmd & 0x40) { - disk_printf("extended for stat_code 3!\n"); - } - /* DIB for unit 1 */ - set_memory_c(status_ptr, stat_val, 0); - set_memory24_c(status_ptr +1, size, 0); - if(cmd & 0x40) { - set_memory_c(status_ptr + 4, - (size >> 24) & 0xff, 0); - status_ptr++; - } - set_memory_c(status_ptr +4, 4, 0); - for(i = 5; i < 21; i++) { - set_memory_c(status_ptr +i, 0x20, 0); - } - set_memory_c(status_ptr +5, 'K', 0); - set_memory_c(status_ptr +6, 'E', 0); - set_memory_c(status_ptr +7, 'G', 0); - set_memory_c(status_ptr +8, 'S', 0); + disk_printf("just finished unit %d, stat 0\n", unit); + return; + } else if(status_code == 3) { + if(unit > MAX_C7_DISKS || (!iwm.smartport[unit-1].file)) { + stat_val = 0x80; + size = 0; + } else { + stat_val = 0xf8; + size = iwm.smartport[unit-1].image_size; + size = (size+511) / 512; + } + if(cmd & 0x40) { + disk_printf("extended for stat_code 3!\n"); + } + /* DIB for unit 1 */ + set_memory_c(status_ptr, stat_val, 0); + set_memory24_c(status_ptr +1, size, 0); + if(cmd & 0x40) { + set_memory_c(status_ptr + 4, + (size >> 24) & 0xff, 0); + status_ptr++; + } + set_memory_c(status_ptr +4, 4, 0); + for(i = 5; i < 21; i++) { + set_memory_c(status_ptr +i, 0x20, 0); + } + set_memory_c(status_ptr +5, 'K', 0); + set_memory_c(status_ptr +6, 'E', 0); + set_memory_c(status_ptr +7, 'G', 0); + set_memory_c(status_ptr +8, 'S', 0); - /* hard disk supporting extended calls */ - set_memory16_c(status_ptr + 21, 0xa002, 0); - set_memory16_c(status_ptr + 23, 0x0000, 0); + /* hard disk supporting extended calls */ + set_memory16_c(status_ptr + 21, 0xa002, 0); + set_memory16_c(status_ptr + 23, 0x0000, 0); - if(cmd & 0x40) { - engine.xreg = 26; - } else { - engine.xreg = 25; - } - engine.yreg = 0; - engine.acc &= 0xff00; - engine.psr &= ~1; - engine.kpc = (rts_addr + 3 + ext) & 0xffff; + if(cmd & 0x40) { + engine.xreg = 26; + } else { + engine.xreg = 25; + } + engine.yreg = 0; + engine.acc &= 0xff00; + engine.psr &= ~1; + engine.kpc = (rts_addr + 3 + ext) & 0xffff; - disk_printf("Just finished unit %d, stat 3\n", unit); - if(unit == 0 || unit > MAX_C7_DISKS) { - engine.acc |= 0x28; - engine.psr |= 1; - } - return; - } - printf("cmd: 00, unknown unit/status code!\n"); - break; - case 0x01: /* Read Block == 0x01 and 0x41 */ - if(param_cnt != 3) { - halt_printf("param_cnt %d is != 3!\n", param_cnt); - return; - } - unit = get_memory_c((cmd_list+1) & mask, 0); - buf_ptr_lo = get_memory_c((cmd_list+2) & mask, 0); - buf_ptr_hi = get_memory_c((cmd_list+3) & mask, 0); + disk_printf("Just finished unit %d, stat 3\n", unit); + if(unit == 0 || unit > MAX_C7_DISKS) { + engine.acc |= 0x28; + engine.psr |= 1; + } + return; + } + printf("cmd: 00, unknown unit/status code!\n"); + break; + case 0x01: /* Read Block == 0x01 and 0x41 */ + if(param_cnt != 3) { + halt_printf("param_cnt %d is != 3!\n", param_cnt); + return; + } + unit = get_memory_c((cmd_list+1) & mask, 0); + buf_ptr_lo = get_memory_c((cmd_list+2) & mask, 0); + buf_ptr_hi = get_memory_c((cmd_list+3) & mask, 0); - buf_ptr = buf_ptr_lo + (256*buf_ptr_hi); - if(cmd & 0x40) { - buf_ptr_lo = get_memory_c((cmd_list+4) & mask, 0); - buf_ptr_hi = get_memory_c((cmd_list+5) & mask, 0); - buf_ptr += ((buf_ptr_hi*256) + buf_ptr_lo)*65536; - cmd_list += 2; - } - block_lo = get_memory_c((cmd_list+4) & mask, 0); - block_mid = get_memory_c((cmd_list+5) & mask, 0); - block_hi = get_memory_c((cmd_list+6) & mask, 0); - block = ((block_hi*256) + block_mid)*256 + block_lo; - disk_printf("smartport read unit %d of block %04x into %04x\n", - unit, block, buf_ptr); - if(unit < 1 || unit > MAX_C7_DISKS) { - halt_printf("Unknown unit #: %d\n", unit); - } + buf_ptr = buf_ptr_lo + (256*buf_ptr_hi); + if(cmd & 0x40) { + buf_ptr_lo = get_memory_c((cmd_list+4) & mask, 0); + buf_ptr_hi = get_memory_c((cmd_list+5) & mask, 0); + buf_ptr += ((buf_ptr_hi*256) + buf_ptr_lo)*65536; + cmd_list += 2; + } + block_lo = get_memory_c((cmd_list+4) & mask, 0); + block_mid = get_memory_c((cmd_list+5) & mask, 0); + block_hi = get_memory_c((cmd_list+6) & mask, 0); + block = ((block_hi*256) + block_mid)*256 + block_lo; + disk_printf("smartport read unit %d of block %04x into %04x\n", + unit, block, buf_ptr); + if(unit < 1 || unit > MAX_C7_DISKS) { + halt_printf("Unknown unit #: %d\n", unit); + } - smartport_log(0, unit - 1, buf_ptr, block); + smartport_log(0, unit - 1, buf_ptr, block); - ret = do_read_c7(unit - 1, buf_ptr, block); - engine.xreg = 0; - engine.yreg = 2; - engine.acc = (engine.acc & 0xff00) | (ret & 0xff); - engine.psr &= ~1; - if(ret != 0) { - engine.psr |= 1; - } - engine.kpc = (rts_addr + 3 + ext) & 0xffff; + ret = do_read_c7(unit - 1, buf_ptr, block); + engine.xreg = 0; + engine.yreg = 2; + engine.acc = (engine.acc & 0xff00) | (ret & 0xff); + engine.psr &= ~1; + if(ret != 0) { + engine.psr |= 1; + } + engine.kpc = (rts_addr + 3 + ext) & 0xffff; - return; - break; - case 0x02: /* Write Block == 0x02 and 0x42 */ - if(param_cnt != 3) { - halt_printf("param_cnt %d is != 3!\n", param_cnt); - return; - } - unit = get_memory_c((cmd_list+1) & mask, 0); - buf_ptr_lo = get_memory_c((cmd_list+2) & mask, 0); - buf_ptr_hi = get_memory_c((cmd_list+3) & mask, 0); + return; + break; + case 0x02: /* Write Block == 0x02 and 0x42 */ + if(param_cnt != 3) { + halt_printf("param_cnt %d is != 3!\n", param_cnt); + return; + } + unit = get_memory_c((cmd_list+1) & mask, 0); + buf_ptr_lo = get_memory_c((cmd_list+2) & mask, 0); + buf_ptr_hi = get_memory_c((cmd_list+3) & mask, 0); - buf_ptr = buf_ptr_lo + (256*buf_ptr_hi); - if(cmd & 0x40) { - buf_ptr_lo = get_memory_c((cmd_list+4) & mask, 0); - buf_ptr_hi = get_memory_c((cmd_list+5) & mask, 0); - buf_ptr += ((buf_ptr_hi*256) + buf_ptr_lo)*65536; - cmd_list += 2; - } - block_lo = get_memory_c((cmd_list+4) & mask, 0); - block_mid = get_memory_c((cmd_list+5) & mask, 0); - block_hi = get_memory_c((cmd_list+6) & mask, 0); - block = ((block_hi*256) + block_mid)*256 + block_lo; - disk_printf("smartport write unit %d of block %04x from %04x\n", - unit, block, buf_ptr); - if(unit < 1 || unit > MAX_C7_DISKS) { - halt_printf("Unknown unit #: %d\n", unit); - } + buf_ptr = buf_ptr_lo + (256*buf_ptr_hi); + if(cmd & 0x40) { + buf_ptr_lo = get_memory_c((cmd_list+4) & mask, 0); + buf_ptr_hi = get_memory_c((cmd_list+5) & mask, 0); + buf_ptr += ((buf_ptr_hi*256) + buf_ptr_lo)*65536; + cmd_list += 2; + } + block_lo = get_memory_c((cmd_list+4) & mask, 0); + block_mid = get_memory_c((cmd_list+5) & mask, 0); + block_hi = get_memory_c((cmd_list+6) & mask, 0); + block = ((block_hi*256) + block_mid)*256 + block_lo; + disk_printf("smartport write unit %d of block %04x from %04x\n", + unit, block, buf_ptr); + if(unit < 1 || unit > MAX_C7_DISKS) { + halt_printf("Unknown unit #: %d\n", unit); + } - smartport_log(0, unit - 1, buf_ptr, block); + smartport_log(0, unit - 1, buf_ptr, block); - ret = do_write_c7(unit - 1, buf_ptr, block); - engine.xreg = 0; - engine.yreg = 2; - engine.acc = (engine.acc & 0xff00) | (ret & 0xff); - engine.psr &= ~1; - if(ret != 0) { - engine.psr |= 1; - } - engine.kpc = (rts_addr + 3 + ext) & 0xffff; + ret = do_write_c7(unit - 1, buf_ptr, block); + engine.xreg = 0; + engine.yreg = 2; + engine.acc = (engine.acc & 0xff00) | (ret & 0xff); + engine.psr &= ~1; + if(ret != 0) { + engine.psr |= 1; + } + engine.kpc = (rts_addr + 3 + ext) & 0xffff; - HALT_ON(HALT_ON_C70D_WRITES, "c70d Write done\n"); - return; - break; - case 0x03: /* Format == 0x03 and 0x43 */ - if(param_cnt != 1) { - halt_printf("param_cnt %d is != 1!\n", param_cnt); - return; - } - unit = get_memory_c((cmd_list+1) & mask, 0); + HALT_ON(HALT_ON_C70D_WRITES, "c70d Write done\n"); + return; + break; + case 0x03: /* Format == 0x03 and 0x43 */ + if(param_cnt != 1) { + halt_printf("param_cnt %d is != 1!\n", param_cnt); + return; + } + unit = get_memory_c((cmd_list+1) & mask, 0); - if(unit < 1 || unit > MAX_C7_DISKS) { - halt_printf("Unknown unit #: %d\n", unit); - } + if(unit < 1 || unit > MAX_C7_DISKS) { + halt_printf("Unknown unit #: %d\n", unit); + } - smartport_log(0, unit - 1, 0, 0); + smartport_log(0, unit - 1, 0, 0); - ret = do_format_c7(unit - 1); - engine.xreg = 0; - engine.yreg = 2; - engine.acc = (engine.acc & 0xff00) | (ret & 0xff); - engine.psr &= ~1; - if(ret != 0) { - engine.psr |= 1; - } - engine.kpc = (rts_addr + 3 + ext) & 0xffff; + ret = do_format_c7(unit - 1); + engine.xreg = 0; + engine.yreg = 2; + engine.acc = (engine.acc & 0xff00) | (ret & 0xff); + engine.psr &= ~1; + if(ret != 0) { + engine.psr |= 1; + } + engine.kpc = (rts_addr + 3 + ext) & 0xffff; - HALT_ON(HALT_ON_C70D_WRITES, "c70d Format done\n"); - return; - break; - case 0x04: /* Control == 0x04 and 0x44 */ - if(cmd == 0x44) { - halt_printf("smartport code 0x44 not supported\n"); - } - if(param_cnt != 3) { - halt_printf("param_cnt %d is != 3!\n", param_cnt); - return; - } - unit = get_memory_c((cmd_list+1) & mask, 0); - ctl_ptr_lo = get_memory_c((cmd_list+2) & mask, 0); - ctl_ptr_hi = get_memory_c((cmd_list+3) & mask, 0); - - ctl_ptr = (ctl_ptr_hi << 8) + ctl_ptr_lo; - if(cmd & 0x40) { - ctl_ptr_lo = get_memory_c((cmd_list+4) & mask, 0); - ctl_ptr_hi = get_memory_c((cmd_list+5) & mask, 0); - ctl_ptr += ((ctl_ptr_hi << 8) + ctl_ptr_lo) << 16; - cmd_list += 2; - } + HALT_ON(HALT_ON_C70D_WRITES, "c70d Format done\n"); + return; + break; + case 0x04: /* Control == 0x04 and 0x44 */ + if(cmd == 0x44) { + halt_printf("smartport code 0x44 not supported\n"); + } + if(param_cnt != 3) { + halt_printf("param_cnt %d is != 3!\n", param_cnt); + return; + } + unit = get_memory_c((cmd_list+1) & mask, 0); + ctl_ptr_lo = get_memory_c((cmd_list+2) & mask, 0); + ctl_ptr_hi = get_memory_c((cmd_list+3) & mask, 0); - ctl_code = get_memory_c((cmd_list +4) & mask, 0); + ctl_ptr = (ctl_ptr_hi << 8) + ctl_ptr_lo; + if(cmd & 0x40) { + ctl_ptr_lo = get_memory_c((cmd_list+4) & mask, 0); + ctl_ptr_hi = get_memory_c((cmd_list+5) & mask, 0); + ctl_ptr += ((ctl_ptr_hi << 8) + ctl_ptr_lo) << 16; + cmd_list += 2; + } - switch(ctl_code) { - case 0x00: - printf("Performing a reset on unit %d\n", unit); - break; - default: - halt_printf("control code: %02x unknown!\n", ctl_code); - } + ctl_code = get_memory_c((cmd_list +4) & mask, 0); - engine.xreg = 0; - engine.yreg = 2; - engine.acc &= 0xff00; - engine.psr &= ~1; - engine.kpc = (rts_addr + 3 + ext) & 0xffff; - return; - break; - default: /* Unknown command! */ - /* set acc = 1, and set carry, and set kpc */ - engine.xreg = (rts_addr) & 0xff; - engine.yreg = (rts_addr >> 8) & 0xff; - engine.acc = (engine.acc & 0xff00) + 0x01; - engine.psr |= 0x01; /* set carry */ - engine.kpc = (rts_addr + 3 + ext) & 0xffff; - if(cmd != 0x4a && cmd != 0x48) { - /* Finder does 0x4a call before formatting disk */ - /* Many things do 0x48 call to see online drives */ - halt_printf("Just did smtport cmd:%02x rts_addr:%04x, " - "cmdlst:%06x\n", cmd, rts_addr, cmd_list); - } - return; - } + switch(ctl_code) { + case 0x00: + printf("Performing a reset on unit %d\n", unit); + break; + default: + halt_printf("control code: %02x unknown!\n", ctl_code); + } - halt_printf("Unknown smtport cmd:%02x, cmd_list:%06x, rts_addr:%06x\n", - cmd, cmd_list, rts_addr); + engine.xreg = 0; + engine.yreg = 2; + engine.acc &= 0xff00; + engine.psr &= ~1; + engine.kpc = (rts_addr + 3 + ext) & 0xffff; + return; + break; + default: /* Unknown command! */ + /* set acc = 1, and set carry, and set kpc */ + engine.xreg = (rts_addr) & 0xff; + engine.yreg = (rts_addr >> 8) & 0xff; + engine.acc = (engine.acc & 0xff00) + 0x01; + engine.psr |= 0x01; /* set carry */ + engine.kpc = (rts_addr + 3 + ext) & 0xffff; + if(cmd != 0x4a && cmd != 0x48) { + /* Finder does 0x4a call before formatting disk */ + /* Many things do 0x48 call to see online drives */ + halt_printf("Just did smtport cmd:%02x rts_addr:%04x, " + "cmdlst:%06x\n", cmd, rts_addr, cmd_list); + } + return; + } + + halt_printf("Unknown smtport cmd:%02x, cmd_list:%06x, rts_addr:%06x\n", + cmd, cmd_list, rts_addr); } -void -do_c70a(word32 arg0) -{ - int cmd, unit; - int buf_lo, buf_hi; - int blk_lo, blk_hi; - int blk, buf; - int prodos_unit; - int size; - int ret; +void do_c70a(word32 arg0) { + int cmd, unit; + int buf_lo, buf_hi; + int blk_lo, blk_hi; + int blk, buf; + int prodos_unit; + int size; + int ret; - set_memory_c(0x7f8, 0xc7, 0); + set_memory_c(0x7f8, 0xc7, 0); - cmd = get_memory_c((engine.direct + 0x42) & 0xffff, 0); - prodos_unit = get_memory_c((engine.direct + 0x43) & 0xffff, 0); - buf_lo = get_memory_c((engine.direct + 0x44) & 0xffff, 0); - buf_hi = get_memory_c((engine.direct + 0x45) & 0xffff, 0); - blk_lo = get_memory_c((engine.direct + 0x46) & 0xffff, 0); - blk_hi = get_memory_c((engine.direct + 0x47) & 0xffff, 0); + cmd = get_memory_c((engine.direct + 0x42) & 0xffff, 0); + prodos_unit = get_memory_c((engine.direct + 0x43) & 0xffff, 0); + buf_lo = get_memory_c((engine.direct + 0x44) & 0xffff, 0); + buf_hi = get_memory_c((engine.direct + 0x45) & 0xffff, 0); + blk_lo = get_memory_c((engine.direct + 0x46) & 0xffff, 0); + blk_hi = get_memory_c((engine.direct + 0x47) & 0xffff, 0); - blk = (blk_hi << 8) + blk_lo; - buf = (buf_hi << 8) + buf_lo; - disk_printf("cmd: %02x, pro_unit: %02x, buf: %04x, blk: %04x\n", - cmd, prodos_unit, buf, blk); + blk = (blk_hi << 8) + blk_lo; + buf = (buf_hi << 8) + buf_lo; + disk_printf("cmd: %02x, pro_unit: %02x, buf: %04x, blk: %04x\n", + cmd, prodos_unit, buf, blk); - if((prodos_unit & 0x7f) == 0x70) { - unit = 0 + (prodos_unit >> 7); - } else if((prodos_unit & 0x7f) == 0x40) { - unit = 2 + (prodos_unit >> 7); - } else { - halt_printf("Unknown prodos_unit: %d\n", prodos_unit); - return; - } + if((prodos_unit & 0x7f) == 0x70) { + unit = 0 + (prodos_unit >> 7); + } else if((prodos_unit & 0x7f) == 0x40) { + unit = 2 + (prodos_unit >> 7); + } else { + halt_printf("Unknown prodos_unit: %d\n", prodos_unit); + return; + } - smartport_log(0xc70a, cmd, blk, buf); + smartport_log(0xc70a, cmd, blk, buf); - engine.psr &= ~1; /* clear carry */ - if(g_rom_version >= 3) { - engine.kpc = 0xc764; - } else { - engine.kpc = 0xc765; - } + engine.psr &= ~1; /* clear carry */ + if(g_rom_version >= 3) { + engine.kpc = 0xc764; + } else { + engine.kpc = 0xc765; + } - ret = 0x27; /* I/O error */ - if(cmd == 0x00) { - size = iwm.smartport[unit].image_size; - size = (size+511) / 512; + ret = 0x27; /* I/O error */ + if(cmd == 0x00) { + size = iwm.smartport[unit].image_size; + size = (size+511) / 512; - smartport_log(0, unit, size, 0); + smartport_log(0, unit, size, 0); - ret = 0; - engine.xreg = size & 0xff; - engine.yreg = size >> 8; - } else if(cmd == 0x01) { - smartport_log(0, unit, buf, blk); - ret = do_read_c7(unit, buf, blk); - } else if(cmd == 0x02) { - smartport_log(0, unit, buf, blk); - ret = do_write_c7(unit, buf, blk); - } else if(cmd == 0x03) { /* format */ - smartport_log(0, unit, buf, blk); - ret = do_format_c7(unit); - } + ret = 0; + engine.xreg = size & 0xff; + engine.yreg = size >> 8; + } else if(cmd == 0x01) { + smartport_log(0, unit, buf, blk); + ret = do_read_c7(unit, buf, blk); + } else if(cmd == 0x02) { + smartport_log(0, unit, buf, blk); + ret = do_write_c7(unit, buf, blk); + } else if(cmd == 0x03) { /* format */ + smartport_log(0, unit, buf, blk); + ret = do_format_c7(unit); + } - engine.acc = (engine.acc & 0xff00) | (ret & 0xff); - if(ret != 0) { - engine.psr |= 1; - } - return; + engine.acc = (engine.acc & 0xff00) | (ret & 0xff); + if(ret != 0) { + engine.psr |= 1; + } + return; } -int -do_read_c7(int unit_num, word32 buf, int blk) -{ - byte local_buf[0x200]; - register word32 start_time; - register word32 end_time; - word32 val; - FILE *file; - int len; - int image_start; - int image_size; - int ret; - int i; +int do_read_c7(int unit_num, word32 buf, int blk) { + byte local_buf[0x200]; + register word32 start_time; + register word32 end_time; + word32 val; + FILE *file; + int len; + int image_start; + int image_size; + int ret; + int i; - if(unit_num < 0 || unit_num > MAX_C7_DISKS) { - halt_printf("do_read_c7: unit_num: %d\n", unit_num); - smartport_error(); - return 0x28; - } + if(unit_num < 0 || unit_num > MAX_C7_DISKS) { + halt_printf("do_read_c7: unit_num: %d\n", unit_num); + smartport_error(); + return 0x28; + } - file = iwm.smartport[unit_num].file; - image_start = iwm.smartport[unit_num].image_start; - image_size = iwm.smartport[unit_num].image_size; - if(!file) { - glog("Trying file mapped to $C7xx, but it's null!"); + file = iwm.smartport[unit_num].file; + image_start = iwm.smartport[unit_num].image_start; + image_size = iwm.smartport[unit_num].image_size; + if(!file) { + glog("Trying file mapped to $C7xx, but it's null!"); #if 0 - if(blk != 2 && blk != 0) { - /* don't print error if only reading directory */ - smartport_error(); - halt_printf("Read unit:%02x blk:%04x\n", unit_num, blk); - } + if(blk != 2 && blk != 0) { + /* don't print error if only reading directory */ + smartport_error(); + halt_printf("Read unit:%02x blk:%04x\n", unit_num, blk); + } #endif - return 0x2f; - } + return 0x2f; + } - ret = fseek(file, image_start + blk*0x200, SEEK_SET); - if(ret != 0) { - halt_printf("fseek errno: %d\n", errno); - smartport_error(); - return 0x27; - } + ret = fseek(file, image_start + blk*0x200, SEEK_SET); + if(ret != 0) { + halt_printf("fseek errno: %d\n", errno); + smartport_error(); + return 0x27; + } - if(image_start + blk*0x200 > image_start + image_size) { - halt_printf("Tried to read from pos %08x on disk, (blk:%04x)\n", - image_start + blk*0x200, blk); - smartport_error(); - return 0x27; - } + if(image_start + blk*0x200 > image_start + image_size) { + halt_printf("Tried to read from pos %08x on disk, (blk:%04x)\n", + image_start + blk*0x200, blk); + smartport_error(); + return 0x27; + } - len = fread(&local_buf[0], 1, 0x200, file); - if(len != 0x200) { - printf("fread returned %08x, errno:%d, blk:%04x, unit: %02x\n", - len, errno, blk, unit_num); - halt_printf("name: %s\n", iwm.smartport[unit_num].name_ptr); - smartport_error(); - return 0x27; - } + len = fread(&local_buf[0], 1, 0x200, file); + if(len != 0x200) { + printf("fread returned %08x, errno:%d, blk:%04x, unit: %02x\n", + len, errno, blk, unit_num); + halt_printf("name: %s\n", iwm.smartport[unit_num].name_ptr); + smartport_error(); + return 0x27; + } - g_io_amt += 0x200; + g_io_amt += 0x200; - if(buf >= 0xfc0000) { - disk_printf("reading into ROM, just returning\n"); - return 0; - } + if(buf >= 0xfc0000) { + disk_printf("reading into ROM, just returning\n"); + return 0; + } - GET_ITIMER(start_time); + GET_ITIMER(start_time); - for(i = 0; i < 0x200; i += 2) { - val = (local_buf[i+1] << 8) + local_buf[i]; - set_memory16_c(buf + i, val, 0); - } + for(i = 0; i < 0x200; i += 2) { + val = (local_buf[i+1] << 8) + local_buf[i]; + set_memory16_c(buf + i, val, 0); + } - GET_ITIMER(end_time); + GET_ITIMER(end_time); - g_cycs_in_io_read += (end_time - start_time); + g_cycs_in_io_read += (end_time - start_time); - return 0; + return 0; } -int -do_write_c7(int unit_num, word32 buf, int blk) -{ - word32 local_buf[0x200/4]; - Disk *dsk; - word32 *ptr; - word32 val1, val2; - word32 val; - FILE *file; - int len; - int ret; - int image_start; - int image_size; - int i; +int do_write_c7(int unit_num, word32 buf, int blk) { + word32 local_buf[0x200/4]; + Disk *dsk; + word32 *ptr; + word32 val1, val2; + word32 val; + FILE *file; + int len; + int ret; + int image_start; + int image_size; + int i; - if(unit_num < 0 || unit_num > MAX_C7_DISKS) { - halt_printf("do_write_c7: unit_num: %d\n", unit_num); - smartport_error(); - return 0x28; - } + if(unit_num < 0 || unit_num > MAX_C7_DISKS) { + halt_printf("do_write_c7: unit_num: %d\n", unit_num); + smartport_error(); + return 0x28; + } - dsk = &(iwm.smartport[unit_num]); - file = dsk->file; - image_start = dsk->image_start; - image_size = dsk->image_size; - if(!file) { - halt_printf("c7_file is null!\n"); - smartport_error(); - return 0x28; - } + dsk = &(iwm.smartport[unit_num]); + file = dsk->file; + image_start = dsk->image_start; + image_size = dsk->image_size; + if(!file) { + halt_printf("c7_file is null!\n"); + smartport_error(); + return 0x28; + } - ptr = &(local_buf[0]); - for(i = 0; i < 0x200; i += 4) { - val1 = get_memory16_c(buf + i, 0); - val2 = get_memory16_c(buf + i + 2, 0); - /* reorder the little-endian bytes to be big-endian */ + ptr = &(local_buf[0]); + for(i = 0; i < 0x200; i += 4) { + val1 = get_memory16_c(buf + i, 0); + val2 = get_memory16_c(buf + i + 2, 0); + /* reorder the little-endian bytes to be big-endian */ #if defined(GSPLUS_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) // OSX needs to calculate endianness mid-compilation, can't be passed on compile command - val = (val2 << 16) + val1; + val = (val2 << 16) + val1; #else - val = (val1 << 24) + ((val1 << 8) & 0xff0000) + - ((val2 << 8) & 0xff00) + (val2 >> 8); + val = (val1 << 24) + ((val1 << 8) & 0xff0000) + + ((val2 << 8) & 0xff00) + (val2 >> 8); #endif - *ptr++ = val; - } + *ptr++ = val; + } - ret = fseek(file, image_start + blk*0x200, SEEK_SET); - if(ret != 0) { - halt_printf("fseek errno: %d\n", errno); - smartport_error(); - return 0x27; - } + ret = fseek(file, image_start + blk*0x200, SEEK_SET); + if(ret != 0) { + halt_printf("fseek errno: %d\n", errno); + smartport_error(); + return 0x27; + } - if(image_start + blk*0x200 > image_start + image_size) { - halt_printf("Tried to write to %08x\n", ret); - smartport_error(); - return 0x27; - } + if(image_start + blk*0x200 > image_start + image_size) { + halt_printf("Tried to write to %08x\n", ret); + smartport_error(); + return 0x27; + } - if(dsk->write_prot) { - printf("Write, but %s is write protected!\n", dsk->name_ptr); - return 0x2b; - } + if(dsk->write_prot) { + printf("Write, but %s is write protected!\n", dsk->name_ptr); + return 0x2b; + } - if(dsk->write_through_to_unix == 0) { - halt_printf("Write to %s, but not wr_thru!\n", dsk->name_ptr); - return 0x00; - } + if(dsk->write_through_to_unix == 0) { + halt_printf("Write to %s, but not wr_thru!\n", dsk->name_ptr); + return 0x00; + } - len = fwrite((byte *)&local_buf[0], 1, 0x200, file); - if(len != 0x200) { - halt_printf("fwrite ret %08x bytes, errno: %d\n", len, errno); - smartport_error(); - return 0x27; - } + len = fwrite((byte *)&local_buf[0], 1, 0x200, file); + if(len != 0x200) { + halt_printf("fwrite ret %08x bytes, errno: %d\n", len, errno); + smartport_error(); + return 0x27; + } - g_io_amt += 0x200; + g_io_amt += 0x200; - return 0; + return 0; } -int -do_format_c7(int unit_num) -{ - byte local_buf[0x1000]; - Disk *dsk; - FILE *file; - int len; - int ret; - int sum; - int total; - int max; - int image_start; - int image_size; - int i; +int do_format_c7(int unit_num) { + byte local_buf[0x1000]; + Disk *dsk; + FILE *file; + int len; + int ret; + int sum; + int total; + int max; + int image_start; + int image_size; + int i; - if(unit_num < 0 || unit_num > MAX_C7_DISKS) { - halt_printf("do_format_c7: unit_num: %d\n", unit_num); - smartport_error(); - return 0x28; - } + if(unit_num < 0 || unit_num > MAX_C7_DISKS) { + halt_printf("do_format_c7: unit_num: %d\n", unit_num); + smartport_error(); + return 0x28; + } - dsk = &(iwm.smartport[unit_num]); - file = dsk->file; - image_start = dsk->image_start; - image_size = dsk->image_size; - if(!file) { - halt_printf("c7_file is null!\n"); - smartport_error(); - return 0x28; - } + dsk = &(iwm.smartport[unit_num]); + file = dsk->file; + image_start = dsk->image_start; + image_size = dsk->image_size; + if(!file) { + halt_printf("c7_file is null!\n"); + smartport_error(); + return 0x28; + } - for(i = 0; i < 0x1000; i++) { - local_buf[i] = 0; - } + for(i = 0; i < 0x1000; i++) { + local_buf[i] = 0; + } - ret = fseek(file, image_start, SEEK_SET); - if(ret != 0) { - halt_printf("fseek errno: %d\n", errno); - smartport_error(); - return 0x27; - } + ret = fseek(file, image_start, SEEK_SET); + if(ret != 0) { + halt_printf("fseek errno: %d\n", errno); + smartport_error(); + return 0x27; + } - if(dsk->write_prot) { - printf("Format, but %s is write protected!\n", dsk->name_ptr); - return 0x2b; - } + if(dsk->write_prot) { + printf("Format, but %s is write protected!\n", dsk->name_ptr); + return 0x2b; + } - if(dsk->write_through_to_unix == 0) { - printf("Format of %s ignored\n", dsk->name_ptr); - return 0x00; - } + if(dsk->write_through_to_unix == 0) { + printf("Format of %s ignored\n", dsk->name_ptr); + return 0x00; + } - sum = 0; - total = image_size; + sum = 0; + total = image_size; - while(sum < total) { - max = MIN(0x1000, total-sum); - len = fwrite(&local_buf[0], 1, max, file); - if(len != max) { - halt_printf("write ret %08x, errno:%d\n", len, errno); - smartport_error(); - return 0x27; - } - sum += len; - } + while(sum < total) { + max = MIN(0x1000, total-sum); + len = fwrite(&local_buf[0], 1, max, file); + if(len != max) { + halt_printf("write ret %08x, errno:%d\n", len, errno); + smartport_error(); + return 0x27; + } + sum += len; + } - return 0; + return 0; } @@ -749,30 +735,28 @@ extern byte* g_bram_ptr; extern byte g_temp_boot_slot; extern byte g_orig_boot_slot; extern int g_config_gsplus_update_needed; -void -do_c700(word32 ret) -{ - disk_printf("do_c700 called, ret: %08x\n", ret); - if (g_temp_boot_slot != 254) { - // Booting from slot 7 - now is a good time to turn off the temp boot slot, if it was on. - g_temp_boot_slot = 254; - g_bram_ptr[40] = g_orig_boot_slot; - clk_calculate_bram_checksum(); - g_config_gsplus_update_needed = 1; - } - ret = do_read_c7(0, 0x800, 0); +void do_c700(word32 ret) { + disk_printf("do_c700 called, ret: %08x\n", ret); + if (g_temp_boot_slot != 254) { + // Booting from slot 7 - now is a good time to turn off the temp boot slot, if it was on. + g_temp_boot_slot = 254; + g_bram_ptr[40] = g_orig_boot_slot; + clk_calculate_bram_checksum(); + g_config_gsplus_update_needed = 1; + } + ret = do_read_c7(0, 0x800, 0); - set_memory_c(0x7f8, 7, 0); - set_memory16_c(0x42, 0x7001, 0); - set_memory16_c(0x44, 0x0800, 0); - set_memory16_c(0x46, 0x0000, 0); - engine.xreg = 0x70; - engine.kpc = 0x801; + set_memory_c(0x7f8, 7, 0); + set_memory16_c(0x42, 0x7001, 0); + set_memory16_c(0x44, 0x0800, 0); + set_memory16_c(0x46, 0x0000, 0); + engine.xreg = 0x70; + engine.kpc = 0x801; - if(ret != 0) { - glog("Failure reading boot disk in s7d1!"); - glog(" Press to enter config menu, or edit your config"); - glog(" file if you intended to use slot slot 7."); - engine.kpc = 0xff59; /* Jump to monitor, fix $36-$39 */ - } + if(ret != 0) { + glog("Failure reading boot disk in s7d1!"); + glog(" Press to enter config menu, or edit your config"); + glog(" file if you intended to use slot slot 7."); + engine.kpc = 0xff59; /* Jump to monitor, fix $36-$39 */ + } } diff --git a/src/sound.c b/src/sound.c index dc7c5da..2b113a2 100644 --- a/src/sound.c +++ b/src/sound.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include "sound.h" @@ -21,27 +21,27 @@ byte doc_ram[0x10000 + 16]; word32 doc_sound_ctl = 0; word32 doc_saved_val = 0; -int g_doc_num_osc_en = 1; -double g_dcycs_per_doc_update = 1.0; -double g_dupd_per_dcyc = 1.0; -double g_drecip_osc_en_plus_2 = 1.0 / (double)(1 + 2); +int g_doc_num_osc_en = 1; +double g_dcycs_per_doc_update = 1.0; +double g_dupd_per_dcyc = 1.0; +double g_drecip_osc_en_plus_2 = 1.0 / (double)(1 + 2); -int g_doc_saved_ctl = 0; -int g_queued_samps = 0; -int g_queued_nonsamps = 0; -int g_num_osc_interrupting = 0; +int g_doc_saved_ctl = 0; +int g_queued_samps = 0; +int g_queued_nonsamps = 0; +int g_num_osc_interrupting = 0; int g_sound_play_depth = 0; /* Workaround - gcc in cygwin wasn't defining _WIN32, substituted WIN_SOUND instead */ #if defined(HPUX) || defined(__linux__) || defined(WIN_SOUND) || defined(MAC) || defined(HAVE_SDL) -int g_audio_enable = -1; +int g_audio_enable = -1; #else # if defined(OSS) /* default to off for now */ -int g_audio_enable = 0; +int g_audio_enable = 0; # else /* Default to sound off */ -int g_audio_enable = 0; +int g_audio_enable = 0; # endif #endif @@ -53,319 +53,311 @@ word32 doc_reg_e0 = 0xff; void doc_write_ctl_reg(int osc, int val, double dsamps); void sound_write_sdl(int real_samps, int size); -int g_audio_rate = 0; -double g_daudio_rate = 0.0; -double g_drecip_audio_rate = 0.0; -double g_dsamps_per_dcyc = 0.0; -double g_dcycs_per_samp = 0.0; -float g_fsamps_per_dcyc = 0.0; +int g_audio_rate = 0; +double g_daudio_rate = 0.0; +double g_drecip_audio_rate = 0.0; +double g_dsamps_per_dcyc = 0.0; +double g_dcycs_per_samp = 0.0; +float g_fsamps_per_dcyc = 0.0; -int g_doc_vol = 2; +int g_doc_vol = 2; -#define MAX_C030_TIMES 18000 +#define MAX_C030_TIMES 18000 double g_last_sound_play_dsamp = 0.0; float c030_fsamps[MAX_C030_TIMES + 1]; int g_num_c030_fsamps = 0; -#define DOC_SCAN_RATE (DCYCS_28_MHZ/32.0) +#define DOC_SCAN_RATE (DCYCS_28_MHZ/32.0) -int g_pipe_fd[2] = { -1, -1 }; -int g_pipe2_fd[2] = { -1, -1 }; -word32 *g_sound_shm_addr = 0; -int g_sound_shm_pos = 0; +int g_pipe_fd[2] = { -1, -1 }; +int g_pipe2_fd[2] = { -1, -1 }; +word32 *g_sound_shm_addr = 0; +int g_sound_shm_pos = 0; -#define LEN_DOC_LOG 128 +#define LEN_DOC_LOG 128 STRUCT(Doc_log) { - char *msg; - int osc; - double dsamps; - double dtmp2; - int etc; - Doc_reg doc_reg; + char *msg; + int osc; + double dsamps; + double dtmp2; + int etc; + Doc_reg doc_reg; }; Doc_log g_doc_log[LEN_DOC_LOG]; -int g_doc_log_pos = 0; +int g_doc_log_pos = 0; #ifdef DO_DOC_LOG -# define DOC_LOG(a,b,c,d) doc_log_rout(a,b,c,d) +# define DOC_LOG(a,b,c,d) doc_log_rout(a,b,c,d) #else # define DOC_LOG(a,b,c,d) #endif -#define UPDATE_G_DCYCS_PER_DOC_UPDATE(osc_en) \ - g_dcycs_per_doc_update = (double)((osc_en + 2) * DCYCS_1_MHZ) / \ - DOC_SCAN_RATE; \ - g_dupd_per_dcyc = 1.0 / g_dcycs_per_doc_update; \ - g_drecip_osc_en_plus_2 = 1.0 / (double)(osc_en + 2); +#define UPDATE_G_DCYCS_PER_DOC_UPDATE(osc_en) \ + g_dcycs_per_doc_update = (double)((osc_en + 2) * DCYCS_1_MHZ) / \ + DOC_SCAN_RATE; \ + g_dupd_per_dcyc = 1.0 / g_dcycs_per_doc_update; \ + g_drecip_osc_en_plus_2 = 1.0 / (double)(osc_en + 2); -#define SND_PTR_SHIFT 14 -#define SND_PTR_SHIFT_DBL ((double)(1 << SND_PTR_SHIFT)) +#define SND_PTR_SHIFT 14 +#define SND_PTR_SHIFT_DBL ((double)(1 << SND_PTR_SHIFT)) -void -doc_log_rout(char *msg, int osc, double dsamps, int etc) -{ - int pos; +void doc_log_rout(char *msg, int osc, double dsamps, int etc) { + int pos; - pos = g_doc_log_pos; - g_doc_log[pos].msg = msg; - g_doc_log[pos].osc = osc; - g_doc_log[pos].dsamps = dsamps; - g_doc_log[pos].dtmp2 = g_last_sound_play_dsamp; - g_doc_log[pos].etc = etc; - if(osc >= 0 && osc < 32) { - g_doc_log[pos].doc_reg = g_doc_regs[osc]; - } - pos++; - if(pos >= LEN_DOC_LOG) { - pos = 0; - } + pos = g_doc_log_pos; + g_doc_log[pos].msg = msg; + g_doc_log[pos].osc = osc; + g_doc_log[pos].dsamps = dsamps; + g_doc_log[pos].dtmp2 = g_last_sound_play_dsamp; + g_doc_log[pos].etc = etc; + if(osc >= 0 && osc < 32) { + g_doc_log[pos].doc_reg = g_doc_regs[osc]; + } + pos++; + if(pos >= LEN_DOC_LOG) { + pos = 0; + } - doc_printf("log: %s, osc:%d dsamp:%f, etc:%d\n", msg, osc, dsamps, etc); + doc_printf("log: %s, osc:%d dsamp:%f, etc:%d\n", msg, osc, dsamps, etc); - g_doc_log_pos = pos; + g_doc_log_pos = pos; } extern double g_cur_dcycs; -void -show_doc_log(void) -{ - FILE *docfile; - Doc_reg *rptr; - double dsamp_start; - int osc, ctl, freq; - int pos; - int i; +void show_doc_log(void) { + FILE *docfile; + Doc_reg *rptr; + double dsamp_start; + int osc, ctl, freq; + int pos; + int i; - docfile = fopen("doc_log_out", "w"); - if(docfile == 0) { - printf("fopen failed, errno: %d\n", errno); - return; - } - pos = g_doc_log_pos; - fprintf(docfile, "DOC log pos: %d\n", pos); - dsamp_start = g_doc_log[pos].dsamps; - for(i = 0; i < LEN_DOC_LOG; i++) { - rptr = &(g_doc_log[pos].doc_reg); - osc = g_doc_log[pos].osc; - ctl = rptr->ctl; - freq = rptr->freq; - if(osc < 0) { - ctl = 0; - freq = 0; - } - fprintf(docfile, "%03x:%03x: %-11s ds:%11.1f dt2:%10.1f " - "etc:%08x o:%02x c:%02x fq:%04x\n", - i, pos, g_doc_log[pos].msg, - g_doc_log[pos].dsamps - dsamp_start, - g_doc_log[pos].dtmp2, - g_doc_log[pos].etc, osc & 0xff, ctl, freq); - if(osc >= 0) { - fprintf(docfile, " ire:%d,%d,%d ptr4:%08x " - "inc4:%08x comp_ds:%.1f left:%04x, vol:%02x " - "wptr:%02x, wsz:%02x, 4st:%08x, 4end:%08x\n", - rptr->has_irq_pending, rptr->running, - rptr->event, 4*rptr->cur_acc, 4*rptr->cur_inc, - rptr->complete_dsamp - dsamp_start, - rptr->samps_left, rptr->vol, rptr->waveptr, - rptr->wavesize, 4*rptr->cur_start, - 4*rptr->cur_end); - } - pos++; - if(pos >= LEN_DOC_LOG) { - pos = 0; - } - } + docfile = fopen("doc_log_out", "w"); + if(docfile == 0) { + printf("fopen failed, errno: %d\n", errno); + return; + } + pos = g_doc_log_pos; + fprintf(docfile, "DOC log pos: %d\n", pos); + dsamp_start = g_doc_log[pos].dsamps; + for(i = 0; i < LEN_DOC_LOG; i++) { + rptr = &(g_doc_log[pos].doc_reg); + osc = g_doc_log[pos].osc; + ctl = rptr->ctl; + freq = rptr->freq; + if(osc < 0) { + ctl = 0; + freq = 0; + } + fprintf(docfile, "%03x:%03x: %-11s ds:%11.1f dt2:%10.1f " + "etc:%08x o:%02x c:%02x fq:%04x\n", + i, pos, g_doc_log[pos].msg, + g_doc_log[pos].dsamps - dsamp_start, + g_doc_log[pos].dtmp2, + g_doc_log[pos].etc, osc & 0xff, ctl, freq); + if(osc >= 0) { + fprintf(docfile, " ire:%d,%d,%d ptr4:%08x " + "inc4:%08x comp_ds:%.1f left:%04x, vol:%02x " + "wptr:%02x, wsz:%02x, 4st:%08x, 4end:%08x\n", + rptr->has_irq_pending, rptr->running, + rptr->event, 4*rptr->cur_acc, 4*rptr->cur_inc, + rptr->complete_dsamp - dsamp_start, + rptr->samps_left, rptr->vol, rptr->waveptr, + rptr->wavesize, 4*rptr->cur_start, + 4*rptr->cur_end); + } + pos++; + if(pos >= LEN_DOC_LOG) { + pos = 0; + } + } - fprintf(docfile, "cur_dcycs: %f\n", g_cur_dcycs); - fprintf(docfile, "dsamps_now: %f\n", - (g_cur_dcycs * g_dsamps_per_dcyc) - dsamp_start); - fprintf(docfile, "g_doc_num_osc_en: %d\n", g_doc_num_osc_en); - fclose(docfile); + fprintf(docfile, "cur_dcycs: %f\n", g_cur_dcycs); + fprintf(docfile, "dsamps_now: %f\n", + (g_cur_dcycs * g_dsamps_per_dcyc) - dsamp_start); + fprintf(docfile, "g_doc_num_osc_en: %d\n", g_doc_num_osc_en); + fclose(docfile); } -void -sound_init() -{ - Doc_reg *rptr; - int i; +void sound_init() { + Doc_reg *rptr; + int i; - for(i = 0; i < 32; i++) { - rptr = &(g_doc_regs[i]); - rptr->dsamp_ev = 0.0; - rptr->dsamp_ev2 = 0.0; - rptr->complete_dsamp = 0.0; - rptr->samps_left = 0; - rptr->cur_acc = 0; - rptr->cur_inc = 0; - rptr->cur_start = 0; - rptr->cur_end = 0; - rptr->cur_mask = 0; - rptr->size_bytes = 0; - rptr->event = 0; - rptr->running = 0; - rptr->has_irq_pending = 0; - rptr->freq = 0; - rptr->vol = 0; - rptr->waveptr = 0; - rptr->ctl = 1; - rptr->wavesize = 0; - rptr->last_samp_val = 0; - } + for(i = 0; i < 32; i++) { + rptr = &(g_doc_regs[i]); + rptr->dsamp_ev = 0.0; + rptr->dsamp_ev2 = 0.0; + rptr->complete_dsamp = 0.0; + rptr->samps_left = 0; + rptr->cur_acc = 0; + rptr->cur_inc = 0; + rptr->cur_start = 0; + rptr->cur_end = 0; + rptr->cur_mask = 0; + rptr->size_bytes = 0; + rptr->event = 0; + rptr->running = 0; + rptr->has_irq_pending = 0; + rptr->freq = 0; + rptr->vol = 0; + rptr->waveptr = 0; + rptr->ctl = 1; + rptr->wavesize = 0; + rptr->last_samp_val = 0; + } - // OG sound globals initialization - g_num_c030_fsamps = 0; - g_sound_shm_pos = 0; - g_queued_samps = 0; - g_queued_nonsamps = 0; + // OG sound globals initialization + g_num_c030_fsamps = 0; + g_sound_shm_pos = 0; + g_queued_samps = 0; + g_queued_nonsamps = 0; - doc_sound_ctl = 0; - doc_saved_val = 0; - g_doc_num_osc_en = 1; - g_dcycs_per_doc_update = 1.0; - g_dupd_per_dcyc = 1.0; - g_drecip_osc_en_plus_2 = 1.0 / (double)(1 + 2); + doc_sound_ctl = 0; + doc_saved_val = 0; + g_doc_num_osc_en = 1; + g_dcycs_per_doc_update = 1.0; + g_dupd_per_dcyc = 1.0; + g_drecip_osc_en_plus_2 = 1.0 / (double)(1 + 2); - doc_reg_e0 = 0xff; - g_audio_rate = 0; - g_daudio_rate = 0.0; - g_drecip_audio_rate = 0.0; - g_dsamps_per_dcyc = 0.0; - g_dcycs_per_samp = 0.0; - g_fsamps_per_dcyc = 0.0; + doc_reg_e0 = 0xff; + g_audio_rate = 0; + g_daudio_rate = 0.0; + g_drecip_audio_rate = 0.0; + g_dsamps_per_dcyc = 0.0; + g_dcycs_per_samp = 0.0; + g_fsamps_per_dcyc = 0.0; - g_doc_vol = 2; + g_doc_vol = 2; - g_last_sound_play_dsamp = 0.0; + g_last_sound_play_dsamp = 0.0; - sound_init_general(); + sound_init_general(); } -void -sound_init_general() -{ +void sound_init_general() { /* Workaround - gcc in cygwin wasn't defining _WIN32 */ #if !defined(WIN_SOUND) && !defined(__CYGWIN__) && !defined(MAC) && !defined(HAVE_SDL) - int pid; - int shmid; - int tmp; - int i; + int pid; + int shmid; + int tmp; + int i; #endif - word32 *shmaddr; - int size; - int ret; + word32 *shmaddr; + int size; + int ret; /* Workaround - gcc in cygwin wasn't defining _WIN32 */ #if !defined(WIN_SOUND) && !defined(__CYGWIN__) && !defined(MAC) && !defined(HAVE_SDL) - if(!g_use_shmem) { - if(g_audio_enable < 0) { - printf("Defaulting audio off for slow X display\n"); - g_audio_enable = 0; - } - } + if(!g_use_shmem) { + if(g_audio_enable < 0) { + printf("Defaulting audio off for slow X display\n"); + g_audio_enable = 0; + } + } #endif - ret = 0; + ret = 0; - if(g_audio_enable == 0) { - set_audio_rate(g_preferred_rate); - return; - } + if(g_audio_enable == 0) { + set_audio_rate(g_preferred_rate); + return; + } - size = SOUND_SHM_SAMP_SIZE * SAMPLE_CHAN_SIZE; + size = SOUND_SHM_SAMP_SIZE * SAMPLE_CHAN_SIZE; /* Workaround - gcc in cygwin wasn't defining _WIN32 */ #if !defined(WIN_SOUND) && !defined(__CYGWIN__) && !defined(MAC) && !defined(HAVE_SDL) - shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777); - if(shmid < 0) { - printf("sound_init: shmget ret: %d, errno: %d\n", shmid, errno); - exit(2); - } + shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | 0777); + if(shmid < 0) { + printf("sound_init: shmget ret: %d, errno: %d\n", shmid, errno); + exit(2); + } - shmaddr = (word32*)shmat(shmid, 0, 0); - tmp = (int)PTR2WORD(shmaddr); - if(tmp == -1) { - printf("sound_init: shmat ret: %p, errno: %d\n", shmaddr, - errno); - exit(3); - } + shmaddr = (word32*)shmat(shmid, 0, 0); + tmp = (int)PTR2WORD(shmaddr); + if(tmp == -1) { + printf("sound_init: shmat ret: %p, errno: %d\n", shmaddr, + errno); + exit(3); + } - ret = shmctl(shmid, IPC_RMID, 0); - if(ret < 0) { - printf("sound_init: shmctl ret: %d, errno: %d\n", ret, errno); - exit(4); - } + ret = shmctl(shmid, IPC_RMID, 0); + if(ret < 0) { + printf("sound_init: shmctl ret: %d, errno: %d\n", ret, errno); + exit(4); + } #else // windows and mac and sdl - shmaddr = (word32*)malloc(size); //size = 131072 - memset(shmaddr, 0, size); + shmaddr = (word32*)malloc(size); //size = 131072 + memset(shmaddr, 0, size); #endif - g_sound_shm_addr = shmaddr; + g_sound_shm_addr = shmaddr; - fflush(stdout); + fflush(stdout); /* Workaround - gcc in cygwin wasn't defining _WIN32 */ #if !defined(MAC) && !defined(WIN_SOUND) && !defined(__CYGWIN__) && !defined(HAVE_SDL) - /* prepare pipe so parent can signal child each other */ - /* pipe[0] = read side, pipe[1] = write end */ - ret = pipe(&g_pipe_fd[0]); - if(ret < 0) { - printf("sound_init: pipe ret: %d, errno: %d\n", ret, errno); - exit(5); - } - ret = pipe(&g_pipe2_fd[0]); - if(ret < 0) { - printf("sound_init: pipe ret: %d, errno: %d\n", ret, errno); - exit(5); - } + /* prepare pipe so parent can signal child each other */ + /* pipe[0] = read side, pipe[1] = write end */ + ret = pipe(&g_pipe_fd[0]); + if(ret < 0) { + printf("sound_init: pipe ret: %d, errno: %d\n", ret, errno); + exit(5); + } + ret = pipe(&g_pipe2_fd[0]); + if(ret < 0) { + printf("sound_init: pipe ret: %d, errno: %d\n", ret, errno); + exit(5); + } - printf("pipes: pipe_fd = %d, %d pipe2_fd: %d,%d\n", - g_pipe_fd[0], g_pipe_fd[1], g_pipe2_fd[0], g_pipe2_fd[1]); - fflush(stdout); + printf("pipes: pipe_fd = %d, %d pipe2_fd: %d,%d\n", + g_pipe_fd[0], g_pipe_fd[1], g_pipe2_fd[0], g_pipe2_fd[1]); + fflush(stdout); - pid = fork(); - switch(pid) { - case 0: - /* child */ - /* close stdin and write-side of pipe */ - close(0); - /* Close other fds to make sure X window fd is closed */ - for(i = 3; i < 100; i++) { - if((i != g_pipe_fd[0]) && (i != g_pipe2_fd[1])) { - close(i); - } - } - close(g_pipe_fd[1]); /*make sure write pipe closed*/ - close(g_pipe2_fd[0]); /*make sure read pipe closed*/ - child_sound_loop(g_pipe_fd[0], g_pipe2_fd[1], g_sound_shm_addr); - printf("Child sound loop returned\n"); - exit(0); - case -1: - /* error */ - printf("sound_init: fork ret: -1, errno: %d\n", errno); - exit(6); - default: - /* parent */ - /* close read-side of pipe1, and the write side of pipe2 */ - close(g_pipe_fd[0]); - close(g_pipe2_fd[1]); - doc_printf("Child is pid: %d\n", pid); - } + pid = fork(); + switch(pid) { + case 0: + /* child */ + /* close stdin and write-side of pipe */ + close(0); + /* Close other fds to make sure X window fd is closed */ + for(i = 3; i < 100; i++) { + if((i != g_pipe_fd[0]) && (i != g_pipe2_fd[1])) { + close(i); + } + } + close(g_pipe_fd[1]); /*make sure write pipe closed*/ + close(g_pipe2_fd[0]); /*make sure read pipe closed*/ + child_sound_loop(g_pipe_fd[0], g_pipe2_fd[1], g_sound_shm_addr); + printf("Child sound loop returned\n"); + exit(0); + case -1: + /* error */ + printf("sound_init: fork ret: -1, errno: %d\n", errno); + exit(6); + default: + /* parent */ + /* close read-side of pipe1, and the write side of pipe2 */ + close(g_pipe_fd[0]); + close(g_pipe2_fd[1]); + doc_printf("Child is pid: %d\n", pid); + } - parent_sound_get_sample_rate(g_pipe2_fd[0]); + parent_sound_get_sample_rate(g_pipe2_fd[0]); #else # if defined (HAVE_SDL) - sdlsnd_init(shmaddr); + sdlsnd_init(shmaddr); # elif defined (WIN_SOUND) - win32snd_init(shmaddr); + win32snd_init(shmaddr); # elif defined (MAC) && !defined(HAVE_SDL) macsnd_init(shmaddr); # endif @@ -373,112 +365,102 @@ sound_init_general() } -void -parent_sound_get_sample_rate(int read_fd) -{ - word32 tmp; - int ret; +void parent_sound_get_sample_rate(int read_fd) { + word32 tmp; + int ret; - ret = read(read_fd, (char*)&tmp, 4); - if(ret != 4) { - printf("parent could not get audio sample rate from child, disabling sound.\n"); - printf("ret: %d, fd: %d errno:%d\n", ret, read_fd, errno); - g_audio_enable = 0; - } - close(read_fd); + ret = read(read_fd, (char*)&tmp, 4); + if(ret != 4) { + printf("parent could not get audio sample rate from child, disabling sound.\n"); + printf("ret: %d, fd: %d errno:%d\n", ret, read_fd, errno); + g_audio_enable = 0; + } + close(read_fd); - set_audio_rate(tmp); + set_audio_rate(tmp); } -void -set_audio_rate(int rate) -{ - g_audio_rate = rate; - g_daudio_rate = (rate)*1.0; - g_drecip_audio_rate = 1.0/(rate); - g_dsamps_per_dcyc = ((rate*1.0) / DCYCS_1_MHZ); - g_dcycs_per_samp = (DCYCS_1_MHZ / (rate*1.0)); - g_fsamps_per_dcyc = (float)((rate*1.0) / DCYCS_1_MHZ); +void set_audio_rate(int rate) { + g_audio_rate = rate; + g_daudio_rate = (rate)*1.0; + g_drecip_audio_rate = 1.0/(rate); + g_dsamps_per_dcyc = ((rate*1.0) / DCYCS_1_MHZ); + g_dcycs_per_samp = (DCYCS_1_MHZ / (rate*1.0)); + g_fsamps_per_dcyc = (float)((rate*1.0) / DCYCS_1_MHZ); } -void -sound_reset(double dcycs) -{ - double dsamps; - int i; +void sound_reset(double dcycs) { + double dsamps; + int i; - dsamps = dcycs * g_dsamps_per_dcyc; - for(i = 0; i < 32; i++) { - doc_write_ctl_reg(i, g_doc_regs[i].ctl | 1, dsamps); - doc_reg_e0 = 0xff; - if(g_doc_regs[i].has_irq_pending) { - halt_printf("reset: has_irq[%02x] = %d\n", i, - g_doc_regs[i].has_irq_pending); - } - g_doc_regs[i].has_irq_pending = 0; - } - if(g_num_osc_interrupting) { - halt_printf("reset: num_osc_int:%d\n", g_num_osc_interrupting); - } - g_num_osc_interrupting = 0; + dsamps = dcycs * g_dsamps_per_dcyc; + for(i = 0; i < 32; i++) { + doc_write_ctl_reg(i, g_doc_regs[i].ctl | 1, dsamps); + doc_reg_e0 = 0xff; + if(g_doc_regs[i].has_irq_pending) { + halt_printf("reset: has_irq[%02x] = %d\n", i, + g_doc_regs[i].has_irq_pending); + } + g_doc_regs[i].has_irq_pending = 0; + } + if(g_num_osc_interrupting) { + halt_printf("reset: num_osc_int:%d\n", g_num_osc_interrupting); + } + g_num_osc_interrupting = 0; // OG No reason to reset the number of active oscillo on reset : this should only be done on startup. - /* - g_doc_num_osc_en = 1; - UPDATE_G_DCYCS_PER_DOC_UPDATE(1); - */ + /* + g_doc_num_osc_en = 1; + UPDATE_G_DCYCS_PER_DOC_UPDATE(1); + */ } -void -sound_shutdown() -{ - // OG stop sound and free memory on sound_shutdown - sound_reset(g_cur_dcycs); +void sound_shutdown() { + // OG stop sound and free memory on sound_shutdown + sound_reset(g_cur_dcycs); -#ifdef WIN_SOUND /* Workaround - gcc in cygwin wasn't defining _WIN32 */ - win32snd_shutdown(); +#ifdef WIN_SOUND /* Workaround - gcc in cygwin wasn't defining _WIN32 */ + win32snd_shutdown(); #elif defined(HAVE_SDL) if((g_audio_enable != 0)) { //sdlsnd_shutdown(); - sound_shutdown_sdl(); + sound_shutdown_sdl(); } #else - if((g_audio_enable != 0) && g_pipe_fd[1] != 0) { - close(g_pipe_fd[1]); - } + if((g_audio_enable != 0) && g_pipe_fd[1] != 0) { + close(g_pipe_fd[1]); + } #endif - // OG Free up allocated memory - if (g_sound_shm_addr) - { - free(g_sound_shm_addr); - g_sound_shm_addr = NULL; - } + // OG Free up allocated memory + if (g_sound_shm_addr) + { + free(g_sound_shm_addr); + g_sound_shm_addr = NULL; + } } -void -sound_update(double dcycs) -{ - double dsamps; - /* Called every VBL time to update sound status */ +void sound_update(double dcycs) { + double dsamps; + /* Called every VBL time to update sound status */ - /* "play" sounds for this vbl */ + /* "play" sounds for this vbl */ - dsamps = dcycs * g_dsamps_per_dcyc; - DOC_LOG("do_snd_pl", -1, dsamps, 0); - sound_play(dsamps); + dsamps = dcycs * g_dsamps_per_dcyc; + DOC_LOG("do_snd_pl", -1, dsamps, 0); + sound_play(dsamps); } -#define MAX_SND_BUF 65536 +#define MAX_SND_BUF 65536 int g_samp_buf[2*MAX_SND_BUF]; word32 zero_buf[SOUND_SHM_SAMP_SIZE]; double g_doc_dsamps_extra = 0.0; -float g_fvoices = 0.0; +float g_fvoices = 0.0; word32 g_cycs_in_sound1 = 0; word32 g_cycs_in_sound2 = 0; @@ -487,1581 +469,1538 @@ word32 g_cycs_in_sound4 = 0; word32 g_cycs_in_start_sound = 0; word32 g_cycs_in_est_sound = 0; -int g_num_snd_plays = 0; -int g_num_doc_events = 0; -int g_num_start_sounds = 0; -int g_num_scan_osc = 0; -int g_num_recalc_snd_parms = 0; +int g_num_snd_plays = 0; +int g_num_doc_events = 0; +int g_num_start_sounds = 0; +int g_num_scan_osc = 0; +int g_num_recalc_snd_parms = 0; -word32 g_last_c030_vbl_count = 0; -int g_c030_state = 0; +word32 g_last_c030_vbl_count = 0; +int g_c030_state = 0; -#define VAL_C030_RANGE (32768) -#define VAL_C030_BASE (-16384) +#define VAL_C030_RANGE (32768) +#define VAL_C030_BASE (-16384) -int g_sound_file_num = 0; -FILE *g_sound_file_fd = 0; -int g_send_sound_to_file = 0; -int g_send_file_bytes = 0; +int g_sound_file_num = 0; +FILE *g_sound_file_fd = 0; +int g_send_sound_to_file = 0; +int g_send_file_bytes = 0; -void -open_sound_file() -{ - char name[256]; - FILE *fd; +void open_sound_file() { + char name[256]; + FILE *fd; - sprintf(name, "snd.out.%d", g_sound_file_num); + sprintf(name, "snd.out.%d", g_sound_file_num); - fd = fopen(name, "wb+"); - if(fd == 0) { - printf("open_sound_file open errno: %d\n", errno); - exit(1); - } + fd = fopen(name, "wb+"); + if(fd == 0) { + printf("open_sound_file open errno: %d\n", errno); + exit(1); + } - g_sound_file_fd = fd; - g_sound_file_num++; - g_send_file_bytes = 0; + g_sound_file_fd = fd; + g_sound_file_num++; + g_send_file_bytes = 0; } -void -close_sound_file() -{ - if(g_sound_file_fd != 0) { - fclose(g_sound_file_fd); - } +void close_sound_file() { + if(g_sound_file_fd != 0) { + fclose(g_sound_file_fd); + } - g_sound_file_fd = 0; + g_sound_file_fd = 0; } -void -check_for_range(word32 *addr, int num_samps, int offset) -{ - short *shortptr; - int i; - int left; - int right; - int max; +void check_for_range(word32 *addr, int num_samps, int offset) { + short *shortptr; + int i; + int left; + int right; + int max; - max = -32768; + max = -32768; - if(num_samps > SOUND_SHM_SAMP_SIZE) { - halt_printf("num_samps: %d > %d!\n", num_samps, - SOUND_SHM_SAMP_SIZE); - } + if(num_samps > SOUND_SHM_SAMP_SIZE) { + halt_printf("num_samps: %d > %d!\n", num_samps, + SOUND_SHM_SAMP_SIZE); + } - for(i = 0; i < num_samps; i++) { - shortptr = (short *)&(addr[i]); - left = shortptr[0]; - right = shortptr[1]; - if((left > 0x3000) || (right > 0x3000)) { - halt_printf("Sample %d of %d at snd_buf: %p is: " - "%d/%d\n", i + offset, num_samps, - &addr[i], left, right); - return; - } + for(i = 0; i < num_samps; i++) { + shortptr = (short *)&(addr[i]); + left = shortptr[0]; + right = shortptr[1]; + if((left > 0x3000) || (right > 0x3000)) { + halt_printf("Sample %d of %d at snd_buf: %p is: " + "%d/%d\n", i + offset, num_samps, + &addr[i], left, right); + return; + } - max = MAX(max, left); - max = MAX(max, right); - } + max = MAX(max, left); + max = MAX(max, right); + } - printf("check4 max: %d over %d\n", max, num_samps); + printf("check4 max: %d over %d\n", max, num_samps); } -void -send_sound_to_file(word32 *addr, int shm_pos, int num_samps) -{ - int size; - int ret; +void send_sound_to_file(word32 *addr, int shm_pos, int num_samps) { + int size; + int ret; - if(g_sound_file_fd == 0) { - open_sound_file(); - } + if(g_sound_file_fd == 0) { + open_sound_file(); + } - size = 0; - if((num_samps + shm_pos) > SOUND_SHM_SAMP_SIZE) { - size = SOUND_SHM_SAMP_SIZE - shm_pos; - g_send_file_bytes += (size * 4); + size = 0; + if((num_samps + shm_pos) > SOUND_SHM_SAMP_SIZE) { + size = SOUND_SHM_SAMP_SIZE - shm_pos; + g_send_file_bytes += (size * 4); - ret = fwrite(&(addr[shm_pos]), 1, 4*size, g_sound_file_fd); - if(ret != 4*size) { - halt_printf("wrote %d not %d\n", ret, 4*size); - } + ret = fwrite(&(addr[shm_pos]), 1, 4*size, g_sound_file_fd); + if(ret != 4*size) { + halt_printf("wrote %d not %d\n", ret, 4*size); + } - if(g_doc_vol < 3) { - check_for_range(&(addr[shm_pos]), size, 0); - } else { - printf("Not checking %d bytes since vol: %d\n", - 4*size, g_doc_vol); - } - shm_pos = 0; - num_samps -= size; - } + if(g_doc_vol < 3) { + check_for_range(&(addr[shm_pos]), size, 0); + } else { + printf("Not checking %d bytes since vol: %d\n", + 4*size, g_doc_vol); + } + shm_pos = 0; + num_samps -= size; + } - g_send_file_bytes += (num_samps * 4); + g_send_file_bytes += (num_samps * 4); - ret = fwrite(&(addr[shm_pos]), 1, 4*num_samps, g_sound_file_fd); - if(ret != 4*num_samps) { - halt_printf("wrote %d not %d\n", ret, 4*num_samps); - } + ret = fwrite(&(addr[shm_pos]), 1, 4*num_samps, g_sound_file_fd); + if(ret != 4*num_samps) { + halt_printf("wrote %d not %d\n", ret, 4*num_samps); + } - if(g_doc_vol < 3) { - check_for_range(&(addr[shm_pos]), num_samps, size); - } else { - printf("Not checking2 %d bytes since vol: %d\n", - 4*num_samps, g_doc_vol); - } + if(g_doc_vol < 3) { + check_for_range(&(addr[shm_pos]), num_samps, size); + } else { + printf("Not checking2 %d bytes since vol: %d\n", + 4*num_samps, g_doc_vol); + } } // is called with real_samps = 1 to output sound -void -send_sound(int real_samps, int size) -{ - // real_samps = 1, size = 1602 - word32 tmp; +void send_sound(int real_samps, int size) { + // real_samps = 1, size = 1602 + word32 tmp; - if(g_audio_enable == 0) { - printf("Entered send_sound but audio off!\n"); - exit(2); - } - // really does add this crazy high value. i guess it gets taken off / checked later - // so if size = 1602 (0x0642) it becomes 0xa2000642 - // wtf? - if(real_samps) { - tmp = size + 0xa2000000; - } else { - tmp = size + 0xa1000000; - } - DOC_LOG("send_sound", -1, g_last_sound_play_dsamp, - (real_samps << 30) + size); + if(g_audio_enable == 0) { + printf("Entered send_sound but audio off!\n"); + exit(2); + } + // really does add this crazy high value. i guess it gets taken off / checked later + // so if size = 1602 (0x0642) it becomes 0xa2000642 + // wtf? + if(real_samps) { + tmp = size + 0xa2000000; + } else { + tmp = size + 0xa1000000; + } + DOC_LOG("send_sound", -1, g_last_sound_play_dsamp, + (real_samps << 30) + size); // Workaround - gcc in cygwin wasn't defining _WIN32 #if defined(WIN_SOUND) || defined(MAC) && !defined(HAVE_SDL) - child_sound_playit(tmp); + child_sound_playit(tmp); #elif defined(HAVE_SDL) - sound_write_sdl( real_samps, size); + sound_write_sdl( real_samps, size); #else - /* Although this looks like a big/little-endian issue, since the */ - /* child is also reading an int, it just works with no byte swap */ - int ret = 0; - ret = write(g_pipe_fd[1], &tmp, 4); - if(ret != 4) { - halt_printf("send_sound, wr ret: %d, errno: %d\n", ret, errno); - } + /* Although this looks like a big/little-endian issue, since the */ + /* child is also reading an int, it just works with no byte swap */ + int ret = 0; + ret = write(g_pipe_fd[1], &tmp, 4); + if(ret != 4) { + halt_printf("send_sound, wr ret: %d, errno: %d\n", ret, errno); + } #endif } -void show_c030_state() -{ - show_c030_samps(&(g_samp_buf[0]), 100); +void show_c030_state() { + show_c030_samps(&(g_samp_buf[0]), 100); } -void show_c030_samps(int *outptr, int num) -{ - int i; +void show_c030_samps(int *outptr, int num) { + int i; - printf("c030_fsamps[]: %d\n", g_num_c030_fsamps); + printf("c030_fsamps[]: %d\n", g_num_c030_fsamps); - for(i = 0; i < g_num_c030_fsamps+2; i++) { - printf("%3d: %5.3f\n", i, c030_fsamps[i]); - } + for(i = 0; i < g_num_c030_fsamps+2; i++) { + printf("%3d: %5.3f\n", i, c030_fsamps[i]); + } - printf("Samples[] = %d\n", num); + printf("Samples[] = %d\n", num); - for(i = 0; i < num+2; i++) { - printf("%4d: %d %d\n", i, outptr[0], outptr[1]); - outptr += 2; - } + for(i = 0; i < num+2; i++) { + printf("%4d: %d %d\n", i, outptr[0], outptr[1]); + outptr += 2; + } } -void sound_play(double dsamps) -{ - register word32 start_time1, start_time2, start_time3, start_time4; - register word32 end_time1, end_time2, end_time3; - Doc_reg *rptr; - int *outptr; - int *outptr_start; - word32 *sndptr; - double complete_dsamp; - double cur_dsamp; - double last_dsamp; - double dsamp_now; - double dnum_samps; - int val, val2; - int new_val; - float ftmp; - int imul; - int off; - int num; - float fsampnum; - float next_fsampnum; - int c030_lo_val, c030_hi_val; - float fc030_range; - float fc030_base; - int sampnum; - int next_sampnum; - float fpercent; - int c030_state; - int val0, val1; - word32 cur_acc; - word32 cur_pos; - word32 cur_mask; - word32 cur_inc; - word32 cur_end; - int ctl; - int num_osc_en; - int samps_left; - int samps_to_do; - int samps_played; - int samp_offset; - int snd_buf_init; - int pos; - int num_running; - int num_samps; - int osc; - int done; - int i, j; +void sound_play(double dsamps) { + register word32 start_time1, start_time2, start_time3, start_time4; + register word32 end_time1, end_time2, end_time3; + Doc_reg *rptr; + int *outptr; + int *outptr_start; + word32 *sndptr; + double complete_dsamp; + double cur_dsamp; + double last_dsamp; + double dsamp_now; + double dnum_samps; + int val, val2; + int new_val; + float ftmp; + int imul; + int off; + int num; + float fsampnum; + float next_fsampnum; + int c030_lo_val, c030_hi_val; + float fc030_range; + float fc030_base; + int sampnum; + int next_sampnum; + float fpercent; + int c030_state; + int val0, val1; + word32 cur_acc; + word32 cur_pos; + word32 cur_mask; + word32 cur_inc; + word32 cur_end; + int ctl; + int num_osc_en; + int samps_left; + int samps_to_do; + int samps_played; + int samp_offset; + int snd_buf_init; + int pos; + int num_running; + int num_samps; + int osc; + int done; + int i, j; - GET_ITIMER(start_time1); + GET_ITIMER(start_time1); - g_num_snd_plays++; - if(g_sound_play_depth) { - halt_printf("Nested sound_play!\n"); - } + g_num_snd_plays++; + if(g_sound_play_depth) { + halt_printf("Nested sound_play!\n"); + } - g_sound_play_depth++; + g_sound_play_depth++; - /* calc sample num */ + /* calc sample num */ - last_dsamp = g_last_sound_play_dsamp; - num_samps = (int)(dsamps - g_last_sound_play_dsamp); - dnum_samps = (double)num_samps; + last_dsamp = g_last_sound_play_dsamp; + num_samps = (int)(dsamps - g_last_sound_play_dsamp); + dnum_samps = (double)num_samps; - dsamp_now = last_dsamp + dnum_samps; + dsamp_now = last_dsamp + dnum_samps; - if(num_samps < 1) { - /* just say no */ - g_sound_play_depth--; - return; - } + if(num_samps < 1) { + /* just say no */ + g_sound_play_depth--; + return; + } - DOC_LOG("sound_play", -1, dsamp_now, num_samps); + DOC_LOG("sound_play", -1, dsamp_now, num_samps); - if(num_samps > MAX_SND_BUF) { - printf("num_samps: %d, too big!\n", num_samps); - g_sound_play_depth--; - return; - } + if(num_samps > MAX_SND_BUF) { + printf("num_samps: %d, too big!\n", num_samps); + g_sound_play_depth--; + return; + } - GET_ITIMER(start_time4); + GET_ITIMER(start_time4); - outptr_start = &(g_samp_buf[0]); - outptr = outptr_start; + outptr_start = &(g_samp_buf[0]); + outptr = outptr_start; - snd_buf_init = 0; + snd_buf_init = 0; - samps_played = 0; + samps_played = 0; - num = g_num_c030_fsamps; + num = g_num_c030_fsamps; - if(num || ((g_vbl_count - g_last_c030_vbl_count) < 240)) { + if(num || ((g_vbl_count - g_last_c030_vbl_count) < 240)) { - if(num) { - g_last_c030_vbl_count = g_vbl_count; - } + if(num) { + g_last_c030_vbl_count = g_vbl_count; + } - pos = 0; - outptr = outptr_start; - c030_state = g_c030_state; + pos = 0; + outptr = outptr_start; + c030_state = g_c030_state; - c030_hi_val = ((VAL_C030_BASE + VAL_C030_RANGE)*g_doc_vol) >> 4; - c030_lo_val = (VAL_C030_BASE * g_doc_vol) >> 4; + c030_hi_val = ((VAL_C030_BASE + VAL_C030_RANGE)*g_doc_vol) >> 4; + c030_lo_val = (VAL_C030_BASE * g_doc_vol) >> 4; - fc030_range = (float)(((VAL_C030_RANGE) * g_doc_vol) >> 4); - fc030_base = (float)(((VAL_C030_BASE) * g_doc_vol) >> 4); + fc030_range = (float)(((VAL_C030_RANGE) *g_doc_vol) >> 4); + fc030_base = (float)(((VAL_C030_BASE) *g_doc_vol) >> 4); - val = c030_lo_val; - if(c030_state) { - val = c030_hi_val; - } + val = c030_lo_val; + if(c030_state) { + val = c030_hi_val; + } - snd_buf_init++; + snd_buf_init++; - c030_fsamps[num] = (float)(num_samps); - c030_fsamps[num+1] = (float)(num_samps+1); + c030_fsamps[num] = (float)(num_samps); + c030_fsamps[num+1] = (float)(num_samps+1); - ftmp = (float)num_samps; - /* ensure that all samps are in range */ - for(i = num - 1; i >= 0; i--) { - if(c030_fsamps[i] > ftmp) { - c030_fsamps[i] = ftmp; - } - } + ftmp = (float)num_samps; + /* ensure that all samps are in range */ + for(i = num - 1; i >= 0; i--) { + if(c030_fsamps[i] > ftmp) { + c030_fsamps[i] = ftmp; + } + } - num++; - fsampnum = c030_fsamps[0]; - sampnum = (int)fsampnum; - fpercent = (float)0.0; - i = 0; + num++; + fsampnum = c030_fsamps[0]; + sampnum = (int)fsampnum; + fpercent = (float)0.0; + i = 0; - while(i < num) { - next_fsampnum = c030_fsamps[i+1]; - next_sampnum = (int)next_fsampnum; - if(sampnum < 0 || sampnum > num_samps) { - halt_printf("play c030: [%d]:%f is %d, > %d\n", - i, fsampnum, sampnum, num_samps); - break; - } + while(i < num) { + next_fsampnum = c030_fsamps[i+1]; + next_sampnum = (int)next_fsampnum; + if(sampnum < 0 || sampnum > num_samps) { + halt_printf("play c030: [%d]:%f is %d, > %d\n", + i, fsampnum, sampnum, num_samps); + break; + } - /* write in samples to all samps < me */ - new_val = c030_lo_val; - if(c030_state) { - new_val = c030_hi_val; - } - for(j = pos; j < sampnum; j++) { - outptr[0] = new_val; - outptr[1] = new_val; - outptr += 2; - pos++; - } + /* write in samples to all samps < me */ + new_val = c030_lo_val; + if(c030_state) { + new_val = c030_hi_val; + } + for(j = pos; j < sampnum; j++) { + outptr[0] = new_val; + outptr[1] = new_val; + outptr += 2; + pos++; + } - /* now, calculate me */ - fpercent = (float)0.0; - if(c030_state) { - fpercent = (fsampnum - (float)sampnum); - } + /* now, calculate me */ + fpercent = (float)0.0; + if(c030_state) { + fpercent = (fsampnum - (float)sampnum); + } - c030_state = !c030_state; + c030_state = !c030_state; - while(next_sampnum == sampnum) { - if(c030_state) { - fpercent += (next_fsampnum - fsampnum); - } - i++; - fsampnum = next_fsampnum; + while(next_sampnum == sampnum) { + if(c030_state) { + fpercent += (next_fsampnum - fsampnum); + } + i++; + fsampnum = next_fsampnum; - next_fsampnum = c030_fsamps[i+1]; - next_sampnum = (int)next_fsampnum; - c030_state = !c030_state; - } + next_fsampnum = c030_fsamps[i+1]; + next_sampnum = (int)next_fsampnum; + c030_state = !c030_state; + } - if(c030_state) { - /* add in fractional time */ - ftmp = (float)(int)(fsampnum + 1.0f); //OG added cast - fpercent += (ftmp - fsampnum); - } + if(c030_state) { + /* add in fractional time */ + ftmp = (float)(int)(fsampnum + 1.0f); //OG added cast + fpercent += (ftmp - fsampnum); + } - if((fpercent < (float)0.0) || (fpercent > (float)1.0)) { - halt_printf("fpercent: %d = %f\n", i, fpercent); - show_c030_samps(outptr_start, num_samps); - break; - } + if((fpercent < (float)0.0) || (fpercent > (float)1.0)) { + halt_printf("fpercent: %d = %f\n", i, fpercent); + show_c030_samps(outptr_start, num_samps); + break; + } - val = (int)((fpercent * fc030_range) + fc030_base); - outptr[0] = val; - outptr[1] = val; - outptr += 2; - pos++; - i++; + val = (int)((fpercent * fc030_range) + fc030_base); + outptr[0] = val; + outptr[1] = val; + outptr += 2; + pos++; + i++; - sampnum = next_sampnum; - fsampnum = next_fsampnum; - } + sampnum = next_sampnum; + fsampnum = next_fsampnum; + } - samps_played += num_samps; + samps_played += num_samps; - /* since we pretended to get one extra sample, we will */ - /* have toggled the speaker one time too many. Fix it */ - g_c030_state = !c030_state; + /* since we pretended to get one extra sample, we will */ + /* have toggled the speaker one time too many. Fix it */ + g_c030_state = !c030_state; - if(g_send_sound_to_file) { - show_c030_samps(outptr_start, num_samps); - } - } + if(g_send_sound_to_file) { + show_c030_samps(outptr_start, num_samps); + } + } - g_num_c030_fsamps = 0; + g_num_c030_fsamps = 0; - GET_ITIMER(start_time2); + GET_ITIMER(start_time2); - num_running = 0; + num_running = 0; - num_osc_en = g_doc_num_osc_en; + num_osc_en = g_doc_num_osc_en; - done = 0; - while(!done) { - done = 1; - for(j = 0; j < num_osc_en; j++) { - osc = j; - rptr = &(g_doc_regs[osc]); - complete_dsamp = rptr->complete_dsamp; - samps_left = rptr->samps_left; - cur_acc = rptr->cur_acc; - cur_mask = rptr->cur_mask; - cur_inc = rptr->cur_inc; - cur_end = rptr->cur_end; - if(!rptr->running || cur_inc == 0 || - (complete_dsamp >= dsamp_now)) { - continue; - } + done = 0; + while(!done) { + done = 1; + for(j = 0; j < num_osc_en; j++) { + osc = j; + rptr = &(g_doc_regs[osc]); + complete_dsamp = rptr->complete_dsamp; + samps_left = rptr->samps_left; + cur_acc = rptr->cur_acc; + cur_mask = rptr->cur_mask; + cur_inc = rptr->cur_inc; + cur_end = rptr->cur_end; + if(!rptr->running || cur_inc == 0 || + (complete_dsamp >= dsamp_now)) { + continue; + } - done = 0; - ctl = rptr->ctl; + done = 0; + ctl = rptr->ctl; - samp_offset = 0; - if(complete_dsamp > last_dsamp) { - samp_offset = (int)(complete_dsamp- last_dsamp); - if(samp_offset > num_samps) { - rptr->complete_dsamp = dsamp_now; - continue; - } - } - outptr = outptr_start + 2 * samp_offset; - if(ctl & 0x10) { - /* other channel */ - outptr += 1; - } + samp_offset = 0; + if(complete_dsamp > last_dsamp) { + samp_offset = (int)(complete_dsamp- last_dsamp); + if(samp_offset > num_samps) { + rptr->complete_dsamp = dsamp_now; + continue; + } + } + outptr = outptr_start + 2 * samp_offset; + if(ctl & 0x10) { + /* other channel */ + outptr += 1; + } - imul = (rptr->vol * g_doc_vol); - off = imul * 128; + imul = (rptr->vol * g_doc_vol); + off = imul * 128; - samps_to_do = MIN(samps_left, num_samps - samp_offset); - if(imul == 0 || samps_to_do == 0) { - /* produce no sound */ - samps_left = samps_left - samps_to_do; - cur_acc += cur_inc * samps_to_do; - rptr->samps_left = samps_left; - rptr->cur_acc = cur_acc; - cur_dsamp = last_dsamp + - (double)(samps_to_do + samp_offset); - DOC_LOG("nosnd", osc, cur_dsamp, samps_to_do); - rptr->complete_dsamp = dsamp_now; - cur_pos = rptr->cur_start+(cur_acc & cur_mask); - if(samps_left <= 0) { - doc_sound_end(osc, 1, cur_dsamp, - dsamp_now); - val = 0; - j--; - } else { - val = doc_ram[cur_pos >> SND_PTR_SHIFT]; - } - rptr->last_samp_val = val; - continue; - } + samps_to_do = MIN(samps_left, num_samps - samp_offset); + if(imul == 0 || samps_to_do == 0) { + /* produce no sound */ + samps_left = samps_left - samps_to_do; + cur_acc += cur_inc * samps_to_do; + rptr->samps_left = samps_left; + rptr->cur_acc = cur_acc; + cur_dsamp = last_dsamp + + (double)(samps_to_do + samp_offset); + DOC_LOG("nosnd", osc, cur_dsamp, samps_to_do); + rptr->complete_dsamp = dsamp_now; + cur_pos = rptr->cur_start+(cur_acc & cur_mask); + if(samps_left <= 0) { + doc_sound_end(osc, 1, cur_dsamp, + dsamp_now); + val = 0; + j--; + } else { + val = doc_ram[cur_pos >> SND_PTR_SHIFT]; + } + rptr->last_samp_val = val; + continue; + } - if(snd_buf_init == 0) { - memset(outptr_start, 0, - 2*sizeof(outptr_start[0])*num_samps); - snd_buf_init++; - } + if(snd_buf_init == 0) { + memset(outptr_start, 0, + 2*sizeof(outptr_start[0])*num_samps); + snd_buf_init++; + } - val = 0; - rptr->complete_dsamp = dsamp_now; - cur_pos = rptr->cur_start + (cur_acc & cur_mask); - for(i = 0; i < samps_to_do; i++) { - pos = cur_pos >> SND_PTR_SHIFT; - cur_pos += cur_inc; - cur_acc += cur_inc; - val = doc_ram[pos]; + val = 0; + rptr->complete_dsamp = dsamp_now; + cur_pos = rptr->cur_start + (cur_acc & cur_mask); + for(i = 0; i < samps_to_do; i++) { + pos = cur_pos >> SND_PTR_SHIFT; + cur_pos += cur_inc; + cur_acc += cur_inc; + val = doc_ram[pos]; - val2 = (val * imul - off) >> 4; - if((val == 0) || (cur_pos >= cur_end)) { - cur_dsamp = last_dsamp + - (double)(samp_offset + i + 1); - rptr->cur_acc = cur_acc; - rptr->samps_left = 0; - DOC_LOG("end or 0", osc, cur_dsamp, - (pos << 16) + ((i &0xff) << 8) + - val); - doc_sound_end(osc, val, cur_dsamp, - dsamp_now); - val = 0; - break; - } + val2 = (val * imul - off) >> 4; + if((val == 0) || (cur_pos >= cur_end)) { + cur_dsamp = last_dsamp + + (double)(samp_offset + i + 1); + rptr->cur_acc = cur_acc; + rptr->samps_left = 0; + DOC_LOG("end or 0", osc, cur_dsamp, + (pos << 16) + ((i &0xff) << 8) + + val); + doc_sound_end(osc, val, cur_dsamp, + dsamp_now); + val = 0; + break; + } - val2 = outptr[0] + val2; + val2 = outptr[0] + val2; - samps_left--; - *outptr = val2; - outptr += 2; - } + samps_left--; + *outptr = val2; + outptr += 2; + } - rptr->last_samp_val = val; + rptr->last_samp_val = val; - if(val != 0) { - rptr->cur_acc = cur_acc; - rptr->samps_left = samps_left; - rptr->complete_dsamp = dsamp_now; - } + if(val != 0) { + rptr->cur_acc = cur_acc; + rptr->samps_left = samps_left; + rptr->complete_dsamp = dsamp_now; + } - samps_played += samps_to_do; - DOC_LOG("splayed", osc, dsamp_now, - (samps_to_do << 16) + (pos & 0xffff)); - } - } + samps_played += samps_to_do; + DOC_LOG("splayed", osc, dsamp_now, + (samps_to_do << 16) + (pos & 0xffff)); + } + } - GET_ITIMER(end_time2); + GET_ITIMER(end_time2); - g_cycs_in_sound2 += (end_time2 - start_time2); + g_cycs_in_sound2 += (end_time2 - start_time2); - g_last_sound_play_dsamp = dsamp_now; + g_last_sound_play_dsamp = dsamp_now; - GET_ITIMER(start_time3); + GET_ITIMER(start_time3); - outptr = outptr_start; + outptr = outptr_start; - pos = g_sound_shm_pos; - sndptr = g_sound_shm_addr; + pos = g_sound_shm_pos; + sndptr = g_sound_shm_addr; #if 0 - printf("samps_left: %d, num_samps: %d\n", samps_left, num_samps); + printf("samps_left: %d, num_samps: %d\n", samps_left, num_samps); #endif - if(g_audio_enable != 0) { + if(g_audio_enable != 0) { - if(snd_buf_init) { - /* convert sound buf */ + if(snd_buf_init) { + /* convert sound buf */ - for(i = 0; i < num_samps; i++) { - val0 = outptr[0]; - val1 = outptr[1]; - val = val0; - if(val0 > 32767) { - val = 32767; - } - if(val0 < -32768) { - val = -32768; - } + for(i = 0; i < num_samps; i++) { + val0 = outptr[0]; + val1 = outptr[1]; + val = val0; + if(val0 > 32767) { + val = 32767; + } + if(val0 < -32768) { + val = -32768; + } - val0 = val; - val = val1; - if(val1 > 32767) { - val = 32767; - } - if(val1 < -32768) { - val = -32768; - } + val0 = val; + val = val1; + if(val1 > 32767) { + val = 32767; + } + if(val1 < -32768) { + val = -32768; + } - outptr += 2; + outptr += 2; #if defined(__linux__) || defined(OSS) - /* Linux seems to expect little-endian */ - /* samples always, even on PowerPC */ + /* Linux seems to expect little-endian */ + /* samples always, even on PowerPC */ #if defined(GSPLUS_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) // OSX needs to calculate endianness mid-compilation, can't be passed on compile command - sndptr[pos] = (val << 16) + (val0 & 0xffff); + sndptr[pos] = (val << 16) + (val0 & 0xffff); # else - sndptr[pos] = ((val & 0xff) << 24) + - ((val & 0xff00) << 8) + - ((val0 & 0xff) << 8) + - ((val0 >> 8) & 0xff); + sndptr[pos] = ((val & 0xff) << 24) + + ((val & 0xff00) << 8) + + ((val0 & 0xff) << 8) + + ((val0 >> 8) & 0xff); # endif #else #if defined(GSPLUS_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) // OSX needs to calculate endianness mid-compilation, can't be passed on compile command - sndptr[pos] = (val << 16) + (val0 & 0xffff); + sndptr[pos] = (val << 16) + (val0 & 0xffff); # else - sndptr[pos] = (val0 << 16) + (val & 0xffff); + sndptr[pos] = (val0 << 16) + (val & 0xffff); # endif #endif - // printf("%08x ", sndptr[pos]); - pos++; - if(pos >= SOUND_SHM_SAMP_SIZE) { - pos = 0; - } - } + // printf("%08x ", sndptr[pos]); + pos++; + if(pos >= SOUND_SHM_SAMP_SIZE) { + pos = 0; + } + } - if(g_queued_nonsamps) { - /* force out old 0 samps */ - send_sound(0, g_queued_nonsamps); - g_queued_nonsamps = 0; - } + if(g_queued_nonsamps) { + /* force out old 0 samps */ + send_sound(0, g_queued_nonsamps); + g_queued_nonsamps = 0; + } - if(g_send_sound_to_file) { - send_sound_to_file(g_sound_shm_addr, - g_sound_shm_pos, num_samps); - } + if(g_send_sound_to_file) { + send_sound_to_file(g_sound_shm_addr, + g_sound_shm_pos, num_samps); + } - g_queued_samps += num_samps; - } else { - /* move pos */ - pos += num_samps; - while(pos >= SOUND_SHM_SAMP_SIZE) { - pos -= SOUND_SHM_SAMP_SIZE; - } + g_queued_samps += num_samps; + } else { + /* move pos */ + pos += num_samps; + while(pos >= SOUND_SHM_SAMP_SIZE) { + pos -= SOUND_SHM_SAMP_SIZE; + } - if(g_send_sound_to_file) { - send_sound_to_file(zero_buf, g_sound_shm_pos, - num_samps); - } + if(g_send_sound_to_file) { + send_sound_to_file(zero_buf, g_sound_shm_pos, + num_samps); + } - if(g_queued_samps) { - /* force out old non-0 samps */ - send_sound(1, g_queued_samps); - g_queued_samps = 0; - } + if(g_queued_samps) { + /* force out old non-0 samps */ + send_sound(1, g_queued_samps); + g_queued_samps = 0; + } - g_queued_nonsamps += num_samps; - } + g_queued_nonsamps += num_samps; + } - } + } - g_sound_shm_pos = pos; + g_sound_shm_pos = pos; - GET_ITIMER(end_time3); + GET_ITIMER(end_time3); - g_fvoices += ((float)(samps_played) * (float)(g_drecip_audio_rate)); + g_fvoices += ((float)(samps_played) * (float)(g_drecip_audio_rate)); - if(g_audio_enable != 0) { - if(g_queued_samps >= (g_audio_rate/32)) { - send_sound(1, g_queued_samps); - g_queued_samps = 0; - } + if(g_audio_enable != 0) { + if(g_queued_samps >= (g_audio_rate/32)) { + send_sound(1, g_queued_samps); + g_queued_samps = 0; + } - if(g_queued_nonsamps >= (g_audio_rate/32)) { - send_sound(0, g_queued_nonsamps); - g_queued_nonsamps = 0; - } - } + if(g_queued_nonsamps >= (g_audio_rate/32)) { + send_sound(0, g_queued_nonsamps); + g_queued_nonsamps = 0; + } + } - GET_ITIMER(end_time1); + GET_ITIMER(end_time1); - g_cycs_in_sound1 += (end_time1 - start_time1); - g_cycs_in_sound3 += (end_time3 - start_time3); - g_cycs_in_sound4 += (start_time2 - start_time4); + g_cycs_in_sound1 += (end_time1 - start_time1); + g_cycs_in_sound3 += (end_time3 - start_time3); + g_cycs_in_sound4 += (start_time2 - start_time4); - g_last_sound_play_dsamp = dsamp_now; + g_last_sound_play_dsamp = dsamp_now; - g_sound_play_depth--; + g_sound_play_depth--; } -void -doc_handle_event(int osc, double dcycs) -{ - double dsamps; +void doc_handle_event(int osc, double dcycs) { + double dsamps; - /* handle osc stopping and maybe interrupting */ + /* handle osc stopping and maybe interrupting */ - g_num_doc_events++; + g_num_doc_events++; - dsamps = dcycs * g_dsamps_per_dcyc; + dsamps = dcycs * g_dsamps_per_dcyc; - DOC_LOG("doc_ev", osc, dcycs, 0); + DOC_LOG("doc_ev", osc, dcycs, 0); - g_doc_regs[osc].event = 0; + g_doc_regs[osc].event = 0; - sound_play(dsamps); + sound_play(dsamps); } -void -doc_sound_end(int osc, int can_repeat, double eff_dsamps, double dsamps) -{ - Doc_reg *rptr, *orptr; - int mode, omode; - int other_osc; - int ctl; +void doc_sound_end(int osc, int can_repeat, double eff_dsamps, double dsamps) { + Doc_reg *rptr, *orptr; + int mode, omode; + int other_osc; + int ctl; - /* handle osc stopping and maybe interrupting */ + /* handle osc stopping and maybe interrupting */ - if(osc < 0 || osc > 31) { - printf("doc_handle_event: osc: %d!\n", osc); - return; - } + if(osc < 0 || osc > 31) { + printf("doc_handle_event: osc: %d!\n", osc); + return; + } - rptr = &(g_doc_regs[osc]); - ctl = rptr->ctl; + rptr = &(g_doc_regs[osc]); + ctl = rptr->ctl; - if(rptr->event) { - remove_event_doc(osc); - } - rptr->event = 0; + if(rptr->event) { + remove_event_doc(osc); + } + rptr->event = 0; - /* check to make sure osc is running */ - if(ctl & 0x01) { - /* Oscillator already stopped. */ - halt_printf("Osc %d interrupt, but it was already stop!\n",osc); + /* check to make sure osc is running */ + if(ctl & 0x01) { + /* Oscillator already stopped. */ + halt_printf("Osc %d interrupt, but it was already stop!\n",osc); #ifdef HPUX - U_STACK_TRACE(); + U_STACK_TRACE(); #endif - return; - } + return; + } - if(ctl & 0x08) { - if(rptr->has_irq_pending == 0) { - add_sound_irq(osc); - } - } + if(ctl & 0x08) { + if(rptr->has_irq_pending == 0) { + add_sound_irq(osc); + } + } - if(!rptr->running) { - halt_printf("Doc event for osc %d, but ! running\n", osc); - } + if(!rptr->running) { + halt_printf("Doc event for osc %d, but ! running\n", osc); + } - rptr->running = 0; + rptr->running = 0; - mode = (ctl >> 1) & 3; - other_osc = osc ^ 1; - orptr = &(g_doc_regs[other_osc]); - omode = (orptr->ctl >> 1) & 3; + mode = (ctl >> 1) & 3; + other_osc = osc ^ 1; + orptr = &(g_doc_regs[other_osc]); + omode = (orptr->ctl >> 1) & 3; - /* If either this osc or it's partner is in swap mode, treat the */ - /* pair as being in swap mode. This Ensoniq feature pointed out */ - /* by Ian Schmidt */ - if(mode == 0 && can_repeat) { - /* free-running mode with no 0 byte! */ - /* start doing it again */ + /* If either this osc or it's partner is in swap mode, treat the */ + /* pair as being in swap mode. This Ensoniq feature pointed out */ + /* by Ian Schmidt */ + if(mode == 0 && can_repeat) { + /* free-running mode with no 0 byte! */ + /* start doing it again */ - start_sound(osc, eff_dsamps, dsamps); + start_sound(osc, eff_dsamps, dsamps); - return; - } - rptr->cur_acc = 0; /* reset internal accumulator*/ - if((mode == 3) || (omode == 3)) { - /* swap mode (even if we're one_shot and partner is swap)! */ - rptr->ctl |= 1; - if(!orptr->running && - (orptr->ctl & 0x1)) { - orptr->ctl = orptr->ctl & (~1); - start_sound(other_osc, eff_dsamps, dsamps); - } - return; - } else { - /* stop the oscillator */ - rptr->ctl |= 1; - } + return; + } + rptr->cur_acc = 0; /* reset internal accumulator*/ + if((mode == 3) || (omode == 3)) { + /* swap mode (even if we're one_shot and partner is swap)! */ + rptr->ctl |= 1; + if(!orptr->running && + (orptr->ctl & 0x1)) { + orptr->ctl = orptr->ctl & (~1); + start_sound(other_osc, eff_dsamps, dsamps); + } + return; + } else { + /* stop the oscillator */ + rptr->ctl |= 1; + } - return; + return; } -void -add_sound_irq(int osc) -{ - int num_osc_interrupting; +void add_sound_irq(int osc) { + int num_osc_interrupting; - if(g_doc_regs[osc].has_irq_pending) { - halt_printf("Adding sound_irq for %02x, but irq_p: %d\n", osc, - g_doc_regs[osc].has_irq_pending); - } + if(g_doc_regs[osc].has_irq_pending) { + halt_printf("Adding sound_irq for %02x, but irq_p: %d\n", osc, + g_doc_regs[osc].has_irq_pending); + } - num_osc_interrupting = g_num_osc_interrupting + 1; - g_doc_regs[osc].has_irq_pending = num_osc_interrupting; - g_num_osc_interrupting = num_osc_interrupting; + num_osc_interrupting = g_num_osc_interrupting + 1; + g_doc_regs[osc].has_irq_pending = num_osc_interrupting; + g_num_osc_interrupting = num_osc_interrupting; - add_irq(IRQ_PENDING_DOC); - if(num_osc_interrupting == 1) { - doc_reg_e0 = 0x00 + (osc << 1); - } + add_irq(IRQ_PENDING_DOC); + if(num_osc_interrupting == 1) { + doc_reg_e0 = 0x00 + (osc << 1); + } - DOC_LOG("add_irq", osc, g_cur_dcycs * g_dsamps_per_dcyc, 0); + DOC_LOG("add_irq", osc, g_cur_dcycs * g_dsamps_per_dcyc, 0); } -void -remove_sound_irq(int osc, int must) -{ - Doc_reg *rptr; - int num_osc_interrupt; - int has_irq_pending; - int first; - int i; +void remove_sound_irq(int osc, int must) { + Doc_reg *rptr; + int num_osc_interrupt; + int has_irq_pending; + int first; + int i; - doc_printf("remove irq for osc: %d, has_irq: %d\n", - osc, g_doc_regs[osc].has_irq_pending); + doc_printf("remove irq for osc: %d, has_irq: %d\n", + osc, g_doc_regs[osc].has_irq_pending); - num_osc_interrupt = g_doc_regs[osc].has_irq_pending; - first = 0; - if(num_osc_interrupt) { - g_num_osc_interrupting--; - g_doc_regs[osc].has_irq_pending = 0; - DOC_LOG("rem_irq", osc, g_cur_dcycs * g_dsamps_per_dcyc, 0); - if(g_num_osc_interrupting == 0) { - remove_irq(IRQ_PENDING_DOC); - } + num_osc_interrupt = g_doc_regs[osc].has_irq_pending; + first = 0; + if(num_osc_interrupt) { + g_num_osc_interrupting--; + g_doc_regs[osc].has_irq_pending = 0; + DOC_LOG("rem_irq", osc, g_cur_dcycs * g_dsamps_per_dcyc, 0); + if(g_num_osc_interrupting == 0) { + remove_irq(IRQ_PENDING_DOC); + } - first = 0x40 | (doc_reg_e0 >> 1); - /* if none found, then def = no ints */ - for(i = 0; i < g_doc_num_osc_en; i++) { - rptr = &(g_doc_regs[i]); - has_irq_pending = rptr->has_irq_pending; - if(has_irq_pending > num_osc_interrupt) { - has_irq_pending--; - rptr->has_irq_pending = has_irq_pending; - } - if(has_irq_pending == 1) { - first = i; - } - } - if(num_osc_interrupt == 1) { - doc_reg_e0 = (first << 1); - } else { + first = 0x40 | (doc_reg_e0 >> 1); + /* if none found, then def = no ints */ + for(i = 0; i < g_doc_num_osc_en; i++) { + rptr = &(g_doc_regs[i]); + has_irq_pending = rptr->has_irq_pending; + if(has_irq_pending > num_osc_interrupt) { + has_irq_pending--; + rptr->has_irq_pending = has_irq_pending; + } + if(has_irq_pending == 1) { + first = i; + } + } + if(num_osc_interrupt == 1) { + doc_reg_e0 = (first << 1); + } else { #if 0 - halt_printf("remove_sound_irq[%02x]=%d, first:%d\n", - osc, num_osc_interrupt, first); + halt_printf("remove_sound_irq[%02x]=%d, first:%d\n", + osc, num_osc_interrupt, first); #endif - } - } else { + } + } else { #if 0 - /* make sure no int pending */ - if(doc_reg_e0 != 0xff) { - halt_printf("remove_sound_irq[%02x]=0, but e0: %02x\n", - osc, doc_reg_e0); - } + /* make sure no int pending */ + if(doc_reg_e0 != 0xff) { + halt_printf("remove_sound_irq[%02x]=0, but e0: %02x\n", + osc, doc_reg_e0); + } #endif - if(must) { - halt_printf("REMOVE_sound_irq[%02x]=0, but e0: %02x\n", - osc, doc_reg_e0); - } - } + if(must) { + halt_printf("REMOVE_sound_irq[%02x]=0, but e0: %02x\n", + osc, doc_reg_e0); + } + } - if(doc_reg_e0 & 0x80) { - for(i = 0; i < 0x20; i++) { - has_irq_pending = g_doc_regs[i].has_irq_pending; - if(has_irq_pending) { - halt_printf("remove_sound_irq[%02x], but " - "[%02x]=%d!\n", osc,i,has_irq_pending); - printf("num_osc_int: %d, first: %02x\n", - num_osc_interrupt, first); - } - } - } + if(doc_reg_e0 & 0x80) { + for(i = 0; i < 0x20; i++) { + has_irq_pending = g_doc_regs[i].has_irq_pending; + if(has_irq_pending) { + halt_printf("remove_sound_irq[%02x], but " + "[%02x]=%d!\n", osc,i,has_irq_pending); + printf("num_osc_int: %d, first: %02x\n", + num_osc_interrupt, first); + } + } + } } -void -start_sound(int osc, double eff_dsamps, double dsamps) -{ - register word32 start_time1; - register word32 end_time1; - Doc_reg *rptr; - int ctl; - int mode; - word32 sz; - word32 size; - word32 wave_size; +void start_sound(int osc, double eff_dsamps, double dsamps) { + register word32 start_time1; + register word32 end_time1; + Doc_reg *rptr; + int ctl; + int mode; + word32 sz; + word32 size; + word32 wave_size; - if(osc < 0 || osc > 31) { - halt_printf("start_sound: osc: %02x!\n", osc); - } + if(osc < 0 || osc > 31) { + halt_printf("start_sound: osc: %02x!\n", osc); + } - g_num_start_sounds++; + g_num_start_sounds++; - rptr = &(g_doc_regs[osc]); + rptr = &(g_doc_regs[osc]); - if(osc >= g_doc_num_osc_en) { - rptr->ctl |= 1; - return; - } + if(osc >= g_doc_num_osc_en) { + rptr->ctl |= 1; + return; + } - GET_ITIMER(start_time1); + GET_ITIMER(start_time1); - ctl = rptr->ctl; + ctl = rptr->ctl; - mode = (ctl >> 1) & 3; + mode = (ctl >> 1) & 3; - wave_size = rptr->wavesize; + wave_size = rptr->wavesize; - sz = ((wave_size >> 3) & 7) + 8; - size = 1 << sz; + sz = ((wave_size >> 3) & 7) + 8; + size = 1 << sz; - if(size < 0x100) { - halt_printf("size: %08x is too small, sz: %08x!\n", size, sz); - } + if(size < 0x100) { + halt_printf("size: %08x is too small, sz: %08x!\n", size, sz); + } - if(rptr->running) { - halt_printf("start_sound osc: %d, already running!\n", osc); - } + if(rptr->running) { + halt_printf("start_sound osc: %d, already running!\n", osc); + } - rptr->running = 1; + rptr->running = 1; - rptr->complete_dsamp = eff_dsamps; + rptr->complete_dsamp = eff_dsamps; - doc_printf("Starting osc %02x, dsamp: %f\n", osc, dsamps); - doc_printf("size: %04x\n", size); + doc_printf("Starting osc %02x, dsamp: %f\n", osc, dsamps); + doc_printf("size: %04x\n", size); - if((mode == 2) && ((osc & 1) == 0)) { - printf("Sync mode osc %d starting!\n", osc); - /* set_halt(1); */ + if((mode == 2) && ((osc & 1) == 0)) { + printf("Sync mode osc %d starting!\n", osc); + /* set_halt(1); */ - /* see if we should start our odd partner */ - if((rptr[1].ctl & 7) == 5) { - /* odd partner stopped in sync mode--start him */ - rptr[1].ctl &= (~1); - start_sound(osc + 1, eff_dsamps, dsamps); - } else { - printf("Osc %d starting sync, but osc %d ctl: %02x\n", - osc, osc+1, rptr[1].ctl); - } - } + /* see if we should start our odd partner */ + if((rptr[1].ctl & 7) == 5) { + /* odd partner stopped in sync mode--start him */ + rptr[1].ctl &= (~1); + start_sound(osc + 1, eff_dsamps, dsamps); + } else { + printf("Osc %d starting sync, but osc %d ctl: %02x\n", + osc, osc+1, rptr[1].ctl); + } + } - wave_end_estimate(osc, eff_dsamps, dsamps); + wave_end_estimate(osc, eff_dsamps, dsamps); - DOC_LOG("st playing", osc, eff_dsamps, size); + DOC_LOG("st playing", osc, eff_dsamps, size); #if 0 - if(rptr->cur_acc != 0) { - halt_printf("Start osc %02x, acc: %08x\n", osc, rptr->cur_acc); - } + if(rptr->cur_acc != 0) { + halt_printf("Start osc %02x, acc: %08x\n", osc, rptr->cur_acc); + } #endif - GET_ITIMER(end_time1); + GET_ITIMER(end_time1); - g_cycs_in_start_sound += (end_time1 - start_time1); + g_cycs_in_start_sound += (end_time1 - start_time1); } -void -wave_end_estimate(int osc, double eff_dsamps, double dsamps) -{ - register word32 start_time1; - register word32 end_time1; - Doc_reg *rptr; - byte *ptr1; - double event_dsamp; - double event_dcycs; - double dcycs_per_samp; - double dsamps_per_byte; - double num_dsamps; - double dcur_inc; - word32 tmp1; - word32 cur_inc; - word32 save_val; - int save_size; - int pos; - int size; - int estimate; +void wave_end_estimate(int osc, double eff_dsamps, double dsamps) { + register word32 start_time1; + register word32 end_time1; + Doc_reg *rptr; + byte *ptr1; + double event_dsamp; + double event_dcycs; + double dcycs_per_samp; + double dsamps_per_byte; + double num_dsamps; + double dcur_inc; + word32 tmp1; + word32 cur_inc; + word32 save_val; + int save_size; + int pos; + int size; + int estimate; - GET_ITIMER(start_time1); + GET_ITIMER(start_time1); - dcycs_per_samp = g_dcycs_per_samp; + dcycs_per_samp = g_dcycs_per_samp; - rptr = &(g_doc_regs[osc]); + rptr = &(g_doc_regs[osc]); - cur_inc = rptr->cur_inc; - dcur_inc = (double)cur_inc; - dsamps_per_byte = 0.0; - if(cur_inc) { - dsamps_per_byte = SND_PTR_SHIFT_DBL / (double)dcur_inc; - } + cur_inc = rptr->cur_inc; + dcur_inc = (double)cur_inc; + dsamps_per_byte = 0.0; + if(cur_inc) { + dsamps_per_byte = SND_PTR_SHIFT_DBL / (double)dcur_inc; + } - /* see if there's a zero byte */ - tmp1 = rptr->cur_start + (rptr->cur_acc & rptr->cur_mask); - pos = tmp1 >> SND_PTR_SHIFT; - size = ((rptr->cur_end) >> SND_PTR_SHIFT) - pos; + /* see if there's a zero byte */ + tmp1 = rptr->cur_start + (rptr->cur_acc & rptr->cur_mask); + pos = tmp1 >> SND_PTR_SHIFT; + size = ((rptr->cur_end) >> SND_PTR_SHIFT) - pos; - ptr1 = &doc_ram[pos]; + ptr1 = &doc_ram[pos]; - estimate = 0; - if(rptr->ctl & 0x08 || g_doc_regs[osc ^ 1].ctl & 0x08) { - estimate = 1; - } + estimate = 0; + if(rptr->ctl & 0x08 || g_doc_regs[osc ^ 1].ctl & 0x08) { + estimate = 1; + } #if 0 - estimate = 1; + estimate = 1; #endif - if(estimate) { - save_size = size; - save_val = ptr1[size]; - ptr1[size] = 0; - size = strlen((char *)ptr1); - ptr1[save_size] = save_val; - } + if(estimate) { + save_size = size; + save_val = ptr1[size]; + ptr1[size] = 0; + size = strlen((char *)ptr1); + ptr1[save_size] = save_val; + } - /* calc samples to play */ - num_dsamps = (dsamps_per_byte * (double)size) + 1.0; + /* calc samples to play */ + num_dsamps = (dsamps_per_byte * (double)size) + 1.0; - rptr->samps_left = (int)num_dsamps; + rptr->samps_left = (int)num_dsamps; - if(rptr->event) { - remove_event_doc(osc); - } - rptr->event = 0; + if(rptr->event) { + remove_event_doc(osc); + } + rptr->event = 0; - event_dsamp = eff_dsamps + num_dsamps; - if(estimate) { - rptr->event = 1; - rptr->dsamp_ev = event_dsamp; - rptr->dsamp_ev2 = dsamps; - event_dcycs = (event_dsamp * dcycs_per_samp) + 1.0; - add_event_doc(event_dcycs, osc); - } + event_dsamp = eff_dsamps + num_dsamps; + if(estimate) { + rptr->event = 1; + rptr->dsamp_ev = event_dsamp; + rptr->dsamp_ev2 = dsamps; + event_dcycs = (event_dsamp * dcycs_per_samp) + 1.0; + add_event_doc(event_dcycs, osc); + } - GET_ITIMER(end_time1); + GET_ITIMER(end_time1); - g_cycs_in_est_sound += (end_time1 - start_time1); + g_cycs_in_est_sound += (end_time1 - start_time1); } -void -remove_sound_event(int osc) -{ - if(g_doc_regs[osc].event) { - g_doc_regs[osc].event = 0; - remove_event_doc(osc); - } +void remove_sound_event(int osc) { + if(g_doc_regs[osc].event) { + g_doc_regs[osc].event = 0; + remove_event_doc(osc); + } } -void -doc_write_ctl_reg(int osc, int val, double dsamps) -{ - Doc_reg *rptr; - double eff_dsamps; - word32 old_halt; - word32 new_halt; - int old_val; - int mode; +void doc_write_ctl_reg(int osc, int val, double dsamps) { + Doc_reg *rptr; + double eff_dsamps; + word32 old_halt; + word32 new_halt; + int old_val; + int mode; - if(osc < 0 || osc >= 0x20) { - halt_printf("doc_write_ctl_reg: osc: %02x, val: %02x\n", - osc, val); - return; - } + if(osc < 0 || osc >= 0x20) { + halt_printf("doc_write_ctl_reg: osc: %02x, val: %02x\n", + osc, val); + return; + } - eff_dsamps = dsamps; - rptr = &(g_doc_regs[osc]); - old_val = rptr->ctl; - g_doc_saved_ctl = old_val; + eff_dsamps = dsamps; + rptr = &(g_doc_regs[osc]); + old_val = rptr->ctl; + g_doc_saved_ctl = old_val; - if(old_val == val) { - return; - } + if(old_val == val) { + return; + } - DOC_LOG("ctl_reg", osc, dsamps, (old_val << 16) + val); + DOC_LOG("ctl_reg", osc, dsamps, (old_val << 16) + val); - mode = (val >> 1) & 3; + mode = (val >> 1) & 3; - old_halt = (old_val & 1); - new_halt = (val & 1); + old_halt = (old_val & 1); + new_halt = (val & 1); - /* bits are: 28: old int bit */ - /* 29: old halt bit */ - /* 30: new int bit */ - /* 31: new halt bit */ + /* bits are: 28: old int bit */ + /* 29: old halt bit */ + /* 30: new int bit */ + /* 31: new halt bit */ #if 0 - if(osc == 0x10) { - printf("osc %d new_ctl: %02x, old: %02x\n", osc, val, old_val); - } + if(osc == 0x10) { + printf("osc %d new_ctl: %02x, old: %02x\n", osc, val, old_val); + } #endif - /* no matter what, remove any pending IRQs on this osc */ - remove_sound_irq(osc, 0); + /* no matter what, remove any pending IRQs on this osc */ + remove_sound_irq(osc, 0); #if 0 - if(old_halt) { - printf("doc_write_ctl to osc %d, val: %02x, old: %02x\n", - osc, val, old_val); - } + if(old_halt) { + printf("doc_write_ctl to osc %d, val: %02x, old: %02x\n", + osc, val, old_val); + } #endif - if(new_halt != 0) { - /* make sure sound is stopped */ - remove_sound_event(osc); - if(old_halt == 0) { - /* it was playing, finish it up */ + if(new_halt != 0) { + /* make sure sound is stopped */ + remove_sound_event(osc); + if(old_halt == 0) { + /* it was playing, finish it up */ #if 0 - halt_printf("Aborted osc %d at eff_dsamps: %f, ctl: " - "%02x, oldctl: %02x\n", osc, eff_dsamps, - val, old_val); + halt_printf("Aborted osc %d at eff_dsamps: %f, ctl: " + "%02x, oldctl: %02x\n", osc, eff_dsamps, + val, old_val); #endif - sound_play(eff_dsamps); - } - if(((old_val >> 1) & 3) > 0) { - /* don't clear acc if free-running */ - g_doc_regs[osc].cur_acc = 0; - } + sound_play(eff_dsamps); + } + if(((old_val >> 1) & 3) > 0) { + /* don't clear acc if free-running */ + g_doc_regs[osc].cur_acc = 0; + } - g_doc_regs[osc].ctl = val; - g_doc_regs[osc].running = 0; - } else { - /* new halt == 0 = make sure sound is running */ - if(old_halt != 0) { - /* start sound */ - DOC_LOG("ctl_sound_play", osc, eff_dsamps, val); + g_doc_regs[osc].ctl = val; + g_doc_regs[osc].running = 0; + } else { + /* new halt == 0 = make sure sound is running */ + if(old_halt != 0) { + /* start sound */ + DOC_LOG("ctl_sound_play", osc, eff_dsamps, val); - // OG If the sound_play is executed, it may restart a oscillo we thought was stopped at time, - // hence crashing the start_sound function (cf. game Arrgh!) - //sound_play(eff_dsamps); - g_doc_regs[osc].ctl = val; + // OG If the sound_play is executed, it may restart a oscillo we thought was stopped at time, + // hence crashing the start_sound function (cf. game Arrgh!) + //sound_play(eff_dsamps); + g_doc_regs[osc].ctl = val; - start_sound(osc, eff_dsamps, dsamps); - } else { - /* was running, and something changed */ - doc_printf("osc %d old ctl:%02x new:%02x!\n", - osc, old_val, val); + start_sound(osc, eff_dsamps, dsamps); + } else { + /* was running, and something changed */ + doc_printf("osc %d old ctl:%02x new:%02x!\n", + osc, old_val, val); #if 0 - sound_play(eff_dsamps); + sound_play(eff_dsamps); /* HACK: fix this??? */ #endif - g_doc_regs[osc].ctl = val; - if((old_val ^ val) & val & 0x8) { - /* now has ints on */ - wave_end_estimate(osc, dsamps, dsamps); - } - } - } + g_doc_regs[osc].ctl = val; + if((old_val ^ val) & val & 0x8) { + /* now has ints on */ + wave_end_estimate(osc, dsamps, dsamps); + } + } + } } -void -doc_recalc_sound_parms(int osc, double eff_dcycs, double dsamps) -{ - Doc_reg *rptr; - double dfreq; - double dtmp1; - double dacc, dacc_recip; - word32 res; - word32 sz; - word32 size; - word32 wave_size; - word32 cur_start; - word32 shifted_size; +void doc_recalc_sound_parms(int osc, double eff_dcycs, double dsamps) { + Doc_reg *rptr; + double dfreq; + double dtmp1; + double dacc, dacc_recip; + word32 res; + word32 sz; + word32 size; + word32 wave_size; + word32 cur_start; + word32 shifted_size; - g_num_recalc_snd_parms++; + g_num_recalc_snd_parms++; - rptr = &(g_doc_regs[osc]); + rptr = &(g_doc_regs[osc]); - wave_size = rptr->wavesize; + wave_size = rptr->wavesize; - dfreq = (double)rptr->freq; + dfreq = (double)rptr->freq; - sz = ((wave_size >> 3) & 7) + 8; - size = 1 << sz; - rptr->size_bytes = size; - res = wave_size & 7; + sz = ((wave_size >> 3) & 7) + 8; + size = 1 << sz; + rptr->size_bytes = size; + res = wave_size & 7; - shifted_size = size << SND_PTR_SHIFT; + shifted_size = size << SND_PTR_SHIFT; - cur_start = (rptr->waveptr << (8 + SND_PTR_SHIFT)) & (-(int)shifted_size); // OG + cur_start = (rptr->waveptr << (8 + SND_PTR_SHIFT)) & (-(int)shifted_size); // OG - dtmp1 = dfreq * (DOC_SCAN_RATE * g_drecip_audio_rate); - dacc = (double)(1 << (20 - (17 - sz + res))); - dacc_recip = (SND_PTR_SHIFT_DBL) / ((double)(1 << 20)); - dtmp1 = dtmp1 * g_drecip_osc_en_plus_2 * dacc * dacc_recip; + dtmp1 = dfreq * (DOC_SCAN_RATE * g_drecip_audio_rate); + dacc = (double)(1 << (20 - (17 - sz + res))); + dacc_recip = (SND_PTR_SHIFT_DBL) / ((double)(1 << 20)); + dtmp1 = dtmp1 * g_drecip_osc_en_plus_2 * dacc * dacc_recip; - rptr->cur_inc = (int)(dtmp1); - rptr->cur_start = cur_start; - rptr->cur_end = cur_start + shifted_size; - rptr->cur_mask = (shifted_size - 1); + rptr->cur_inc = (int)(dtmp1); + rptr->cur_start = cur_start; + rptr->cur_end = cur_start + shifted_size; + rptr->cur_mask = (shifted_size - 1); - DOC_LOG("recalc", osc, dsamps, (rptr->waveptr << 16) + wave_size); + DOC_LOG("recalc", osc, dsamps, (rptr->waveptr << 16) + wave_size); } -int -doc_read_c030(double dcycs) -{ - int num; +int doc_read_c030(double dcycs) { + int num; - num = g_num_c030_fsamps; - if(num >= MAX_C030_TIMES) { - halt_printf("Too many clicks per vbl: %d\n", num); - return 0; - } + num = g_num_c030_fsamps; + if(num >= MAX_C030_TIMES) { + halt_printf("Too many clicks per vbl: %d\n", num); + return 0; + } - c030_fsamps[num] = (float)(dcycs * g_dsamps_per_dcyc - - g_last_sound_play_dsamp); - g_num_c030_fsamps = num + 1; + c030_fsamps[num] = (float)(dcycs * g_dsamps_per_dcyc - + g_last_sound_play_dsamp); + g_num_c030_fsamps = num + 1; - doc_printf("read c030, num this vbl: %04x\n", num); + doc_printf("read c030, num this vbl: %04x\n", num); - return 0; + return 0; } -int -doc_read_c03c(double dcycs) -{ - return doc_sound_ctl; +int doc_read_c03c(double dcycs) { + return doc_sound_ctl; } -int -doc_read_c03d(double dcycs) -{ - double dsamps; - Doc_reg *rptr; - int osc; - int type; - int ret; +int doc_read_c03d(double dcycs) { + double dsamps; + Doc_reg *rptr; + int osc; + int type; + int ret; - ret = doc_saved_val; - dsamps = dcycs * g_dsamps_per_dcyc; + ret = doc_saved_val; + dsamps = dcycs * g_dsamps_per_dcyc; - if(doc_sound_ctl & 0x40) { - /* Read RAM */ - doc_saved_val = doc_ram[g_c03ef_doc_ptr]; - } else { - /* Read DOC */ - doc_saved_val = 0; + if(doc_sound_ctl & 0x40) { + /* Read RAM */ + doc_saved_val = doc_ram[g_c03ef_doc_ptr]; + } else { + /* Read DOC */ + doc_saved_val = 0; - osc = g_c03ef_doc_ptr & 0x1f; - type = (g_c03ef_doc_ptr >> 5) & 0x7; - rptr = &(g_doc_regs[osc]); + osc = g_c03ef_doc_ptr & 0x1f; + type = (g_c03ef_doc_ptr >> 5) & 0x7; + rptr = &(g_doc_regs[osc]); - switch(type) { - case 0x0: /* freq lo */ - doc_saved_val = rptr->freq & 0xff; - break; - case 0x1: /* freq hi */ - doc_saved_val = rptr->freq >> 8; - break; - case 0x2: /* vol */ - doc_saved_val = rptr->vol; - break; - case 0x3: /* data register */ - /* HACK: make this call sound_play sometimes */ - doc_saved_val = rptr->last_samp_val; - break; - case 0x4: /* wave ptr register */ - doc_saved_val = rptr->waveptr; - break; - case 0x5: /* control register */ - doc_saved_val = rptr->ctl; - break; - case 0x6: /* control register */ - doc_saved_val = rptr->wavesize; - break; - case 0x7: /* 0xe0-0xff */ - switch(osc) { - case 0x00: /* 0xe0 */ - doc_saved_val = doc_reg_e0; - doc_printf("Reading doc 0xe0, ret: %02x\n", - doc_saved_val); + switch(type) { + case 0x0: /* freq lo */ + doc_saved_val = rptr->freq & 0xff; + break; + case 0x1: /* freq hi */ + doc_saved_val = rptr->freq >> 8; + break; + case 0x2: /* vol */ + doc_saved_val = rptr->vol; + break; + case 0x3: /* data register */ + /* HACK: make this call sound_play sometimes */ + doc_saved_val = rptr->last_samp_val; + break; + case 0x4: /* wave ptr register */ + doc_saved_val = rptr->waveptr; + break; + case 0x5: /* control register */ + doc_saved_val = rptr->ctl; + break; + case 0x6: /* control register */ + doc_saved_val = rptr->wavesize; + break; + case 0x7: /* 0xe0-0xff */ + switch(osc) { + case 0x00: /* 0xe0 */ + doc_saved_val = doc_reg_e0; + doc_printf("Reading doc 0xe0, ret: %02x\n", + doc_saved_val); - /* Clear IRQ on read of e0, if any irq pend */ - if((doc_reg_e0 & 0x80) == 0) { - remove_sound_irq(doc_reg_e0 >> 1, 1); - } - break; - case 0x01: /* 0xe1 */ - doc_saved_val = (g_doc_num_osc_en - 1) << 1; - break; - case 0x02: /* 0xe2 */ - doc_saved_val = 0x80; + /* Clear IRQ on read of e0, if any irq pend */ + if((doc_reg_e0 & 0x80) == 0) { + remove_sound_irq(doc_reg_e0 >> 1, 1); + } + break; + case 0x01: /* 0xe1 */ + doc_saved_val = (g_doc_num_osc_en - 1) << 1; + break; + case 0x02: /* 0xe2 */ + doc_saved_val = 0x80; #if 0 - halt_printf("Reading doc 0xe2, ret: %02x\n", - doc_saved_val); + halt_printf("Reading doc 0xe2, ret: %02x\n", + doc_saved_val); #endif - break; - default: - doc_saved_val = 0; - halt_printf("Reading bad doc_reg[%04x]: %02x\n", - g_c03ef_doc_ptr, doc_saved_val); - } - break; - default: - doc_saved_val = 0; - halt_printf("Reading bad doc_reg[%04x]: %02x\n", - g_c03ef_doc_ptr, doc_saved_val); - } - } + break; + default: + doc_saved_val = 0; + halt_printf("Reading bad doc_reg[%04x]: %02x\n", + g_c03ef_doc_ptr, doc_saved_val); + } + break; + default: + doc_saved_val = 0; + halt_printf("Reading bad doc_reg[%04x]: %02x\n", + g_c03ef_doc_ptr, doc_saved_val); + } + } - doc_printf("read c03d, doc_ptr: %04x, ret: %02x, saved: %02x\n", - g_c03ef_doc_ptr, ret, doc_saved_val); + doc_printf("read c03d, doc_ptr: %04x, ret: %02x, saved: %02x\n", + g_c03ef_doc_ptr, ret, doc_saved_val); - DOC_LOG("read c03d", -1, dsamps, (g_c03ef_doc_ptr << 16) + - (doc_saved_val << 8) + ret); + DOC_LOG("read c03d", -1, dsamps, (g_c03ef_doc_ptr << 16) + + (doc_saved_val << 8) + ret); - if(doc_sound_ctl & 0x20) { - g_c03ef_doc_ptr = (g_c03ef_doc_ptr + 1) & 0xffff; - } + if(doc_sound_ctl & 0x20) { + g_c03ef_doc_ptr = (g_c03ef_doc_ptr + 1) & 0xffff; + } - return ret; + return ret; } -void -doc_write_c03c(int val, double dcycs) -{ - int vol; +void doc_write_c03c(int val, double dcycs) { + int vol; - vol = val & 0xf; - if(g_doc_vol != vol) { - /* don't bother playing sound..wait till next update */ - /* sound_play(dcycs); */ + vol = val & 0xf; + if(g_doc_vol != vol) { + /* don't bother playing sound..wait till next update */ + /* sound_play(dcycs); */ - g_doc_vol = vol; - doc_printf("Setting doc vol to 0x%x at %f\n", - vol, dcycs); - } - DOC_LOG("c03c write", -1, dcycs * g_dsamps_per_dcyc, val); + g_doc_vol = vol; + doc_printf("Setting doc vol to 0x%x at %f\n", + vol, dcycs); + } + DOC_LOG("c03c write", -1, dcycs * g_dsamps_per_dcyc, val); - doc_sound_ctl = val; + doc_sound_ctl = val; } -void -doc_write_c03d(int val, double dcycs) -{ - double dsamps; - double eff_dsamps; - Doc_reg *rptr; - int osc; - int type; - int ctl; - int tmp; - int i; +void doc_write_c03d(int val, double dcycs) { + double dsamps; + double eff_dsamps; + Doc_reg *rptr; + int osc; + int type; + int ctl; + int tmp; + int i; - val = val & 0xff; + val = val & 0xff; - dsamps = dcycs * g_dsamps_per_dcyc; - eff_dsamps = dsamps; - doc_printf("write c03d, doc_ptr: %04x, val: %02x\n", - g_c03ef_doc_ptr, val); + dsamps = dcycs * g_dsamps_per_dcyc; + eff_dsamps = dsamps; + doc_printf("write c03d, doc_ptr: %04x, val: %02x\n", + g_c03ef_doc_ptr, val); - DOC_LOG("write c03d", -1, dsamps, (g_c03ef_doc_ptr << 16) + val); + DOC_LOG("write c03d", -1, dsamps, (g_c03ef_doc_ptr << 16) + val); - if(doc_sound_ctl & 0x40) { - /* RAM */ - doc_ram[g_c03ef_doc_ptr] = val; - } else { - /* DOC */ - osc = g_c03ef_doc_ptr & 0x1f; - type = (g_c03ef_doc_ptr >> 5) & 0x7; + if(doc_sound_ctl & 0x40) { + /* RAM */ + doc_ram[g_c03ef_doc_ptr] = val; + } else { + /* DOC */ + osc = g_c03ef_doc_ptr & 0x1f; + type = (g_c03ef_doc_ptr >> 5) & 0x7; - rptr = &(g_doc_regs[osc]); - ctl = rptr->ctl; + rptr = &(g_doc_regs[osc]); + ctl = rptr->ctl; #if 0 - if((ctl & 1) == 0) { - if(type < 2 || type == 4 || type == 6) { - halt_printf("Osc %d is running, old ctl: %02x, " - "but write reg %02x=%02x\n", - osc, ctl, g_c03ef_doc_ptr & 0xff, val); - } - } + if((ctl & 1) == 0) { + if(type < 2 || type == 4 || type == 6) { + halt_printf("Osc %d is running, old ctl: %02x, " + "but write reg %02x=%02x\n", + osc, ctl, g_c03ef_doc_ptr & 0xff, val); + } + } #endif - switch(type) { - case 0x0: /* freq lo */ - if((rptr->freq & 0xff) == (word32)val) { - break; - } - if((ctl & 1) == 0) { - /* play through current status */ - DOC_LOG("flo_sound_play", osc, dsamps, val); - sound_play(dsamps); - } - rptr->freq = (rptr->freq & 0xff00) + val; - doc_recalc_sound_parms(osc, eff_dsamps, dsamps); - break; - case 0x1: /* freq hi */ - if((rptr->freq >> 8) == (word32)val) { - break; - } - if((ctl & 1) == 0) { - /* play through current status */ - DOC_LOG("fhi_sound_play", osc, dsamps, val); - sound_play(dsamps); - } - rptr->freq = (rptr->freq & 0xff) + (val << 8); - doc_recalc_sound_parms(osc, eff_dsamps, dsamps); - break; - case 0x2: /* vol */ - if(rptr->vol == (word32)val) { - break; - } - if((ctl & 1) == 0) { - /* play through current status */ - DOC_LOG("vol_sound_play", osc, dsamps, val); - sound_play(dsamps); + switch(type) { + case 0x0: /* freq lo */ + if((rptr->freq & 0xff) == (word32)val) { + break; + } + if((ctl & 1) == 0) { + /* play through current status */ + DOC_LOG("flo_sound_play", osc, dsamps, val); + sound_play(dsamps); + } + rptr->freq = (rptr->freq & 0xff00) + val; + doc_recalc_sound_parms(osc, eff_dsamps, dsamps); + break; + case 0x1: /* freq hi */ + if((rptr->freq >> 8) == (word32)val) { + break; + } + if((ctl & 1) == 0) { + /* play through current status */ + DOC_LOG("fhi_sound_play", osc, dsamps, val); + sound_play(dsamps); + } + rptr->freq = (rptr->freq & 0xff) + (val << 8); + doc_recalc_sound_parms(osc, eff_dsamps, dsamps); + break; + case 0x2: /* vol */ + if(rptr->vol == (word32)val) { + break; + } + if((ctl & 1) == 0) { + /* play through current status */ + DOC_LOG("vol_sound_play", osc, dsamps, val); + sound_play(dsamps); #if 0 - halt_printf("vol_sound_play at %.1f osc:%d " - "val:%d\n", dsamps, osc, val); + halt_printf("vol_sound_play at %.1f osc:%d " + "val:%d\n", dsamps, osc, val); #endif - } - rptr->vol = val; - break; - case 0x3: /* data register */ + } + rptr->vol = val; + break; + case 0x3: /* data register */ #if 0 - printf("Writing %02x into doc_data_reg[%02x]!\n", - val, osc); + printf("Writing %02x into doc_data_reg[%02x]!\n", + val, osc); #endif - break; - case 0x4: /* wave ptr register */ - if(rptr->waveptr == (word32)val) { - break; - } - if((ctl & 1) == 0) { - /* play through current status */ - DOC_LOG("wptr_sound_play", osc, dsamps, val); - sound_play(dsamps); - } - rptr->waveptr = val; - doc_recalc_sound_parms(osc, eff_dsamps, dsamps); - break; - case 0x5: /* control register */ + break; + case 0x4: /* wave ptr register */ + if(rptr->waveptr == (word32)val) { + break; + } + if((ctl & 1) == 0) { + /* play through current status */ + DOC_LOG("wptr_sound_play", osc, dsamps, val); + sound_play(dsamps); + } + rptr->waveptr = val; + doc_recalc_sound_parms(osc, eff_dsamps, dsamps); + break; + case 0x5: /* control register */ #if 0 - printf("doc_write ctl osc %d, val: %02x\n", osc, val); + printf("doc_write ctl osc %d, val: %02x\n", osc, val); #endif - if(rptr->ctl == (word32)val) { - break; - } - doc_write_ctl_reg(osc, val, dsamps); - break; - case 0x6: /* wavesize register */ - if(rptr->wavesize == (word32)val) { - break; - } - if((ctl & 1) == 0) { - /* play through current status */ - DOC_LOG("wsz_sound_play", osc, dsamps, val); - sound_play(dsamps); - } - rptr->wavesize = val; - doc_recalc_sound_parms(osc, eff_dsamps, dsamps); - break; - case 0x7: /* 0xe0-0xff */ - switch(osc) { - case 0x00: /* 0xe0 */ - doc_printf("writing doc 0xe0 with %02x, " - "was:%02x\n", val, doc_reg_e0); + if(rptr->ctl == (word32)val) { + break; + } + doc_write_ctl_reg(osc, val, dsamps); + break; + case 0x6: /* wavesize register */ + if(rptr->wavesize == (word32)val) { + break; + } + if((ctl & 1) == 0) { + /* play through current status */ + DOC_LOG("wsz_sound_play", osc, dsamps, val); + sound_play(dsamps); + } + rptr->wavesize = val; + doc_recalc_sound_parms(osc, eff_dsamps, dsamps); + break; + case 0x7: /* 0xe0-0xff */ + switch(osc) { + case 0x00: /* 0xe0 */ + doc_printf("writing doc 0xe0 with %02x, " + "was:%02x\n", val, doc_reg_e0); #if 0 - if(val != doc_reg_e0) { - halt_printf("writing doc 0xe0 with " - "%02x, was:%02x\n", val, - doc_reg_e0); - } + if(val != doc_reg_e0) { + halt_printf("writing doc 0xe0 with " + "%02x, was:%02x\n", val, + doc_reg_e0); + } #endif - break; - case 0x01: /* 0xe1 */ - doc_printf("Writing doc 0xe1 with %02x\n", val); - tmp = val & 0x3e; - tmp = (tmp >> 1) + 1; - if(tmp < 1) { - tmp = 1; - } - if(tmp > 32) { - halt_printf("doc 0xe1: %02x!\n", val); - tmp = 32; - } - g_doc_num_osc_en = tmp; - UPDATE_G_DCYCS_PER_DOC_UPDATE(tmp); + break; + case 0x01: /* 0xe1 */ + doc_printf("Writing doc 0xe1 with %02x\n", val); + tmp = val & 0x3e; + tmp = (tmp >> 1) + 1; + if(tmp < 1) { + tmp = 1; + } + if(tmp > 32) { + halt_printf("doc 0xe1: %02x!\n", val); + tmp = 32; + } + g_doc_num_osc_en = tmp; + UPDATE_G_DCYCS_PER_DOC_UPDATE(tmp); - // OG Update any oscs that were running to take care of the new numbers of oscillo - for(i = 0; ictl, rptr->waveptr, rptr->wavesize, rptr->freq, - rptr->vol, rptr->event, rptr->running, - rptr->has_irq_pending, rptr->size_bytes); - printf(" acc:%08x inc:%08x st:%08x end:%08x m:%08x\n", - rptr->cur_acc, rptr->cur_inc, rptr->cur_start, - rptr->cur_end, rptr->cur_mask); - printf(" compl_ds:%f samps_left:%d ev:%f ev2:%f\n", - rptr->complete_dsamp, rptr->samps_left, - rptr->dsamp_ev, rptr->dsamp_ev2); - } + for(i = 0; i < 32; i++) { + rptr = &(g_doc_regs[i]); + printf("%2d: ctl:%02x wp:%02x ws:%02x freq:%04x vol:%02x " + "ev:%d run:%d irq:%d sz:%04x\n", i, + rptr->ctl, rptr->waveptr, rptr->wavesize, rptr->freq, + rptr->vol, rptr->event, rptr->running, + rptr->has_irq_pending, rptr->size_bytes); + printf(" acc:%08x inc:%08x st:%08x end:%08x m:%08x\n", + rptr->cur_acc, rptr->cur_inc, rptr->cur_start, + rptr->cur_end, rptr->cur_mask); + printf(" compl_ds:%f samps_left:%d ev:%f ev2:%f\n", + rptr->complete_dsamp, rptr->samps_left, + rptr->dsamp_ev, rptr->dsamp_ev2); + } #if 0 - for(osc = 0; osc < 32; osc++) { - fmax = 0.0; - printf("osc %d has %d samps\n", osc, g_fsamp_num[osc]); - for(i = 0; i < g_fsamp_num[osc]; i++) { - printf("%4d: %f\n", i, g_fsamps[osc][i]); - fmax = MAX(fmax, g_fsamps[osc][i]); - } - printf("osc %d, fmax: %f\n", osc, fmax); - } + for(osc = 0; osc < 32; osc++) { + fmax = 0.0; + printf("osc %d has %d samps\n", osc, g_fsamp_num[osc]); + for(i = 0; i < g_fsamp_num[osc]; i++) { + printf("%4d: %f\n", i, g_fsamps[osc][i]); + fmax = MAX(fmax, g_fsamps[osc][i]); + } + printf("osc %d, fmax: %f\n", osc, fmax); + } #endif } diff --git a/src/sound_driver.c b/src/sound_driver.c index 39b0c11..322af25 100644 --- a/src/sound_driver.c +++ b/src/sound_driver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include "sound.h" @@ -21,7 +21,7 @@ long sound_init_device_sdl(); # include #endif -#ifndef WIN_SOUND /* Workaround - gcc in cygwin wasn't defining _WIN32 */ +#ifndef WIN_SOUND /* Workaround - gcc in cygwin wasn't defining _WIN32 */ # include # include #endif @@ -34,22 +34,22 @@ extern int Verbose; extern int g_audio_rate; -int g_preferred_rate = 48000; -int g_audio_socket = -1; -int g_bytes_written = 0; +int g_preferred_rate = 48000; +int g_audio_socket = -1; +int g_bytes_written = 0; -#define ZERO_BUF_SIZE 2048 +#define ZERO_BUF_SIZE 2048 word32 g_snd_zero_buf[ZERO_BUF_SIZE]; -#define ZERO_PAUSE_SAFETY_SAMPS (g_audio_rate >> 5) -#define ZERO_PAUSE_NUM_SAMPS (4*g_audio_rate) +#define ZERO_PAUSE_SAFETY_SAMPS (g_audio_rate >> 5) +#define ZERO_PAUSE_NUM_SAMPS (4*g_audio_rate) -int g_zeroes_buffered = 0; -int g_zeroes_seen = 0; -int g_sound_paused = 0; -int g_childsnd_vbl = 0; -int g_childsnd_pos = 0; +int g_zeroes_buffered = 0; +int g_zeroes_seen = 0; +int g_sound_paused = 0; +int g_childsnd_vbl = 0; +int g_childsnd_pos = 0; word32 *g_childsnd_shm_addr = 0; void child_sound_init_linux(); @@ -59,364 +59,352 @@ void child_sound_init_mac(); void child_sound_init_sdl(); long sound_init_device_sdl(); -void -reliable_buf_write(word32 *shm_addr, int pos, int size) -{ - byte *ptr; - int ret = 0; +void reliable_buf_write(word32 *shm_addr, int pos, int size) { + byte *ptr; + int ret = 0; - if(size < 1 || pos < 0 || pos > SOUND_SHM_SAMP_SIZE || - size > SOUND_SHM_SAMP_SIZE || - (pos + size) > SOUND_SHM_SAMP_SIZE) { - printf("reliable_buf_write: pos: %04x, size: %04x\n", pos, size); - exit(1); - } + if(size < 1 || pos < 0 || pos > SOUND_SHM_SAMP_SIZE || + size > SOUND_SHM_SAMP_SIZE || + (pos + size) > SOUND_SHM_SAMP_SIZE) { + printf("reliable_buf_write: pos: %04x, size: %04x\n", pos, size); + exit(1); + } - ptr = (byte *)&(shm_addr[pos]); - size = size * 4; + ptr = (byte *)&(shm_addr[pos]); + size = size * 4; - while(size > 0) { + while(size > 0) { #if defined(HAVE_SDL) //ret = sdl_send_audio(ptr, size); #elif defined(WIN_SOUND) - ret = win32_send_audio(ptr, size); + ret = win32_send_audio(ptr, size); #elif defined(MAC) && !defined(HAVE_SDL) - ret = mac_send_audio(ptr, size); + ret = mac_send_audio(ptr, size); #else - ret = write(g_audio_socket, ptr, size); + ret = write(g_audio_socket, ptr, size); #endif - if(ret < 0) { - printf("audio write, errno: %d\n", errno); - exit(1); - } - size = size - ret; - ptr += ret; - g_bytes_written += ret; - } + if(ret < 0) { + printf("audio write, errno: %d\n", errno); + exit(1); + } + size = size - ret; + ptr += ret; + g_bytes_written += ret; + } } -void -reliable_zero_write(int amt) -{ - int len; +void reliable_zero_write(int amt) { + int len; - while(amt > 0) { - len = MIN(amt, ZERO_BUF_SIZE); - reliable_buf_write(g_snd_zero_buf, 0, len); - amt -= len; - } + while(amt > 0) { + len = MIN(amt, ZERO_BUF_SIZE); + reliable_buf_write(g_snd_zero_buf, 0, len); + amt -= len; + } } -void -child_sound_loop(int read_fd, int write_fd, word32 *shm_addr) -{ - word32 tmp; - int ret; +void child_sound_loop(int read_fd, int write_fd, word32 *shm_addr) { + word32 tmp; + int ret; - g_audio_rate = g_preferred_rate; + g_audio_rate = g_preferred_rate; - g_zeroes_buffered = 0; - g_zeroes_seen = 0; - g_sound_paused = 0; + g_zeroes_buffered = 0; + g_zeroes_seen = 0; + g_sound_paused = 0; - g_childsnd_pos = 0; - g_childsnd_vbl = 0; - g_childsnd_shm_addr = shm_addr; + g_childsnd_pos = 0; + g_childsnd_vbl = 0; + g_childsnd_shm_addr = shm_addr; #if defined(HAVE_SDL) //child_sound_init_sdl(); - sound_init_device_sdl(); // ignores long return value of sample rate + sound_init_device_sdl(); // ignores long return value of sample rate return; #elif defined(__linux__) || defined(OSS) - child_sound_init_linux(); + child_sound_init_linux(); #elif HPUX - child_sound_init_hpdev(); + child_sound_init_hpdev(); #elif WIN_SOUND - child_sound_init_win32(); - return; + child_sound_init_win32(); + return; #elif defined(MAC) && !defined(HAVE_SDL) - child_sound_init_mac(); - return; + child_sound_init_mac(); + return; #endif - doc_printf("Child pipe fd: %d\n", read_fd); + doc_printf("Child pipe fd: %d\n", read_fd); - tmp = g_audio_rate; - ret = write(write_fd, &tmp, 4); - if(ret != 4) { - printf("Unable to send back audio rate to parent\n"); - printf("ret: %d fd: %d, errno: %d\n", ret, write_fd, errno); - exit(1); - } - printf("Wrote to fd %d the audio rate\n", write_fd); + tmp = g_audio_rate; + ret = write(write_fd, &tmp, 4); + if(ret != 4) { + printf("Unable to send back audio rate to parent\n"); + printf("ret: %d fd: %d, errno: %d\n", ret, write_fd, errno); + exit(1); + } + printf("Wrote to fd %d the audio rate\n", write_fd); - close(write_fd); + close(write_fd); - while(1) { - errno = 0; - ret = read(read_fd, (char*)&tmp, 4); - if(ret <= 0) { - printf("child dying from ret: %d, errno: %d\n", - ret, errno); - break; - } + while(1) { + errno = 0; + ret = read(read_fd, (char*)&tmp, 4); + if(ret <= 0) { + printf("child dying from ret: %d, errno: %d\n", + ret, errno); + break; + } - child_sound_playit(tmp); - } + child_sound_playit(tmp); + } #ifdef HPUX - ioctl(g_audio_socket, AUDIO_DRAIN, 0); + ioctl(g_audio_socket, AUDIO_DRAIN, 0); #endif - close(g_audio_socket); + close(g_audio_socket); - exit(0); + exit(0); } // called by sound.c:send_sound() -void -child_sound_playit(word32 tmp) -{ - int size; +void child_sound_playit(word32 tmp) { + int size; - size = tmp & 0xffffff; + size = tmp & 0xffffff; - if((tmp >> 24) == 0xa2) { - /* play sound here */ + if((tmp >> 24) == 0xa2) { + /* play sound here */ #if 0 - g_childsnd_pos += g_zeroes_buffered; - while(g_childsnd_pos >= SOUND_SHM_SAMP_SIZE) { - g_childsnd_pos -= SOUND_SHM_SAMP_SIZE; - } + g_childsnd_pos += g_zeroes_buffered; + while(g_childsnd_pos >= SOUND_SHM_SAMP_SIZE) { + g_childsnd_pos -= SOUND_SHM_SAMP_SIZE; + } #endif - if(g_zeroes_buffered) { - reliable_zero_write(g_zeroes_buffered); - } + if(g_zeroes_buffered) { + reliable_zero_write(g_zeroes_buffered); + } - g_zeroes_buffered = 0; - g_zeroes_seen = 0; + g_zeroes_buffered = 0; + g_zeroes_seen = 0; - // only write up to end of buffer - if((size + g_childsnd_pos) > SOUND_SHM_SAMP_SIZE) { - reliable_buf_write(g_childsnd_shm_addr, g_childsnd_pos, - SOUND_SHM_SAMP_SIZE - g_childsnd_pos); - size = (g_childsnd_pos + size) - SOUND_SHM_SAMP_SIZE; - g_childsnd_pos = 0; - } + // only write up to end of buffer + if((size + g_childsnd_pos) > SOUND_SHM_SAMP_SIZE) { + reliable_buf_write(g_childsnd_shm_addr, g_childsnd_pos, + SOUND_SHM_SAMP_SIZE - g_childsnd_pos); + size = (g_childsnd_pos + size) - SOUND_SHM_SAMP_SIZE; + g_childsnd_pos = 0; + } - reliable_buf_write(g_childsnd_shm_addr, g_childsnd_pos, size); + reliable_buf_write(g_childsnd_shm_addr, g_childsnd_pos, size); - if(g_sound_paused) { - glogf("Unpausing sound, zb: %d\n", g_zeroes_buffered); - g_sound_paused = 0; - } + if(g_sound_paused) { + glogf("Unpausing sound, zb: %d\n", g_zeroes_buffered); + g_sound_paused = 0; + } - } else if((tmp >> 24) == 0xa1) { - if(g_sound_paused) { - if(g_zeroes_buffered < ZERO_PAUSE_SAFETY_SAMPS) { - g_zeroes_buffered += size; - } - } else { - /* not paused, send it through */ - g_zeroes_seen += size; + } else if((tmp >> 24) == 0xa1) { + if(g_sound_paused) { + if(g_zeroes_buffered < ZERO_PAUSE_SAFETY_SAMPS) { + g_zeroes_buffered += size; + } + } else { + /* not paused, send it through */ + g_zeroes_seen += size; - reliable_zero_write(size); + reliable_zero_write(size); - if(g_zeroes_seen >= ZERO_PAUSE_NUM_SAMPS) { - glog("Pausing sound"); - g_sound_paused = 1; - } - } - } else { - printf("tmp received bad: %08x\n", tmp); - exit(3); - } + if(g_zeroes_seen >= ZERO_PAUSE_NUM_SAMPS) { + glog("Pausing sound"); + g_sound_paused = 1; + } + } + } else { + printf("tmp received bad: %08x\n", tmp); + exit(3); + } - g_childsnd_pos += size; - while(g_childsnd_pos >= SOUND_SHM_SAMP_SIZE) { - g_childsnd_pos -= SOUND_SHM_SAMP_SIZE; - } + g_childsnd_pos += size; + while(g_childsnd_pos >= SOUND_SHM_SAMP_SIZE) { + g_childsnd_pos -= SOUND_SHM_SAMP_SIZE; + } - g_childsnd_vbl++; - if(g_childsnd_vbl >= 60) { - g_childsnd_vbl = 0; - g_bytes_written = 0; - } + g_childsnd_vbl++; + if(g_childsnd_vbl >= 60) { + g_childsnd_vbl = 0; + g_bytes_written = 0; + } } #ifdef HPUX -void -child_sound_init_hpdev() -{ - struct audio_describe audio_descr; - int output_channel; - char *str; - int speaker; - int ret; - int i; +void child_sound_init_hpdev() { + struct audio_describe audio_descr; + int output_channel; + char *str; + int speaker; + int ret; + int i; - g_audio_socket = open("/dev/audio", O_WRONLY, 0); - if(g_audio_socket < 0) { - printf("open /dev/audio failed, ret: %d, errno:%d\n", - g_audio_socket, errno); - exit(1); - } + g_audio_socket = open("/dev/audio", O_WRONLY, 0); + if(g_audio_socket < 0) { + printf("open /dev/audio failed, ret: %d, errno:%d\n", + g_audio_socket, errno); + exit(1); + } - ret = ioctl(g_audio_socket, AUDIO_DESCRIBE, &audio_descr); - if(ret < 0) { - printf("ioctl AUDIO_DESCRIBE failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } + ret = ioctl(g_audio_socket, AUDIO_DESCRIBE, &audio_descr); + if(ret < 0) { + printf("ioctl AUDIO_DESCRIBE failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } - for(i = 0; i < audio_descr.nrates; i++) { - printf("Audio rate[%d] = %d\n", i, - audio_descr.sample_rate[i]); - } + for(i = 0; i < audio_descr.nrates; i++) { + printf("Audio rate[%d] = %d\n", i, + audio_descr.sample_rate[i]); + } - ret = ioctl(g_audio_socket, AUDIO_SET_DATA_FORMAT, - AUDIO_FORMAT_LINEAR16BIT); - if(ret < 0) { - printf("ioctl AUDIO_SET_DATA_FORMAT failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } + ret = ioctl(g_audio_socket, AUDIO_SET_DATA_FORMAT, + AUDIO_FORMAT_LINEAR16BIT); + if(ret < 0) { + printf("ioctl AUDIO_SET_DATA_FORMAT failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } - ret = ioctl(g_audio_socket, AUDIO_SET_CHANNELS, NUM_CHANNELS); - if(ret < 0) { - printf("ioctl AUDIO_SET_CHANNELS failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } + ret = ioctl(g_audio_socket, AUDIO_SET_CHANNELS, NUM_CHANNELS); + if(ret < 0) { + printf("ioctl AUDIO_SET_CHANNELS failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } - ret = ioctl(g_audio_socket, AUDIO_SET_TXBUFSIZE, 16*1024); - if(ret < 0) { - printf("ioctl AUDIO_SET_TXBUFSIZE failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } + ret = ioctl(g_audio_socket, AUDIO_SET_TXBUFSIZE, 16*1024); + if(ret < 0) { + printf("ioctl AUDIO_SET_TXBUFSIZE failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } - ret = ioctl(g_audio_socket, AUDIO_SET_SAMPLE_RATE, g_audio_rate); - if(ret < 0) { - printf("ioctl AUDIO_SET_SAMPLE_RATE failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } + ret = ioctl(g_audio_socket, AUDIO_SET_SAMPLE_RATE, g_audio_rate); + if(ret < 0) { + printf("ioctl AUDIO_SET_SAMPLE_RATE failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } - ret = ioctl(g_audio_socket, AUDIO_GET_OUTPUT, &output_channel); - if(ret < 0) { - printf("ioctl AUDIO_GET_OUTPUT failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } + ret = ioctl(g_audio_socket, AUDIO_GET_OUTPUT, &output_channel); + if(ret < 0) { + printf("ioctl AUDIO_GET_OUTPUT failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } - speaker = 1; - str = getenv("SPEAKER"); - if(str) { - if(str[0] != 'i' && str[0] != 'I') { - speaker = 0; - } - } + speaker = 1; + str = getenv("SPEAKER"); + if(str) { + if(str[0] != 'i' && str[0] != 'I') { + speaker = 0; + } + } - if(speaker) { - printf("Sending sound to internal speaker\n"); - output_channel |= AUDIO_OUT_SPEAKER; - } else { - printf("Sending sound to external jack\n"); - output_channel &= (~AUDIO_OUT_SPEAKER); - output_channel |= AUDIO_OUT_HEADPHONE; - } + if(speaker) { + printf("Sending sound to internal speaker\n"); + output_channel |= AUDIO_OUT_SPEAKER; + } else { + printf("Sending sound to external jack\n"); + output_channel &= (~AUDIO_OUT_SPEAKER); + output_channel |= AUDIO_OUT_HEADPHONE; + } - ret = ioctl(g_audio_socket, AUDIO_SET_OUTPUT, output_channel); - if(ret < 0) { - printf("ioctl AUDIO_SET_OUTPUT failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } + ret = ioctl(g_audio_socket, AUDIO_SET_OUTPUT, output_channel); + if(ret < 0) { + printf("ioctl AUDIO_SET_OUTPUT failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } } -#endif /* HPUX */ +#endif /* HPUX */ #if defined(__linux__) || defined(OSS) -void -child_sound_init_linux() -{ - int stereo; - int sample_size; - int rate; - int fragment; - int fmt; - int ret; +void child_sound_init_linux() { + int stereo; + int sample_size; + int rate; + int fragment; + int fmt; + int ret; - g_audio_socket = open("/dev/dsp", O_WRONLY, 0); - if(g_audio_socket < 0) { - printf("open /dev/dsp failed, ret: %d, errno:%d\n", - g_audio_socket, errno); - exit(1); - } + g_audio_socket = open("/dev/dsp", O_WRONLY, 0); + if(g_audio_socket < 0) { + printf("open /dev/dsp failed, ret: %d, errno:%d\n", + g_audio_socket, errno); + exit(1); + } - fragment = 0x00200009; + fragment = 0x00200009; #if 0 - ret = ioctl(g_audio_socket, SNDCTL_DSP_SETFRAGMENT, &fragment); - if(ret < 0) { - printf("ioctl SETFRAGEMNT failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } + ret = ioctl(g_audio_socket, SNDCTL_DSP_SETFRAGMENT, &fragment); + if(ret < 0) { + printf("ioctl SETFRAGEMNT failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } #endif - sample_size = 16; - ret = ioctl(g_audio_socket, SNDCTL_DSP_SAMPLESIZE, &sample_size); - if(ret < 0) { - printf("ioctl SNDCTL_DSP_SAMPLESIZE failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } + sample_size = 16; + ret = ioctl(g_audio_socket, SNDCTL_DSP_SAMPLESIZE, &sample_size); + if(ret < 0) { + printf("ioctl SNDCTL_DSP_SAMPLESIZE failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } #if defined(GSPLUS_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) // OSX needs to calculate endianness mid-compilation, can't be passed on compile command - fmt = AFMT_S16_LE; + fmt = AFMT_S16_LE; #else - fmt = AFMT_S16_BE; + fmt = AFMT_S16_BE; #endif - ret = ioctl(g_audio_socket, SNDCTL_DSP_SETFMT, &fmt); - if(ret < 0) { - printf("ioctl SNDCTL_DSP_SETFMT failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } + ret = ioctl(g_audio_socket, SNDCTL_DSP_SETFMT, &fmt); + if(ret < 0) { + printf("ioctl SNDCTL_DSP_SETFMT failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } - stereo = 1; - ret = ioctl(g_audio_socket, SNDCTL_DSP_STEREO, &stereo); - if(ret < 0) { - printf("ioctl SNDCTL_DSP_STEREO failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } + stereo = 1; + ret = ioctl(g_audio_socket, SNDCTL_DSP_STEREO, &stereo); + if(ret < 0) { + printf("ioctl SNDCTL_DSP_STEREO failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } - rate = g_audio_rate; - ret = ioctl(g_audio_socket, SNDCTL_DSP_SPEED, &rate); - if(ret < 0) { - printf("ioctl SNDCTL_DSP_SPEED failed, ret:%d, errno:%d\n", - ret, errno); - exit(1); - } - if(ret > 0) { - rate = ret; /* rate is returned value */ - } - if(rate < 8000) { - printf("Audio rate of %d which is < 8000!\n", rate); - exit(1); - } + rate = g_audio_rate; + ret = ioctl(g_audio_socket, SNDCTL_DSP_SPEED, &rate); + if(ret < 0) { + printf("ioctl SNDCTL_DSP_SPEED failed, ret:%d, errno:%d\n", + ret, errno); + exit(1); + } + if(ret > 0) { + rate = ret; /* rate is returned value */ + } + if(rate < 8000) { + printf("Audio rate of %d which is < 8000!\n", rate); + exit(1); + } - g_audio_rate = rate; + g_audio_rate = rate; - printf("Sound initialized\n"); + printf("Sound initialized\n"); } #endif diff --git a/src/tfe/tfe.c b/src/tfe/tfe.c index f0f9698..43c5b3a 100644 --- a/src/tfe/tfe.c +++ b/src/tfe/tfe.c @@ -4,7 +4,7 @@ * Written by * Spiro Trikaliotis * Christian Vogelgsang - * + * * This file is part of VICE, the Versatile Commodore Emulator. * See README for copyright notice. * @@ -26,8 +26,6 @@ */ - - #include #include #include @@ -61,9 +59,9 @@ /* variables needed */ /* - This variable is used when we need to postpone the initialization - because tfe_init() is not yet called -*/ + This variable is used when we need to postpone the initialization + because tfe_init() is not yet called + */ static int should_activate = 0; static int init_tfe_flag = 0; typedef signed int log_t; @@ -73,22 +71,22 @@ typedef signed int log_t; static log_t tfe_log = LOG_ERR; -/* status which received packages to accept +/* status which received packages to accept This is used in tfe_should_accept(). -*/ -static byte tfe_ia_mac[6] = { 0,0,0,0,0,0 }; + */ +static byte tfe_ia_mac[6] = { 0,0,0,0,0,0 }; /* remember the value of the hash mask */ static DWORD tfe_hash_mask[2]; /* reveiver setup */ static word16 tfe_recv_control = 0; /* copy of CC_RXCTL (contains all bits below) */ -static int tfe_recv_broadcast = 0; /* broadcast */ -static int tfe_recv_mac = 0; /* individual address (IA) */ -static int tfe_recv_multicast = 0; /* multicast if address passes the hash filter */ -static int tfe_recv_correct = 0; /* accept correct frames */ -static int tfe_recv_promiscuous = 0; /* promiscuous mode */ -static int tfe_recv_hashfilter = 0; /* accept if IA passes the hash filter */ +static int tfe_recv_broadcast = 0; /* broadcast */ +static int tfe_recv_mac = 0; /* individual address (IA) */ +static int tfe_recv_multicast = 0; /* multicast if address passes the hash filter */ +static int tfe_recv_correct = 0; /* accept correct frames */ +static int tfe_recv_promiscuous = 0; /* promiscuous mode */ +static int tfe_recv_hashfilter = 0; /* accept if IA passes the hash filter */ /* Flag: Can we even use TFE, or is the hardware not available? */ static int tfe_cannot_use = 0; @@ -107,7 +105,7 @@ static char *tfe_interface = NULL; REMARK: The TFE operatoes the cs8900a in IO space configuration, as it generates I/OW and I/OR signals. -*/ + */ #define TFE_COUNT_IO_REGISTER 0x10 /* we have 16 I/O register */ static byte *tfe = NULL; @@ -115,12 +113,12 @@ static byte *tfe = NULL; RW: RXTXDATA = DE00/DE01 RW: RXTXDATA2 = DE02/DE03 (for 32-bit-operation) -W: TXCMD = DE04/DE05 (TxCMD, Transmit Command) mapped to PP + 0144 (Reg. 9, Sec. 4.4, page 46) - -W: TXLENGTH = DE06/DE07 (TxLenght, Transmit Length) mapped to PP + 0146 + -W: TXLENGTH = DE06/DE07 (TxLenght, Transmit Length) mapped to PP + 0146 R-: INTSTQUEUE = DE08/DE09 (Interrupt Status Queue) mapped to PP + 0120 (ISQ, Sec. 5.1, page 78) RW: PP_PTR = DE0A/DE0B (PacketPage Pointer) (see. page 75p: Read -011.---- ----.----) - RW: PP_DATA0 = DE0C/DE0D (PacketPage Data (Port 0)) + RW: PP_DATA0 = DE0C/DE0D (PacketPage Data (Port 0)) RW: PP_DATA1 = DE0E/DE0F (PacketPage Data (Port 1)) (for 32 bit only) -*/ + */ #define TFE_ADDR_RXTXDATA 0x00 /* RW */ #define TFE_ADDR_RXTXDATA2 0x02 /* RW 32 bit only! */ @@ -129,31 +127,31 @@ static byte *tfe = NULL; #define TFE_ADDR_INTSTQUEUE 0x08 /* R- Interrupt status queue, maps to PP + 0120 */ #define TFE_ADDR_PP_PTR 0x0a /* RW PacketPage Pointer */ #define TFE_ADDR_PP_DATA 0x0c /* RW PacketPage Data, Port 0 */ -#define TFE_ADDR_PP_DATA2 0x0e /* RW PacketPage Data, Port 1 - 32 bit only */ +#define TFE_ADDR_PP_DATA2 0x0e /* RW PacketPage Data, Port 1 - 32 bit only */ /* Makros for reading and writing the visible TFE register: */ #define GET_TFE_8( _xxx_ ) \ - ( assert(_xxx_> 8) & 0xff; \ - } while (0) + do { \ + assert(_xxx_> 8) & 0xff; \ + } while (0) /* The PacketPage register */ /* note: The locations 0 to MAX_PACKETPAGE_ARRAY-1 are handled in this array. */ @@ -167,49 +165,49 @@ static word16 tfe_packetpage_ptr = 0; /* Makros for reading and writing the PacketPage register: */ #define GET_PP_8( _xxx_ ) \ - (assert(_xxx_> 8) & 0xFF; \ - } while (0) + do { \ + assert(_xxx_> 8) & 0xFF; \ + } while (0) #define SET_PP_32( _xxx_, _val_ ) \ - do { \ - assert(_xxx_> 8) & 0xFF; \ - tfe_packetpage[_xxx_+2] = (_val_>>16) & 0xFF; \ - tfe_packetpage[_xxx_+3] = (_val_>>24) & 0xFF; \ - } while (0) + do { \ + assert(_xxx_> 8) & 0xFF; \ + tfe_packetpage[_xxx_+2] = (_val_>>16) & 0xFF; \ + tfe_packetpage[_xxx_+3] = (_val_>>24) & 0xFF; \ + } while (0) /* The packetpage register: see p. 39f */ @@ -316,27 +314,26 @@ static int rxevent_read_mask = 3; /* set if L and/or H byte was read in RXEVENT? static int TfeDebugMaxFrameLengthToDump = 150; -static char *debug_outbuffer(const int length, const unsigned char * const buffer) -{ +static char *debug_outbuffer(const int length, const unsigned char * const buffer) { #define MAXLEN_DEBUG 1600 - int i; - static char outbuffer[MAXLEN_DEBUG*4+1]; - char *p = outbuffer; + int i; + static char outbuffer[MAXLEN_DEBUG*4+1]; + char *p = outbuffer; - assert( TfeDebugMaxFrameLengthToDump <= MAXLEN_DEBUG ); - - *p = 0; + assert( TfeDebugMaxFrameLengthToDump <= MAXLEN_DEBUG ); - for (i=0; i=length) - break; - - sprintf( p, "%02X%c", buffer[i], ((i+1)%16==0)?'*':(((i+1)%8==0)?'-':' ')); - p+=3; - } + *p = 0; - return outbuffer; + for (i=0; i=length) + break; + + sprintf( p, "%02X%c", buffer[i], ((i+1)%16==0) ? '*' : (((i+1)%8==0) ? '-' : ' ')); + p+=3; + } + + return outbuffer; } #endif @@ -344,266 +341,257 @@ static char *debug_outbuffer(const int length, const unsigned char * const buffe /* ------------------------------------------------------------------------- */ /* initialization and deinitialization functions */ -static void tfe_set_tx_status(int ready,int error) -{ - word16 old_status = GET_PP_16(TFE_PP_ADDR_SE_BUSST); - - /* mask out TxBidErr and Rdy4TxNOW */ - word16 new_status = old_status & ~0x180; - if(ready) - new_status |= 0x100; /* set Rdy4TxNOW */ - if(error) - new_status |= 0x080; /* set TxBidErr */ - - if(new_status!=old_status) { - SET_PP_16(TFE_PP_ADDR_SE_BUSST,new_status); +static void tfe_set_tx_status(int ready,int error) { + word16 old_status = GET_PP_16(TFE_PP_ADDR_SE_BUSST); + + /* mask out TxBidErr and Rdy4TxNOW */ + word16 new_status = old_status & ~0x180; + if(ready) + new_status |= 0x100; /* set Rdy4TxNOW */ + if(error) + new_status |= 0x080; /* set TxBidErr */ + + if(new_status!=old_status) { + SET_PP_16(TFE_PP_ADDR_SE_BUSST,new_status); #ifdef TFE_DEBUG_RXTX_STATE - log_message(tfe_log,"TX: set status Rdy4TxNOW=%d TxBidErr=%d", - ready,error); + log_message(tfe_log,"TX: set status Rdy4TxNOW=%d TxBidErr=%d", + ready,error); #endif - } + } } -static void tfe_set_receiver(int enabled) -{ - rx_enabled = enabled; - rx_state = TFE_RX_IDLE; +static void tfe_set_receiver(int enabled) { + rx_enabled = enabled; + rx_state = TFE_RX_IDLE; - rxevent_read_mask = 3; /* was L or H byte read in RXEVENT? */ + rxevent_read_mask = 3; /* was L or H byte read in RXEVENT? */ } -static void tfe_set_transmitter(int enabled) -{ - tx_enabled = enabled; - tx_state = TFE_TX_IDLE; - - tfe_set_tx_status(0,0); +static void tfe_set_transmitter(int enabled) { + tx_enabled = enabled; + tx_state = TFE_TX_IDLE; + + tfe_set_tx_status(0,0); } -void tfe_reset(void) -{ - if (tfe_enabled && !should_activate) - { - int i; - - assert( tfe ); - assert( tfe_packetpage ); +void tfe_reset(void) { + if (tfe_enabled && !should_activate) + { + int i; - tfe_arch_pre_reset(); + assert( tfe ); + assert( tfe_packetpage ); - /* initialize visible IO register and PacketPage registers */ - memset( tfe, 0, TFE_COUNT_IO_REGISTER ); - memset( tfe_packetpage, 0, MAX_PACKETPAGE_ARRAY ); + tfe_arch_pre_reset(); - /* according to page 19 unless stated otherwise */ - SET_PP_32(TFE_PP_ADDR_PRODUCTID, 0x0900630E ); /* p.41: 0E630009 for Rev. D; reversed order! */ - SET_PP_16(TFE_PP_ADDR_IOBASE, 0x0300); - SET_PP_16(TFE_PP_ADDR_INTNO, 0x0004); /* xxxx xxxx xxxx x100b */ - SET_PP_16(TFE_PP_ADDR_DMA_CHAN, 0x0003); /* xxxx xxxx xxxx xx11b */ + /* initialize visible IO register and PacketPage registers */ + memset( tfe, 0, TFE_COUNT_IO_REGISTER ); + memset( tfe_packetpage, 0, MAX_PACKETPAGE_ARRAY ); + + /* according to page 19 unless stated otherwise */ + SET_PP_32(TFE_PP_ADDR_PRODUCTID, 0x0900630E ); /* p.41: 0E630009 for Rev. D; reversed order! */ + SET_PP_16(TFE_PP_ADDR_IOBASE, 0x0300); + SET_PP_16(TFE_PP_ADDR_INTNO, 0x0004); /* xxxx xxxx xxxx x100b */ + SET_PP_16(TFE_PP_ADDR_DMA_CHAN, 0x0003); /* xxxx xxxx xxxx xx11b */ #if 0 /* not needed since all memory is initialized with 0 */ - SET_PP_16(TFE_PP_ADDR_DMA_SOF, 0x0000); - SET_PP_16(TFE_PP_ADDR_DMA_FC, 0x0000); /* x000h */ - SET_PP_16(TFE_PP_ADDR_RXDMA_BC, 0x0000); - SET_PP_32(TFE_PP_ADDR_MEMBASE, 0x0000); /* xxx0 0000h */ - SET_PP_32(TFE_PP_ADDR_BPROM_BASE, 0x00000000); /* xxx0 0000h */ - SET_PP_32(TFE_PP_ADDR_BPROM_MASK, 0x00000000); /* xxx0 0000h */ + SET_PP_16(TFE_PP_ADDR_DMA_SOF, 0x0000); + SET_PP_16(TFE_PP_ADDR_DMA_FC, 0x0000); /* x000h */ + SET_PP_16(TFE_PP_ADDR_RXDMA_BC, 0x0000); + SET_PP_32(TFE_PP_ADDR_MEMBASE, 0x0000); /* xxx0 0000h */ + SET_PP_32(TFE_PP_ADDR_BPROM_BASE, 0x00000000); /* xxx0 0000h */ + SET_PP_32(TFE_PP_ADDR_BPROM_MASK, 0x00000000); /* xxx0 0000h */ - SET_PP_16(TFE_PP_ADDR_SE_ISQ, 0x0000); /* p. 51 */ + SET_PP_16(TFE_PP_ADDR_SE_ISQ, 0x0000); /* p. 51 */ #endif - /* according to descriptions of the registers, see definitions of - TFE_PP_ADDR_CC_... and TFE_PP_ADDR_SE_... above! */ + /* according to descriptions of the registers, see definitions of + TFE_PP_ADDR_CC_... and TFE_PP_ADDR_SE_... above! */ - SET_PP_16(TFE_PP_ADDR_CC_RXCFG, 0x0003); - SET_PP_16(TFE_PP_ADDR_CC_RXCTL, 0x0005); - SET_PP_16(TFE_PP_ADDR_CC_TXCFG, 0x0007); - SET_PP_16(TFE_PP_ADDR_CC_TXCMD, 0x0009); - SET_PP_16(TFE_PP_ADDR_CC_BUFCFG, 0x000B); - SET_PP_16(TFE_PP_ADDR_CC_LINECTL, 0x0013); - SET_PP_16(TFE_PP_ADDR_CC_SELFCTL, 0x0015); - SET_PP_16(TFE_PP_ADDR_CC_BUSCTL, 0x0017); - SET_PP_16(TFE_PP_ADDR_CC_TESTCTL, 0x0019); + SET_PP_16(TFE_PP_ADDR_CC_RXCFG, 0x0003); + SET_PP_16(TFE_PP_ADDR_CC_RXCTL, 0x0005); + SET_PP_16(TFE_PP_ADDR_CC_TXCFG, 0x0007); + SET_PP_16(TFE_PP_ADDR_CC_TXCMD, 0x0009); + SET_PP_16(TFE_PP_ADDR_CC_BUFCFG, 0x000B); + SET_PP_16(TFE_PP_ADDR_CC_LINECTL, 0x0013); + SET_PP_16(TFE_PP_ADDR_CC_SELFCTL, 0x0015); + SET_PP_16(TFE_PP_ADDR_CC_BUSCTL, 0x0017); + SET_PP_16(TFE_PP_ADDR_CC_TESTCTL, 0x0019); - SET_PP_16(TFE_PP_ADDR_SE_ISQ, 0x0000); - SET_PP_16(TFE_PP_ADDR_SE_RXEVENT, 0x0004); - SET_PP_16(TFE_PP_ADDR_SE_TXEVENT, 0x0008); - SET_PP_16(TFE_PP_ADDR_SE_BUFEVENT, 0x000C); - SET_PP_16(TFE_PP_ADDR_SE_RXMISS, 0x0010); - SET_PP_16(TFE_PP_ADDR_SE_TXCOL, 0x0012); - SET_PP_16(TFE_PP_ADDR_SE_LINEST, 0x0014); - SET_PP_16(TFE_PP_ADDR_SE_SELFST, 0x0016); - SET_PP_16(TFE_PP_ADDR_SE_BUSST, 0x0018); - SET_PP_16(TFE_PP_ADDR_SE_TDR, 0x001C); - - SET_PP_16(TFE_PP_ADDR_TXCMD, 0x0009); + SET_PP_16(TFE_PP_ADDR_SE_ISQ, 0x0000); + SET_PP_16(TFE_PP_ADDR_SE_RXEVENT, 0x0004); + SET_PP_16(TFE_PP_ADDR_SE_TXEVENT, 0x0008); + SET_PP_16(TFE_PP_ADDR_SE_BUFEVENT, 0x000C); + SET_PP_16(TFE_PP_ADDR_SE_RXMISS, 0x0010); + SET_PP_16(TFE_PP_ADDR_SE_TXCOL, 0x0012); + SET_PP_16(TFE_PP_ADDR_SE_LINEST, 0x0014); + SET_PP_16(TFE_PP_ADDR_SE_SELFST, 0x0016); + SET_PP_16(TFE_PP_ADDR_SE_BUSST, 0x0018); + SET_PP_16(TFE_PP_ADDR_SE_TDR, 0x001C); - /* 4.4.19 Self Status Register, p. 65 - Important: set INITD (Bit 7) to signal device is ready */ - SET_PP_16(TFE_PP_ADDR_SE_SELFST, 0x0896); + SET_PP_16(TFE_PP_ADDR_TXCMD, 0x0009); - tfe_recv_control = GET_PP_16(TFE_PP_ADDR_CC_RXCTL); - - /* spec: mac address is undefined after reset. - real HW: keeps the last set address. */ - for(i=0;i<6;i++) - SET_PP_8(TFE_PP_ADDR_MAC_ADDR+i,tfe_ia_mac[i]); - - /* reset state */ - tfe_set_transmitter(0); - tfe_set_receiver(0); + /* 4.4.19 Self Status Register, p. 65 + Important: set INITD (Bit 7) to signal device is ready */ + SET_PP_16(TFE_PP_ADDR_SE_SELFST, 0x0896); - tfe_arch_post_reset(); + tfe_recv_control = GET_PP_16(TFE_PP_ADDR_CC_RXCTL); - printf("CS8900a rev.D reset\n"); - } + /* spec: mac address is undefined after reset. + real HW: keeps the last set address. */ + for(i=0; i<6; i++) + SET_PP_8(TFE_PP_ADDR_MAC_ADDR+i,tfe_ia_mac[i]); + + /* reset state */ + tfe_set_transmitter(0); + tfe_set_receiver(0); + + tfe_arch_post_reset(); + + printf("CS8900a rev.D reset\n"); + } } #ifdef DOS_TFE -static void set_standard_tfe_interface(void) -{ - char *dev, errbuf[PCAP_ERRBUF_SIZE]; - dev = pcap_lookupdev(errbuf); - util_string_set(&tfe_interface, dev); +static void set_standard_tfe_interface(void) { + char *dev, errbuf[PCAP_ERRBUF_SIZE]; + dev = pcap_lookupdev(errbuf); + util_string_set(&tfe_interface, dev); } #endif static -int tfe_activate_i(void) -{ - assert( tfe == NULL ); - assert( tfe_packetpage == NULL ); +int tfe_activate_i(void) { + assert( tfe == NULL ); + assert( tfe_packetpage == NULL ); #ifdef TFE_DEBUG - log_message( tfe_log, "tfe_activate_i()." ); + log_message( tfe_log, "tfe_activate_i()." ); #endif - /* allocate memory for visible IO register */ - tfe = (byte *)lib_malloc( TFE_COUNT_IO_REGISTER ); - if (tfe==NULL) - { + /* allocate memory for visible IO register */ + tfe = (byte *)lib_malloc( TFE_COUNT_IO_REGISTER ); + if (tfe==NULL) + { #ifdef TFE_DEBUG_INIT - log_message(tfe_log, "tfe_activate_i: Allocating tfe failed."); + log_message(tfe_log, "tfe_activate_i: Allocating tfe failed."); #endif - tfe_enabled = 0; - return 0; - } - - /* allocate memory for PacketPage register */ - tfe_packetpage = (byte *)lib_malloc( MAX_PACKETPAGE_ARRAY ); - if (tfe_packetpage==NULL) - { -#ifdef TFE_DEBUG_INIT - log_message(tfe_log, "tfe_activate: Allocating tfe_packetpage failed."); -#endif - lib_free(tfe); - tfe=NULL; - tfe_enabled = 0; - return 0; - } - -#ifdef TFE_DEBUG_INIT - log_message(tfe_log, "tfe_activate: Allocated memory successfully."); - log_message(tfe_log, "\ttfe at $%08X, tfe_packetpage at $%08X", tfe, tfe_packetpage ); -#endif - -#ifdef DOS_TFE - set_standard_tfe_interface(); -#endif - - if (!tfe_arch_activate(tfe_interface)) { - lib_free(tfe_packetpage); - lib_free(tfe); - tfe=NULL; - tfe_packetpage=NULL; - tfe_enabled = 0; - tfe_cannot_use = 1; - return 0; - } - - /* virtually reset the LAN chip */ - tfe_reset(); - + tfe_enabled = 0; return 0; -} + } -static -int tfe_deactivate_i(void) -{ -#ifdef TFE_DEBUG - log_message( tfe_log, "tfe_deactivate_i()." ); + /* allocate memory for PacketPage register */ + tfe_packetpage = (byte *)lib_malloc( MAX_PACKETPAGE_ARRAY ); + if (tfe_packetpage==NULL) + { +#ifdef TFE_DEBUG_INIT + log_message(tfe_log, "tfe_activate: Allocating tfe_packetpage failed."); #endif - - assert(tfe && tfe_packetpage); - - tfe_arch_deactivate(); - lib_free(tfe); - tfe = NULL; - lib_free(tfe_packetpage); - tfe_packetpage = NULL; + tfe=NULL; + tfe_enabled = 0; return 0; + } + +#ifdef TFE_DEBUG_INIT + log_message(tfe_log, "tfe_activate: Allocated memory successfully."); + log_message(tfe_log, "\ttfe at $%08X, tfe_packetpage at $%08X", tfe, tfe_packetpage ); +#endif + +#ifdef DOS_TFE + set_standard_tfe_interface(); +#endif + + if (!tfe_arch_activate(tfe_interface)) { + lib_free(tfe_packetpage); + lib_free(tfe); + tfe=NULL; + tfe_packetpage=NULL; + tfe_enabled = 0; + tfe_cannot_use = 1; + return 0; + } + + /* virtually reset the LAN chip */ + tfe_reset(); + + return 0; +} + +static +int tfe_deactivate_i(void) { +#ifdef TFE_DEBUG + log_message( tfe_log, "tfe_deactivate_i()." ); +#endif + + assert(tfe && tfe_packetpage); + + tfe_arch_deactivate(); + + lib_free(tfe); + tfe = NULL; + lib_free(tfe_packetpage); + tfe_packetpage = NULL; + return 0; } static int tfe_activate(void) { #ifdef TFE_DEBUG - log_message( tfe_log, "tfe_activate()." ); + log_message( tfe_log, "tfe_activate()." ); #endif - if (init_tfe_flag) { - return tfe_activate_i(); - } - else { - should_activate = 1; - } - return 0; + if (init_tfe_flag) { + return tfe_activate_i(); + } + else { + should_activate = 1; + } + return 0; } static int tfe_deactivate(void) { #ifdef TFE_DEBUG - log_message( tfe_log, "tfe_deactivate()." ); + log_message( tfe_log, "tfe_deactivate()." ); #endif - if (should_activate) - should_activate = 0; - else { - if (tfe_log != LOG_ERR) - return tfe_deactivate_i(); - } + if (should_activate) + should_activate = 0; + else { + if (tfe_log != LOG_ERR) + return tfe_deactivate_i(); + } - return 0; + return 0; } -void tfe_init(void) -{ - tfe_enabled = 1; - init_tfe_flag = 1; - should_activate = 1; - if (!tfe_arch_init()) { - tfe_enabled = 0; - tfe_cannot_use = 1; - } +void tfe_init(void) { + tfe_enabled = 1; + init_tfe_flag = 1; + should_activate = 1; + if (!tfe_arch_init()) { + tfe_enabled = 0; + tfe_cannot_use = 1; + } - if (should_activate) { - should_activate = 0; - if (tfe_activate() < 0) { - tfe_enabled = 0; - tfe_cannot_use = 1; - } + if (should_activate) { + should_activate = 0; + if (tfe_activate() < 0) { + tfe_enabled = 0; + tfe_cannot_use = 1; } + } } -void tfe_shutdown(void) -{ - assert( (tfe && tfe_packetpage) || (!tfe && !tfe_packetpage)); +void tfe_shutdown(void) { + assert( (tfe && tfe_packetpage) || (!tfe && !tfe_packetpage)); - if (tfe) - tfe_deactivate(); + if (tfe) + tfe_deactivate(); - if (tfe_interface != NULL) - lib_free(tfe_interface); + if (tfe_interface != NULL) + lib_free(tfe_interface); } @@ -611,665 +599,658 @@ void tfe_shutdown(void) /* reading and writing TFE register functions */ /* -These registers are currently fully or partially supported: + These registers are currently fully or partially supported: -TFE_PP_ADDR_CC_RXCFG 0x0102 * # RW - 4.4.6., p. 52 - 0003 * -TFE_PP_ADDR_CC_RXCTL 0x0104 * # RW - 4.4.8., p. 54 - 0005 * -TFE_PP_ADDR_CC_LINECTL 0x0112 * # RW - 4.4.16., p. 62 - 0013 * -TFE_PP_ADDR_SE_RXEVENT 0x0124 * # R- - 4.4.7., p. 53 - 0004 * -TFE_PP_ADDR_SE_BUSST 0x0138 * # R- - 4.4.21., p. 67 - 0018 * -TFE_PP_ADDR_TXCMD 0x0144 * # -W - 4.5., p. 70 - 5.7., p. 98 * -TFE_PP_ADDR_TXLENGTH 0x0146 * # -W - 4.5., p. 70 - 5.7., p. 98 * -TFE_PP_ADDR_MAC_ADDR 0x0158 * # RW - 4.6., p. 71 - 5.3., p. 86 * + TFE_PP_ADDR_CC_RXCFG 0x0102 * # RW - 4.4.6., p. 52 - 0003 * + TFE_PP_ADDR_CC_RXCTL 0x0104 * # RW - 4.4.8., p. 54 - 0005 * + TFE_PP_ADDR_CC_LINECTL 0x0112 * # RW - 4.4.16., p. 62 - 0013 * + TFE_PP_ADDR_SE_RXEVENT 0x0124 * # R- - 4.4.7., p. 53 - 0004 * + TFE_PP_ADDR_SE_BUSST 0x0138 * # R- - 4.4.21., p. 67 - 0018 * + TFE_PP_ADDR_TXCMD 0x0144 * # -W - 4.5., p. 70 - 5.7., p. 98 * + TFE_PP_ADDR_TXLENGTH 0x0146 * # -W - 4.5., p. 70 - 5.7., p. 98 * + TFE_PP_ADDR_MAC_ADDR 0x0158 * # RW - 4.6., p. 71 - 5.3., p. 86 * 0x015a 0x015c -*/ + */ #ifdef TFE_DEBUG_FRAMES #define return( _x_ ) \ - { \ - int retval = _x_; \ + { \ + int retval = _x_; \ \ - log_message(tfe_log, "%s correct_mac=%u, broadcast=%u, multicast=%u, hashed=%u, hash_index=%u", (retval? "+++ ACCEPTED":"--- rejected"), *pcorrect_mac, *pbroadcast, *pmulticast, *phashed, *phash_index); \ + log_message(tfe_log, "%s correct_mac=%u, broadcast=%u, multicast=%u, hashed=%u, hash_index=%u", (retval ? "+++ ACCEPTED" : "--- rejected"), *pcorrect_mac, *pbroadcast, *pmulticast, *phashed, *phash_index); \ \ - return retval; \ - } + return retval; \ + } #endif /* - This is a helper for tfe_receive() to determine if the received frame should be accepted - according to the settings. + This is a helper for tfe_receive() to determine if the received frame should be accepted + according to the settings. - This function is even allowed to be called in tfearch.c from tfe_arch_receive() if - necessary, which is the reason why its prototype is included here in tfearch.h. -*/ -int tfe_should_accept(unsigned char *buffer, int length, int *phashed, int *phash_index, - int *pcorrect_mac, int *pbroadcast, int *pmulticast) -{ - int hashreg; /* Hash Register (for hash computation) */ + This function is even allowed to be called in tfearch.c from tfe_arch_receive() if + necessary, which is the reason why its prototype is included here in tfearch.h. + */ +int tfe_should_accept(unsigned char *buffer, int length, int *phashed, int *phash_index, + int *pcorrect_mac, int *pbroadcast, int *pmulticast) { + int hashreg; /* Hash Register (for hash computation) */ - assert(length>=6); /* we need at least 6 octets since the DA has this length */ + assert(length>=6); /* we need at least 6 octets since the DA has this length */ - /* first of all, delete any status */ - *phashed = 0; - *phash_index = 0; - *pcorrect_mac = 0; - *pbroadcast = 0; - *pmulticast = 0; + /* first of all, delete any status */ + *phashed = 0; + *phash_index = 0; + *pcorrect_mac = 0; + *pbroadcast = 0; + *pmulticast = 0; #ifdef TFE_DEBUG_FRAMES - log_message(tfe_log, "tfe_should_accept called with %02X:%02X:%02X:%02X:%02X:%02X, length=%4u and buffer %s", - tfe_ia_mac[0], tfe_ia_mac[1], tfe_ia_mac[2], - tfe_ia_mac[3], tfe_ia_mac[4], tfe_ia_mac[5], - length, - debug_outbuffer(length, buffer) - ); + log_message(tfe_log, "tfe_should_accept called with %02X:%02X:%02X:%02X:%02X:%02X, length=%4u and buffer %s", + tfe_ia_mac[0], tfe_ia_mac[1], tfe_ia_mac[2], + tfe_ia_mac[3], tfe_ia_mac[4], tfe_ia_mac[5], + length, + debug_outbuffer(length, buffer) + ); #endif - if ( buffer[0]==tfe_ia_mac[0] - && buffer[1]==tfe_ia_mac[1] - && buffer[2]==tfe_ia_mac[2] - && buffer[3]==tfe_ia_mac[3] - && buffer[4]==tfe_ia_mac[4] - && buffer[5]==tfe_ia_mac[5] - ) { - /* this is our individual address (IA) */ + if ( buffer[0]==tfe_ia_mac[0] + && buffer[1]==tfe_ia_mac[1] + && buffer[2]==tfe_ia_mac[2] + && buffer[3]==tfe_ia_mac[3] + && buffer[4]==tfe_ia_mac[4] + && buffer[5]==tfe_ia_mac[5] + ) { + /* this is our individual address (IA) */ - *pcorrect_mac = 1; + *pcorrect_mac = 1; - /* if we don't want "correct MAC", we might have the chance - * that this address fits the hash index - */ - if (tfe_recv_mac || tfe_recv_promiscuous) - return(1); + /* if we don't want "correct MAC", we might have the chance + * that this address fits the hash index + */ + if (tfe_recv_mac || tfe_recv_promiscuous) + return(1); + } + + if ( buffer[0]==0xFF + && buffer[1]==0xFF + && buffer[2]==0xFF + && buffer[3]==0xFF + && buffer[4]==0xFF + && buffer[5]==0xFF + ) { + /* this is a broadcast address */ + *pbroadcast = 1; + + /* broadcasts cannot be accepted by the hash filter */ + return((tfe_recv_broadcast || tfe_recv_promiscuous) ? 1 : 0); + } + + /* now check if DA passes the hash filter */ + hashreg = (~crc32_buf((char *)buffer,6) >> 26) & 0x3F; + + *phashed = (tfe_hash_mask[(hashreg>=32) ? 1 : 0] & (1 << (hashreg&0x1F))) ? 1 : 0; + if (*phashed) { + *phash_index = hashreg; + + if (buffer[0] & 0x80) { + /* we have a multicast address */ + *pmulticast = 1; + + /* if the multicast address fits into the hash filter, + * the hashed bit has to be clear + */ + *phashed = 0; + + return((tfe_recv_multicast || tfe_recv_promiscuous) ? 1 : 0); } + return((tfe_recv_hashfilter || tfe_recv_promiscuous) ? 1 : 0); + } - if ( buffer[0]==0xFF - && buffer[1]==0xFF - && buffer[2]==0xFF - && buffer[3]==0xFF - && buffer[4]==0xFF - && buffer[5]==0xFF - ) { - /* this is a broadcast address */ - *pbroadcast = 1; - - /* broadcasts cannot be accepted by the hash filter */ - return((tfe_recv_broadcast || tfe_recv_promiscuous) ? 1 : 0); - } - - /* now check if DA passes the hash filter */ - hashreg = (~crc32_buf((char *)buffer,6) >> 26) & 0x3F; - - *phashed = (tfe_hash_mask[(hashreg>=32)?1:0] & (1 << (hashreg&0x1F))) ? 1 : 0; - if (*phashed) { - *phash_index = hashreg; - - if (buffer[0] & 0x80) { - /* we have a multicast address */ - *pmulticast = 1; - - /* if the multicast address fits into the hash filter, - * the hashed bit has to be clear - */ - *phashed = 0; - - return((tfe_recv_multicast || tfe_recv_promiscuous) ? 1 : 0); - } - return((tfe_recv_hashfilter || tfe_recv_promiscuous) ? 1 : 0); - } - - return(tfe_recv_promiscuous ? 1 : 0); + return(tfe_recv_promiscuous ? 1 : 0); } #ifdef TFE_DEBUG_FRAMES #undef return #endif -static -word16 tfe_receive(void) -{ - word16 ret_val = 0x0004; +static +word16 tfe_receive(void) { + word16 ret_val = 0x0004; - byte buffer[MAX_RXLENGTH]; + byte buffer[MAX_RXLENGTH]; - int len; - int hashed; - int hash_index; - int rx_ok; - int correct_mac; - int broadcast; - int multicast; - int crc_error; + int len; + int hashed; + int hash_index; + int rx_ok; + int correct_mac; + int broadcast; + int multicast; + int crc_error; - int newframe; + int newframe; - int ready; + int ready; - do { - len = MAX_RXLENGTH; + do { + len = MAX_RXLENGTH; - ready = 1 ; /* assume we will find a good frame */ + ready = 1; /* assume we will find a good frame */ - newframe = tfe_arch_receive( - buffer, /* where to store a frame */ - &len, /* length of received frame */ - &hashed, /* set if the dest. address is accepted by the hash filter */ - &hash_index, /* hash table index if hashed == TRUE */ - &rx_ok, /* set if good CRC and valid length */ - &correct_mac, /* set if dest. address is exactly our IA */ - &broadcast, /* set if dest. address is a broadcast address */ - &crc_error /* set if received frame had a CRC error */ - ); + newframe = tfe_arch_receive( + buffer, /* where to store a frame */ + &len, /* length of received frame */ + &hashed, /* set if the dest. address is accepted by the hash filter */ + &hash_index, /* hash table index if hashed == TRUE */ + &rx_ok, /* set if good CRC and valid length */ + &correct_mac, /* set if dest. address is exactly our IA */ + &broadcast, /* set if dest. address is a broadcast address */ + &crc_error /* set if received frame had a CRC error */ + ); - assert((len&1) == 0); /* length has to be even! */ + assert((len&1) == 0); /* length has to be even! */ - if (newframe) { - if (hashed || correct_mac || broadcast) { - /* we already know the type of frame: Trust it! */ + if (newframe) { + if (hashed || correct_mac || broadcast) { + /* we already know the type of frame: Trust it! */ #ifdef TFE_DEBUG_FRAMES - log_message( tfe_log, "+++ tfe_receive(): *** hashed=%u, correct_mac=%u, " - "broadcast=%u", hashed, correct_mac, broadcast); + log_message( tfe_log, "+++ tfe_receive(): *** hashed=%u, correct_mac=%u, " + "broadcast=%u", hashed, correct_mac, broadcast); #endif - } - else { - /* determine ourself the type of frame */ - if (!tfe_should_accept(buffer, - len, &hashed, &hash_index, &correct_mac, &broadcast, &multicast)) { + } + else { + /* determine ourself the type of frame */ + if (!tfe_should_accept(buffer, + len, &hashed, &hash_index, &correct_mac, &broadcast, &multicast)) { - /* if we should not accept this frame, just do nothing - * now, look for another one */ - ready = 0; /* try another frame */ - continue; - } - } - - - /* we did receive a frame, return that status */ - ret_val |= rx_ok ? 0x0100 : 0; - ret_val |= multicast ? 0x0200 : 0; - - if (!multicast) { - ret_val |= hashed ? 0x0040 : 0; - } - - if (hashed && rx_ok) { - /* we have the 2nd, special format with hash index: */ - assert(hash_index < 64); - ret_val |= hash_index << 9; - } - else { - /* we have the regular format */ - ret_val |= correct_mac ? 0x0400 : 0; - ret_val |= broadcast ? 0x0800 : 0; - ret_val |= crc_error ? 0x1000 : 0; - ret_val |= (lenMAX_RXLENGTH) ? 0x4000 : 0; - } - - /* discard any octets that are beyond the MAX_RXLEN */ - if (len>MAX_RXLENGTH) { - len = MAX_RXLENGTH; - } - - if (rx_ok) { - int i; - - /* set relevant parts of the PP area to correct values */ - SET_PP_16(TFE_PP_ADDR_RXLENGTH, len); - - for (i=0;iMAX_RXLENGTH) ? 0x4000 : 0; + } + + /* discard any octets that are beyond the MAX_RXLEN */ + if (len>MAX_RXLENGTH) { + len = MAX_RXLENGTH; + } + + if (rx_ok) { + int i; + + /* set relevant parts of the PP area to correct values */ + SET_PP_16(TFE_PP_ADDR_RXLENGTH, len); + + for (i=0; i=6) && (!odd_address)) - rx_buffer += 2; - - addr += rx_buffer; - value = GET_PP_8(addr); - rx_count++; - } - -#ifdef TFE_DEBUG_RXTX_DATA - log_message(tfe_log,"RX: %04x/%04x: %02x (buffer=%04x,odd=%d)", - rx_count,rx_length+4,value,addr,odd_address); -#endif - - /* check frame end */ - if(rx_count>=rx_length+4) { - /* reset receiver state to idle */ - rx_state = TFE_RX_IDLE; -#ifdef TFE_DEBUG_RXTX_STATE - log_message(tfe_log,"RX: read frame (length=%04x)",rx_length); -#endif - } - return value; + /* incr after RXSTATUS or RX_LENGTH even (L) read */ + if(!odd_address) + rx_buffer += 2; } + /* read frame data */ + else { + /* incr before frame read (but not in first word) */ + if((rx_count>=6) && (!odd_address)) + rx_buffer += 2; + + addr += rx_buffer; + value = GET_PP_8(addr); + rx_count++; + } + +#ifdef TFE_DEBUG_RXTX_DATA + log_message(tfe_log,"RX: %04x/%04x: %02x (buffer=%04x,odd=%d)", + rx_count,rx_length+4,value,addr,odd_address); +#endif + + /* check frame end */ + if(rx_count>=rx_length+4) { + /* reset receiver state to idle */ + rx_state = TFE_RX_IDLE; +#ifdef TFE_DEBUG_RXTX_STATE + log_message(tfe_log,"RX: read frame (length=%04x)",rx_length); +#endif + } + return value; + } } /* ------------------------------------------------------------------------- */ /* handle side-effects of read and write operations */ /* - This is called *after* the relevant octets are written -*/ + This is called *after* the relevant octets are written + */ static -void tfe_sideeffects_write_pp(word16 ppaddress, int odd_address) -{ +void tfe_sideeffects_write_pp(word16 ppaddress, int odd_address) { // const char *on_off[2] = { "on","off" }; //#define on_off_str(x) ((x) ? on_off[0] : on_off[1]) - word16 content = GET_PP_16( ppaddress ); + word16 content = GET_PP_16( ppaddress ); - assert((ppaddress & 1) == 0); + assert((ppaddress & 1) == 0); - switch (ppaddress) - { + switch (ppaddress) + { case TFE_PP_ADDR_CC_RXCFG: - /* Skip_1 Flag: remove current (partial) tx frame and restore state */ - if (content & 0x40) { - -/* tfe_arch_receive_remove_committed_frame(); */ - - /* restore tx state */ - if(tx_state!=TFE_TX_IDLE) { - tx_state = TFE_TX_IDLE; -#ifdef TFE_DEBUG_RXTX_STATE - log_message(tfe_log,"TX: skipping current frame"); -#endif - } + /* Skip_1 Flag: remove current (partial) tx frame and restore state */ + if (content & 0x40) { - /* reset transmitter */ - tfe_set_transmitter(tx_enabled); - - /* this is an "act once" bit, thus restore it to zero. */ - content &= ~0x40; - SET_PP_16( ppaddress, content ); +/* tfe_arch_receive_remove_committed_frame(); */ + + /* restore tx state */ + if(tx_state!=TFE_TX_IDLE) { + tx_state = TFE_TX_IDLE; +#ifdef TFE_DEBUG_RXTX_STATE + log_message(tfe_log,"TX: skipping current frame"); +#endif } - break; + + /* reset transmitter */ + tfe_set_transmitter(tx_enabled); + + /* this is an "act once" bit, thus restore it to zero. */ + content &= ~0x40; + SET_PP_16( ppaddress, content ); + } + break; case TFE_PP_ADDR_CC_RXCTL: - if(tfe_recv_control!=content) { - tfe_recv_broadcast = content & 0x0800; /* broadcast */ - tfe_recv_mac = content & 0x0400; /* individual address (IA) */ - tfe_recv_multicast = content & 0x0200; /* multicast if address passes the hash filter */ - tfe_recv_correct = content & 0x0100; /* accept correct frames */ - tfe_recv_promiscuous = content & 0x0080; /* promiscuous mode */ - tfe_recv_hashfilter = content & 0x0040; /* accept if IA passes the hash filter */ - tfe_recv_control = content; - - /*log_message(tfe_log,"setup receiver: broadcast=%s mac=%s multicast=%s correct=%s promiscuous=%s hashfilter=%s", - on_off_str(tfe_recv_broadcast), - on_off_str(tfe_recv_mac), - on_off_str(tfe_recv_multicast), - on_off_str(tfe_recv_correct), - on_off_str(tfe_recv_promiscuous), - on_off_str(tfe_recv_hashfilter));*/ + if(tfe_recv_control!=content) { + tfe_recv_broadcast = content & 0x0800; /* broadcast */ + tfe_recv_mac = content & 0x0400; /* individual address (IA) */ + tfe_recv_multicast = content & 0x0200; /* multicast if address passes the hash filter */ + tfe_recv_correct = content & 0x0100; /* accept correct frames */ + tfe_recv_promiscuous = content & 0x0080; /* promiscuous mode */ + tfe_recv_hashfilter = content & 0x0040; /* accept if IA passes the hash filter */ + tfe_recv_control = content; - tfe_arch_recv_ctl( tfe_recv_broadcast, - tfe_recv_mac, - tfe_recv_multicast, - tfe_recv_correct, - tfe_recv_promiscuous, - tfe_recv_hashfilter - ); - } - break; + /*log_message(tfe_log,"setup receiver: broadcast=%s mac=%s multicast=%s correct=%s promiscuous=%s hashfilter=%s", + on_off_str(tfe_recv_broadcast), + on_off_str(tfe_recv_mac), + on_off_str(tfe_recv_multicast), + on_off_str(tfe_recv_correct), + on_off_str(tfe_recv_promiscuous), + on_off_str(tfe_recv_hashfilter));*/ + + tfe_arch_recv_ctl( tfe_recv_broadcast, + tfe_recv_mac, + tfe_recv_multicast, + tfe_recv_correct, + tfe_recv_promiscuous, + tfe_recv_hashfilter + ); + } + break; case TFE_PP_ADDR_CC_LINECTL: - { - int enable_tx = (content & 0x0080) == 0x0080; - int enable_rx = (content & 0x0040) == 0x0040; - - if((enable_tx!=tx_enabled)||(enable_rx!=rx_enabled)) { - tfe_arch_line_ctl(enable_tx,enable_rx); - tfe_set_transmitter(enable_tx); - tfe_set_receiver(enable_rx); - - /*log_message(tfe_log,"line control: transmitter=%s receiver=%s", - on_off_str(enable_tx), - on_off_str(enable_rx));*/ - } - } - break; + { + int enable_tx = (content & 0x0080) == 0x0080; + int enable_rx = (content & 0x0040) == 0x0040; + + if((enable_tx!=tx_enabled)||(enable_rx!=rx_enabled)) { + tfe_arch_line_ctl(enable_tx,enable_rx); + tfe_set_transmitter(enable_tx); + tfe_set_receiver(enable_rx); + + /*log_message(tfe_log,"line control: transmitter=%s receiver=%s", + on_off_str(enable_tx), + on_off_str(enable_rx));*/ + } + } + break; case TFE_PP_ADDR_CC_SELFCTL: - { - /* reset chip? */ - if((content & 0x40)==0x40) { - tfe_reset(); - } - } - break; - + { + /* reset chip? */ + if((content & 0x40)==0x40) { + tfe_reset(); + } + } + break; + case TFE_PP_ADDR_TXCMD: - { - if(odd_address) { - word16 txcommand = GET_PP_16(TFE_PP_ADDR_TXCMD); - - /* already transmitting? */ - if(tx_state == TFE_TX_READ_BUSST) { + { + if(odd_address) { + word16 txcommand = GET_PP_16(TFE_PP_ADDR_TXCMD); + + /* already transmitting? */ + if(tx_state == TFE_TX_READ_BUSST) { #ifdef TFE_DEBUG_WARN_RXTX - log_message(tfe_log, "WARNING! Early abort of transmitted frame"); + log_message(tfe_log, "WARNING! Early abort of transmitted frame"); #endif - } - - /* The transmit status command gets the last transmit command */ - SET_PP_16(TFE_PP_ADDR_CC_TXCMD, txcommand); - - /* set transmit state */ - tx_state = TFE_TX_GOT_CMD; - tfe_set_tx_status(0,0); - -#ifdef TFE_DEBUG_RXTX_STATE - log_message(tfe_log, "TX: COMMAND accepted (%04x)",txcommand); -#endif - } } - break; + + /* The transmit status command gets the last transmit command */ + SET_PP_16(TFE_PP_ADDR_CC_TXCMD, txcommand); + + /* set transmit state */ + tx_state = TFE_TX_GOT_CMD; + tfe_set_tx_status(0,0); + +#ifdef TFE_DEBUG_RXTX_STATE + log_message(tfe_log, "TX: COMMAND accepted (%04x)",txcommand); +#endif + } + } + break; case TFE_PP_ADDR_TXLENGTH: - { - if(odd_address && (tx_state == TFE_TX_GOT_CMD)) { - word16 txlength = GET_PP_16(TFE_PP_ADDR_TXLENGTH); - word16 txcommand = GET_PP_16(TFE_PP_ADDR_CC_TXCMD); - - if(txlength<4) { - /* frame to short */ + { + if(odd_address && (tx_state == TFE_TX_GOT_CMD)) { + word16 txlength = GET_PP_16(TFE_PP_ADDR_TXLENGTH); + word16 txcommand = GET_PP_16(TFE_PP_ADDR_CC_TXCMD); + + if(txlength<4) { + /* frame to short */ #ifdef TFE_DEBUG_RXTX_STATE - log_message(tfe_log, "TX: LENGTH rejected - too short! (%04x)",txlength); + log_message(tfe_log, "TX: LENGTH rejected - too short! (%04x)",txlength); #endif - /* mask space available but do not commit */ - tx_state = TFE_TX_IDLE; - tfe_set_tx_status(1,0); - } - else if ( (txlength>MAX_TXLENGTH) - || ((txlength>MAX_TXLENGTH-4) && (!(txcommand&0x1000))) - ) { - tx_state = TFE_TX_IDLE; + /* mask space available but do not commit */ + tx_state = TFE_TX_IDLE; + tfe_set_tx_status(1,0); + } + else if ( (txlength>MAX_TXLENGTH) + || ((txlength>MAX_TXLENGTH-4) && (!(txcommand&0x1000))) + ) { + tx_state = TFE_TX_IDLE; #ifdef TFE_DEBUG_RXTX_STATE - log_message(tfe_log, "TX: LENGTH rejected - too long! (%04x)",txlength); + log_message(tfe_log, "TX: LENGTH rejected - too long! (%04x)",txlength); #endif - /* txlength too big, mark an error */ - tfe_set_tx_status(0,1); - } - else { - /* make sure we put the octets to transmit at the right place */ - tx_buffer = TFE_PP_ADDR_TX_FRAMELOC; - tx_count = 0; - tx_length = txlength; - tx_state = TFE_TX_GOT_LEN; + /* txlength too big, mark an error */ + tfe_set_tx_status(0,1); + } + else { + /* make sure we put the octets to transmit at the right place */ + tx_buffer = TFE_PP_ADDR_TX_FRAMELOC; + tx_count = 0; + tx_length = txlength; + tx_state = TFE_TX_GOT_LEN; #ifdef TFE_DEBUG_RXTX_STATE - log_message(tfe_log, "TX: LENGTH accepted (%04x)",txlength); + log_message(tfe_log, "TX: LENGTH accepted (%04x)",txlength); #endif - /* all right, signal that we're ready for the next frame */ - tfe_set_tx_status(1,0); - } - } + /* all right, signal that we're ready for the next frame */ + tfe_set_tx_status(1,0); } - break; + } + } + break; case TFE_PP_ADDR_LOG_ADDR_FILTER: case TFE_PP_ADDR_LOG_ADDR_FILTER+2: case TFE_PP_ADDR_LOG_ADDR_FILTER+4: case TFE_PP_ADDR_LOG_ADDR_FILTER+6: - { - unsigned int pos = 8 * (ppaddress - TFE_PP_ADDR_LOG_ADDR_FILTER + odd_address); - DWORD *p = (pos < 32) ? &tfe_hash_mask[0] : &tfe_hash_mask[1]; + { + unsigned int pos = 8 * (ppaddress - TFE_PP_ADDR_LOG_ADDR_FILTER + odd_address); + DWORD *p = (pos < 32) ? &tfe_hash_mask[0] : &tfe_hash_mask[1]; - *p &= ~(0xFF << pos); /* clear out relevant bits */ - *p |= GET_PP_8(ppaddress+odd_address) << pos; + *p &= ~(0xFF << pos); /* clear out relevant bits */ + *p |= GET_PP_8(ppaddress+odd_address) << pos; - // tfe_arch_set_hashfilter(tfe_hash_mask); + // tfe_arch_set_hashfilter(tfe_hash_mask); #if 0 - if(odd_address && (ppaddress == TFE_PP_ADDR_LOG_ADDR_FILTER+6)) - log_message(tfe_log,"set hash filter: %02x:%02x:%02x:%02x:%02x:%02x", - tfe_hash_mask[0], - tfe_hash_mask[1], - tfe_hash_mask[2], - tfe_hash_mask[3], - tfe_hash_mask[4], - tfe_hash_mask[5]); + if(odd_address && (ppaddress == TFE_PP_ADDR_LOG_ADDR_FILTER+6)) + log_message(tfe_log,"set hash filter: %02x:%02x:%02x:%02x:%02x:%02x", + tfe_hash_mask[0], + tfe_hash_mask[1], + tfe_hash_mask[2], + tfe_hash_mask[3], + tfe_hash_mask[4], + tfe_hash_mask[5]); #endif - } - break; + } + break; case TFE_PP_ADDR_MAC_ADDR: case TFE_PP_ADDR_MAC_ADDR+2: case TFE_PP_ADDR_MAC_ADDR+4: - /* the MAC address has been changed */ - tfe_ia_mac[ppaddress-TFE_PP_ADDR_MAC_ADDR+odd_address] = - GET_PP_8(ppaddress+odd_address); - tfe_arch_set_mac(tfe_ia_mac); - - if(odd_address && (ppaddress == TFE_PP_ADDR_MAC_ADDR+4)) - /*log_message(tfe_log,"set MAC address: %02x:%02x:%02x:%02x:%02x:%02x", - tfe_ia_mac[0],tfe_ia_mac[1],tfe_ia_mac[2], - tfe_ia_mac[3],tfe_ia_mac[4],tfe_ia_mac[5]);*/ + /* the MAC address has been changed */ + tfe_ia_mac[ppaddress-TFE_PP_ADDR_MAC_ADDR+odd_address] = + GET_PP_8(ppaddress+odd_address); + tfe_arch_set_mac(tfe_ia_mac); + + if(odd_address && (ppaddress == TFE_PP_ADDR_MAC_ADDR+4)) + /*log_message(tfe_log,"set MAC address: %02x:%02x:%02x:%02x:%02x:%02x", + tfe_ia_mac[0],tfe_ia_mac[1],tfe_ia_mac[2], + tfe_ia_mac[3],tfe_ia_mac[4],tfe_ia_mac[5]);*/ break; - } + } #undef on_off_str } /* - This is called *before* the relevant octets are read -*/ + This is called *before* the relevant octets are read + */ static -void tfe_sideeffects_read_pp(word16 ppaddress,int odd_address) -{ - switch (ppaddress) - { +void tfe_sideeffects_read_pp(word16 ppaddress,int odd_address) { + switch (ppaddress) + { case TFE_PP_ADDR_SE_RXEVENT: - /* reading this before all octets of the frame are read - performs an "implied skip" */ - { - int access_mask = (odd_address) ? 1 : 2; - - /* update the status register only if the full word of the last - status was read! unfortunately different access patterns are - possible: either the status is read LH, LH, LH... - or HL, HL, HL, or even L, L, L or H, H, H */ - if((access_mask & rxevent_read_mask)!=0) { - /* receiver is not enabled */ - if(!rx_enabled) { -#ifdef TFE_DEBUG_WARN_RXTX - log_message(tfe_log,"WARNING! Can't receive any frame (Receiver is not enabled)!"); -#endif - } else { - /* perform frame reception */ - word16 ret_val = tfe_receive(); + /* reading this before all octets of the frame are read + performs an "implied skip" */ + { + int access_mask = (odd_address) ? 1 : 2; - /* RXSTATUS and RXEVENT are the same, except that RXSTATUS buffers - the old value while RXEVENT sets a new value whenever it is called - */ - SET_PP_16(TFE_PP_ADDR_RXSTATUS, ret_val); - SET_PP_16(TFE_PP_ADDR_SE_RXEVENT, ret_val); - } - - /* reset read mask of (possible) other access */ - rxevent_read_mask = access_mask; - } else { - /* add access bit to mask */ - rxevent_read_mask |= access_mask; - } + /* update the status register only if the full word of the last + status was read! unfortunately different access patterns are + possible: either the status is read LH, LH, LH... + or HL, HL, HL, or even L, L, L or H, H, H */ + if((access_mask & rxevent_read_mask)!=0) { + /* receiver is not enabled */ + if(!rx_enabled) { +#ifdef TFE_DEBUG_WARN_RXTX + log_message(tfe_log,"WARNING! Can't receive any frame (Receiver is not enabled)!"); +#endif + } else { + /* perform frame reception */ + word16 ret_val = tfe_receive(); + + /* RXSTATUS and RXEVENT are the same, except that RXSTATUS buffers + the old value while RXEVENT sets a new value whenever it is called + */ + SET_PP_16(TFE_PP_ADDR_RXSTATUS, ret_val); + SET_PP_16(TFE_PP_ADDR_SE_RXEVENT, ret_val); } - break; + /* reset read mask of (possible) other access */ + rxevent_read_mask = access_mask; + } else { + /* add access bit to mask */ + rxevent_read_mask |= access_mask; + } + } + + break; case TFE_PP_ADDR_SE_BUSST: - if(odd_address) { - /* read busst before transmit condition is fullfilled */ - if(tx_state == TFE_TX_GOT_LEN) { - word16 bus_status = GET_PP_16(TFE_PP_ADDR_SE_BUSST); - /* check Rdy4TXNow flag */ - if((bus_status & 0x100) == 0x100) { - tx_state = TFE_TX_READ_BUSST; + if(odd_address) { + /* read busst before transmit condition is fullfilled */ + if(tx_state == TFE_TX_GOT_LEN) { + word16 bus_status = GET_PP_16(TFE_PP_ADDR_SE_BUSST); + /* check Rdy4TXNow flag */ + if((bus_status & 0x100) == 0x100) { + tx_state = TFE_TX_READ_BUSST; #ifdef TFE_DEBUG_RXTX_STATE - log_message(tfe_log, "TX: Ready4TXNow set! (%04x)", - bus_status); + log_message(tfe_log, "TX: Ready4TXNow set! (%04x)", + bus_status); #endif - } else { + } else { #ifdef TFE_DEBUG_RXTX_STATE - log_message(tfe_log, "TX: waiting for Ready4TXNow! (%04x)", - bus_status); -#endif - } - } + log_message(tfe_log, "TX: waiting for Ready4TXNow! (%04x)", + bus_status); +#endif + } } - break; - } + } + break; + } } /* ------------------------------------------------------------------------- */ /* read/write from packet page register */ /* read a register from packet page */ -static word16 tfe_read_register(word16 ppaddress) -{ +static word16 tfe_read_register(word16 ppaddress) { word16 value = GET_PP_16(ppaddress); - + /* --- check the register address --- */ if(ppaddress<0x100) { /* reserved range reads 0x0300 on real HW */ @@ -1277,18 +1258,18 @@ static word16 tfe_read_register(word16 ppaddress) return 0x0300; } } - + /* --- read control register range --- */ else if(ppaddress<0x120) { word16 regNum = ppaddress - 0x100; regNum &= ~1; - regNum ++; + regNum++; #ifdef TFE_DEBUG_REGISTERS log_message(tfe_log, "Read Control Register %04x: %04x (reg=%02x)", ppaddress,value,regNum); #endif - + /* reserved register? */ if((regNum==0x01)||(regNum==0x11)||(regNum>0x19)) { #ifdef TFE_DEBUG_WARN_REG @@ -1301,9 +1282,9 @@ static word16 tfe_read_register(word16 ppaddress) } /* make sure interal address is always valid */ - assert((value&0x3f) == regNum); + assert((value&0x3f) == regNum); } - + /* --- read status register range --- */ else if(ppaddress<0x140) { word16 regNum = ppaddress - 0x120; @@ -1332,7 +1313,7 @@ static word16 tfe_read_register(word16 ppaddress) /* make sure interal address is always valid */ assert((value&0x3f) == regNum); } - + /* --- read transmit register range --- */ else if(ppaddress<0x150) { if(ppaddress==0x144) { @@ -1362,7 +1343,7 @@ static word16 tfe_read_register(word16 ppaddress) return 0x0300; } } - + /* --- read address filter register range --- */ else if(ppaddress<0x160) { /* reserved range */ @@ -1376,10 +1357,10 @@ static word16 tfe_read_register(word16 ppaddress) return 0x0300; } } - + /* --- reserved range below 0x400 --- - returns 0x300 on real HW - */ + returns 0x300 on real HW + */ else if(ppaddress<0x400) { #ifdef TFE_DEBUG_WARN_REG log_message(tfe_log, @@ -1388,7 +1369,7 @@ static word16 tfe_read_register(word16 ppaddress) #endif return 0x0300; } - + /* --- range from 0x400 .. 0x9ff --- RX Frame */ else if(ppaddress<0xa00) { #ifdef TFE_DEBUG_WARN_REG @@ -1398,7 +1379,7 @@ static word16 tfe_read_register(word16 ppaddress) #endif return 0x0000; } - + /* --- range from 0xa00 .. 0xfff --- TX Frame */ else { #ifdef TFE_DEBUG_WARN_REG @@ -1408,13 +1389,12 @@ static word16 tfe_read_register(word16 ppaddress) #endif return 0x0000; } - + /* actually read from pp memory */ return value; } -void tfe_write_register(word16 ppaddress,word16 value) -{ +void tfe_write_register(word16 ppaddress,word16 value) { /* --- write bus interface register range --- */ if(ppaddress<0x100) { int ignore = 0; @@ -1436,7 +1416,7 @@ void tfe_write_register(word16 ppaddress,word16 value) return; } } - + /* --- write to control register range --- */ else if(ppaddress<0x120) { word16 regNum = ppaddress - 0x100; @@ -1464,7 +1444,7 @@ void tfe_write_register(word16 ppaddress,word16 value) return; } } - + /* --- write to status register range --- */ else if(ppaddress<0x140) { #ifdef TFE_DEBUG_WARN_REG @@ -1536,7 +1516,7 @@ void tfe_write_register(word16 ppaddress,word16 value) #endif return; } - else if(ppaddress<0xa00){ + else if(ppaddress<0xa00) { #ifdef TFE_DEBUG_WARN_REG log_message(tfe_log, "WARNING! Ignoring write to RX Buffer Range %04x", @@ -1554,15 +1534,14 @@ void tfe_write_register(word16 ppaddress,word16 value) } /* actually set value */ - SET_PP_16(ppaddress, value); + SET_PP_16(ppaddress, value); } #define PP_PTR_AUTO_INCR_FLAG 0x8000 /* auto increment flag in package pointer */ #define PP_PTR_FLAG_MASK 0xf000 /* is always : x y 1 1 (with x=auto incr) */ #define PP_PTR_ADDR_MASK 0x0fff /* address portion of packet page pointer */ -static void tfe_auto_incr_pp_ptr(void) -{ +static void tfe_auto_incr_pp_ptr(void) { /* perform auto increment of packet page pointer */ if((tfe_packetpage_ptr & PP_PTR_AUTO_INCR_FLAG)==PP_PTR_AUTO_INCR_FLAG) { /* pointer is always increment by one on real HW */ @@ -1581,235 +1560,232 @@ static void tfe_auto_incr_pp_ptr(void) #define LOHI_WORD(x,y) ( (word16)(x) | ( ((word16)(y)) <<8 ) ) /* ----- read byte from I/O range in VICE ----- */ -byte tfe_read(word16 io_address) -{ - byte retval,lo,hi; - word16 word_value; - word16 reg_base; +byte tfe_read(word16 io_address) { + byte retval,lo,hi; + word16 word_value; + word16 reg_base; - assert( tfe ); - assert( tfe_packetpage ); - assert( io_address < 0x10); + assert( tfe ); + assert( tfe_packetpage ); + assert( io_address < 0x10); - /*if (tfe_as_rr_net) { - // rr status register is handled by rr cartidge - if (io_address < 0x02) { - return 0; - } - io_address ^= 0x08; - }*/ - /* register base addr */ - reg_base = io_address & ~1; + /*if (tfe_as_rr_net) { + // rr status register is handled by rr cartidge + if (io_address < 0x02) { + return 0; + } + io_address ^= 0x08; + }*/ + /* register base addr */ + reg_base = io_address & ~1; - /* RX register is special as it reads from RX buffer directly */ - if((reg_base==TFE_ADDR_RXTXDATA)||(reg_base==TFE_ADDR_RXTXDATA2)) { - //io_source=IO_SOURCE_TFE_RR_NET; - return tfe_read_rx_buffer(io_address & 0x01); - } - - /* read packet page pointer */ - if(reg_base==TFE_ADDR_PP_PTR) { - word_value = tfe_packetpage_ptr; - } - /* read a register from packet page */ - else { - word16 ppaddress; - - /* determine read addr in packet page */ - switch (reg_base) { - /* PP_DATA2 behaves like PP_DATA on real HW - both show the contents at the page pointer */ - case TFE_ADDR_PP_DATA: - case TFE_ADDR_PP_DATA2: - /* mask and align address of packet pointer */ - ppaddress = tfe_packetpage_ptr & PP_PTR_ADDR_MASK; - ppaddress &= ~1; - /* if flags match then auto incr pointer */ - tfe_auto_incr_pp_ptr(); - break; - case TFE_ADDR_INTSTQUEUE: - ppaddress = TFE_PP_ADDR_SE_ISQ; - break; - case TFE_ADDR_TXCMD: - ppaddress = TFE_PP_ADDR_TXCMD; - break; - case TFE_ADDR_TXLENGTH: - ppaddress = TFE_PP_ADDR_TXLENGTH; - break; - default: - /* invalid! */ - assert(0); - break; - } - - /* do side effects before access */ - tfe_sideeffects_read_pp(ppaddress,io_address&1); - - /* read register value */ - word_value = tfe_read_register(ppaddress); - -#ifdef TFE_DEBUG_LOAD - log_message(tfe_log, "reading PP Ptr: $%04X => $%04X.",ppaddress,word_value); -#endif - } - - /* extract return value from word_value */ - lo = LO_BYTE(word_value); - hi = HI_BYTE(word_value); - if((io_address & 1) == 0) { - /* low byte on even address */ - retval = lo; - } else { - /* high byte on odd address */ - retval = hi; - } - -#ifdef TFE_DEBUG_LOAD - log_message(tfe_log, "read [$%02X] => $%02X.", io_address, retval); -#endif - - /* update _word_ value in register bank */ - tfe[reg_base] = lo; - tfe[reg_base+1] = hi; - + /* RX register is special as it reads from RX buffer directly */ + if((reg_base==TFE_ADDR_RXTXDATA)||(reg_base==TFE_ADDR_RXTXDATA2)) { //io_source=IO_SOURCE_TFE_RR_NET; - return retval; + return tfe_read_rx_buffer(io_address & 0x01); + } + + /* read packet page pointer */ + if(reg_base==TFE_ADDR_PP_PTR) { + word_value = tfe_packetpage_ptr; + } + /* read a register from packet page */ + else { + word16 ppaddress; + + /* determine read addr in packet page */ + switch (reg_base) { + /* PP_DATA2 behaves like PP_DATA on real HW + both show the contents at the page pointer */ + case TFE_ADDR_PP_DATA: + case TFE_ADDR_PP_DATA2: + /* mask and align address of packet pointer */ + ppaddress = tfe_packetpage_ptr & PP_PTR_ADDR_MASK; + ppaddress &= ~1; + /* if flags match then auto incr pointer */ + tfe_auto_incr_pp_ptr(); + break; + case TFE_ADDR_INTSTQUEUE: + ppaddress = TFE_PP_ADDR_SE_ISQ; + break; + case TFE_ADDR_TXCMD: + ppaddress = TFE_PP_ADDR_TXCMD; + break; + case TFE_ADDR_TXLENGTH: + ppaddress = TFE_PP_ADDR_TXLENGTH; + break; + default: + /* invalid! */ + assert(0); + break; + } + + /* do side effects before access */ + tfe_sideeffects_read_pp(ppaddress,io_address&1); + + /* read register value */ + word_value = tfe_read_register(ppaddress); + +#ifdef TFE_DEBUG_LOAD + log_message(tfe_log, "reading PP Ptr: $%04X => $%04X.",ppaddress,word_value); +#endif + } + + /* extract return value from word_value */ + lo = LO_BYTE(word_value); + hi = HI_BYTE(word_value); + if((io_address & 1) == 0) { + /* low byte on even address */ + retval = lo; + } else { + /* high byte on odd address */ + retval = hi; + } + +#ifdef TFE_DEBUG_LOAD + log_message(tfe_log, "read [$%02X] => $%02X.", io_address, retval); +#endif + + /* update _word_ value in register bank */ + tfe[reg_base] = lo; + tfe[reg_base+1] = hi; + + //io_source=IO_SOURCE_TFE_RR_NET; + return retval; } /* ----- write byte to I/O range of VICE ----- */ -void tfe_store(word16 io_address, byte var) -{ - word16 reg_base; - word16 word_value; - assert( tfe ); - assert( tfe_packetpage ); - assert( io_address < 0x10); +void tfe_store(word16 io_address, byte var) { + word16 reg_base; + word16 word_value; + assert( tfe ); + assert( tfe_packetpage ); + assert( io_address < 0x10); - /*if (tfe_as_rr_net) { - // rr control register is handled by rr cartidge - if (io_address < 0x02) { - return; - } - io_address ^= 0x08; - }*/ + /*if (tfe_as_rr_net) { + // rr control register is handled by rr cartidge + if (io_address < 0x02) { + return; + } + io_address ^= 0x08; + }*/ #ifdef TFE_DEBUG_STORE - log_message(tfe_log, "store [$%02X] <= $%02X.", io_address, (int)byte); + log_message(tfe_log, "store [$%02X] <= $%02X.", io_address, (int)byte); #endif - /* register base addr */ - reg_base = io_address & ~1; + /* register base addr */ + reg_base = io_address & ~1; - /* TX Register is special as it writes to TX buffer directly */ - if((reg_base==TFE_ADDR_RXTXDATA)||(reg_base==TFE_ADDR_RXTXDATA2)) { - tfe_write_tx_buffer(var,io_address & 1); - return; + /* TX Register is special as it writes to TX buffer directly */ + if((reg_base==TFE_ADDR_RXTXDATA)||(reg_base==TFE_ADDR_RXTXDATA2)) { + tfe_write_tx_buffer(var,io_address & 1); + return; + } + + /* combine stored value with new written byte */ + if((io_address & 1) == 0) { + /* overwrite low byte */ + word_value = LOHI_WORD(var,tfe[reg_base+1]); + } else { + /* overwrite high byte */ + word_value = LOHI_WORD(tfe[reg_base],var); + } + + if(reg_base==TFE_ADDR_PP_PTR) { + /* cv: we store the full package pointer in tfe_packetpage_ptr variable. + this includes the mask area (0xf000) and the addr range (0x0fff). + we ensure that the bits 0x3000 are always set (as in real HW). + odd values of the pointer are valid and supported. + only register read and write have to be mapped to word boundary. */ + word_value |= 0x3000; + tfe_packetpage_ptr = word_value; +#ifdef TFE_DEBUG_STORE + log_message(tfe_log, "set PP Ptr to $%04X.", tfe_packetpage_ptr); +#endif + } else { + /* write a register */ + + /*! \TODO: Find a reasonable default */ + word16 ppaddress = TFE_PP_ADDR_PRODUCTID; + + /* now determine address of write in packet page */ + switch(reg_base) { + case TFE_ADDR_PP_DATA: + case TFE_ADDR_PP_DATA2: + /* mask and align ppaddress from page pointer */ + ppaddress = tfe_packetpage_ptr & (MAX_PACKETPAGE_ARRAY-1); + ppaddress &= ~1; + /* auto increment pp ptr */ + tfe_auto_incr_pp_ptr(); + break; + case TFE_ADDR_TXCMD: + ppaddress = TFE_PP_ADDR_TXCMD; + break; + case TFE_ADDR_TXLENGTH: + ppaddress = TFE_PP_ADDR_TXLENGTH; + break; + case TFE_ADDR_INTSTQUEUE: + ppaddress = TFE_PP_ADDR_SE_ISQ; + break; + case TFE_ADDR_PP_PTR: + break; + default: + /* invalid */ + assert(0); + break; } - /* combine stored value with new written byte */ - if((io_address & 1) == 0) { - /* overwrite low byte */ - word_value = LOHI_WORD(var,tfe[reg_base+1]); - } else { - /* overwrite high byte */ - word_value = LOHI_WORD(tfe[reg_base],var); - } - - if(reg_base==TFE_ADDR_PP_PTR) { - /* cv: we store the full package pointer in tfe_packetpage_ptr variable. - this includes the mask area (0xf000) and the addr range (0x0fff). - we ensure that the bits 0x3000 are always set (as in real HW). - odd values of the pointer are valid and supported. - only register read and write have to be mapped to word boundary. */ - word_value |= 0x3000; - tfe_packetpage_ptr = word_value; #ifdef TFE_DEBUG_STORE - log_message(tfe_log, "set PP Ptr to $%04X.", tfe_packetpage_ptr); -#endif - } else { - /* write a register */ - - /*! \TODO: Find a reasonable default */ - word16 ppaddress = TFE_PP_ADDR_PRODUCTID; - - /* now determine address of write in packet page */ - switch(reg_base) { - case TFE_ADDR_PP_DATA: - case TFE_ADDR_PP_DATA2: - /* mask and align ppaddress from page pointer */ - ppaddress = tfe_packetpage_ptr & (MAX_PACKETPAGE_ARRAY-1); - ppaddress &= ~1; - /* auto increment pp ptr */ - tfe_auto_incr_pp_ptr(); - break; - case TFE_ADDR_TXCMD: - ppaddress = TFE_PP_ADDR_TXCMD; - break; - case TFE_ADDR_TXLENGTH: - ppaddress = TFE_PP_ADDR_TXLENGTH; - break; - case TFE_ADDR_INTSTQUEUE: - ppaddress = TFE_PP_ADDR_SE_ISQ; - break; - case TFE_ADDR_PP_PTR: - break; - default: - /* invalid */ - assert(0); - break; - } - -#ifdef TFE_DEBUG_STORE - log_message(tfe_log, "before writing to PP Ptr: $%04X <= $%04X.", - ppaddress,word_value); + log_message(tfe_log, "before writing to PP Ptr: $%04X <= $%04X.", + ppaddress,word_value); #endif - /* perform the write */ - tfe_write_register(ppaddress,word_value); + /* perform the write */ + tfe_write_register(ppaddress,word_value); - /* handle sideeffects */ - tfe_sideeffects_write_pp(ppaddress, io_address&1); + /* handle sideeffects */ + tfe_sideeffects_write_pp(ppaddress, io_address&1); - /* update word value if it was changed in write register or by side effect */ - word_value = GET_PP_16(ppaddress); + /* update word value if it was changed in write register or by side effect */ + word_value = GET_PP_16(ppaddress); #ifdef TFE_DEBUG_STORE - log_message(tfe_log, "after writing to PP Ptr: $%04X <= $%04X.", - ppaddress,word_value); + log_message(tfe_log, "after writing to PP Ptr: $%04X <= $%04X.", + ppaddress,word_value); #endif - } + } - /* update tfe registers */ - tfe[reg_base] = LO_BYTE(word_value); - tfe[reg_base+1] = HI_BYTE(word_value); + /* update tfe registers */ + tfe[reg_base] = LO_BYTE(word_value); + tfe[reg_base+1] = HI_BYTE(word_value); } /* ------------------------------------------------------------------------- */ /* resources support functions */ #if 0 -static int set_tfe_disabled(int val, void *param) -{ - /* dummy function since we don't want "disabled" to be stored on disk */ - return 0; +static int set_tfe_disabled(int val, void *param) { + /* dummy function since we don't want "disabled" to be stored on disk */ + return 0; } /*static int set_tfe_rr_net(int val, void *param) -{ + { if (!tfe_cannot_use) { if (!val) { - // TFE should not be used as rr net + // TFE should not be used as rr net if (tfe_as_rr_net) { tfe_as_rr_net = 0; - // if adapter is already enabled then reset the LAN chip + // if adapter is already enabled then reset the LAN chip if (tfe) { tfe_reset(); } } return 0; - } else { + } else { if (!tfe_as_rr_net) { tfe_as_rr_net = 1; - + // if adapter is already enabled then reset the LAN chip if (tfe) { tfe_reset(); @@ -1820,72 +1796,70 @@ static int set_tfe_disabled(int val, void *param) } return 0; -}*/ + }*/ -static int set_tfe_enabled(int val, void *param) -{ - if (!tfe_cannot_use) { - if (!val) { - /* TFE should be deactived */ - if (tfe_enabled) { - tfe_enabled = 0; - if (tfe_deactivate() < 0) { - return -1; - } - } - return 0; - } else { - if (!tfe_enabled) { - tfe_enabled = 1; - if (tfe_activate() < 0) { - return -1; - } - } - - return 0; +static int set_tfe_enabled(int val, void *param) { + if (!tfe_cannot_use) { + if (!val) { + /* TFE should be deactived */ + if (tfe_enabled) { + tfe_enabled = 0; + if (tfe_deactivate() < 0) { + return -1; } + } + return 0; + } else { + if (!tfe_enabled) { + tfe_enabled = 1; + if (tfe_activate() < 0) { + return -1; + } + } + return 0; } - return 0; + + } + return 0; } #endif /* 0 */ -int set_tfe_interface(const char *name) -{ - if (tfe_interface != NULL && name != NULL - && strcmp(name, tfe_interface) == 0) - return 0; - - util_string_set(&tfe_interface, name); - - if (tfe_enabled) { - /* ethernet is enabled, make sure that the new name is - taken account of - */ - if (tfe_deactivate() < 0) { - return -1; - } - if (tfe_activate() < 0) { - return -1; - } - - /* virtually reset the LAN chip */ - if (tfe) { - tfe_reset(); - } - } +int set_tfe_interface(const char *name) { + if (tfe_interface != NULL && name != NULL + && strcmp(name, tfe_interface) == 0) return 0; + + util_string_set(&tfe_interface, name); + + if (tfe_enabled) { + /* ethernet is enabled, make sure that the new name is + taken account of + */ + if (tfe_deactivate() < 0) { + return -1; + } + if (tfe_activate() < 0) { + return -1; + } + + /* virtually reset the LAN chip */ + if (tfe) { + tfe_reset(); + } + } + return 0; } /*static const resource_string_t resources_string[] = { - { "ETHERNET_INTERFACE", + { "ETHERNET_INTERFACE", ARCHDEP_ETHERNET_DEFAULT_DEVICE, RES_EVENT_NO, NULL, &tfe_interface, set_tfe_interface, NULL }, { NULL } -}; + }; -static const resource_int_t resources_int[] = { + static const resource_int_t resources_int[] = { { "ETHERNET_DISABLED", 0, RES_EVENT_NO, NULL, &tfe_cannot_use, set_tfe_disabled, NULL }, { "ETHERNET_ACTIVE", 0, RES_EVENT_STRICT, (resource_value_t)0, @@ -1893,21 +1867,21 @@ static const resource_int_t resources_int[] = { { "ETHERNET_AS_RR", 0, RES_EVENT_NO, NULL, &tfe_as_rr_net, set_tfe_rr_net, NULL }, { NULL } -};*/ + };*/ /*int tfe_resources_init(void) -{ + { if (resources_register_string(resources_string) < 0) return -1; return resources_register_int(resources_int); -}*/ + }*/ /* ------------------------------------------------------------------------- */ /* commandline support functions */ /*static const cmdline_option_t cmdline_options[] = -{ + { { "-tfe", SET_RESOURCE, 0, NULL, NULL, "ETHERNET_ACTIVE", (resource_value_t)1, USE_PARAM_STRING, USE_DESCRIPTION_ID, @@ -1934,12 +1908,12 @@ static const resource_int_t resources_int[] = { IDCLS_UNUSED, IDCLS_DISABLE_TFE_AS_RRNET, NULL, NULL }, { NULL } -}; + }; -int tfe_cmdline_options_init(void) -{ + int tfe_cmdline_options_init(void) + { return cmdline_register_options(cmdline_options); -}*/ + }*/ /* ------------------------------------------------------------------------- */ @@ -1951,16 +1925,14 @@ static char snap_module_name[] = "TFE1764"; #define SNAP_MAJOR 0 #define SNAP_MINOR 0 -int tfe_read_snapshot_module(struct snapshot_s *s) -{ - /* @SRT TODO: not yet implemented */ - return -1; +int tfe_read_snapshot_module(struct snapshot_s *s) { + /* @SRT TODO: not yet implemented */ + return -1; } -int tfe_write_snapshot_module(struct snapshot_s *s) -{ - /* @SRT TODO: not yet implemented */ - return -1; +int tfe_write_snapshot_module(struct snapshot_s *s) { + /* @SRT TODO: not yet implemented */ + return -1; } #endif /* #if 0 */ @@ -1968,23 +1940,20 @@ int tfe_write_snapshot_module(struct snapshot_s *s) /* ------------------------------------------------------------------------- */ /* functions for selecting and querying available NICs */ -int tfe_enumadapter_open(void) -{ - if (!tfe_arch_enumadapter_open()) { - tfe_cannot_use = 1; - return 0; - } - return 1; +int tfe_enumadapter_open(void) { + if (!tfe_arch_enumadapter_open()) { + tfe_cannot_use = 1; + return 0; + } + return 1; } -int tfe_enumadapter(char **ppname, char **ppdescription) -{ - return tfe_arch_enumadapter(ppname, ppdescription); +int tfe_enumadapter(char **ppname, char **ppdescription) { + return tfe_arch_enumadapter(ppname, ppdescription); } -int tfe_enumadapter_close(void) -{ - return tfe_arch_enumadapter_close(); +int tfe_enumadapter_close(void) { + return tfe_arch_enumadapter_close(); } //#endif /* #ifdef HAVE_TFE */ diff --git a/src/tfe/tfearch.c b/src/tfe/tfearch.c index 8a5dc8a..3149fbc 100644 --- a/src/tfe/tfearch.c +++ b/src/tfe/tfearch.c @@ -1,10 +1,10 @@ /* - * tfearch.c - TFE ("The final ethernet") emulation, + * tfearch.c - TFE ("The final ethernet") emulation, * architecture-dependant stuff * * Written by * Spiro Trikaliotis - * + * * This file is part of VICE, the Versatile Commodore Emulator. * See README for copyright notice. * @@ -24,7 +24,7 @@ * 02111-1307 USA. * */ - + #include #include @@ -33,7 +33,7 @@ #include #include "../atbridge/pcap_delay.h" -#include "tfesupp.h" +#include "tfesupp.h" #include "../defc.h" #include "protos_tfe.h" @@ -60,36 +60,35 @@ static char TfePcapErrbuf[PCAP_ERRBUF_SIZE]; #ifdef TFE_DEBUG_PKTDUMP static -void debug_output( const char *text, unsigned char *what, int count ) -{ - char buffer[256]; - char *p = buffer; - char *pbuffer1 = what; - int len1 = count; - int i; +void debug_output( const char *text, unsigned char *what, int count ) { + char buffer[256]; + char *p = buffer; + char *pbuffer1 = what; + int len1 = count; + int i; - sprintf(buffer, "\n%s: length = %u\n", text, len1); + sprintf(buffer, "\n%s: length = %u\n", text, len1); + OutputDebugString(buffer); + do { + p = buffer; + for (i=0; (i<8) && len1>0; len1--, i++) { + sprintf( p, "%02x ", (unsigned int)(unsigned char)*pbuffer1++); + p += 3; + } + *(p-1) = '\n'; *p = 0; OutputDebugString(buffer); - do { - p = buffer; - for (i=0; (i<8) && len1>0; len1--, i++) { - sprintf( p, "%02x ", (unsigned int)(unsigned char)*pbuffer1++); - p += 3; - } - *(p-1) = '\n'; *p = 0; - OutputDebugString(buffer); - } while (len1>0); + } while (len1>0); } #endif // #ifdef TFE_DEBUG_PKTDUMP /* - These functions let the UI enumerate the available interfaces. + These functions let the UI enumerate the available interfaces. - First, TfeEnumAdapterOpen() is used to start enumeration. + First, TfeEnumAdapterOpen() is used to start enumeration. - TfeEnumAdapter is then used to gather information for each adapter present - on the system, where: + TfeEnumAdapter is then used to gather information for each adapter present + on the system, where: ppname points to a pointer which will hold the name of the interface ppdescription points to a pointer which will hold the description of the interface @@ -97,123 +96,119 @@ void debug_output( const char *text, unsigned char *what, int count ) For each of these parameters, new memory is allocated, so it has to be freed with lib_free(). - TfeEnumAdapterClose() must be used to stop processing. + TfeEnumAdapterClose() must be used to stop processing. - Each function returns 1 on success, and 0 on failure. - TfeEnumAdapter() only fails if there is no more adpater; in this case, - *ppname and *ppdescription are not altered. -*/ -int tfe_arch_enumadapter_open(void) -{ - if (pcapdelay_findalldevs(&TfePcapAlldevs, TfePcapErrbuf) == -1) - { + Each function returns 1 on success, and 0 on failure. + TfeEnumAdapter() only fails if there is no more adpater; in this case, + * ppname and *ppdescription are not altered. + */ +int tfe_arch_enumadapter_open(void) { + if (pcapdelay_findalldevs(&TfePcapAlldevs, TfePcapErrbuf) == -1) + { #ifdef TFE_DEBUG_ARCH - log_message(tfe_arch_log, "ERROR in TfeEnumAdapterOpen: pcap_findalldevs: '%s'", TfePcapErrbuf); + log_message(tfe_arch_log, "ERROR in TfeEnumAdapterOpen: pcap_findalldevs: '%s'", TfePcapErrbuf); #endif - return 0; - } + return 0; + } - if (!TfePcapAlldevs) { + if (!TfePcapAlldevs) { #ifdef TFE_DEBUG_ARCH - log_message(tfe_arch_log, "ERROR in TfeEnumAdapterOpen, finding all pcap devices - " - "Do we have the necessary privilege rights?"); + log_message(tfe_arch_log, "ERROR in TfeEnumAdapterOpen, finding all pcap devices - " + "Do we have the necessary privilege rights?"); #endif - return 0; - } + return 0; + } - TfePcapNextDev = TfePcapAlldevs; + TfePcapNextDev = TfePcapAlldevs; - return 1; + return 1; } -int tfe_arch_enumadapter(char **ppname, char **ppdescription) -{ - if (!TfePcapNextDev || (TfePcapNextDev->name == NULL)) - return 0; +int tfe_arch_enumadapter(char **ppname, char **ppdescription) { + if (!TfePcapNextDev || (TfePcapNextDev->name == NULL)) + return 0; - *ppname = lib_stralloc(TfePcapNextDev->name); - if (TfePcapNextDev->description) - *ppdescription = lib_stralloc(TfePcapNextDev->description); - else - *ppdescription = lib_stralloc(TfePcapNextDev->name); - TfePcapNextDev = TfePcapNextDev->next; + *ppname = lib_stralloc(TfePcapNextDev->name); + if (TfePcapNextDev->description) + *ppdescription = lib_stralloc(TfePcapNextDev->description); + else + *ppdescription = lib_stralloc(TfePcapNextDev->name); + TfePcapNextDev = TfePcapNextDev->next; - return 1; + return 1; } -int tfe_arch_enumadapter_close(void) -{ - if (TfePcapAlldevs) { - pcapdelay_freealldevs(TfePcapAlldevs); - TfePcapAlldevs = NULL; - } - return 1; +int tfe_arch_enumadapter_close(void) { + if (TfePcapAlldevs) { + pcapdelay_freealldevs(TfePcapAlldevs); + TfePcapAlldevs = NULL; + } + return 1; } static -int TfePcapOpenAdapter(const char *interface_name) -{ - pcap_if_t *TfePcapDevice = NULL; +int TfePcapOpenAdapter(const char *interface_name) { + pcap_if_t *TfePcapDevice = NULL; - if (!tfe_enumadapter_open()) { - return FALSE; - } - else { - /* look if we can find the specified adapter */ - char *pname; - char *pdescription; - int found = FALSE; + if (!tfe_enumadapter_open()) { + return FALSE; + } + else { + /* look if we can find the specified adapter */ + char *pname; + char *pdescription; + int found = FALSE; - if (interface_name) { - /* we have an interface name, try it */ - TfePcapDevice = TfePcapAlldevs; + if (interface_name) { + /* we have an interface name, try it */ + TfePcapDevice = TfePcapAlldevs; - while (tfe_enumadapter(&pname, &pdescription)) { - if (strcmp(pname, interface_name)==0) { - found = TRUE; - } - lib_free(pname); - lib_free(pdescription); - if (found) break; - TfePcapDevice = TfePcapNextDev; - } - } - - if (!found) { - /* just take the first adapter */ - TfePcapDevice = TfePcapAlldevs; + while (tfe_enumadapter(&pname, &pdescription)) { + if (strcmp(pname, interface_name)==0) { + found = TRUE; } + lib_free(pname); + lib_free(pdescription); + if (found) break; + TfePcapDevice = TfePcapNextDev; + } } - TfePcapFP = pcapdelay_open_live(TfePcapDevice->name, 1700, 1, 20, TfePcapErrbuf); - if ( TfePcapFP == NULL) - { -#ifdef TFE_DEBUG_ARCH - log_message(tfe_arch_log, "ERROR opening adapter: '%s'", TfePcapErrbuf); -#endif - tfe_enumadapter_close(); - return FALSE; + if (!found) { + /* just take the first adapter */ + TfePcapDevice = TfePcapAlldevs; } + } - if (pcapdelay_setnonblock(TfePcapFP, 1, TfePcapErrbuf)<0) - { + TfePcapFP = pcapdelay_open_live(TfePcapDevice->name, 1700, 1, 20, TfePcapErrbuf); + if ( TfePcapFP == NULL) + { #ifdef TFE_DEBUG_ARCH - log_message(tfe_arch_log, "WARNING: Setting PCAP to non-blocking failed: '%s'", TfePcapErrbuf); + log_message(tfe_arch_log, "ERROR opening adapter: '%s'", TfePcapErrbuf); #endif - } - - /* Check the link layer. We support only Ethernet for simplicity. */ - if(pcapdelay_datalink(TfePcapFP) != DLT_EN10MB) - { -#ifdef TFE_DEBUG_ARCH - log_message(tfe_arch_log, "ERROR: TFE works only on Ethernet networks."); -#endif - tfe_enumadapter_close(); - return FALSE; - } - tfe_enumadapter_close(); - return TRUE; + return FALSE; + } + + if (pcapdelay_setnonblock(TfePcapFP, 1, TfePcapErrbuf)<0) + { +#ifdef TFE_DEBUG_ARCH + log_message(tfe_arch_log, "WARNING: Setting PCAP to non-blocking failed: '%s'", TfePcapErrbuf); +#endif + } + + /* Check the link layer. We support only Ethernet for simplicity. */ + if(pcapdelay_datalink(TfePcapFP) != DLT_EN10MB) + { +#ifdef TFE_DEBUG_ARCH + log_message(tfe_arch_log, "ERROR: TFE works only on Ethernet networks."); +#endif + tfe_enumadapter_close(); + return FALSE; + } + + tfe_enumadapter_close(); + return TRUE; } @@ -221,50 +216,44 @@ int TfePcapOpenAdapter(const char *interface_name) /* the architecture-dependend functions */ -int tfe_arch_init(void) -{ - //tfe_arch_log = log_open("TFEARCH"); +int tfe_arch_init(void) { + //tfe_arch_log = log_open("TFEARCH"); - return 1; + return 1; } -void tfe_arch_pre_reset( void ) -{ +void tfe_arch_pre_reset( void ) { #ifdef TFE_DEBUG_ARCH - log_message( tfe_arch_log, "tfe_arch_pre_reset()." ); + log_message( tfe_arch_log, "tfe_arch_pre_reset()." ); #endif } -void tfe_arch_post_reset( void ) -{ +void tfe_arch_post_reset( void ) { #ifdef TFE_DEBUG_ARCH - log_message( tfe_arch_log, "tfe_arch_post_reset()." ); + log_message( tfe_arch_log, "tfe_arch_post_reset()." ); #endif } -int tfe_arch_activate(const char *interface_name) -{ +int tfe_arch_activate(const char *interface_name) { #ifdef TFE_DEBUG_ARCH - log_message( tfe_arch_log, "tfe_arch_activate()." ); + log_message( tfe_arch_log, "tfe_arch_activate()." ); #endif - if (!TfePcapOpenAdapter(interface_name)) { - return 0; - } - return 1; + if (!TfePcapOpenAdapter(interface_name)) { + return 0; + } + return 1; } -void tfe_arch_deactivate( void ) -{ +void tfe_arch_deactivate( void ) { #ifdef TFE_DEBUG_ARCH - log_message( tfe_arch_log, "tfe_arch_deactivate()." ); + log_message( tfe_arch_log, "tfe_arch_deactivate()." ); #endif } -void tfe_arch_set_mac( const unsigned char mac[6] ) -{ +void tfe_arch_set_mac( const unsigned char mac[6] ) { #if defined(TFE_DEBUG_ARCH) || defined(TFE_DEBUG_FRAMES) - log_message( tfe_arch_log, "New MAC address set: %02X:%02X:%02X:%02X:%02X:%02X.", - mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); + log_message( tfe_arch_log, "New MAC address set: %02X:%02X:%02X:%02X:%02X:%02X.", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); #endif } @@ -275,49 +264,46 @@ void tfe_arch_recv_ctl( int bBroadcast, /* broadcast */ int bCorrect, /* accept correct frames */ int bPromiscuous, /* promiscuous mode */ int bIAHash /* accept if IA passes the hash filter */ - ) -{ + ) { #if defined(TFE_DEBUG_ARCH) || defined(TFE_DEBUG_FRAMES) - log_message( tfe_arch_log, "tfe_arch_recv_ctl() called with the following parameters:" ); - log_message( tfe_arch_log, "\tbBroadcast = %s", bBroadcast ? "TRUE" : "FALSE" ); - log_message( tfe_arch_log, "\tbIA = %s", bIA ? "TRUE" : "FALSE" ); - log_message( tfe_arch_log, "\tbMulticast = %s", bMulticast ? "TRUE" : "FALSE" ); - log_message( tfe_arch_log, "\tbCorrect = %s", bCorrect ? "TRUE" : "FALSE" ); - log_message( tfe_arch_log, "\tbPromiscuous = %s", bPromiscuous ? "TRUE" : "FALSE" ); - log_message( tfe_arch_log, "\tbIAHash = %s", bIAHash ? "TRUE" : "FALSE" ); + log_message( tfe_arch_log, "tfe_arch_recv_ctl() called with the following parameters:" ); + log_message( tfe_arch_log, "\tbBroadcast = %s", bBroadcast ? "TRUE" : "FALSE" ); + log_message( tfe_arch_log, "\tbIA = %s", bIA ? "TRUE" : "FALSE" ); + log_message( tfe_arch_log, "\tbMulticast = %s", bMulticast ? "TRUE" : "FALSE" ); + log_message( tfe_arch_log, "\tbCorrect = %s", bCorrect ? "TRUE" : "FALSE" ); + log_message( tfe_arch_log, "\tbPromiscuous = %s", bPromiscuous ? "TRUE" : "FALSE" ); + log_message( tfe_arch_log, "\tbIAHash = %s", bIAHash ? "TRUE" : "FALSE" ); #endif } -void tfe_arch_line_ctl(int bEnableTransmitter, int bEnableReceiver ) -{ +void tfe_arch_line_ctl(int bEnableTransmitter, int bEnableReceiver ) { #if defined(TFE_DEBUG_ARCH) || defined(TFE_DEBUG_FRAMES) - log_message( tfe_arch_log, "tfe_arch_line_ctl() called with the following parameters:" ); - log_message( tfe_arch_log, "\tbEnableTransmitter = %s", bEnableTransmitter ? "TRUE" : "FALSE" ); - log_message( tfe_arch_log, "\tbEnableReceiver = %s", bEnableReceiver ? "TRUE" : "FALSE" ); + log_message( tfe_arch_log, "tfe_arch_line_ctl() called with the following parameters:" ); + log_message( tfe_arch_log, "\tbEnableTransmitter = %s", bEnableTransmitter ? "TRUE" : "FALSE" ); + log_message( tfe_arch_log, "\tbEnableReceiver = %s", bEnableReceiver ? "TRUE" : "FALSE" ); #endif } typedef struct TFE_PCAP_INTERNAL_tag { - unsigned int len; - unsigned char *buffer; + unsigned int len; + unsigned char *buffer; } TFE_PCAP_INTERNAL; /* Callback function invoked by libpcap for every incoming packet */ static -void TfePcapPacketHandler(unsigned char *param, const struct pcap_pkthdr *header, const unsigned char *pkt_data) -{ - TFE_PCAP_INTERNAL *pinternal = (TFE_PCAP_INTERNAL*)param; +void TfePcapPacketHandler(unsigned char *param, const struct pcap_pkthdr *header, const unsigned char *pkt_data) { + TFE_PCAP_INTERNAL *pinternal = (TFE_PCAP_INTERNAL*)param; - /* determine the count of bytes which has been returned, - * but make sure not to overrun the buffer - */ - if (header->caplen < pinternal->len) - pinternal->len = header->caplen; + /* determine the count of bytes which has been returned, + * but make sure not to overrun the buffer + */ + if (header->caplen < pinternal->len) + pinternal->len = header->caplen; - memcpy(pinternal->buffer, pkt_data, pinternal->len); + memcpy(pinternal->buffer, pkt_data, pinternal->len); } /* the following function receives a frame. @@ -325,27 +311,26 @@ void TfePcapPacketHandler(unsigned char *param, const struct pcap_pkthdr *header If there's none, it returns a -1. If there is one, it returns the length of the frame in bytes. - It copies the frame to *buffer and returns the number of copied + It copies the frame to *buffer and returns the number of copied bytes as return value. At most 'len' bytes are copied. -*/ -static -int tfe_arch_receive_frame(TFE_PCAP_INTERNAL *pinternal) -{ - int ret = -1; + */ +static +int tfe_arch_receive_frame(TFE_PCAP_INTERNAL *pinternal) { + int ret = -1; - /* check if there is something to receive */ - if (pcapdelay_dispatch(TfePcapFP, 1, (pcap_handler)TfePcapPacketHandler, (unsigned char*)pinternal)!=0) { - /* Something has been received */ - ret = pinternal->len; - } + /* check if there is something to receive */ + if (pcapdelay_dispatch(TfePcapFP, 1, (pcap_handler)TfePcapPacketHandler, (unsigned char*)pinternal)!=0) { + /* Something has been received */ + ret = pinternal->len; + } #ifdef TFE_DEBUG_ARCH - log_message( tfe_arch_log, "tfe_arch_receive_frame() called, returns %d.", ret ); + log_message( tfe_arch_log, "tfe_arch_receive_frame() called, returns %d.", ret ); #endif - return ret; + return ret; } void tfe_arch_transmit(int force, /* FORCE: Delete waiting frames in transmit buffer */ @@ -354,105 +339,103 @@ void tfe_arch_transmit(int force, /* FORCE: Delete waiting frames in trans int tx_pad_dis, /* TXPADDIS: Disable padding to 60 Bytes */ int txlength, /* Frame length */ unsigned char *txframe /* Pointer to the frame to be transmitted */ - ) -{ + ) { #ifdef TFE_DEBUG_ARCH - log_message( tfe_arch_log, "tfe_arch_transmit() called, with: " - "force = %s, onecoll = %s, inhibit_crc=%s, tx_pad_dis=%s, txlength=%u", - force ? "TRUE" : "FALSE", - onecoll ? "TRUE" : "FALSE", - inhibit_crc ? "TRUE" : "FALSE", - tx_pad_dis ? "TRUE" : "FALSE", - txlength - ); + log_message( tfe_arch_log, "tfe_arch_transmit() called, with: " + "force = %s, onecoll = %s, inhibit_crc=%s, tx_pad_dis=%s, txlength=%u", + force ? "TRUE" : "FALSE", + onecoll ? "TRUE" : "FALSE", + inhibit_crc ? "TRUE" : "FALSE", + tx_pad_dis ? "TRUE" : "FALSE", + txlength + ); #endif #ifdef TFE_DEBUG_PKTDUMP - debug_output( "Transmit frame: ", txframe, txlength); + debug_output( "Transmit frame: ", txframe, txlength); #endif // #ifdef TFE_DEBUG_PKTDUMP - if (pcapdelay_sendpacket(TfePcapFP, txframe, txlength) == -1) { - //log_message(tfe_arch_log, "WARNING! Could not send packet!"); - } + if (pcapdelay_sendpacket(TfePcapFP, txframe, txlength) == -1) { + //log_message(tfe_arch_log, "WARNING! Could not send packet!"); + } } /* - tfe_arch_receive() + tfe_arch_receive() - This function checks if there was a frame received. - If so, it returns 1, else 0. + This function checks if there was a frame received. + If so, it returns 1, else 0. - If there was no frame, none of the parameters is changed! + If there was no frame, none of the parameters is changed! - If there was a frame, the following actions are done: + If there was a frame, the following actions are done: - - at maximum *plen byte are transferred into the buffer given by pbuffer - - *plen gets the length of the received frame, EVEN if this is more + - at maximum *plen byte are transferred into the buffer given by pbuffer + - *plen gets the length of the received frame, EVEN if this is more than has been copied to pbuffer! - - if the dest. address was accepted by the hash filter, *phashed is set, else + - if the dest. address was accepted by the hash filter, *phashed is set, else cleared. - - if the dest. address was accepted by the hash filter, *phash_index is + - if the dest. address was accepted by the hash filter, *phash_index is set to the number of the rule leading to the acceptance - - if the receive was ok (good CRC and valid length), *prx_ok is set, + - if the receive was ok (good CRC and valid length), *prx_ok is set, else cleared. - - if the dest. address was accepted because it's exactly our MAC address + - if the dest. address was accepted because it's exactly our MAC address (set by tfe_arch_set_mac()), *pcorrect_mac is set, else cleared. - - if the dest. address was accepted since it was a broadcast address, - *pbroadcast is set, else cleared. - - if the received frame had a crc error, *pcrc_error is set, else cleared -*/ -int tfe_arch_receive(unsigned char *pbuffer , /* where to store a frame */ - int *plen, /* IN: maximum length of frame to copy; - OUT: length of received frame + - if the dest. address was accepted since it was a broadcast address, + * pbroadcast is set, else cleared. + - if the received frame had a crc error, *pcrc_error is set, else cleared + */ +int tfe_arch_receive(unsigned char *pbuffer, /* where to store a frame */ + int *plen, /* IN: maximum length of frame to copy; + OUT: length of received frame OUT can be bigger than IN if received frame was longer than supplied buffer */ int *phashed, /* set if the dest. address is accepted by the hash filter */ - int *phash_index, /* hash table index if hashed == TRUE */ + int *phash_index, /* hash table index if hashed == TRUE */ int *prx_ok, /* set if good CRC and valid length */ int *pcorrect_mac, /* set if dest. address is exactly our IA */ int *pbroadcast, /* set if dest. address is a broadcast address */ int *pcrc_error /* set if received frame had a CRC error */ - ) -{ - int len; + ) { + int len; - TFE_PCAP_INTERNAL internal = { *plen, pbuffer }; + TFE_PCAP_INTERNAL internal = { *plen, pbuffer }; #ifdef TFE_DEBUG_ARCH - log_message( tfe_arch_log, "tfe_arch_receive() called, with *plen=%u.", *plen ); + log_message( tfe_arch_log, "tfe_arch_receive() called, with *plen=%u.", *plen ); #endif - assert((*plen&1)==0); + assert((*plen&1)==0); - len = tfe_arch_receive_frame(&internal); + len = tfe_arch_receive_frame(&internal); - if (len!=-1) { + if (len!=-1) { #ifdef TFE_DEBUG_PKTDUMP - debug_output( "Received frame: ", internal.buffer, internal.len ); + debug_output( "Received frame: ", internal.buffer, internal.len ); #endif // #ifdef TFE_DEBUG_PKTDUMP - if (len&1) - ++len; + if (len&1) + ++len; - *plen = len; + *plen = len; - /* we don't decide if this frame fits the needs; - * by setting all zero, we let tfe.c do the work - * for us - */ - *phashed = - *phash_index = - *pbroadcast = - *pcorrect_mac = - *pcrc_error = 0; + /* we don't decide if this frame fits the needs; + * by setting all zero, we let tfe.c do the work + * for us + */ + *phashed = + *phash_index = + *pbroadcast = + *pcorrect_mac = + *pcrc_error = 0; - /* this frame has been received correctly */ - *prx_ok = 1; + /* this frame has been received correctly */ + *prx_ok = 1; - return 1; - } + return 1; + } - return 0; + return 0; } diff --git a/src/tfe/tfesupp.c b/src/tfe/tfesupp.c index 6f54587..9d647da 100644 --- a/src/tfe/tfesupp.c +++ b/src/tfe/tfesupp.c @@ -61,123 +61,117 @@ static unsigned long crc32_table[256]; static int crc32_is_initialized = 0; -void lib_free(void *ptr) -{ +void lib_free(void *ptr) { #ifdef LIB_DEBUG - lib_debug_free(ptr, 1, 1); + lib_debug_free(ptr, 1, 1); #endif #ifdef LIB_DEBUG - lib_debug_libc_free(ptr); + lib_debug_libc_free(ptr); #else - free(ptr); + free(ptr); #endif } -void *lib_malloc(size_t size) -{ +void *lib_malloc(size_t size) { #ifdef LIB_DEBUG - void *ptr = lib_debug_libc_malloc(size); + void *ptr = lib_debug_libc_malloc(size); #else - void *ptr = malloc(size); + void *ptr = malloc(size); #endif - if (ptr == NULL && size > 0) - exit(-1); + if (ptr == NULL && size > 0) + exit(-1); #ifdef LIB_DEBUG - lib_debug_alloc(ptr, size, 3); + lib_debug_alloc(ptr, size, 3); #endif - return ptr; + return ptr; } /*-----------------------------------------------------------------------*/ /* Malloc enough space for `str', copy `str' into it and return its address. */ -char *lib_stralloc(const char *str) -{ - size_t size; - char *ptr; +char *lib_stralloc(const char *str) { + size_t size; + char *ptr; - if (str == NULL) - exit(-1); + if (str == NULL) + exit(-1); - size = strlen(str) + 1; - ptr = (char *)lib_malloc(size); + size = strlen(str) + 1; + ptr = (char *)lib_malloc(size); - memcpy(ptr, str, size); - return ptr; + memcpy(ptr, str, size); + return ptr; } /* Like realloc, but abort if not enough memory is available. */ -void *lib_realloc(void *ptr, size_t size) -{ +void *lib_realloc(void *ptr, size_t size) { #ifdef LIB_DEBUG - void *new_ptr = lib_debug_libc_realloc(ptr, size); + void *new_ptr = lib_debug_libc_realloc(ptr, size); #else - void *new_ptr = realloc(ptr, size); + void *new_ptr = realloc(ptr, size); #endif - if (new_ptr == NULL) - exit(-1); + if (new_ptr == NULL) + exit(-1); #ifdef LIB_DEBUG - lib_debug_free(ptr, 1, 0); - lib_debug_alloc(new_ptr, size, 1); + lib_debug_free(ptr, 1, 0); + lib_debug_alloc(new_ptr, size, 1); #endif - return new_ptr; + return new_ptr; } // Util Stuff /* Set a new value to the dynamically allocated string *str. Returns `-1' if nothing has to be done. */ -int util_string_set(char **str, const char *new_value) -{ - if (*str == NULL) { - if (new_value != NULL) - *str = lib_stralloc(new_value); +int util_string_set(char **str, const char *new_value) { + if (*str == NULL) { + if (new_value != NULL) + *str = lib_stralloc(new_value); + } else { + if (new_value == NULL) { + lib_free(*str); + *str = NULL; } else { - if (new_value == NULL) { - lib_free(*str); - *str = NULL; - } else { - /* Skip copy if src and dest are already the same. */ - if (strcmp(*str, new_value) == 0) - return -1; + /* Skip copy if src and dest are already the same. */ + if (strcmp(*str, new_value) == 0) + return -1; - *str = (char *)lib_realloc(*str, strlen(new_value) + 1); - strcpy(*str, new_value); - } + *str = (char *)lib_realloc(*str, strlen(new_value) + 1); + strcpy(*str, new_value); } - return 0; + } + return 0; } // crc32 Stuff -unsigned long crc32_buf(const char *buffer, unsigned int len) -{ - int i, j; - unsigned long crc, c; - const char *p; +unsigned long crc32_buf(const char *buffer, unsigned int len) { + int i, j; + unsigned long crc, c; + const char *p; - if (!crc32_is_initialized) { - for (i = 0; i < 256; i++) { - c = (unsigned long) i; - for (j = 0; j < 8; j++) - c = c & 1 ? CRC32_POLY ^ (c >> 1) : c >> 1; - crc32_table[i] = c; - } - crc32_is_initialized = 1; + if (!crc32_is_initialized) { + for (i = 0; i < 256; i++) { + c = (unsigned long) i; + for (j = 0; j < 8; j++) + c = c & 1 ? CRC32_POLY ^ (c >> 1) : c >> 1; + crc32_table[i] = c; } + crc32_is_initialized = 1; + } - crc = 0xffffffff; - for (p = buffer; len > 0; ++p, --len) - crc = (crc >> 8) ^ crc32_table[(crc ^ *p) & 0xff]; + crc = 0xffffffff; + for (p = buffer; len > 0; ++p, --len) + crc = (crc >> 8) ^ crc32_table[(crc ^ *p) & 0xff]; - return ~crc; + return ~crc; } diff --git a/src/to_pro.c b/src/to_pro.c index 74a2b67..c74f70b 100755 --- a/src/to_pro.c +++ b/src/to_pro.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include @@ -16,8 +16,8 @@ #include #endif -#define DEF_DISK_SIZE (800*1024) -#define MAX_FILE_NAMES 51 +#define DEF_DISK_SIZE (800*1024) +#define MAX_FILE_NAMES 51 char out_name[] = "POOF1"; @@ -27,945 +27,890 @@ int g_crlf = 0; void make_legal_prodos_name(char *new_name, char *old_name); -static int hex(byte c) -{ - if (c >= '0' && c <= '9') return c - '0'; - if (c >= 'a' && c <= 'f') return c + 10 - 'a'; - if (c >= 'A' && c <= 'F') return c + 10 - 'A'; - return 0; +static int hex(byte c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c + 10 - 'a'; + if (c >= 'A' && c <= 'F') return c + 10 - 'A'; + return 0; } void get_file_type(const char *name, int *file_type, int *aux_type) { #if defined(__APPLE__) - unsigned char buffer[32]; - ssize_t size = getxattr(name, XATTR_FINDERINFO_NAME, buffer, 32, 0, 0); - if (size >= 16) { + unsigned char buffer[32]; + ssize_t size = getxattr(name, XATTR_FINDERINFO_NAME, buffer, 32, 0, 0); + if (size >= 16) { - if (!memcmp("pdos", buffer + 4, 4)) - { - if (buffer[0] == 'p') - { - *file_type = buffer[1]; - *aux_type = (buffer[2] << 8) | buffer[3]; - return; - } - if (!memcmp("PSYS", buffer, 4)) - { - *file_type = 0xff; - *aux_type = 0x0000; - return; - } - if (!memcmp("PS16", buffer, 4)) - { - *file_type = 0xb3; - *aux_type = 0x0000; - return; - } + if (!memcmp("pdos", buffer + 4, 4)) + { + if (buffer[0] == 'p') + { + *file_type = buffer[1]; + *aux_type = (buffer[2] << 8) | buffer[3]; + return; + } + if (!memcmp("PSYS", buffer, 4)) + { + *file_type = 0xff; + *aux_type = 0x0000; + return; + } + if (!memcmp("PS16", buffer, 4)) + { + *file_type = 0xb3; + *aux_type = 0x0000; + return; + } - if (!isxdigit(buffer[0]) && isxdigit(buffer[1]) && buffer[2] == ' ' && buffer[3] == ' ') - { - *file_type = (hex(buffer[0]) << 8) | hex(buffer[1]); - *aux_type = 0; - return; - } - } - if (!memcmp("TEXT", buffer, 4)) - { - *file_type = 0x04; - *aux_type = 0x0000; - return; - } - } + if (!isxdigit(buffer[0]) && isxdigit(buffer[1]) && buffer[2] == ' ' && buffer[3] == ' ') + { + *file_type = (hex(buffer[0]) << 8) | hex(buffer[1]); + *aux_type = 0; + return; + } + } + if (!memcmp("TEXT", buffer, 4)) + { + *file_type = 0x04; + *aux_type = 0x0000; + return; + } + } #endif - char *cp = strrchr(name, '.'); - if (cp) { - if (strcasecmp(cp, ".SDK") == 0) { - *file_type = 0xe0; - *aux_type = 0x8002; - return; - } - if (!strcasecmp(cp, ".SHK")) { - *file_type = 0xe0; - *aux_type = 0x8002; - return; - } - } - *file_type = g_def_file_type; - *aux_type = g_def_aux_type; + char *cp = strrchr(name, '.'); + if (cp) { + if (strcasecmp(cp, ".SDK") == 0) { + *file_type = 0xe0; + *aux_type = 0x8002; + return; + } + if (!strcasecmp(cp, ".SHK")) { + *file_type = 0xe0; + *aux_type = 0x8002; + return; + } + } + *file_type = g_def_file_type; + *aux_type = g_def_aux_type; } -int -main(int argc, char **argv) -{ - char *files[MAX_FILE_NAMES]; - char *name; - struct stat stat_buf; - int in; - int in_size; - int ret; - ProDisk *disk; - char new_name[128]; - byte in_buf[1024]; - int disk_size; - int i; - int done; - int pos; - int size; - int num_files; - int file_type; - int aux_type; +int main(int argc, char **argv) { + char *files[MAX_FILE_NAMES]; + char *name; + struct stat stat_buf; + int in; + int in_size; + int ret; + ProDisk *disk; + char new_name[128]; + byte in_buf[1024]; + int disk_size; + int i; + int done; + int pos; + int size; + int num_files; + int file_type; + int aux_type; - disk_size = DEF_DISK_SIZE; - if(argc < 2) { - fprintf(stderr, "%s: {-[size in K]} {unix_files}\n", - argv[0]); - exit(1); - } + disk_size = DEF_DISK_SIZE; + if(argc < 2) { + fprintf(stderr, "%s: {-[size in K]} {unix_files}\n", + argv[0]); + exit(1); + } - num_files = 0; - for(i = 1; i < argc; i++) { - if(argv[i][0] == '-') { + num_files = 0; + for(i = 1; i < argc; i++) { + if(argv[i][0] == '-') { - if(!strcmp(argv[i], "-cr")) { - g_crlf = 1; - } else if(!strcmp(argv[i], "-txt")) { - g_def_file_type = 4; - g_def_aux_type = 0; - } else if(!strcmp(argv[i], "-bin")) { - g_def_file_type = 6; - g_def_aux_type = 0; - } else { - /* I smell a -size_in_K */ - char *end = NULL; - disk_size = strtoul(&(argv[i][1]), &end, 10) * 1024; - if (*end) { - printf("Invalid size: %s\n", argv[i]+1); - exit(1); - } - printf("disk_size: %d, 0x%x\n", disk_size, disk_size); - if(disk_size < 40*1024) { - printf("Too small!\n"); - exit(1); - } - if(disk_size > 32 * 1024 * 1024) { - printf("Too big!\n"); - exit(1); - } - } - } else { - files[num_files] = argv[i]; - num_files++; - if(num_files >= MAX_FILE_NAMES) { - printf("Too many filenames: %d\n", num_files); - exit(2); - } - } - } + if(!strcmp(argv[i], "-cr")) { + g_crlf = 1; + } else if(!strcmp(argv[i], "-txt")) { + g_def_file_type = 4; + g_def_aux_type = 0; + } else if(!strcmp(argv[i], "-bin")) { + g_def_file_type = 6; + g_def_aux_type = 0; + } else { + /* I smell a -size_in_K */ + char *end = NULL; + disk_size = strtoul(&(argv[i][1]), &end, 10) * 1024; + if (*end) { + printf("Invalid size: %s\n", argv[i]+1); + exit(1); + } + printf("disk_size: %d, 0x%x\n", disk_size, disk_size); + if(disk_size < 40*1024) { + printf("Too small!\n"); + exit(1); + } + if(disk_size > 32 * 1024 * 1024) { + printf("Too big!\n"); + exit(1); + } + } + } else { + files[num_files] = argv[i]; + num_files++; + if(num_files >= MAX_FILE_NAMES) { + printf("Too many filenames: %d\n", num_files); + exit(2); + } + } + } - disk = allocate_memdisk(out_name, disk_size); - format_memdisk(disk, out_name); + disk = allocate_memdisk(out_name, disk_size); + format_memdisk(disk, out_name); - for(i = 0; i < num_files; i++) { - name = files[i]; - in = open(name, O_RDONLY | O_BINARY); - if(in < 0) { - fprintf(stderr, "opening %s returned %d, errno: %d\n", - name, in, errno); - exit(1); - } + for(i = 0; i < num_files; i++) { + name = files[i]; + in = open(name, O_RDONLY | O_BINARY); + if(in < 0) { + fprintf(stderr, "opening %s returned %d, errno: %d\n", + name, in, errno); + exit(1); + } - ret = fstat(in, &stat_buf); - if(ret != 0) { - fprintf(stderr, "fstat returned %d, errno: %d\n", - ret, errno); - } + ret = fstat(in, &stat_buf); + if(ret != 0) { + fprintf(stderr, "fstat returned %d, errno: %d\n", + ret, errno); + } - in_size = stat_buf.st_size; - printf("in size: %d\n", in_size); + in_size = stat_buf.st_size; + printf("in size: %d\n", in_size); - if(in_size > disk->disk_bytes_left) { - printf("File bigger than %d, too big!\n", disk_size); - exit(2); - } + if(in_size > disk->disk_bytes_left) { + printf("File bigger than %d, too big!\n", disk_size); + exit(2); + } - make_legal_prodos_name(new_name, name); + make_legal_prodos_name(new_name, name); - file_type = 0; - aux_type = 0; - get_file_type(name, &file_type, &aux_type); + file_type = 0; + aux_type = 0; + get_file_type(name, &file_type, &aux_type); - create_new_file(disk, 2, 1, new_name, file_type, - 0, 0, 0, 0xc3, aux_type, 0, in_size); + create_new_file(disk, 2, 1, new_name, file_type, + 0, 0, 0, 0xc3, aux_type, 0, in_size); - done = 0; - pos = 0; - while(pos < in_size) { - size = 512; - if(pos + size > in_size) { - size = in_size - pos; - } - ret = read(in, in_buf, size); - if(ret != size || ret <= 0) { - fprintf(stderr, "read returned %d, errno: %d\n", - ret, errno); - exit(2); - } - if (file_type == 4 && g_crlf) { - int i; - for(i = 0; i < size; ++i) { - if(in_buf[i] == '\n') in_buf[i] = '\r'; - } - } - ret = pro_write_file(disk, in_buf, pos, size); - if(ret != 0) { - printf("pro_write returned %d!\n", ret); - exit(3); - } - pos += size; - } + done = 0; + pos = 0; + while(pos < in_size) { + size = 512; + if(pos + size > in_size) { + size = in_size - pos; + } + ret = read(in, in_buf, size); + if(ret != size || ret <= 0) { + fprintf(stderr, "read returned %d, errno: %d\n", + ret, errno); + exit(2); + } + if (file_type == 4 && g_crlf) { + int i; + for(i = 0; i < size; ++i) { + if(in_buf[i] == '\n') in_buf[i] = '\r'; + } + } + ret = pro_write_file(disk, in_buf, pos, size); + if(ret != 0) { + printf("pro_write returned %d!\n", ret); + exit(3); + } + pos += size; + } - close_file(disk); + close_file(disk); - close(in); - } + close(in); + } - flush_disk(disk); - return 0; + flush_disk(disk); + return 0; } -void -make_legal_prodos_name(char *new_name, char *old_name) -{ - char *ptr; - int start_len, start_char; - int len; - int pos; - int c; - int j; +void make_legal_prodos_name(char *new_name, char *old_name) { + char *ptr; + int start_len, start_char; + int len; + int pos; + int c; + int j; - for(j = 0; j < 16; j++) { - /* make sure it ends with null == 15 + 1 */ - new_name[j] = 0; - } + for(j = 0; j < 16; j++) { + /* make sure it ends with null == 15 + 1 */ + new_name[j] = 0; + } - start_char = 0; - start_len = strlen(old_name); - len = 0; - ptr = &old_name[start_len - 1]; - for(j = start_len - 1; j >= 0; j--) { - if(*ptr == '/' || *ptr == ':') { - break; - } - ptr--; - len++; - } - ptr++; + start_char = 0; + start_len = strlen(old_name); + len = 0; + ptr = &old_name[start_len - 1]; + for(j = start_len - 1; j >= 0; j--) { + if(*ptr == '/' || *ptr == ':') { + break; + } + ptr--; + len++; + } + ptr++; - if(len <= 0) { - printf("Filename: %s has len: %d!\n", old_name, len); - exit(1); - } + if(len <= 0) { + printf("Filename: %s has len: %d!\n", old_name, len); + exit(1); + } - printf("mid_name: %s, len:%d\n", ptr, len); + printf("mid_name: %s, len:%d\n", ptr, len); - pos = 0; - for(j = 0; j < 15; j++) { - c = ptr[pos]; - if(isalnum(c)) { - c = toupper(c); - } else if(c != 0) { - c = '.'; - } + pos = 0; + for(j = 0; j < 15; j++) { + c = ptr[pos]; + if(isalnum(c)) { + c = toupper(c); + } else if(c != 0) { + c = '.'; + } - if(j == 0 && !isalpha(c)) { - c = 'A'; - } + if(j == 0 && !isalpha(c)) { + c = 'A'; + } - new_name[j] = c; + new_name[j] = c; - pos++; - if(pos == 7 && len > 15) { - pos = len - 8; - } - } + pos++; + if(pos == 7 && len > 15) { + pos = len - 8; + } + } - printf("new_name: %s\n", new_name); + printf("new_name: %s\n", new_name); } -void -flush_disk(ProDisk *disk) -{ - disk_write_data(disk, 6, disk->bitmap_ptr, - disk->size_bitmap_blocks * 512); - close(disk->fd); - disk->fd = -1; +void flush_disk(ProDisk *disk) { + disk_write_data(disk, 6, disk->bitmap_ptr, + disk->size_bitmap_blocks * 512); + close(disk->fd); + disk->fd = -1; } -void -close_file(ProDisk *disk) -{ - write_ind_block(disk); - write_master_ind_block(disk); - disk_write_data(disk, disk->dir_blk_num, &(disk->dir_blk_data[0]), 512); - disk->file_ptr = 0; +void close_file(ProDisk *disk) { + write_ind_block(disk); + write_master_ind_block(disk); + disk_write_data(disk, disk->dir_blk_num, &(disk->dir_blk_data[0]), 512); + disk->file_ptr = 0; } -ProDisk * -allocate_memdisk(char *out_name, int size) -{ - ProDisk *disk; - int out; +ProDisk *allocate_memdisk(char *out_name, int size) { + ProDisk *disk; + int out; - out = open(out_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0x1b6); - if(out < 0) { - fprintf(stderr, "opening %s returned %d, errno: %d\n", - out_name, out, errno); - exit(1); - } + out = open(out_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0x1b6); + if(out < 0) { + fprintf(stderr, "opening %s returned %d, errno: %d\n", + out_name, out, errno); + exit(1); + } - disk = (ProDisk *)malloc(sizeof(ProDisk)); - if(disk == 0) { - printf("allocate_memdisk failed, errno: %d\n", errno); - } + disk = (ProDisk *)malloc(sizeof(ProDisk)); + if(disk == 0) { + printf("allocate_memdisk failed, errno: %d\n", errno); + } - disk->fd = out; - disk->total_blocks = (size + 511) / 512; - disk->bitmap_ptr = 0; - disk->disk_bytes_left = 0; - disk->size_bitmap_blocks = 0; - disk->dir_blk_num = -1; - disk->ind_blk_num = -1; - disk->master_ind_blk_num = -1; + disk->fd = out; + disk->total_blocks = (size + 511) / 512; + disk->bitmap_ptr = 0; + disk->disk_bytes_left = 0; + disk->size_bitmap_blocks = 0; + disk->dir_blk_num = -1; + disk->ind_blk_num = -1; + disk->master_ind_blk_num = -1; - return disk; + return disk; } -void -format_memdisk(ProDisk *disk, char *name) -{ - byte zero_buf[1024]; - int total_blocks; - byte *bitmap_ptr; - Vol_hdr *vol_hdr; - Directory *dir; - int size_bitmap_bytes; - int size_bitmap_blocks; - int disk_blocks_left; - int i, j; +void format_memdisk(ProDisk *disk, char *name) { + byte zero_buf[1024]; + int total_blocks; + byte *bitmap_ptr; + Vol_hdr *vol_hdr; + Directory *dir; + int size_bitmap_bytes; + int size_bitmap_blocks; + int disk_blocks_left; + int i, j; - total_blocks = disk->total_blocks; + total_blocks = disk->total_blocks; - /* Zero out blocks 0 and 1 */ - for(i = 0; i < 2*512; i++) { - zero_buf[i] = 0; - } - disk_write_data(disk, 0x00000, zero_buf, 2*512); + /* Zero out blocks 0 and 1 */ + for(i = 0; i < 2*512; i++) { + zero_buf[i] = 0; + } + disk_write_data(disk, 0x00000, zero_buf, 2*512); - /* and make the image the right size */ - disk_write_data(disk, total_blocks - 1, zero_buf, 512); + /* and make the image the right size */ + disk_write_data(disk, total_blocks - 1, zero_buf, 512); - dir = disk_read_dir(disk, 2); - set_l2byte(&(dir->prev_blk), 0); - set_l2byte(&(dir->next_blk), 3); - vol_hdr = (Vol_hdr *)&(dir->file_entries[0]); - vol_hdr->storage_type_name_len = 0xf0 + strlen(name); - strncpy((char *)vol_hdr->vol_name, name, strlen(name)); - vol_hdr->version = 0; - vol_hdr->min_version = 0; - vol_hdr->access = 0xc3; - vol_hdr->entry_length = 0x27; - vol_hdr->entries_per_block = 0x0d; - set_l2byte(&(vol_hdr->file_count), 0); - vol_hdr->entries_per_block = 0x0d; - set_l2byte(&(vol_hdr->bit_map), 6); - set_l2byte(&(vol_hdr->total_blocks), total_blocks); - for(i = 1; i < 13; i++) { - set_file_entry(&(dir->file_entries[i]), 0, "", 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0); - } - disk_write_dir(disk, 2); + dir = disk_read_dir(disk, 2); + set_l2byte(&(dir->prev_blk), 0); + set_l2byte(&(dir->next_blk), 3); + vol_hdr = (Vol_hdr *)&(dir->file_entries[0]); + vol_hdr->storage_type_name_len = 0xf0 + strlen(name); + strncpy((char *)vol_hdr->vol_name, name, strlen(name)); + vol_hdr->version = 0; + vol_hdr->min_version = 0; + vol_hdr->access = 0xc3; + vol_hdr->entry_length = 0x27; + vol_hdr->entries_per_block = 0x0d; + set_l2byte(&(vol_hdr->file_count), 0); + vol_hdr->entries_per_block = 0x0d; + set_l2byte(&(vol_hdr->bit_map), 6); + set_l2byte(&(vol_hdr->total_blocks), total_blocks); + for(i = 1; i < 13; i++) { + set_file_entry(&(dir->file_entries[i]), 0, "", 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0); + } + disk_write_dir(disk, 2); - for(i = 3; i < 6; i++) { - dir = disk_read_dir(disk, i); - set_l2byte(&(dir->prev_blk), i - 1); - set_l2byte(&(dir->next_blk), i + 1); - if(i == 5) { - set_l2byte(&(dir->next_blk), 0); - } - for(j = 0; j < 13; j++) { - set_file_entry(&(dir->file_entries[j]), 0, "", 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0); - } + for(i = 3; i < 6; i++) { + dir = disk_read_dir(disk, i); + set_l2byte(&(dir->prev_blk), i - 1); + set_l2byte(&(dir->next_blk), i + 1); + if(i == 5) { + set_l2byte(&(dir->next_blk), 0); + } + for(j = 0; j < 13; j++) { + set_file_entry(&(dir->file_entries[j]), 0, "", 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0); + } - disk_write_dir(disk, i); - } + disk_write_dir(disk, i); + } - size_bitmap_bytes = (total_blocks + 7)/ 8; - size_bitmap_blocks = (size_bitmap_bytes + 511)/ 512; - bitmap_ptr = (byte *)malloc(size_bitmap_blocks * 512); - for(i = 0; i < 6+size_bitmap_blocks; i++) { - set_bitmap_used(bitmap_ptr, i); - } - for(i = 6+size_bitmap_blocks; i < total_blocks; i++) { - set_bitmap_free(bitmap_ptr, i); - } - for(i = total_blocks; i < size_bitmap_blocks*512*8; i++) { - set_bitmap_used(bitmap_ptr, i); - } + size_bitmap_bytes = (total_blocks + 7)/ 8; + size_bitmap_blocks = (size_bitmap_bytes + 511)/ 512; + bitmap_ptr = (byte *)malloc(size_bitmap_blocks * 512); + for(i = 0; i < 6+size_bitmap_blocks; i++) { + set_bitmap_used(bitmap_ptr, i); + } + for(i = 6+size_bitmap_blocks; i < total_blocks; i++) { + set_bitmap_free(bitmap_ptr, i); + } + for(i = total_blocks; i < size_bitmap_blocks*512*8; i++) { + set_bitmap_used(bitmap_ptr, i); + } - disk_write_data(disk, 6, bitmap_ptr, size_bitmap_blocks * 512); - disk->bitmap_ptr = bitmap_ptr; - disk->size_bitmap_blocks = size_bitmap_blocks; - disk->bitmap_bytes = size_bitmap_blocks * 512; - disk->bitmap_cur_pos = 0; + disk_write_data(disk, 6, bitmap_ptr, size_bitmap_blocks * 512); + disk->bitmap_ptr = bitmap_ptr; + disk->size_bitmap_blocks = size_bitmap_blocks; + disk->bitmap_bytes = size_bitmap_blocks * 512; + disk->bitmap_cur_pos = 0; - disk_blocks_left = total_blocks - 6 - size_bitmap_blocks; - disk->disk_bytes_left = disk_blocks_left * 512; + disk_blocks_left = total_blocks - 6 - size_bitmap_blocks; + disk->disk_bytes_left = disk_blocks_left * 512; } -void -disk_write_data(ProDisk *disk, int blk_num, byte *buf, int size) -{ - int size_in_blocks; - int ret; +void disk_write_data(ProDisk *disk, int blk_num, byte *buf, int size) { + int size_in_blocks; + int ret; #if 0 - printf("Writing blk %04x from buf: %08x, %03x\n", blk_num, buf, size); + printf("Writing blk %04x from buf: %08x, %03x\n", blk_num, buf, size); #endif - size_in_blocks = size >> 9; - if(size_in_blocks * 512 != size) { - printf("disk_write: blk: %04x, buf: %08x, size: %08x\n", - blk_num, (word32)buf, size); - exit(1); - } + size_in_blocks = size >> 9; + if(size_in_blocks * 512 != size) { + printf("disk_write: blk: %04x, buf: %08x, size: %08x\n", + blk_num, (word32)buf, size); + exit(1); + } - ret = lseek(disk->fd, 512*blk_num, SEEK_SET); - if(ret != 512*blk_num) { - printf("disk_write: seek: %d, errno: %d, blk: %04x, buf: " - "%08x, sz: %08x\n", ret, errno, blk_num, - (word32)buf, size); - exit(1); - } + ret = lseek(disk->fd, 512*blk_num, SEEK_SET); + if(ret != 512*blk_num) { + printf("disk_write: seek: %d, errno: %d, blk: %04x, buf: " + "%08x, sz: %08x\n", ret, errno, blk_num, + (word32)buf, size); + exit(1); + } - ret = write(disk->fd, buf, size); - if(ret != size) { - printf("disk_write: write: %d, errno: %d, blk: %04x, buf: " - "%08x, sz: %08x\n", ret, errno, blk_num, - (word32)buf, size); - exit(1); - } + ret = write(disk->fd, buf, size); + if(ret != size) { + printf("disk_write: write: %d, errno: %d, blk: %04x, buf: " + "%08x, sz: %08x\n", ret, errno, blk_num, + (word32)buf, size); + exit(1); + } } -void -disk_read_data(ProDisk *disk, int blk_num, byte *buf, int size) -{ - int size_in_blocks; - int ret; - int i; +void disk_read_data(ProDisk *disk, int blk_num, byte *buf, int size) { + int size_in_blocks; + int ret; + int i; - size_in_blocks = size >> 9; - if(size_in_blocks * 512 != size) { - printf("disk_read: blk: %04x, buf: %08x, size: %08x\n", - blk_num, (word32)buf, size); - exit(1); - } + size_in_blocks = size >> 9; + if(size_in_blocks * 512 != size) { + printf("disk_read: blk: %04x, buf: %08x, size: %08x\n", + blk_num, (word32)buf, size); + exit(1); + } - ret = lseek(disk->fd, 512*blk_num, SEEK_SET); - if(ret != 512*blk_num) { - printf("disk_read: seek: %d, errno: %d, blk: %04x, buf: " - "%08x, sz: %08x\n", ret, errno, blk_num, - (word32)buf, size); - exit(1); - } + ret = lseek(disk->fd, 512*blk_num, SEEK_SET); + if(ret != 512*blk_num) { + printf("disk_read: seek: %d, errno: %d, blk: %04x, buf: " + "%08x, sz: %08x\n", ret, errno, blk_num, + (word32)buf, size); + exit(1); + } - ret = read(disk->fd, buf, size); - if(ret != size) { - printf("disk_read: read: %d, errno: %d, blk: %04x, buf: " - "%08x, sz: %08x\n", ret, errno, blk_num, - (word32)buf, size); - for(i = 0; i < size; i++) { - buf[i] = 0; - } - } + ret = read(disk->fd, buf, size); + if(ret != size) { + printf("disk_read: read: %d, errno: %d, blk: %04x, buf: " + "%08x, sz: %08x\n", ret, errno, blk_num, + (word32)buf, size); + for(i = 0; i < size; i++) { + buf[i] = 0; + } + } } -Directory * -disk_read_dir(ProDisk *disk, int blk_num) -{ - disk_write_dir(disk, blk_num); +Directory *disk_read_dir(ProDisk *disk, int blk_num) { + disk_write_dir(disk, blk_num); - disk->dir_blk_num = blk_num; - disk_read_data(disk, blk_num, &(disk->dir_blk_data[0]), 512); + disk->dir_blk_num = blk_num; + disk_read_data(disk, blk_num, &(disk->dir_blk_data[0]), 512); - return (Directory *)&(disk->dir_blk_data[0]); + return (Directory *)&(disk->dir_blk_data[0]); } -void -disk_write_dir(ProDisk *disk, int blk_num) -{ - if(disk->dir_blk_num >= 0) { - if(disk->dir_blk_num != blk_num) { - printf("disk_write_dir: %04x != %04x\n", - disk->dir_blk_num, blk_num); - } - disk_write_data(disk, disk->dir_blk_num, - &(disk->dir_blk_data[0]), 512); - disk->dir_blk_num = -1; - } +void disk_write_dir(ProDisk *disk, int blk_num) { + if(disk->dir_blk_num >= 0) { + if(disk->dir_blk_num != blk_num) { + printf("disk_write_dir: %04x != %04x\n", + disk->dir_blk_num, blk_num); + } + disk_write_data(disk, disk->dir_blk_num, + &(disk->dir_blk_data[0]), 512); + disk->dir_blk_num = -1; + } } -void -create_new_file(ProDisk *disk, int dir_block, int storage_type, char *name, - int file_type, word32 creation_time, int version, int min_version, - int access, int aux_type, word32 last_mod, word32 eof) -{ - Vol_hdr *vol_ptr; - int val; - Directory *dir_ptr; - File_entry *file_ptr; - int file_count; - int pos; - int last_pos; - int done; - int next_blk; - int name_len; +void create_new_file(ProDisk *disk, int dir_block, int storage_type, char *name, + int file_type, word32 creation_time, int version, int min_version, + int access, int aux_type, word32 last_mod, word32 eof) { + Vol_hdr *vol_ptr; + int val; + Directory *dir_ptr; + File_entry *file_ptr; + int file_count; + int pos; + int last_pos; + int done; + int next_blk; + int name_len; - name_len = strlen(name); - dir_ptr = disk_read_dir(disk, dir_block); - next_blk = dir_block; - val = dir_ptr->file_entries[0].storage_type_name_len; - last_pos = 13; - pos = 0; - if(((val & 0xf0) == 0xf0) || ((val & 0xf0) == 0xe0)) { - /* vol dir or subdir header */ - vol_ptr = (Vol_hdr *)&(dir_ptr->file_entries[0]); - file_count = get_l2byte(&(vol_ptr->file_count)); - pos = 1; - last_pos = vol_ptr->entries_per_block; - } else { - printf("dir_block: %04x not subdir or voldir\n", dir_block); - exit(6); - } + name_len = strlen(name); + dir_ptr = disk_read_dir(disk, dir_block); + next_blk = dir_block; + val = dir_ptr->file_entries[0].storage_type_name_len; + last_pos = 13; + pos = 0; + if(((val & 0xf0) == 0xf0) || ((val & 0xf0) == 0xe0)) { + /* vol dir or subdir header */ + vol_ptr = (Vol_hdr *)&(dir_ptr->file_entries[0]); + file_count = get_l2byte(&(vol_ptr->file_count)); + pos = 1; + last_pos = vol_ptr->entries_per_block; + } else { + printf("dir_block: %04x not subdir or voldir\n", dir_block); + exit(6); + } - vol_ptr = 0; + vol_ptr = 0; - done = 0; - while(!done) { - file_ptr = &(dir_ptr->file_entries[pos]); - if(((file_ptr->storage_type_name_len) & 0xf0) == 0) { - /* Got it! */ - file_ptr->storage_type_name_len = - (storage_type << 4) | name_len; - strncpy((char *)file_ptr->file_name, name, 15); - file_ptr->file_type = file_type; - set_l2byte(&(file_ptr->key_pointer), - find_next_free_block(disk)); - set_l2byte(&(file_ptr->blocks_used), 0); - set_pro_time(&(file_ptr->creation_time), - creation_time); - file_ptr->version = version; - file_ptr->min_version = min_version; - file_ptr->access = access; - set_l2byte(&(file_ptr->aux_type), aux_type); - set_pro_time(&(file_ptr->last_mod), last_mod); - set_l2byte(&(file_ptr->header_pointer), - dir_block); - set_l3byte(&(file_ptr->eof), eof); - disk_write_dir(disk, next_blk); + done = 0; + while(!done) { + file_ptr = &(dir_ptr->file_entries[pos]); + if(((file_ptr->storage_type_name_len) & 0xf0) == 0) { + /* Got it! */ + file_ptr->storage_type_name_len = + (storage_type << 4) | name_len; + strncpy((char *)file_ptr->file_name, name, 15); + file_ptr->file_type = file_type; + set_l2byte(&(file_ptr->key_pointer), + find_next_free_block(disk)); + set_l2byte(&(file_ptr->blocks_used), 0); + set_pro_time(&(file_ptr->creation_time), + creation_time); + file_ptr->version = version; + file_ptr->min_version = min_version; + file_ptr->access = access; + set_l2byte(&(file_ptr->aux_type), aux_type); + set_pro_time(&(file_ptr->last_mod), last_mod); + set_l2byte(&(file_ptr->header_pointer), + dir_block); + set_l3byte(&(file_ptr->eof), eof); + disk_write_dir(disk, next_blk); - dir_ptr = disk_read_dir(disk, dir_block); - vol_ptr = (Vol_hdr *)&(dir_ptr->file_entries[0]); - set_l2byte(&(vol_ptr->file_count), file_count+1); - disk_write_dir(disk, dir_block); + dir_ptr = disk_read_dir(disk, dir_block); + vol_ptr = (Vol_hdr *)&(dir_ptr->file_entries[0]); + set_l2byte(&(vol_ptr->file_count), file_count+1); + disk_write_dir(disk, dir_block); - disk_read_dir(disk, next_blk); - /* re-read dir so that ptrs are set up right */ - disk->file_ptr = file_ptr; - disk->file_open = 1; - disk->ind_blk_num = -1; - disk->master_ind_blk_num = -1; - done = 1; - break; - } else { - /* check to make sure name is unique */ - if((file_ptr->storage_type_name_len & 0x0f)== name_len){ - if(!memcmp(file_ptr->file_name, name,name_len)){ - printf("Name %s already on disk!\n", - name); - exit(8); - } - } - pos++; - if(pos >= last_pos) { - /* Go to next block */ - next_blk = get_l2byte(&(dir_ptr->next_blk)); - if(next_blk) { - dir_ptr = disk_read_dir(disk, next_blk); - pos = 0; - } else { - printf("Top directory full!\n"); - exit(2); - } - } - } - } + disk_read_dir(disk, next_blk); + /* re-read dir so that ptrs are set up right */ + disk->file_ptr = file_ptr; + disk->file_open = 1; + disk->ind_blk_num = -1; + disk->master_ind_blk_num = -1; + done = 1; + break; + } else { + /* check to make sure name is unique */ + if((file_ptr->storage_type_name_len & 0x0f)== name_len) { + if(!memcmp(file_ptr->file_name, name,name_len)) { + printf("Name %s already on disk!\n", + name); + exit(8); + } + } + pos++; + if(pos >= last_pos) { + /* Go to next block */ + next_blk = get_l2byte(&(dir_ptr->next_blk)); + if(next_blk) { + dir_ptr = disk_read_dir(disk, next_blk); + pos = 0; + } else { + printf("Top directory full!\n"); + exit(2); + } + } + } + } } -int -pro_write_file(ProDisk *disk, byte *in_buf, int pos, int size) -{ - int block; - int i; +int pro_write_file(ProDisk *disk, byte *in_buf, int pos, int size) { + int block; + int i; - block = get_disk_block(disk, pos, 1); - if(block < 7) { - printf("pro_write_file, get_disk_block: %d\n", block); - exit(3); - } - if(size < 512) { - for(i = size; i < 512; i++) { - in_buf[i] = 0; - } - } else if(size > 512) { - printf("error, pro_write_file size: %d too big\n", size); - exit(4); - } + block = get_disk_block(disk, pos, 1); + if(block < 7) { + printf("pro_write_file, get_disk_block: %d\n", block); + exit(3); + } + if(size < 512) { + for(i = size; i < 512; i++) { + in_buf[i] = 0; + } + } else if(size > 512) { + printf("error, pro_write_file size: %d too big\n", size); + exit(4); + } - disk_write_data(disk, block, in_buf, 512); + disk_write_data(disk, block, in_buf, 512); - return 0; + return 0; } -int -get_disk_block(ProDisk *disk, int pos, int create) -{ - File_entry *file_ptr; - int storage_type; - word32 eof; - int lo, hi; - int offset; - int master_ind_block, ind_block; - int ret_block; - int key_block; +int get_disk_block(ProDisk *disk, int pos, int create) { + File_entry *file_ptr; + int storage_type; + word32 eof; + int lo, hi; + int offset; + int master_ind_block, ind_block; + int ret_block; + int key_block; - if(pos >= 128*256*512) { - printf("offset too big\n"); - exit(3); - } + if(pos >= 128*256*512) { + printf("offset too big\n"); + exit(3); + } - file_ptr = disk->file_ptr; + file_ptr = disk->file_ptr; - eof = get_l3byte(&(file_ptr->eof)); - storage_type = (file_ptr->storage_type_name_len) >> 4; + eof = get_l3byte(&(file_ptr->eof)); + storage_type = (file_ptr->storage_type_name_len) >> 4; - key_block = get_l2byte(&(file_ptr->key_pointer)); + key_block = get_l2byte(&(file_ptr->key_pointer)); - if(storage_type == 1 && pos >= 512) { - /* make it sapling */ - get_new_ind_block(disk); - inc_l2byte(&(file_ptr->blocks_used)); - disk->ind_blk_data[0] = key_block & 0xff; - disk->ind_blk_data[0x100] = key_block >> 8; - key_block = disk->ind_blk_num; - set_l2byte(&(file_ptr->key_pointer), key_block); - file_ptr->storage_type_name_len += 0x10; - storage_type++; - } - if(storage_type == 2 && pos >= 256*512) { - /* make it tree */ - get_new_master_ind_block(disk); - inc_l2byte(&(file_ptr->blocks_used)); - disk->master_ind_blk_data[0] = key_block & 0xff; - disk->master_ind_blk_data[0x100] = key_block >> 8; - key_block = disk->master_ind_blk_num; - set_l2byte(&(file_ptr->key_pointer), key_block); - file_ptr->storage_type_name_len += 0x10; - storage_type++; - } + if(storage_type == 1 && pos >= 512) { + /* make it sapling */ + get_new_ind_block(disk); + inc_l2byte(&(file_ptr->blocks_used)); + disk->ind_blk_data[0] = key_block & 0xff; + disk->ind_blk_data[0x100] = key_block >> 8; + key_block = disk->ind_blk_num; + set_l2byte(&(file_ptr->key_pointer), key_block); + file_ptr->storage_type_name_len += 0x10; + storage_type++; + } + if(storage_type == 2 && pos >= 256*512) { + /* make it tree */ + get_new_master_ind_block(disk); + inc_l2byte(&(file_ptr->blocks_used)); + disk->master_ind_blk_data[0] = key_block & 0xff; + disk->master_ind_blk_data[0x100] = key_block >> 8; + key_block = disk->master_ind_blk_num; + set_l2byte(&(file_ptr->key_pointer), key_block); + file_ptr->storage_type_name_len += 0x10; + storage_type++; + } - switch(storage_type) { - case 1: - if(pos >= 512) { - printf("Error1!\n"); - exit(3); - } - ret_block = key_block; - if(ret_block == 0) { - ret_block = find_next_free_block(disk); - inc_l2byte(&(file_ptr->blocks_used)); - set_l2byte(&(file_ptr->key_pointer), ret_block); - } - return ret_block; - case 2: - ind_block = key_block; - if(ind_block <= 0) { - printf("write failure, ind_block: %d!\n", ind_block); - exit(3); - } - offset = pos >> 9; - if(offset >= 256) { - printf("pos too big!\n"); - exit(3); - } + switch(storage_type) { + case 1: + if(pos >= 512) { + printf("Error1!\n"); + exit(3); + } + ret_block = key_block; + if(ret_block == 0) { + ret_block = find_next_free_block(disk); + inc_l2byte(&(file_ptr->blocks_used)); + set_l2byte(&(file_ptr->key_pointer), ret_block); + } + return ret_block; + case 2: + ind_block = key_block; + if(ind_block <= 0) { + printf("write failure, ind_block: %d!\n", ind_block); + exit(3); + } + offset = pos >> 9; + if(offset >= 256) { + printf("pos too big!\n"); + exit(3); + } - lo = disk->ind_blk_data[offset]; - hi = disk->ind_blk_data[offset + 0x100]; - ret_block = hi*256 + lo; - if(ret_block == 0) { - /* Need to alloc a block for this guy */ - ret_block = find_next_free_block(disk); - inc_l2byte(&(file_ptr->blocks_used)); - disk->ind_blk_data[offset] = ret_block & 0xff; - disk->ind_blk_data[offset + 0x100] = - ((ret_block >> 8) & 0xff); - } - return ret_block; - case 3: - /* tree */ - master_ind_block = key_block; - if(master_ind_block <= 0) { - printf("write failure, master_ind_block: %d!\n", - master_ind_block); - exit(3); - } - offset = pos >> 17; - if(offset >= 128) { - printf("master too big!\n"); - exit(4); - } - lo = disk->master_ind_blk_data[offset]; - hi = disk->master_ind_blk_data[offset + 0x100]; - ind_block = hi*256 + lo; - if(ind_block == 0) { - /* Need to alloc an ind block */ - get_new_ind_block(disk); - ind_block = disk->ind_blk_num; - inc_l2byte(&(file_ptr->blocks_used)); - disk->master_ind_blk_data[offset] = ind_block & 0xff; - disk->master_ind_blk_data[offset + 0x100] = - ((ind_block >> 8) & 0xff); - } + lo = disk->ind_blk_data[offset]; + hi = disk->ind_blk_data[offset + 0x100]; + ret_block = hi*256 + lo; + if(ret_block == 0) { + /* Need to alloc a block for this guy */ + ret_block = find_next_free_block(disk); + inc_l2byte(&(file_ptr->blocks_used)); + disk->ind_blk_data[offset] = ret_block & 0xff; + disk->ind_blk_data[offset + 0x100] = + ((ret_block >> 8) & 0xff); + } + return ret_block; + case 3: + /* tree */ + master_ind_block = key_block; + if(master_ind_block <= 0) { + printf("write failure, master_ind_block: %d!\n", + master_ind_block); + exit(3); + } + offset = pos >> 17; + if(offset >= 128) { + printf("master too big!\n"); + exit(4); + } + lo = disk->master_ind_blk_data[offset]; + hi = disk->master_ind_blk_data[offset + 0x100]; + ind_block = hi*256 + lo; + if(ind_block == 0) { + /* Need to alloc an ind block */ + get_new_ind_block(disk); + ind_block = disk->ind_blk_num; + inc_l2byte(&(file_ptr->blocks_used)); + disk->master_ind_blk_data[offset] = ind_block & 0xff; + disk->master_ind_blk_data[offset + 0x100] = + ((ind_block >> 8) & 0xff); + } - offset = (pos >> 9) & 0xff; - lo = disk->ind_blk_data[offset]; - hi = disk->ind_blk_data[offset + 0x100]; - ret_block = hi*256 + lo; + offset = (pos >> 9) & 0xff; + lo = disk->ind_blk_data[offset]; + hi = disk->ind_blk_data[offset + 0x100]; + ret_block = hi*256 + lo; - if(ret_block == 0) { - /* Need to alloc a block for this guy */ - ret_block = find_next_free_block(disk); - inc_l2byte(&(file_ptr->blocks_used)); - disk->ind_blk_data[offset] = ret_block & 0xff; - disk->ind_blk_data[offset + 0x100] = - ((ret_block >> 8) & 0xff); - } - return ret_block; - default: - printf("unknown storage type: %d\n", storage_type); - exit(4); - } + if(ret_block == 0) { + /* Need to alloc a block for this guy */ + ret_block = find_next_free_block(disk); + inc_l2byte(&(file_ptr->blocks_used)); + disk->ind_blk_data[offset] = ret_block & 0xff; + disk->ind_blk_data[offset + 0x100] = + ((ret_block >> 8) & 0xff); + } + return ret_block; + default: + printf("unknown storage type: %d\n", storage_type); + exit(4); + } - printf("Can't get here!\n"); - exit(5); + printf("Can't get here!\n"); + exit(5); } -void -get_new_ind_block(ProDisk *disk) -{ - int ind_blk_num; - int i; +void get_new_ind_block(ProDisk *disk) { + int ind_blk_num; + int i; - write_ind_block(disk); + write_ind_block(disk); - ind_blk_num = find_next_free_block(disk); - for(i = 0; i < 512; i++) { - disk->ind_blk_data[i] = 0; - } + ind_blk_num = find_next_free_block(disk); + for(i = 0; i < 512; i++) { + disk->ind_blk_data[i] = 0; + } - disk->ind_blk_num = ind_blk_num; + disk->ind_blk_num = ind_blk_num; } -void -write_ind_block(ProDisk *disk) -{ - int ind_blk_num; +void write_ind_block(ProDisk *disk) { + int ind_blk_num; - ind_blk_num = disk->ind_blk_num; - if(ind_blk_num > 0) { - printf("Write ind block: %04x\n", ind_blk_num); - disk_write_data(disk, ind_blk_num, &(disk->ind_blk_data[0]), - 512); - disk->ind_blk_num = -1; - } + ind_blk_num = disk->ind_blk_num; + if(ind_blk_num > 0) { + printf("Write ind block: %04x\n", ind_blk_num); + disk_write_data(disk, ind_blk_num, &(disk->ind_blk_data[0]), + 512); + disk->ind_blk_num = -1; + } } -void -get_new_master_ind_block(ProDisk *disk) -{ - int master_ind_blk_num; - int i; +void get_new_master_ind_block(ProDisk *disk) { + int master_ind_blk_num; + int i; - write_master_ind_block(disk); + write_master_ind_block(disk); - master_ind_blk_num = find_next_free_block(disk); - for(i = 0; i < 512; i++) { - disk->master_ind_blk_data[i] = 0; - } + master_ind_blk_num = find_next_free_block(disk); + for(i = 0; i < 512; i++) { + disk->master_ind_blk_data[i] = 0; + } - disk->master_ind_blk_num = master_ind_blk_num; + disk->master_ind_blk_num = master_ind_blk_num; } -void -write_master_ind_block(ProDisk *disk) -{ - int master_ind_blk_num; +void write_master_ind_block(ProDisk *disk) { + int master_ind_blk_num; - master_ind_blk_num = disk->master_ind_blk_num; - if(master_ind_blk_num > 0) { - printf("Write master_ind block: %04x\n", master_ind_blk_num); - disk_write_data(disk, master_ind_blk_num, - &(disk->master_ind_blk_data[0]), 512); - disk->master_ind_blk_num = -1; - } + master_ind_blk_num = disk->master_ind_blk_num; + if(master_ind_blk_num > 0) { + printf("Write master_ind block: %04x\n", master_ind_blk_num); + disk_write_data(disk, master_ind_blk_num, + &(disk->master_ind_blk_data[0]), 512); + disk->master_ind_blk_num = -1; + } } -int -find_next_free_block(ProDisk *disk) -{ - byte *bitmap_ptr; - int pos; - int bitmap_bytes; - int i, j; - word32 val; +int find_next_free_block(ProDisk *disk) { + byte *bitmap_ptr; + int pos; + int bitmap_bytes; + int i, j; + word32 val; - bitmap_ptr = disk->bitmap_ptr; - bitmap_bytes = disk->bitmap_bytes; - pos = disk->bitmap_cur_pos; + bitmap_ptr = disk->bitmap_ptr; + bitmap_bytes = disk->bitmap_bytes; + pos = disk->bitmap_cur_pos; - for(i = pos; i < bitmap_bytes; i++) { - val = bitmap_ptr[i]; - if(val == 0) { - continue; - } - for(j = 0; j < 8; j++) { - if(val & (0x80 >> j)) { - set_bitmap_used(bitmap_ptr, 8*i+j); - disk->bitmap_cur_pos = i; - return 8*i + j; - } - } - return -1; - } - return -1; + for(i = pos; i < bitmap_bytes; i++) { + val = bitmap_ptr[i]; + if(val == 0) { + continue; + } + for(j = 0; j < 8; j++) { + if(val & (0x80 >> j)) { + set_bitmap_used(bitmap_ptr, 8*i+j); + disk->bitmap_cur_pos = i; + return 8*i + j; + } + } + return -1; + } + return -1; } -void -set_bitmap_used(byte *ptr, int i) -{ - word32 offset, bit; - word32 val; +void set_bitmap_used(byte *ptr, int i) { + word32 offset, bit; + word32 val; - offset = i >> 3; - bit = i & 7; + offset = i >> 3; + bit = i & 7; - val = ~(0x80 >> bit); - ptr[offset] &= val; + val = ~(0x80 >> bit); + ptr[offset] &= val; } -void -set_bitmap_free(byte *ptr, int i) -{ - int offset, bit; - int val; +void set_bitmap_free(byte *ptr, int i) { + int offset, bit; + int val; - offset = i >> 3; - bit = i & 7; + offset = i >> 3; + bit = i & 7; - val = (0x80 >> bit); - ptr[offset] |= val; + val = (0x80 >> bit); + ptr[offset] |= val; } -void -set_file_entry(File_entry *entry, int storage_type_name_len, char *file_name, - int file_type, int key_pointer, int blocks_used, int eof, - word32 creation_time, int version, int min_version, int access, - int aux_type, word32 last_mod, int header_pointer) -{ +void set_file_entry(File_entry *entry, int storage_type_name_len, char *file_name, + int file_type, int key_pointer, int blocks_used, int eof, + word32 creation_time, int version, int min_version, int access, + int aux_type, word32 last_mod, int header_pointer) { - entry->storage_type_name_len = storage_type_name_len; - strncpy((char *)entry->file_name, file_name, 15); - entry->file_type = file_type; - set_l2byte(&(entry->key_pointer), key_pointer); - set_l2byte(&(entry->blocks_used), blocks_used); - set_l3byte(&(entry->eof), eof); - set_pro_time(&(entry->creation_time), creation_time); - entry->version = version; - entry->min_version = min_version; - entry->access = access; - set_l2byte(&(entry->aux_type), aux_type); - set_pro_time(&(entry->last_mod), last_mod); - set_l2byte(&(entry->aux_type), header_pointer); + entry->storage_type_name_len = storage_type_name_len; + strncpy((char *)entry->file_name, file_name, 15); + entry->file_type = file_type; + set_l2byte(&(entry->key_pointer), key_pointer); + set_l2byte(&(entry->blocks_used), blocks_used); + set_l3byte(&(entry->eof), eof); + set_pro_time(&(entry->creation_time), creation_time); + entry->version = version; + entry->min_version = min_version; + entry->access = access; + set_l2byte(&(entry->aux_type), aux_type); + set_pro_time(&(entry->last_mod), last_mod); + set_l2byte(&(entry->aux_type), header_pointer); } -void -set_l2byte(L2byte *ptr, int val) -{ - ptr->low = (val & 0xff); - ptr->hi = ((val >> 8) & 0xff); +void set_l2byte(L2byte *ptr, int val) { + ptr->low = (val & 0xff); + ptr->hi = ((val >> 8) & 0xff); } -void -set_l3byte(L3byte *ptr, int val) -{ - ptr->low = (val & 0xff); - ptr->hi = ((val >> 8) & 0xff); - ptr->higher = ((val >> 16) & 0xff); +void set_l3byte(L3byte *ptr, int val) { + ptr->low = (val & 0xff); + ptr->hi = ((val >> 8) & 0xff); + ptr->higher = ((val >> 16) & 0xff); } -void -set_pro_time(Pro_time *ptr, word32 val) -{ - ptr->times[0] = ((val >> 24) & 0xff); - ptr->times[1] = ((val >> 16) & 0xff); - ptr->times[2] = ((val >> 8) & 0xff); - ptr->times[3] = ((val) & 0xff); +void set_pro_time(Pro_time *ptr, word32 val) { + ptr->times[0] = ((val >> 24) & 0xff); + ptr->times[1] = ((val >> 16) & 0xff); + ptr->times[2] = ((val >> 8) & 0xff); + ptr->times[3] = ((val) & 0xff); } -int -get_l2byte(L2byte *ptr) -{ - int val; +int get_l2byte(L2byte *ptr) { + int val; - val = ((ptr->hi) * 256) + ptr->low; - return val; + val = ((ptr->hi) * 256) + ptr->low; + return val; } -int -get_l3byte(L3byte *ptr) -{ - int val; +int get_l3byte(L3byte *ptr) { + int val; - val = ((ptr->higher) * 65536) + ((ptr->hi) * 256) + ptr->low; - return val; + val = ((ptr->higher) * 65536) + ((ptr->hi) * 256) + ptr->low; + return val; } -void -inc_l2byte(L2byte *ptr) -{ - set_l2byte(ptr, get_l2byte(ptr) + 1); +void inc_l2byte(L2byte *ptr) { + set_l2byte(ptr, get_l2byte(ptr) + 1); } diff --git a/src/video.c b/src/video.c index 0df3435..61d4944 100644 --- a/src/video.c +++ b/src/video.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include @@ -26,8 +26,8 @@ byte g_cur_border_colors[270]; byte g_new_special_border[64][64]; byte g_cur_special_border[64][64]; -word32 g_a2_screen_buffer_changed = (word32)-1; -word32 g_full_refresh_needed = (word32)-1; +word32 g_a2_screen_buffer_changed = (word32)-1; +word32 g_full_refresh_needed = (word32)-1; word32 g_cycs_in_40col = 0; word32 g_cycs_in_xredraw = 0; @@ -67,80 +67,80 @@ Kimage g_mainwin_kimage; extern double g_last_vbl_dcycs; -double g_video_dcycs_check_input = 0.0; -int g_video_extra_check_inputs = 0; // OG Not recommended to use it (or apps might miss mouse changes) -int g_video_act_margin_left = BASE_MARGIN_LEFT; -int g_video_act_margin_right = BASE_MARGIN_RIGHT; -int g_video_act_margin_top = BASE_MARGIN_TOP; -int g_video_act_margin_bottom = BASE_MARGIN_BOTTOM; -int g_video_act_width = X_A2_WINDOW_WIDTH; -int g_video_act_height = X_A2_WINDOW_HEIGHT; +double g_video_dcycs_check_input = 0.0; +int g_video_extra_check_inputs = 0; // OG Not recommended to use it (or apps might miss mouse changes) +int g_video_act_margin_left = BASE_MARGIN_LEFT; +int g_video_act_margin_right = BASE_MARGIN_RIGHT; +int g_video_act_margin_top = BASE_MARGIN_TOP; +int g_video_act_margin_bottom = BASE_MARGIN_BOTTOM; +int g_video_act_width = X_A2_WINDOW_WIDTH; +int g_video_act_height = X_A2_WINDOW_HEIGHT; -int g_need_redraw = 1; -int g_palette_change_summary = 0; -word32 g_palette_change_cnt[16]; -int g_border_sides_refresh_needed = 1; -int g_border_special_refresh_needed = 1; -int g_border_line24_refresh_needed = 1; -int g_status_refresh_needed = 1; +int g_need_redraw = 1; +int g_palette_change_summary = 0; +word32 g_palette_change_cnt[16]; +int g_border_sides_refresh_needed = 1; +int g_border_special_refresh_needed = 1; +int g_border_line24_refresh_needed = 1; +int g_status_refresh_needed = 1; -int g_vbl_border_color = 0; -int g_border_last_vbl_changes = 0; +int g_vbl_border_color = 0; +int g_border_last_vbl_changes = 0; -int g_use_dhr140 = 0; -int g_use_bw_hires = 0; +int g_use_dhr140 = 0; +int g_use_bw_hires = 0; int g_startx = WINDOWPOS_UNDEFINED; int g_starty = WINDOWPOS_UNDEFINED; int g_highdpi = 0; int g_borderless = 0; int g_resizeable = 0; -int g_a2_new_all_stat[200]; -int g_a2_cur_all_stat[200]; -int g_new_a2_stat_cur_line = 0; -int g_vid_update_last_line = 0; +int g_a2_new_all_stat[200]; +int g_a2_cur_all_stat[200]; +int g_new_a2_stat_cur_line = 0; +int g_vid_update_last_line = 0; -int g_expanded_col_0[16]; -int g_expanded_col_1[16]; -int g_expanded_col_2[16]; +int g_expanded_col_0[16]; +int g_expanded_col_1[16]; +int g_expanded_col_2[16]; int g_cur_a2_stat = ALL_STAT_TEXT | ALL_STAT_ANNUNC3 | - (0xf << BIT_ALL_STAT_TEXT_COLOR); -extern int g_save_cur_a2_stat; /* from config.c */ + (0xf << BIT_ALL_STAT_TEXT_COLOR); +extern int g_save_cur_a2_stat; /* from config.c */ -int g_a2vid_palette = 0xe; -int g_installed_full_superhires_colormap = 0; +int g_a2vid_palette = 0xe; +int g_installed_full_superhires_colormap = 0; -int Max_color_size = 256; +int Max_color_size = 256; word32 g_palette_8to1624[256]; word32 g_a2palette_8to1624[256]; -word32 g_saved_line_palettes[200][8]; -int g_saved_a2vid_palette = -1; -word32 g_a2vid_palette_remap[16]; +word32 g_saved_line_palettes[200][8]; +int g_saved_a2vid_palette = -1; +word32 g_a2vid_palette_remap[16]; word32 g_cycs_in_refresh_line = 0; word32 g_cycs_in_refresh_ximage = 0; -int g_num_lines_superhires = 0; -int g_num_lines_superhires640 = 0; -int g_num_lines_prev_superhires = 0; -int g_num_lines_prev_superhires640 = 0; +int g_num_lines_superhires = 0; +int g_num_lines_superhires640 = 0; +int g_num_lines_prev_superhires = 0; +int g_num_lines_prev_superhires640 = 0; -word32 g_red_mask = 0xff; -word32 g_green_mask = 0xff; -word32 g_blue_mask = 0xff; -int g_red_left_shift = 16; -int g_green_left_shift = 8; -int g_blue_left_shift = 0; -int g_red_right_shift = 0; -int g_green_right_shift = 0; -int g_blue_right_shift = 0; +word32 g_red_mask = 0xff; +word32 g_green_mask = 0xff; +word32 g_blue_mask = 0xff; +int g_red_left_shift = 16; +int g_green_left_shift = 8; +int g_blue_left_shift = 0; +int g_red_right_shift = 0; +int g_green_right_shift = 0; +int g_blue_right_shift = 0; -char g_status_buf[MAX_STATUS_LINES][STATUS_LINE_LENGTH + 1]; -char *g_status_ptrs[MAX_STATUS_LINES] = { 0 }; +char g_status_buf[MAX_STATUS_LINES][STATUS_LINE_LENGTH + 1]; +char *g_status_ptrs[MAX_STATUS_LINES] = { 0 }; // These LORES/DHIRES RGB values were extracted from ROM 3 IIgs video signals by Koichi Nishida #define BLACK_RGB 0x000 @@ -161,3483 +161,3387 @@ char *g_status_ptrs[MAX_STATUS_LINES] = { 0 }; #define WHITE_RGB 0xfff const int g_dbhires_colors[] = { - BLACK_RGB, // 0x0 black - DEEP_RED_RGB, // 0x1 deep red - BROWN_RGB, // 0x2 brown - ORANGE_RGB, // 0x3 orange - DARK_GREEN_RGB, // 0x4 dark green - DARK_GRAY_RGB, // 0x5 dark gray - GREEN_RGB, // 0x6 green - YELLOW_RGB, // 0x7 yellow - DARK_BLUE_RGB, // 0x8 dark blue - PURPLE_RGB, // 0x9 purple - LIGHT_GRAY_RGB, // 0xa light gray - PINK_RGB, // 0xb pink - MEDIUM_BLUE_RGB, // 0xc medium blue - LIGHT_BLUE_RGB, // 0xd light blue - AQUAMARINE_RGB, // 0xe aquamarine - WHITE_RGB // 0xf white + BLACK_RGB, // 0x0 black + DEEP_RED_RGB, // 0x1 deep red + BROWN_RGB, // 0x2 brown + ORANGE_RGB, // 0x3 orange + DARK_GREEN_RGB, // 0x4 dark green + DARK_GRAY_RGB, // 0x5 dark gray + GREEN_RGB, // 0x6 green + YELLOW_RGB, // 0x7 yellow + DARK_BLUE_RGB, // 0x8 dark blue + PURPLE_RGB, // 0x9 purple + LIGHT_GRAY_RGB, // 0xa light gray + PINK_RGB, // 0xb pink + MEDIUM_BLUE_RGB, // 0xc medium blue + LIGHT_BLUE_RGB, // 0xd light blue + AQUAMARINE_RGB, // 0xe aquamarine + WHITE_RGB // 0xf white }; -word32 g_dhires_convert[4096]; /* look up table of 7 bits (concat): */ - /* { 4 bits, |3 prev bits| } */ +word32 g_dhires_convert[4096]; /* look up table of 7 bits (concat): */ + /* { 4 bits, |3 prev bits| } */ const byte g_dhires_colors_16[] = { - 0x00, // 0x0 black - 0x02, // 0x1 dark blue - 0x04, // 0x2 dark green - 0x06, // 0x3 medium blue - 0x08, // 0x4 brown - 0x0a, // 0x5 light gray - 0x0c, // 0x6 green - 0x0e, // 0x7 aquamarine - 0x01, // 0x8 deep red - 0x03, // 0x9 purple - 0x05, // 0xa dark gray - 0x07, // 0xb light blue - 0x09, // 0xc orange - 0x0b, // 0xd pink - 0x0d, // 0xe yellow - 0x0f // 0xf white + 0x00, // 0x0 black + 0x02, // 0x1 dark blue + 0x04, // 0x2 dark green + 0x06, // 0x3 medium blue + 0x08, // 0x4 brown + 0x0a, // 0x5 light gray + 0x0c, // 0x6 green + 0x0e, // 0x7 aquamarine + 0x01, // 0x8 deep red + 0x03, // 0x9 purple + 0x05, // 0xa dark gray + 0x07, // 0xb light blue + 0x09, // 0xc orange + 0x0b, // 0xd pink + 0x0d, // 0xe yellow + 0x0f // 0xf white }; int g_lores_colors[] = { - BLACK_RGB, // 0x0 black - DEEP_RED_RGB, // 0x1 deep red - DARK_BLUE_RGB, // 0x2 dark blue - PURPLE_RGB, // 0x3 purple - DARK_GREEN_RGB, // 0x4 dark green - DARK_GRAY_RGB, // 0x5 dark gray - MEDIUM_BLUE_RGB, // 0x6 medium blue - LIGHT_BLUE_RGB, // 0x7 light blue - BROWN_RGB, // 0x8 brown - ORANGE_RGB, // 0x9 orange - LIGHT_GRAY_RGB, // 0xa light gray - PINK_RGB, // 0xb pink - GREEN_RGB, // 0xc green - YELLOW_RGB, // 0xd yellow - AQUAMARINE_RGB, // 0xe aquamarine - WHITE_RGB // 0xf white + BLACK_RGB, // 0x0 black + DEEP_RED_RGB, // 0x1 deep red + DARK_BLUE_RGB, // 0x2 dark blue + PURPLE_RGB, // 0x3 purple + DARK_GREEN_RGB, // 0x4 dark green + DARK_GRAY_RGB, // 0x5 dark gray + MEDIUM_BLUE_RGB, // 0x6 medium blue + LIGHT_BLUE_RGB, // 0x7 light blue + BROWN_RGB, // 0x8 brown + ORANGE_RGB, // 0x9 orange + LIGHT_GRAY_RGB, // 0xa light gray + PINK_RGB, // 0xb pink + GREEN_RGB, // 0xc green + YELLOW_RGB, // 0xd yellow + AQUAMARINE_RGB, // 0xe aquamarine + WHITE_RGB // 0xf white }; const word32 g_bw_hires_convert[4] = { - BIGEND(0x00000000), - BIGEND(0x0f0f0000), - BIGEND(0x00000f0f), - BIGEND(0x0f0f0f0f) + BIGEND(0x00000000), + BIGEND(0x0f0f0000), + BIGEND(0x00000f0f), + BIGEND(0x0f0f0f0f) }; const word32 g_bw_dhires_convert[16] = { - BIGEND(0x00000000), - BIGEND(0x0f000000), - BIGEND(0x000f0000), - BIGEND(0x0f0f0000), + BIGEND(0x00000000), + BIGEND(0x0f000000), + BIGEND(0x000f0000), + BIGEND(0x0f0f0000), - BIGEND(0x00000f00), - BIGEND(0x0f000f00), - BIGEND(0x000f0f00), - BIGEND(0x0f0f0f00), + BIGEND(0x00000f00), + BIGEND(0x0f000f00), + BIGEND(0x000f0f00), + BIGEND(0x0f0f0f00), - BIGEND(0x0000000f), - BIGEND(0x0f00000f), - BIGEND(0x000f000f), - BIGEND(0x0f0f000f), + BIGEND(0x0000000f), + BIGEND(0x0f00000f), + BIGEND(0x000f000f), + BIGEND(0x0f0f000f), - BIGEND(0x00000f0f), - BIGEND(0x0f000f0f), - BIGEND(0x000f0f0f), - BIGEND(0x0f0f0f0f), + BIGEND(0x00000f0f), + BIGEND(0x0f000f0f), + BIGEND(0x000f0f0f), + BIGEND(0x0f0f0f0f), }; const word32 g_hires_convert[64] = { - BIGEND(0x00000000), /* 00,0000 = black, black, black, black */ - BIGEND(0x00000000), /* 00,0001 = black, black, black, black */ - BIGEND(0x03030000), /* 00,0010 = purp , purp , black, black */ - BIGEND(0x0f0f0000), /* 00,0011 = white, white, black, black */ - BIGEND(0x00000c0c), /* 00,0100 = black, black, green, green */ - BIGEND(0x0c0c0c0c), /* 00,0101 = green, green, green, green */ - BIGEND(0x0f0f0f0f), /* 00,0110 = white, white, white, white */ - BIGEND(0x0f0f0f0f), /* 00,0111 = white, white, white, white */ - BIGEND(0x00000000), /* 00,1000 = black, black, black, black */ - BIGEND(0x00000000), /* 00,1001 = black, black, black, black */ - BIGEND(0x03030303), /* 00,1010 = purp , purp , purp , purp */ - BIGEND(0x0f0f0303), /* 00,1011 = white ,white, purp , purp */ - BIGEND(0x00000f0f), /* 00,1100 = black ,black, white, white */ - BIGEND(0x0c0c0f0f), /* 00,1101 = green ,green, white, white */ - BIGEND(0x0f0f0f0f), /* 00,1110 = white ,white, white, white */ - BIGEND(0x0f0f0f0f), /* 00,1111 = white ,white, white, white */ + BIGEND(0x00000000), /* 00,0000 = black, black, black, black */ + BIGEND(0x00000000), /* 00,0001 = black, black, black, black */ + BIGEND(0x03030000), /* 00,0010 = purp , purp , black, black */ + BIGEND(0x0f0f0000), /* 00,0011 = white, white, black, black */ + BIGEND(0x00000c0c), /* 00,0100 = black, black, green, green */ + BIGEND(0x0c0c0c0c), /* 00,0101 = green, green, green, green */ + BIGEND(0x0f0f0f0f), /* 00,0110 = white, white, white, white */ + BIGEND(0x0f0f0f0f), /* 00,0111 = white, white, white, white */ + BIGEND(0x00000000), /* 00,1000 = black, black, black, black */ + BIGEND(0x00000000), /* 00,1001 = black, black, black, black */ + BIGEND(0x03030303), /* 00,1010 = purp , purp , purp , purp */ + BIGEND(0x0f0f0303), /* 00,1011 = white ,white, purp , purp */ + BIGEND(0x00000f0f), /* 00,1100 = black ,black, white, white */ + BIGEND(0x0c0c0f0f), /* 00,1101 = green ,green, white, white */ + BIGEND(0x0f0f0f0f), /* 00,1110 = white ,white, white, white */ + BIGEND(0x0f0f0f0f), /* 00,1111 = white ,white, white, white */ - BIGEND(0x00000000), /* 01,0000 = black, black, black, black */ - BIGEND(0x00000000), /* 01,0001 = black, black, black, black */ - BIGEND(0x06060000), /* 01,0010 = blue , blue , black, black */ - BIGEND(0x0f0f0000), /* 01,0011 = white, white, black, black */ - BIGEND(0x00000c0c), /* 01,0100 = black, black, green, green */ - BIGEND(0x09090c0c), /* 01,0101 = orang, orang, green, green */ - BIGEND(0x0f0f0f0f), /* 01,0110 = white, white, white, white */ - BIGEND(0x0f0f0f0f), /* 01,0111 = white, white, white, white */ - BIGEND(0x00000000), /* 01,1000 = black, black, black, black */ - BIGEND(0x00000000), /* 01,1001 = black, black, black, black */ - BIGEND(0x06060303), /* 01,1010 = blue , blue , purp , purp */ - BIGEND(0x0f0f0303), /* 01,1011 = white ,white, purp , purp */ - BIGEND(0x00000f0f), /* 01,1100 = black ,black, white, white */ - BIGEND(0x09090f0f), /* 01,1101 = orang ,orang, white, white */ - BIGEND(0x0f0f0f0f), /* 01,1110 = white ,white, white, white */ - BIGEND(0x0f0f0f0f), /* 01,1111 = white ,white, white, white */ + BIGEND(0x00000000), /* 01,0000 = black, black, black, black */ + BIGEND(0x00000000), /* 01,0001 = black, black, black, black */ + BIGEND(0x06060000), /* 01,0010 = blue , blue , black, black */ + BIGEND(0x0f0f0000), /* 01,0011 = white, white, black, black */ + BIGEND(0x00000c0c), /* 01,0100 = black, black, green, green */ + BIGEND(0x09090c0c), /* 01,0101 = orang, orang, green, green */ + BIGEND(0x0f0f0f0f), /* 01,0110 = white, white, white, white */ + BIGEND(0x0f0f0f0f), /* 01,0111 = white, white, white, white */ + BIGEND(0x00000000), /* 01,1000 = black, black, black, black */ + BIGEND(0x00000000), /* 01,1001 = black, black, black, black */ + BIGEND(0x06060303), /* 01,1010 = blue , blue , purp , purp */ + BIGEND(0x0f0f0303), /* 01,1011 = white ,white, purp , purp */ + BIGEND(0x00000f0f), /* 01,1100 = black ,black, white, white */ + BIGEND(0x09090f0f), /* 01,1101 = orang ,orang, white, white */ + BIGEND(0x0f0f0f0f), /* 01,1110 = white ,white, white, white */ + BIGEND(0x0f0f0f0f), /* 01,1111 = white ,white, white, white */ - BIGEND(0x00000000), /* 10,0000 = black, black, black, black */ - BIGEND(0x00000000), /* 10,0001 = black, black, black, black */ - BIGEND(0x03030000), /* 10,0010 = purp , purp , black, black */ - BIGEND(0x0f0f0000), /* 10,0011 = white, white, black, black */ - BIGEND(0x00000909), /* 10,0100 = black, black, orang, orang */ - BIGEND(0x0c0c0909), /* 10,0101 = green, green, orang, orang */ - BIGEND(0x0f0f0f0f), /* 10,0110 = white, white, white, white */ - BIGEND(0x0f0f0f0f), /* 10,0111 = white, white, white, white */ - BIGEND(0x00000000), /* 10,1000 = black, black, black, black */ - BIGEND(0x00000000), /* 10,1001 = black, black, black, black */ - BIGEND(0x03030606), /* 10,1010 = purp , purp , blue , blue */ - BIGEND(0x0f0f0606), /* 10,1011 = white ,white, blue , blue */ - BIGEND(0x00000f0f), /* 10,1100 = black ,black, white, white */ - BIGEND(0x0c0c0f0f), /* 10,1101 = green ,green, white, white */ - BIGEND(0x0f0f0f0f), /* 10,1110 = white ,white, white, white */ - BIGEND(0x0f0f0f0f), /* 10,1111 = white ,white, white, white */ + BIGEND(0x00000000), /* 10,0000 = black, black, black, black */ + BIGEND(0x00000000), /* 10,0001 = black, black, black, black */ + BIGEND(0x03030000), /* 10,0010 = purp , purp , black, black */ + BIGEND(0x0f0f0000), /* 10,0011 = white, white, black, black */ + BIGEND(0x00000909), /* 10,0100 = black, black, orang, orang */ + BIGEND(0x0c0c0909), /* 10,0101 = green, green, orang, orang */ + BIGEND(0x0f0f0f0f), /* 10,0110 = white, white, white, white */ + BIGEND(0x0f0f0f0f), /* 10,0111 = white, white, white, white */ + BIGEND(0x00000000), /* 10,1000 = black, black, black, black */ + BIGEND(0x00000000), /* 10,1001 = black, black, black, black */ + BIGEND(0x03030606), /* 10,1010 = purp , purp , blue , blue */ + BIGEND(0x0f0f0606), /* 10,1011 = white ,white, blue , blue */ + BIGEND(0x00000f0f), /* 10,1100 = black ,black, white, white */ + BIGEND(0x0c0c0f0f), /* 10,1101 = green ,green, white, white */ + BIGEND(0x0f0f0f0f), /* 10,1110 = white ,white, white, white */ + BIGEND(0x0f0f0f0f), /* 10,1111 = white ,white, white, white */ - BIGEND(0x00000000), /* 11,0000 = black, black, black, black */ - BIGEND(0x00000000), /* 11,0001 = black, black, black, black */ - BIGEND(0x06060000), /* 11,0010 = blue , blue , black, black */ - BIGEND(0x0f0f0000), /* 11,0011 = white, white, black, black */ - BIGEND(0x00000909), /* 11,0100 = black, black, orang, orang */ - BIGEND(0x09090909), /* 11,0101 = orang, orang, orang, orang */ - BIGEND(0x0f0f0f0f), /* 11,0110 = white, white, white, white */ - BIGEND(0x0f0f0f0f), /* 11,0111 = white, white, white, white */ - BIGEND(0x00000000), /* 11,1000 = black, black, black, black */ - BIGEND(0x00000000), /* 11,1001 = black, black, black, black */ - BIGEND(0x06060606), /* 11,1010 = blue , blue , blue , blue */ - BIGEND(0x0f0f0606), /* 11,1011 = white ,white, blue , blue */ - BIGEND(0x00000f0f), /* 11,1100 = black ,black, white, white */ - BIGEND(0x09090f0f), /* 11,1101 = orang ,orang, white, white */ - BIGEND(0x0f0f0f0f), /* 11,1110 = white ,white, white, white */ - BIGEND(0x0f0f0f0f), /* 11,1111 = white ,white, white, white */ + BIGEND(0x00000000), /* 11,0000 = black, black, black, black */ + BIGEND(0x00000000), /* 11,0001 = black, black, black, black */ + BIGEND(0x06060000), /* 11,0010 = blue , blue , black, black */ + BIGEND(0x0f0f0000), /* 11,0011 = white, white, black, black */ + BIGEND(0x00000909), /* 11,0100 = black, black, orang, orang */ + BIGEND(0x09090909), /* 11,0101 = orang, orang, orang, orang */ + BIGEND(0x0f0f0f0f), /* 11,0110 = white, white, white, white */ + BIGEND(0x0f0f0f0f), /* 11,0111 = white, white, white, white */ + BIGEND(0x00000000), /* 11,1000 = black, black, black, black */ + BIGEND(0x00000000), /* 11,1001 = black, black, black, black */ + BIGEND(0x06060606), /* 11,1010 = blue , blue , blue , blue */ + BIGEND(0x0f0f0606), /* 11,1011 = white ,white, blue , blue */ + BIGEND(0x00000f0f), /* 11,1100 = black ,black, white, white */ + BIGEND(0x09090f0f), /* 11,1101 = orang ,orang, white, white */ + BIGEND(0x0f0f0f0f), /* 11,1110 = white ,white, white, white */ + BIGEND(0x0f0f0f0f), /* 11,1111 = white ,white, white, white */ }; - int g_screen_index[] = { - 0x000, 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, - 0x028, 0x0a8, 0x128, 0x1a8, 0x228, 0x2a8, 0x328, 0x3a8, - 0x050, 0x0d0, 0x150, 0x1d0, 0x250, 0x2d0, 0x350, 0x3d0 +int g_screen_index[] = { + 0x000, 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, + 0x028, 0x0a8, 0x128, 0x1a8, 0x228, 0x2a8, 0x328, 0x3a8, + 0x050, 0x0d0, 0x150, 0x1d0, 0x250, 0x2d0, 0x350, 0x3d0 }; -void -video_init() -{ - word32 col[4]; - Kimage *kimage_ptr; - word32 *ptr; - word32 val0, val1, val2, val3; - word32 match_col; - word32 next_col, next2_col, next3_col; - word32 val; - word32 cur_col; - int width, height; - int total_bytes; - int i, j; +void video_init() { + word32 col[4]; + Kimage *kimage_ptr; + word32 *ptr; + word32 val0, val1, val2, val3; + word32 match_col; + word32 next_col, next2_col, next3_col; + word32 val; + word32 cur_col; + int width, height; + int total_bytes; + int i, j; /* Initialize video system */ // OG Reinit globals - g_a2_screen_buffer_changed = (word32)-1; - g_full_refresh_needed = (word32)-1; - g_cycs_in_40col = 0; - g_cycs_in_xredraw = 0; - g_refresh_bytes_xfer = 0; + g_a2_screen_buffer_changed = (word32)-1; + g_full_refresh_needed = (word32)-1; + g_cycs_in_40col = 0; + g_cycs_in_xredraw = 0; + g_refresh_bytes_xfer = 0; - g_video_dcycs_check_input = 0.0; - //g_video_extra_check_inputs = 0; - g_video_act_margin_left = BASE_MARGIN_LEFT; - g_video_act_margin_right = BASE_MARGIN_RIGHT; - g_video_act_margin_top = BASE_MARGIN_TOP; - g_video_act_margin_bottom = BASE_MARGIN_BOTTOM; - g_video_act_width = X_A2_WINDOW_WIDTH; - g_video_act_height = X_A2_WINDOW_HEIGHT; + g_video_dcycs_check_input = 0.0; + //g_video_extra_check_inputs = 0; + g_video_act_margin_left = BASE_MARGIN_LEFT; + g_video_act_margin_right = BASE_MARGIN_RIGHT; + g_video_act_margin_top = BASE_MARGIN_TOP; + g_video_act_margin_bottom = BASE_MARGIN_BOTTOM; + g_video_act_width = X_A2_WINDOW_WIDTH; + g_video_act_height = X_A2_WINDOW_HEIGHT; - g_need_redraw = 1; - g_palette_change_summary = 0; + g_need_redraw = 1; + g_palette_change_summary = 0; - g_border_sides_refresh_needed = 1; - g_border_special_refresh_needed = 1; - g_border_line24_refresh_needed = 1; - g_status_refresh_needed = 1; + g_border_sides_refresh_needed = 1; + g_border_special_refresh_needed = 1; + g_border_line24_refresh_needed = 1; + g_status_refresh_needed = 1; - g_vbl_border_color = 0; - g_border_last_vbl_changes = 0; + g_vbl_border_color = 0; + g_border_last_vbl_changes = 0; - g_use_dhr140 = 0; - g_use_bw_hires = 0; + g_use_dhr140 = 0; + g_use_bw_hires = 0; - g_new_a2_stat_cur_line = 0; - g_vid_update_last_line = 0; + g_new_a2_stat_cur_line = 0; + g_vid_update_last_line = 0; - g_cur_a2_stat = ALL_STAT_TEXT | ALL_STAT_ANNUNC3 |(0xf << BIT_ALL_STAT_TEXT_COLOR); + g_cur_a2_stat = ALL_STAT_TEXT | ALL_STAT_ANNUNC3 |(0xf << BIT_ALL_STAT_TEXT_COLOR); - g_a2vid_palette = 0xe; - g_installed_full_superhires_colormap = 0; + g_a2vid_palette = 0xe; + g_installed_full_superhires_colormap = 0; - Max_color_size = 256; + Max_color_size = 256; - g_saved_a2vid_palette = -1; + g_saved_a2vid_palette = -1; - g_cycs_in_refresh_line = 0; - g_cycs_in_refresh_ximage = 0; + g_cycs_in_refresh_line = 0; + g_cycs_in_refresh_ximage = 0; - g_num_lines_superhires = 0; - g_num_lines_superhires640 = 0; - g_num_lines_prev_superhires = 0; - g_num_lines_prev_superhires640 = 0; + g_num_lines_superhires = 0; + g_num_lines_superhires640 = 0; + g_num_lines_prev_superhires = 0; + g_num_lines_prev_superhires640 = 0; - /* - g_red_mask = 0xff; - g_green_mask = 0xff; - g_blue_mask = 0xff; - g_red_left_shift = 16; - g_green_left_shift = 8; - g_blue_left_shift = 0; - g_red_right_shift = 0; - g_green_right_shift = 0; - g_blue_right_shift = 0; -*/ + /* + g_red_mask = 0xff; + g_green_mask = 0xff; + g_blue_mask = 0xff; + g_red_left_shift = 16; + g_green_left_shift = 8; + g_blue_left_shift = 0; + g_red_right_shift = 0; + g_green_right_shift = 0; + g_blue_right_shift = 0; + */ /* Initialize video system */ - for(i = 0; i < 200; i++) { - g_a2_line_kimage[i] = (Kimage *)0; // OG Changed from void* to kimage* - g_a2_line_stat[i] = -1; - g_a2_line_left_edge[i] = 0; - g_a2_line_right_edge[i] = 0; - } - for(i = 0; i < 200; i++) { - g_a2_new_all_stat[i] = 0; - g_a2_cur_all_stat[i] = 1; - for(j = 0; j < 8; j++) { - g_saved_line_palettes[i][j] = (word32)-1; - } - } - for(i = 0; i < 262; i++) { - g_cur_border_colors[i] = -1; - } + for(i = 0; i < 200; i++) { + g_a2_line_kimage[i] = (Kimage *)0; // OG Changed from void* to kimage* + g_a2_line_stat[i] = -1; + g_a2_line_left_edge[i] = 0; + g_a2_line_right_edge[i] = 0; + } + for(i = 0; i < 200; i++) { + g_a2_new_all_stat[i] = 0; + g_a2_cur_all_stat[i] = 1; + for(j = 0; j < 8; j++) { + g_saved_line_palettes[i][j] = (word32)-1; + } + } + for(i = 0; i < 262; i++) { + g_cur_border_colors[i] = -1; + } - g_new_a2_stat_cur_line = 0; + g_new_a2_stat_cur_line = 0; - dev_video_init(); + dev_video_init(); - read_a2_font(); + read_a2_font(); - vid_printf("Zeroing out video memory\n"); + vid_printf("Zeroing out video memory\n"); - for(i = 0; i < 7; i++) { - switch(i) { - case 0: - kimage_ptr = &(g_kimage_text[0]); - break; - case 1: - kimage_ptr = &(g_kimage_text[1]); - break; - case 2: - kimage_ptr = &(g_kimage_hires[0]); - break; - case 3: - kimage_ptr = &(g_kimage_hires[1]); - break; - case 4: - kimage_ptr = &g_kimage_superhires; - break; - case 5: - kimage_ptr = &g_kimage_border_sides; - break; - case 6: - kimage_ptr = &g_kimage_border_special; - break; - default: - printf("i: %d, unknown\n", i); - exit(3); - } + for(i = 0; i < 7; i++) { + switch(i) { + case 0: + kimage_ptr = &(g_kimage_text[0]); + break; + case 1: + kimage_ptr = &(g_kimage_text[1]); + break; + case 2: + kimage_ptr = &(g_kimage_hires[0]); + break; + case 3: + kimage_ptr = &(g_kimage_hires[1]); + break; + case 4: + kimage_ptr = &g_kimage_superhires; + break; + case 5: + kimage_ptr = &g_kimage_border_sides; + break; + case 6: + kimage_ptr = &g_kimage_border_special; + break; + default: + printf("i: %d, unknown\n", i); + exit(3); + } - ptr = (word32 *)kimage_ptr->data_ptr; - width = kimage_ptr->width_act; - height = kimage_ptr->height; - total_bytes = (kimage_ptr->mdepth >> 3) * width * height; + ptr = (word32 *)kimage_ptr->data_ptr; + width = kimage_ptr->width_act; + height = kimage_ptr->height; + total_bytes = (kimage_ptr->mdepth >> 3) * width * height; - for(j = 0; j < total_bytes >> 2; j++) { - *ptr++ = 0; - } - } + for(j = 0; j < total_bytes >> 2; j++) { + *ptr++ = 0; + } + } - for(i = 0; i < SLOW_MEM_CH_SIZE; i++) { - slow_mem_changed[i] = (word32)-1; - } + for(i = 0; i < SLOW_MEM_CH_SIZE; i++) { + slow_mem_changed[i] = (word32)-1; + } - /* create g_expanded_col_* */ - for(i = 0; i < 16; i++) { - val = (g_lores_colors[i] >> 0) & 0xf; - g_expanded_col_0[i] = val; + /* create g_expanded_col_* */ + for(i = 0; i < 16; i++) { + val = (g_lores_colors[i] >> 0) & 0xf; + g_expanded_col_0[i] = val; - val = (g_lores_colors[i] >> 4) & 0xf; - g_expanded_col_1[i] = val; + val = (g_lores_colors[i] >> 4) & 0xf; + g_expanded_col_1[i] = val; - val = (g_lores_colors[i] >> 8) & 0xf; - g_expanded_col_2[i] = val; - } + val = (g_lores_colors[i] >> 8) & 0xf; + g_expanded_col_2[i] = val; + } - /* create g_dhires_convert[] array */ - for(i = 0; i < 4096; i++) { - /* Convert index bits 11:0 where 3:0 is the previous color */ - /* and 7:4 is the current color to translate */ - /* Bit 4 will be the first pixel displayed on the screen */ - match_col = i & 0xf; - for(j = 0; j < 4; j++) { - cur_col = (i >> (1 + j)) & 0xf; - next_col = (i >> (2 + j)) & 0xf; - next2_col = (i >> (3 + j)) & 0xf; - next3_col = (i >> (4 + j)) & 0xf; - cur_col = (((cur_col << 4) + cur_col) >> (3 - j)) & 0xf; + /* create g_dhires_convert[] array */ + for(i = 0; i < 4096; i++) { + /* Convert index bits 11:0 where 3:0 is the previous color */ + /* and 7:4 is the current color to translate */ + /* Bit 4 will be the first pixel displayed on the screen */ + match_col = i & 0xf; + for(j = 0; j < 4; j++) { + cur_col = (i >> (1 + j)) & 0xf; + next_col = (i >> (2 + j)) & 0xf; + next2_col = (i >> (3 + j)) & 0xf; + next3_col = (i >> (4 + j)) & 0xf; + cur_col = (((cur_col << 4) + cur_col) >> (3 - j)) & 0xf; - if((cur_col == 0xf) || (next_col == 0xf) || - (next2_col == 0xf) || - (next3_col == 0xf)) { - cur_col = 0xf; - col[j] = cur_col; - match_col = cur_col; - } else if((cur_col == 0) || (next_col == 0) || - (next2_col == 0) || (next3_col == 0)) { - cur_col = 0; - col[j] = cur_col; - match_col = cur_col; - } else { - col[j] = cur_col; - match_col = cur_col; - } - } - if(g_use_dhr140) { - for(j = 0; j < 4; j++) { - col[j] = (i >> 4) & 0xf; - } - } - val0 = g_dhires_colors_16[col[0] & 0xf]; - val1 = g_dhires_colors_16[col[1] & 0xf]; - val2 = g_dhires_colors_16[col[2] & 0xf]; - val3 = g_dhires_colors_16[col[3] & 0xf]; + if((cur_col == 0xf) || (next_col == 0xf) || + (next2_col == 0xf) || + (next3_col == 0xf)) { + cur_col = 0xf; + col[j] = cur_col; + match_col = cur_col; + } else if((cur_col == 0) || (next_col == 0) || + (next2_col == 0) || (next3_col == 0)) { + cur_col = 0; + col[j] = cur_col; + match_col = cur_col; + } else { + col[j] = cur_col; + match_col = cur_col; + } + } + if(g_use_dhr140) { + for(j = 0; j < 4; j++) { + col[j] = (i >> 4) & 0xf; + } + } + val0 = g_dhires_colors_16[col[0] & 0xf]; + val1 = g_dhires_colors_16[col[1] & 0xf]; + val2 = g_dhires_colors_16[col[2] & 0xf]; + val3 = g_dhires_colors_16[col[3] & 0xf]; #if defined(GSPLUS_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) // OSX needs to calculate endianness mid-compilation, can't be passed on compile command - val = (val3 << 24) + (val2 << 16) + (val1 << 8) + val0; + val = (val3 << 24) + (val2 << 16) + (val1 << 8) + val0; #else - val = (val0 << 24) + (val1 << 16) + (val2 << 8) + val3; + val = (val0 << 24) + (val1 << 16) + (val2 << 8) + val3; #endif - g_dhires_convert[i] = val; - } + g_dhires_convert[i] = val; + } - change_display_mode(g_cur_dcycs); - video_reset(); - display_screen(); + change_display_mode(g_cur_dcycs); + video_reset(); + display_screen(); - fflush(stdout); + fflush(stdout); } -void -show_a2_line_stuff() -{ - int i; +void show_a2_line_stuff() { + int i; - for(i = 0; i < 200; i++) { - printf("line: %d: stat: %04x, ptr: %p, " - "left_edge:%d, right_edge:%d\n", - i, g_a2_line_stat[i], g_a2_line_kimage[i], - g_a2_line_left_edge[i], - g_a2_line_right_edge[i]); - } + for(i = 0; i < 200; i++) { + printf("line: %d: stat: %04x, ptr: %p, " + "left_edge:%d, right_edge:%d\n", + i, g_a2_line_stat[i], g_a2_line_kimage[i], + g_a2_line_left_edge[i], + g_a2_line_right_edge[i]); + } - printf("new_a2_stat_cur_line: %d, cur_a2_stat:%04x\n", - g_new_a2_stat_cur_line, g_cur_a2_stat); - for(i = 0; i < 200; i++) { - printf("cur_all[%d]: %03x new_all: %03x\n", i, - g_a2_cur_all_stat[i], g_a2_new_all_stat[i]); - } + printf("new_a2_stat_cur_line: %d, cur_a2_stat:%04x\n", + g_new_a2_stat_cur_line, g_cur_a2_stat); + for(i = 0; i < 200; i++) { + printf("cur_all[%d]: %03x new_all: %03x\n", i, + g_a2_cur_all_stat[i], g_a2_new_all_stat[i]); + } } -int g_flash_count = 0; +int g_flash_count = 0; -void -video_reset() -{ - int stat; - int i; +void video_reset() { + int stat; + int i; - g_installed_full_superhires_colormap = (g_screen_depth != 8); - stat = ALL_STAT_TEXT | ALL_STAT_ANNUNC3 | - (0xf << BIT_ALL_STAT_TEXT_COLOR); - if(g_use_bw_hires) { - stat |= ALL_STAT_COLOR_C021; - } - if(g_config_control_panel) { - /* Don't update cur_a2_stat when in configuration panel */ - g_save_cur_a2_stat = stat; - } else { - g_cur_a2_stat = stat; - } + g_installed_full_superhires_colormap = (g_screen_depth != 8); + stat = ALL_STAT_TEXT | ALL_STAT_ANNUNC3 | + (0xf << BIT_ALL_STAT_TEXT_COLOR); + if(g_use_bw_hires) { + stat |= ALL_STAT_COLOR_C021; + } + if(g_config_control_panel) { + /* Don't update cur_a2_stat when in configuration panel */ + g_save_cur_a2_stat = stat; + } else { + g_cur_a2_stat = stat; + } - g_palette_change_summary = 0; - for(i = 0; i < 16; i++) { - g_palette_change_cnt[i] = 0; - } + g_palette_change_summary = 0; + for(i = 0; i < 16; i++) { + g_palette_change_cnt[i] = 0; + } - /* install_a2vid_colormap(); */ - video_update_colormap(); + /* install_a2vid_colormap(); */ + video_update_colormap(); } -int g_screen_redraw_skip_count = 0; -int g_screen_redraw_skip_amt = -1; +int g_screen_redraw_skip_count = 0; +int g_screen_redraw_skip_amt = -1; -word32 g_cycs_in_check_input = 0; +word32 g_cycs_in_check_input = 0; -int g_needfullrefreshfornextframe = 1 ; +int g_needfullrefreshfornextframe = 1; -void video_update() -{ - int did_video; +void video_update() { + int did_video; - // OG g_needfullrefreshfornextframe - if (g_needfullrefreshfornextframe) - { - g_full_refresh_needed = -1; - g_a2_screen_buffer_changed = -1; - g_status_refresh_needed = 1; - g_border_sides_refresh_needed = 1; - g_border_special_refresh_needed = 1; - g_needfullrefreshfornextframe = 0; - } + // OG g_needfullrefreshfornextframe + if (g_needfullrefreshfornextframe) + { + g_full_refresh_needed = -1; + g_a2_screen_buffer_changed = -1; + g_status_refresh_needed = 1; + g_border_sides_refresh_needed = 1; + g_border_special_refresh_needed = 1; + g_needfullrefreshfornextframe = 0; + } - update_border_info(); + update_border_info(); - video_check_input_events(); + video_check_input_events(); - g_screen_redraw_skip_count--; - did_video = 0; - if(g_screen_redraw_skip_count < 0) { - did_video = 1; - video_update_event_line(262); - g_screen_redraw_skip_count = g_screen_redraw_skip_amt; - } + g_screen_redraw_skip_count--; + did_video = 0; + if(g_screen_redraw_skip_count < 0) { + did_video = 1; + video_update_event_line(262); + g_screen_redraw_skip_count = g_screen_redraw_skip_amt; + } - /* update flash */ - g_flash_count++; - if(g_flash_count >= 30) { - g_flash_count = 0; - g_cur_a2_stat ^= ALL_STAT_FLASH_STATE; - change_display_mode(g_cur_dcycs); - } + /* update flash */ + g_flash_count++; + if(g_flash_count >= 30) { + g_flash_count = 0; + g_cur_a2_stat ^= ALL_STAT_FLASH_STATE; + change_display_mode(g_cur_dcycs); + } - check_a2vid_palette(); + check_a2vid_palette(); - if(did_video) { - g_new_a2_stat_cur_line = 0; - g_a2_new_all_stat[0] = g_cur_a2_stat; - g_vid_update_last_line = 0; - video_update_through_line(0); - } + if(did_video) { + g_new_a2_stat_cur_line = 0; + g_a2_new_all_stat[0] = g_cur_a2_stat; + g_vid_update_last_line = 0; + video_update_through_line(0); + } // OG Notify host that video has been uodated #if defined(ACTIVEGSPLUGIN) && defined(MAC) - { - extern void x_need2refresh(); - x_need2refresh(); - } + { + extern void x_need2refresh(); + x_need2refresh(); + } #endif } -int -video_all_stat_to_line_stat(int line, int new_all_stat) -{ - int page, color, dbl; - int st80, hires, annunc3, mix_t_gr; - int altchar, text_color, bg_color, flash_state; - int mode; +int video_all_stat_to_line_stat(int line, int new_all_stat) { + int page, color, dbl; + int st80, hires, annunc3, mix_t_gr; + int altchar, text_color, bg_color, flash_state; + int mode; - st80 = new_all_stat & ALL_STAT_ST80; - hires = new_all_stat & ALL_STAT_HIRES; - annunc3 = new_all_stat & ALL_STAT_ANNUNC3; - mix_t_gr = new_all_stat & ALL_STAT_MIX_T_GR; + st80 = new_all_stat & ALL_STAT_ST80; + hires = new_all_stat & ALL_STAT_HIRES; + annunc3 = new_all_stat & ALL_STAT_ANNUNC3; + mix_t_gr = new_all_stat & ALL_STAT_MIX_T_GR; - page = EXTRU(new_all_stat, 31 - BIT_ALL_STAT_PAGE2, 1) && !st80; - color = EXTRU(new_all_stat, 31 - BIT_ALL_STAT_COLOR_C021, 1); - dbl = EXTRU(new_all_stat, 31 - BIT_ALL_STAT_VID80, 1); + page = EXTRU(new_all_stat, 31 - BIT_ALL_STAT_PAGE2, 1) && !st80; + color = EXTRU(new_all_stat, 31 - BIT_ALL_STAT_COLOR_C021, 1); + dbl = EXTRU(new_all_stat, 31 - BIT_ALL_STAT_VID80, 1); - altchar = 0; text_color = 0; bg_color = 0; flash_state = 0; + altchar = 0; text_color = 0; bg_color = 0; flash_state = 0; - if(new_all_stat & ALL_STAT_SUPER_HIRES) { - mode = MODE_SUPER_HIRES; - page = 0; dbl = 0; color = 0; - } else { - if(line >= 192) { - mode = MODE_BORDER; - page = 0; dbl = 0; color = 0; - } else if((new_all_stat & ALL_STAT_TEXT) || - (line >= 160 && mix_t_gr)) { - mode = MODE_TEXT; - color = 0; - altchar = EXTRU(new_all_stat, - 31 - BIT_ALL_STAT_ALTCHARSET, 1); - text_color = EXTRU(new_all_stat, - 31 - BIT_ALL_STAT_TEXT_COLOR, 4); - bg_color = EXTRU(new_all_stat, - 31 - BIT_ALL_STAT_BG_COLOR, 4); - flash_state = EXTRU(new_all_stat, - 31 - BIT_ALL_STAT_FLASH_STATE, 1); - if(altchar) { - /* don't bother flashing if altchar on */ - flash_state = 0; - } - } else { - /* obey the graphics mode */ - dbl = dbl && !annunc3; - if(hires) { - color = color | EXTRU(new_all_stat, - 31 - BIT_ALL_STAT_DIS_COLOR_DHIRES, 1); - mode = MODE_HGR; - } else { - mode = MODE_GR; - } - } - } + if(new_all_stat & ALL_STAT_SUPER_HIRES) { + mode = MODE_SUPER_HIRES; + page = 0; dbl = 0; color = 0; + } else { + if(line >= 192) { + mode = MODE_BORDER; + page = 0; dbl = 0; color = 0; + } else if((new_all_stat & ALL_STAT_TEXT) || + (line >= 160 && mix_t_gr)) { + mode = MODE_TEXT; + color = 0; + altchar = EXTRU(new_all_stat, + 31 - BIT_ALL_STAT_ALTCHARSET, 1); + text_color = EXTRU(new_all_stat, + 31 - BIT_ALL_STAT_TEXT_COLOR, 4); + bg_color = EXTRU(new_all_stat, + 31 - BIT_ALL_STAT_BG_COLOR, 4); + flash_state = EXTRU(new_all_stat, + 31 - BIT_ALL_STAT_FLASH_STATE, 1); + if(altchar) { + /* don't bother flashing if altchar on */ + flash_state = 0; + } + } else { + /* obey the graphics mode */ + dbl = dbl && !annunc3; + if(hires) { + color = color | EXTRU(new_all_stat, + 31 - BIT_ALL_STAT_DIS_COLOR_DHIRES, 1); + mode = MODE_HGR; + } else { + mode = MODE_GR; + } + } + } - return((text_color << 12) + (bg_color << 8) + (altchar << 7) + - (mode << 4) + (flash_state << 3) + (page << 2) + - (color << 1) + dbl); + return((text_color << 12) + (bg_color << 8) + (altchar << 7) + + (mode << 4) + (flash_state << 3) + (page << 2) + + (color << 1) + dbl); } -int * -video_update_kimage_ptr(int line, int new_stat) -{ - Kimage *kimage_ptr; - int *mode_ptr; - int page; - int mode; +int *video_update_kimage_ptr(int line, int new_stat) { + Kimage *kimage_ptr; + int *mode_ptr; + int page; + int mode; - page = (new_stat >> 2) & 1; - mode = (new_stat >> 4) & 7; + page = (new_stat >> 2) & 1; + mode = (new_stat >> 4) & 7; - switch(mode) { - case MODE_TEXT: - case MODE_GR: - kimage_ptr = &(g_kimage_text[page]); - mode_ptr = &(g_mode_text[page][0]); - break; - case MODE_HGR: - kimage_ptr = &(g_kimage_hires[page]); - mode_ptr = &(g_mode_hires[page][0]); - /* arrange to force superhires reparse since we use the */ - /* same memory */ - g_mode_superhires[line] = -1; - break; - case MODE_SUPER_HIRES: - kimage_ptr = &g_kimage_superhires; - mode_ptr = &(g_mode_superhires[0]); - /* arrange to force hires reparse since we use the */ - /* same memory */ - g_mode_hires[0][line] = -1; - g_mode_hires[1][line] = -1; - break; - case MODE_BORDER: - /* Hack: reuse text page last line as the special border */ - kimage_ptr = &(g_kimage_text[0]); - mode_ptr = &(g_mode_border[0]); - break; - default: - halt_printf("update_a2_ptrs: mode: %d unknown!\n", mode); - return &(g_mode_superhires[0]); - } + switch(mode) { + case MODE_TEXT: + case MODE_GR: + kimage_ptr = &(g_kimage_text[page]); + mode_ptr = &(g_mode_text[page][0]); + break; + case MODE_HGR: + kimage_ptr = &(g_kimage_hires[page]); + mode_ptr = &(g_mode_hires[page][0]); + /* arrange to force superhires reparse since we use the */ + /* same memory */ + g_mode_superhires[line] = -1; + break; + case MODE_SUPER_HIRES: + kimage_ptr = &g_kimage_superhires; + mode_ptr = &(g_mode_superhires[0]); + /* arrange to force hires reparse since we use the */ + /* same memory */ + g_mode_hires[0][line] = -1; + g_mode_hires[1][line] = -1; + break; + case MODE_BORDER: + /* Hack: reuse text page last line as the special border */ + kimage_ptr = &(g_kimage_text[0]); + mode_ptr = &(g_mode_border[0]); + break; + default: + halt_printf("update_a2_ptrs: mode: %d unknown!\n", mode); + return &(g_mode_superhires[0]); + } - g_a2_line_kimage[line] = kimage_ptr; - return mode_ptr; + g_a2_line_kimage[line] = kimage_ptr; + return mode_ptr; } -void -change_a2vid_palette(int new_palette) -{ - int i; +void change_a2vid_palette(int new_palette) { + int i; - for(i = 0; i < 200; i++) { - g_mode_text[0][i] = -1; - g_mode_text[1][i] = -1; - g_mode_hires[0][i] = -1; - g_mode_hires[1][i] = -1; - g_mode_superhires[i] = -1; - g_mode_border[i] = -1; - } + for(i = 0; i < 200; i++) { + g_mode_text[0][i] = -1; + g_mode_text[1][i] = -1; + g_mode_hires[0][i] = -1; + g_mode_hires[1][i] = -1; + g_mode_superhires[i] = -1; + g_mode_border[i] = -1; + } - printf("Changed a2vid_palette to %x\n", new_palette); + printf("Changed a2vid_palette to %x\n", new_palette); - g_a2vid_palette = new_palette; - g_cur_a2_stat = (g_cur_a2_stat & (~ALL_STAT_A2VID_PALETTE)) + - (new_palette << BIT_ALL_STAT_A2VID_PALETTE); - change_display_mode(g_cur_dcycs); + g_a2vid_palette = new_palette; + g_cur_a2_stat = (g_cur_a2_stat & (~ALL_STAT_A2VID_PALETTE)) + + (new_palette << BIT_ALL_STAT_A2VID_PALETTE); + change_display_mode(g_cur_dcycs); - g_border_sides_refresh_needed = 1; - g_border_special_refresh_needed = 1; - g_status_refresh_needed = 1; - g_palette_change_cnt[new_palette]++; - g_border_last_vbl_changes = 1; - for(i = 0; i < 262; i++) { - g_cur_border_colors[i] = -1; - } + g_border_sides_refresh_needed = 1; + g_border_special_refresh_needed = 1; + g_status_refresh_needed = 1; + g_palette_change_cnt[new_palette]++; + g_border_last_vbl_changes = 1; + for(i = 0; i < 262; i++) { + g_cur_border_colors[i] = -1; + } } int g_num_a2vid_palette_checks = 1; int g_shr_palette_used[16]; -void -check_a2vid_palette() -{ - int sum; - int min; - int val; - int min_pos; - int count_cur; - int i; +void check_a2vid_palette() { + int sum; + int min; + int val; + int min_pos; + int count_cur; + int i; - /* determine if g_a2vid_palette should change */ - /* This is the palette of least use on superhires so that the */ - /* borders don't change when all 256 superhires colors are used */ + /* determine if g_a2vid_palette should change */ + /* This is the palette of least use on superhires so that the */ + /* borders don't change when all 256 superhires colors are used */ - g_num_a2vid_palette_checks--; - if(g_num_a2vid_palette_checks || g_installed_full_superhires_colormap){ - return; - } + g_num_a2vid_palette_checks--; + if(g_num_a2vid_palette_checks || g_installed_full_superhires_colormap) { + return; + } - g_num_a2vid_palette_checks = 60; + g_num_a2vid_palette_checks = 60; - sum = 0; - min = 0x100000; - min_pos = -1; - count_cur = g_shr_palette_used[g_a2vid_palette]; + sum = 0; + min = 0x100000; + min_pos = -1; + count_cur = g_shr_palette_used[g_a2vid_palette]; - for(i = 0; i < 16; i++) { - val = g_shr_palette_used[i]; - g_shr_palette_used[i] = 0; - if(val < min) { - min = val; - min_pos = i; - } - sum += val; - } + for(i = 0; i < 16; i++) { + val = g_shr_palette_used[i]; + g_shr_palette_used[i] = 0; + if(val < min) { + min = val; + min_pos = i; + } + sum += val; + } - if(g_a2vid_palette != min_pos && (count_cur > min)) { - change_a2vid_palette(min_pos); - } + if(g_a2vid_palette != min_pos && (count_cur > min)) { + change_a2vid_palette(min_pos); + } } -void -change_display_mode(double dcycs) -{ - int line, tmp_line; +void change_display_mode(double dcycs) { + int line, tmp_line; - line = ((get_lines_since_vbl(dcycs) + 0xff) >> 8); - if(line < 0) { - line = 0; - halt_printf("Line < 0!\n"); - } - tmp_line = MIN(199, line); + line = ((get_lines_since_vbl(dcycs) + 0xff) >> 8); + if(line < 0) { + line = 0; + halt_printf("Line < 0!\n"); + } + tmp_line = MIN(199, line); - video_update_all_stat_through_line(tmp_line); + video_update_all_stat_through_line(tmp_line); - if(line < 200) { - g_a2_new_all_stat[line] = g_cur_a2_stat; - } - /* otherwise, g_cur_a2_stat is covered at the end of vbl */ + if(line < 200) { + g_a2_new_all_stat[line] = g_cur_a2_stat; + } + /* otherwise, g_cur_a2_stat is covered at the end of vbl */ } -void -video_update_all_stat_through_line(int line) -{ - int start_line; - int prev_stat; - int max_line; - int i; +void video_update_all_stat_through_line(int line) { + int start_line; + int prev_stat; + int max_line; + int i; - start_line = g_new_a2_stat_cur_line; - prev_stat = g_a2_new_all_stat[start_line]; + start_line = g_new_a2_stat_cur_line; + prev_stat = g_a2_new_all_stat[start_line]; - max_line = MIN(199, line); + max_line = MIN(199, line); - for(i = start_line + 1; i <= max_line; i++) { - g_a2_new_all_stat[i] = prev_stat; - } - g_new_a2_stat_cur_line = max_line; + for(i = start_line + 1; i <= max_line; i++) { + g_a2_new_all_stat[i] = prev_stat; + } + g_new_a2_stat_cur_line = max_line; } -#define MAX_BORDER_CHANGES 16384 +#define MAX_BORDER_CHANGES 16384 STRUCT(Border_changes) { - float fcycs; - int val; + float fcycs; + int val; }; -int g_border_color = 0; // OG Expose border color +int g_border_color = 0; // OG Expose border color Border_changes g_border_changes[MAX_BORDER_CHANGES]; -int g_num_border_changes = 0; +int g_num_border_changes = 0; -void -change_border_color(double dcycs, int val) -{ - int pos; +void change_border_color(double dcycs, int val) { + int pos; - g_border_color = val; // OG Expose border color + g_border_color = val; // OG Expose border color - pos = g_num_border_changes; - g_border_changes[pos].fcycs = (float)(dcycs - g_last_vbl_dcycs); - g_border_changes[pos].val = val; + pos = g_num_border_changes; + g_border_changes[pos].fcycs = (float)(dcycs - g_last_vbl_dcycs); + g_border_changes[pos].val = val; - pos++; - g_num_border_changes = pos; + pos++; + g_num_border_changes = pos; - if(pos >= MAX_BORDER_CHANGES) { - halt_printf("num border changes: %d\n", pos); - g_num_border_changes = 0; - } + if(pos >= MAX_BORDER_CHANGES) { + halt_printf("num border changes: %d\n", pos); + g_num_border_changes = 0; + } } extern int first; -void -update_border_info() -{ - double dlines_per_dcyc; - double dcycs, dline, dcyc_line_start; - int offset; - int new_line_offset, last_line_offset; - int new_line; - int new_val; - int limit; - int color_now; - int i; +void update_border_info() { + double dlines_per_dcyc; + double dcycs, dline, dcyc_line_start; + int offset; + int new_line_offset, last_line_offset; + int new_line; + int new_val; + int limit; + int color_now; + int i; - /* to get this routine to redraw the border, change */ - /* g_vbl_border_color, set g_border_last_vbl_changes = 1 */ - /* and change the cur_border_colors[] array */ + /* to get this routine to redraw the border, change */ + /* g_vbl_border_color, set g_border_last_vbl_changes = 1 */ + /* and change the cur_border_colors[] array */ - color_now = g_vbl_border_color; + color_now = g_vbl_border_color; - dlines_per_dcyc = (double)(1.0 / 65.0); - limit = g_num_border_changes; - if(g_border_last_vbl_changes || limit) { - /* add a dummy entry */ - g_border_changes[limit].fcycs = DCYCS_IN_16MS + 21.0; - g_border_changes[limit].val = (g_c034_val & 0xf); - limit++; - } - last_line_offset = (-1 << 8) + 44; - for(i = 0; i < limit; i++) { - dcycs = g_border_changes[i].fcycs; - dline = dcycs * dlines_per_dcyc; - new_line = (int)dline; - dcyc_line_start = (double)new_line * 65.0; - offset = ((int)(dcycs - dcyc_line_start)) & 0xff; + dlines_per_dcyc = (double)(1.0 / 65.0); + limit = g_num_border_changes; + if(g_border_last_vbl_changes || limit) { + /* add a dummy entry */ + g_border_changes[limit].fcycs = DCYCS_IN_16MS + 21.0; + g_border_changes[limit].val = (g_c034_val & 0xf); + limit++; + } + last_line_offset = (-1 << 8) + 44; + for(i = 0; i < limit; i++) { + dcycs = g_border_changes[i].fcycs; + dline = dcycs * dlines_per_dcyc; + new_line = (int)dline; + dcyc_line_start = (double)new_line * 65.0; + offset = ((int)(dcycs - dcyc_line_start)) & 0xff; - /* here comes the tricky part */ - /* offset is from 0 to 65, where 0-3 is the right border of */ - /* the previous line, 4-20 is horiz blanking, 21-24 is the */ - /* left border and 25-64 is the main window */ - /* Convert this to a new notation which is 0-3 is the left */ - /* border, 4-43 is the main window, and 44-47 is the right */ - /* basically, add -21 to offset, and wrap < 0 to previous ln */ - /* note this makes line -1 offset 44-47 the left hand border */ - /* for true line 261 on the screen */ - offset -= 21; - if(offset < 0) { - new_line--; - offset += 64; - } - new_val = g_border_changes[i].val; - new_line_offset = (new_line << 8) + offset; + /* here comes the tricky part */ + /* offset is from 0 to 65, where 0-3 is the right border of */ + /* the previous line, 4-20 is horiz blanking, 21-24 is the */ + /* left border and 25-64 is the main window */ + /* Convert this to a new notation which is 0-3 is the left */ + /* border, 4-43 is the main window, and 44-47 is the right */ + /* basically, add -21 to offset, and wrap < 0 to previous ln */ + /* note this makes line -1 offset 44-47 the left hand border */ + /* for true line 261 on the screen */ + offset -= 21; + if(offset < 0) { + new_line--; + offset += 64; + } + new_val = g_border_changes[i].val; + new_line_offset = (new_line << 8) + offset; - if(new_line_offset < -256 || new_line_offset >(262*256 + 0x80)){ - printf("new_line_offset: %05x\n", new_line_offset); - new_line_offset = last_line_offset; - } - while(last_line_offset < new_line_offset) { - /* see if this will finish it */ - if((last_line_offset & -256)==(new_line_offset & -256)){ - update_border_line(last_line_offset, - new_line_offset, color_now); - last_line_offset = new_line_offset; - } else { - update_border_line(last_line_offset, - (last_line_offset & -256) + 65, - color_now); - last_line_offset =(last_line_offset & -256)+256; - } - } + if(new_line_offset < -256 || new_line_offset >(262*256 + 0x80)) { + printf("new_line_offset: %05x\n", new_line_offset); + new_line_offset = last_line_offset; + } + while(last_line_offset < new_line_offset) { + /* see if this will finish it */ + if((last_line_offset & -256)==(new_line_offset & -256)) { + update_border_line(last_line_offset, + new_line_offset, color_now); + last_line_offset = new_line_offset; + } else { + update_border_line(last_line_offset, + (last_line_offset & -256) + 65, + color_now); + last_line_offset =(last_line_offset & -256)+256; + } + } - color_now = new_val; - } + color_now = new_val; + } #if 0 - if(g_num_border_changes) { - printf("Border changes: %d\n", g_num_border_changes); - } + if(g_num_border_changes) { + printf("Border changes: %d\n", g_num_border_changes); + } #endif - if(limit > 1) { - g_border_last_vbl_changes = 1; - } else { - g_border_last_vbl_changes = 0; - } + if(limit > 1) { + g_border_last_vbl_changes = 1; + } else { + g_border_last_vbl_changes = 0; + } - g_num_border_changes = 0; - g_vbl_border_color = (g_c034_val & 0xf); + g_num_border_changes = 0; + g_vbl_border_color = (g_c034_val & 0xf); } -void -update_border_line(int st_line_offset, int end_line_offset, int color) -{ - word32 val; - int st_offset, end_offset; - int left, right; - int line; +void update_border_line(int st_line_offset, int end_line_offset, int color) { + word32 val; + int st_offset, end_offset; + int left, right; + int line; - line = st_line_offset >> 8; - if(line != (end_line_offset >> 8)) { - halt_printf("ubl, %04x %04x %02x!\n", st_line_offset, - end_line_offset, color); - } - if(line < -1 || line >= 262) { - halt_printf("ubl-b, mod line is %d\n", line); - line = 0; - } - if(line < 0 || line >= 262) { - line = 0; - } + line = st_line_offset >> 8; + if(line != (end_line_offset >> 8)) { + halt_printf("ubl, %04x %04x %02x!\n", st_line_offset, + end_line_offset, color); + } + if(line < -1 || line >= 262) { + halt_printf("ubl-b, mod line is %d\n", line); + line = 0; + } + if(line < 0 || line >= 262) { + line = 0; + } - st_offset = st_line_offset & 0xff; - end_offset = end_line_offset & 0xff; + st_offset = st_line_offset & 0xff; + end_offset = end_line_offset & 0xff; - if((st_offset == 0) && (end_offset >= 0x41)) { - /* might be the same as last time, save some work */ - if(g_cur_border_colors[line] == color) { - return; - } - g_cur_border_colors[line] = color; - } else { - g_cur_border_colors[line] = -1; - } + if((st_offset == 0) && (end_offset >= 0x41)) { + /* might be the same as last time, save some work */ + if(g_cur_border_colors[line] == color) { + return; + } + g_cur_border_colors[line] = color; + } else { + g_cur_border_colors[line] = -1; + } - val = (color + (g_a2vid_palette << 4)); - val = (val << 24) + (val << 16) + (val << 8) + val; + val = (color + (g_a2vid_palette << 4)); + val = (val << 24) + (val << 16) + (val << 8) + val; - /* 0-3: left border, 4-43: main window, 44-47: right border */ - /* 48-65: horiz blanking */ - /* first, do the sides from line 0 to line 199 */ - if((line < 200) || (line >= 262)) { - if(line >= 262) { - line = 0; - } - if(st_offset < 4) { - /* left side */ - left = st_offset; - right = MIN(4, end_offset); - video_border_pixel_write(&g_kimage_border_sides, - 2*line, 2, val, (left * BORDER_WIDTH)/4, - (right * BORDER_WIDTH) / 4); + /* 0-3: left border, 4-43: main window, 44-47: right border */ + /* 48-65: horiz blanking */ + /* first, do the sides from line 0 to line 199 */ + if((line < 200) || (line >= 262)) { + if(line >= 262) { + line = 0; + } + if(st_offset < 4) { + /* left side */ + left = st_offset; + right = MIN(4, end_offset); + video_border_pixel_write(&g_kimage_border_sides, + 2*line, 2, val, (left * BORDER_WIDTH)/4, + (right * BORDER_WIDTH) / 4); - g_border_sides_refresh_needed = 1; - } - if((st_offset < 48) && (end_offset >= 44)) { - /* right side */ - left = MAX(0, st_offset - 44); - right = MIN(4, end_offset - 44); - video_border_pixel_write(&g_kimage_border_sides, - 2*line, 2, val, - BORDER_WIDTH + (left * EFF_BORDER_WIDTH/4), - BORDER_WIDTH + (right * EFF_BORDER_WIDTH/4)); - g_border_sides_refresh_needed = 1; - } - } + g_border_sides_refresh_needed = 1; + } + if((st_offset < 48) && (end_offset >= 44)) { + /* right side */ + left = MAX(0, st_offset - 44); + right = MIN(4, end_offset - 44); + video_border_pixel_write(&g_kimage_border_sides, + 2*line, 2, val, + BORDER_WIDTH + (left * EFF_BORDER_WIDTH/4), + BORDER_WIDTH + (right * EFF_BORDER_WIDTH/4)); + g_border_sides_refresh_needed = 1; + } + } - if((line >= 192) && (line < 200)) { - if(st_offset < 44 && end_offset > 4) { - left = MAX(0, st_offset - 4); - right = MIN(40, end_offset - 4); - video_border_pixel_write(&g_kimage_text[0], - 2*line, 2, val, left * 640 / 40, - right * 640 / 40); - g_border_line24_refresh_needed = 1; - } - } + if((line >= 192) && (line < 200)) { + if(st_offset < 44 && end_offset > 4) { + left = MAX(0, st_offset - 4); + right = MIN(40, end_offset - 4); + video_border_pixel_write(&g_kimage_text[0], + 2*line, 2, val, left * 640 / 40, + right * 640 / 40); + g_border_line24_refresh_needed = 1; + } + } - /* now do the bottom, lines 200 to 215 */ - if((line >= 200) && (line < (200 + BASE_MARGIN_BOTTOM/2)) ) { - line -= 200; - left = st_offset; - right = MIN(48, end_offset); - video_border_pixel_write(&g_kimage_border_special, 2*line, 2, - val, (left * X_A2_WINDOW_WIDTH / 48), - (right * X_A2_WINDOW_WIDTH / 48)); - g_border_special_refresh_needed = 1; - } + /* now do the bottom, lines 200 to 215 */ + if((line >= 200) && (line < (200 + BASE_MARGIN_BOTTOM/2)) ) { + line -= 200; + left = st_offset; + right = MIN(48, end_offset); + video_border_pixel_write(&g_kimage_border_special, 2*line, 2, + val, (left * X_A2_WINDOW_WIDTH / 48), + (right * X_A2_WINDOW_WIDTH / 48)); + g_border_special_refresh_needed = 1; + } - /* and top, lines 236 to 262 */ - if((line >= (262 - BASE_MARGIN_TOP/2)) && (line < 262)) { - line -= (262 - BASE_MARGIN_TOP/2); - left = st_offset; - right = MIN(48, end_offset); - video_border_pixel_write(&g_kimage_border_special, - BASE_MARGIN_BOTTOM + 2*line, 2, val, - (left * X_A2_WINDOW_WIDTH / 48), - (right * X_A2_WINDOW_WIDTH / 48)); - g_border_special_refresh_needed = 1; - } + /* and top, lines 236 to 262 */ + if((line >= (262 - BASE_MARGIN_TOP/2)) && (line < 262)) { + line -= (262 - BASE_MARGIN_TOP/2); + left = st_offset; + right = MIN(48, end_offset); + video_border_pixel_write(&g_kimage_border_special, + BASE_MARGIN_BOTTOM + 2*line, 2, val, + (left * X_A2_WINDOW_WIDTH / 48), + (right * X_A2_WINDOW_WIDTH / 48)); + g_border_special_refresh_needed = 1; + } } -void -video_border_pixel_write(Kimage *kimage_ptr, int starty, int num_lines, - word32 val, int st_off, int end_off) -{ - word32 *ptr; - int width; - int width_act; - int mdepth; - int num_words, num_bytes; - int bytes_per_pix; - int i, j; +void video_border_pixel_write(Kimage *kimage_ptr, int starty, int num_lines, + word32 val, int st_off, int end_off) { + word32 *ptr; + int width; + int width_act; + int mdepth; + int num_words, num_bytes; + int bytes_per_pix; + int i, j; - if(end_off <= st_off) { - return; - } + if(end_off <= st_off) { + return; + } - width = end_off - st_off; - width_act = kimage_ptr->width_act; - mdepth = kimage_ptr->mdepth; - bytes_per_pix = mdepth >> 3; - num_bytes = width * bytes_per_pix; - num_words = num_bytes >> 2; + width = end_off - st_off; + width_act = kimage_ptr->width_act; + mdepth = kimage_ptr->mdepth; + bytes_per_pix = mdepth >> 3; + num_bytes = width * bytes_per_pix; + num_words = num_bytes >> 2; - if(width > width_act) { - halt_printf("border write but width %d > act %d\n", width, - width_act); - } + if(width > width_act) { + halt_printf("border write but width %d > act %d\n", width, + width_act); + } - if(mdepth == 16) { - val = g_a2palette_8to1624[val & 0xff]; - val = (val << 16) + val; - } else if(mdepth == 32) { - /* 32-bit pixels */ - val = g_a2palette_8to1624[val & 0xff]; - } + if(mdepth == 16) { + val = g_a2palette_8to1624[val & 0xff]; + val = (val << 16) + val; + } else if(mdepth == 32) { + /* 32-bit pixels */ + val = g_a2palette_8to1624[val & 0xff]; + } - for(i = 0; i < num_lines; i++) { - ptr = (word32 *)&(kimage_ptr->data_ptr[ - (starty + i)*width_act*bytes_per_pix]); - ptr += ((st_off * bytes_per_pix) / 4); - /* HACK: the above isn't really right when bytes_per_pix is */ - /* less than four... */ - for(j = 0; j < num_words; j++) { - *ptr++ = val; - } - } + for(i = 0; i < num_lines; i++) { + ptr = (word32 *)&(kimage_ptr->data_ptr[ + (starty + i)*width_act*bytes_per_pix]); + ptr += ((st_off * bytes_per_pix) / 4); + /* HACK: the above isn't really right when bytes_per_pix is */ + /* less than four... */ + for(j = 0; j < num_words; j++) { + *ptr++ = val; + } + } } -#define CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, do_clear) \ - ch_ptr = &(slow_mem_changed[mem_ptr >> CHANGE_SHIFT]); \ - ch_bitpos = 0; \ - bits_per_line = 40 >> SHIFT_PER_CHANGE; \ - ch_shift_amount = (mem_ptr >> SHIFT_PER_CHANGE) & 0x1f; \ - mask_per_line = (-(1 << (32 - bits_per_line))); \ - mask_per_line = mask_per_line >> ch_shift_amount; \ - ch_mask = *ch_ptr & mask_per_line; \ - if(do_clear) { \ - *ch_ptr = *ch_ptr & (~ch_mask); \ - } \ - ch_mask = ch_mask << ch_shift_amount; \ - \ - if(reparse) { \ - ch_mask = - (1 << (32 - bits_per_line)); \ - } +#define CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, do_clear) \ + ch_ptr = &(slow_mem_changed[mem_ptr >> CHANGE_SHIFT]); \ + ch_bitpos = 0; \ + bits_per_line = 40 >> SHIFT_PER_CHANGE; \ + ch_shift_amount = (mem_ptr >> SHIFT_PER_CHANGE) & 0x1f; \ + mask_per_line = (-(1 << (32 - bits_per_line))); \ + mask_per_line = mask_per_line >> ch_shift_amount; \ + ch_mask = *ch_ptr & mask_per_line; \ + if(do_clear) { \ + *ch_ptr = *ch_ptr & (~ch_mask); \ + } \ + ch_mask = ch_mask << ch_shift_amount; \ + \ + if(reparse) { \ + ch_mask = -(1 << (32 - bits_per_line)); \ + } -#define CH_LOOP_A2_VID(ch_mask, ch_tmp) \ - ch_tmp = ch_mask & 0x80000000; \ - ch_mask = ch_mask << 1; \ - \ - if(!ch_tmp) { \ - continue; \ - } +#define CH_LOOP_A2_VID(ch_mask, ch_tmp) \ + ch_tmp = ch_mask & 0x80000000; \ + ch_mask = ch_mask << 1; \ + \ + if(!ch_tmp) { \ + continue; \ + } -void -redraw_changed_text_40(int start_offset, int start_line, int num_lines, - int reparse, byte *screen_data, int altcharset, int bg_val, int fg_val, - int pixels_per_line) -{ - register word32 start_time, end_time; - word32 *img_ptr, *img_ptr2; - word32 *save_img_ptr, *save_img_ptr2; - word32 *ch_ptr; - const word32 *font_ptr1; - const word32 *font_ptr2; - byte *slow_mem_ptr; - byte *b_ptr; - word32 ch_mask; - word32 ch_tmp; - word32 line_mask; - word32 mask_per_line; - word32 mem_ptr; - word32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; - word32 palette_add; - word32 diff_val; - word32 and_val; - word32 add_val; - word32 ff_val; - word32 val0, val1; - int flash_state; - int y; - int x1, x2; - int ch_bitpos; - int bits_per_line; - int ch_shift_amount; - int shift_per; - int left, right; - int st_line_mod8, st_line; - int i; +void redraw_changed_text_40(int start_offset, int start_line, int num_lines, + int reparse, byte *screen_data, int altcharset, int bg_val, int fg_val, + int pixels_per_line) { + register word32 start_time, end_time; + word32 *img_ptr, *img_ptr2; + word32 *save_img_ptr, *save_img_ptr2; + word32 *ch_ptr; + const word32 *font_ptr1; + const word32 *font_ptr2; + byte *slow_mem_ptr; + byte *b_ptr; + word32 ch_mask; + word32 ch_tmp; + word32 line_mask; + word32 mask_per_line; + word32 mem_ptr; + word32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + word32 palette_add; + word32 diff_val; + word32 and_val; + word32 add_val; + word32 ff_val; + word32 val0, val1; + int flash_state; + int y; + int x1, x2; + int ch_bitpos; + int bits_per_line; + int ch_shift_amount; + int shift_per; + int left, right; + int st_line_mod8, st_line; + int i; - /* always redraws to the next multiple of 8 lines due to redraw */ - /* issues: char changed on one screen redraw at line 0 with */ - /* num_lines=1. We need to have drawn lines 1-7 also since line 1 */ - /* will not see any changed bytes */ - st_line_mod8 = start_line & 7; - st_line = start_line; + /* always redraws to the next multiple of 8 lines due to redraw */ + /* issues: char changed on one screen redraw at line 0 with */ + /* num_lines=1. We need to have drawn lines 1-7 also since line 1 */ + /* will not see any changed bytes */ + st_line_mod8 = start_line & 7; + st_line = start_line; - start_line = start_line >> 3; + start_line = start_line >> 3; - y = start_line; - line_mask = 1 << (y); - mem_ptr = 0x400 + g_screen_index[y] + start_offset; - if(mem_ptr < 0x400 || mem_ptr >= 0xc00) { - halt_printf("redraw_changed_text: mem_ptr: %08x\n", mem_ptr); - } + y = start_line; + line_mask = 1 << (y); + mem_ptr = 0x400 + g_screen_index[y] + start_offset; + if(mem_ptr < 0x400 || mem_ptr >= 0xc00) { + halt_printf("redraw_changed_text: mem_ptr: %08x\n", mem_ptr); + } - CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, (st_line_mod8 == 0)); - /* avoid clearing changed bits unless we are line 0 (mod 8) */ + CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, (st_line_mod8 == 0)); + /* avoid clearing changed bits unless we are line 0 (mod 8) */ - if(ch_mask == 0) { - return; - } + if(ch_mask == 0) { + return; + } - GET_ITIMER(start_time); + GET_ITIMER(start_time); - shift_per = (1 << SHIFT_PER_CHANGE); + shift_per = (1 << SHIFT_PER_CHANGE); - g_a2_screen_buffer_changed |= line_mask; + g_a2_screen_buffer_changed |= line_mask; - palette_add = (g_a2vid_palette << 4); - palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + - (palette_add << 24); + palette_add = (g_a2vid_palette << 4); + palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + + (palette_add << 24); - left = 40; - right = 0; + left = 40; + right = 0; - diff_val = (fg_val - bg_val) & 0xf; - and_val = diff_val + (diff_val << 8) + (diff_val << 16) +(diff_val<<24); - add_val = bg_val + (bg_val << 8) + (bg_val << 16) + (bg_val << 24); - ff_val = 0x0f0f0f0f; + diff_val = (fg_val - bg_val) & 0xf; + and_val = diff_val + (diff_val << 8) + (diff_val << 16) +(diff_val<<24); + add_val = bg_val + (bg_val << 8) + (bg_val << 16) + (bg_val << 24); + ff_val = 0x0f0f0f0f; - flash_state = (g_cur_a2_stat & ALL_STAT_FLASH_STATE); + flash_state = (g_cur_a2_stat & ALL_STAT_FLASH_STATE); - for(x1 = 0; x1 < 40; x1 += shift_per) { + for(x1 = 0; x1 < 40; x1 += shift_per) { - CH_LOOP_A2_VID(ch_mask, ch_tmp); + CH_LOOP_A2_VID(ch_mask, ch_tmp); - left = MIN(x1, left); - right = MAX(x1 + shift_per, right); - slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); - b_ptr = &screen_data[(8*y + st_line_mod8)*2*pixels_per_line + - x1*14]; - img_ptr = (word32 *)b_ptr; - img_ptr2 = (word32 *)(b_ptr + pixels_per_line); + left = MIN(x1, left); + right = MAX(x1 + shift_per, right); + slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); + b_ptr = &screen_data[(8*y + st_line_mod8)*2*pixels_per_line + + x1*14]; + img_ptr = (word32 *)b_ptr; + img_ptr2 = (word32 *)(b_ptr + pixels_per_line); - for(x2 = 0; x2 < shift_per; x2 += 2) { - val0 = *slow_mem_ptr++; - val1 = *slow_mem_ptr++; + for(x2 = 0; x2 < shift_per; x2 += 2) { + val0 = *slow_mem_ptr++; + val1 = *slow_mem_ptr++; - if(!altcharset) { - if(val0 >= 0x40 && val0 < 0x80) { - if(flash_state) { - val0 += 0x40; - } else { - val0 -= 0x40; - } - } - if(val1 >= 0x40 && val1 < 0x80) { - if(flash_state) { - val1 += 0x40; - } else { - val1 -= 0x40; - } - } - } - save_img_ptr = img_ptr; - save_img_ptr2 = img_ptr2; + if(!altcharset) { + if(val0 >= 0x40 && val0 < 0x80) { + if(flash_state) { + val0 += 0x40; + } else { + val0 -= 0x40; + } + } + if(val1 >= 0x40 && val1 < 0x80) { + if(flash_state) { + val1 += 0x40; + } else { + val1 -= 0x40; + } + } + } + save_img_ptr = img_ptr; + save_img_ptr2 = img_ptr2; - for(i = st_line_mod8; i < 8; i++) { - font_ptr1 = &(g_font40_even_bits[val0][i][0]); - tmp0 = (font_ptr1[0] & and_val) + add_val; - tmp1 = (font_ptr1[1] & and_val) + add_val; - tmp2 = (font_ptr1[2] & and_val) + add_val; + for(i = st_line_mod8; i < 8; i++) { + font_ptr1 = &(g_font40_even_bits[val0][i][0]); + tmp0 = (font_ptr1[0] & and_val) + add_val; + tmp1 = (font_ptr1[1] & and_val) + add_val; + tmp2 = (font_ptr1[2] & and_val) + add_val; - font_ptr2 = &(g_font40_odd_bits[val1][i][0]); - tmp3 = ((font_ptr1[3]+font_ptr2[0]) & and_val)+ - add_val; + font_ptr2 = &(g_font40_odd_bits[val1][i][0]); + tmp3 = ((font_ptr1[3]+font_ptr2[0]) & and_val)+ + add_val; - tmp4 = (font_ptr2[1] & and_val) + add_val; - tmp5 = (font_ptr2[2] & and_val) + add_val; - tmp6 = (font_ptr2[3] & and_val) + add_val; + tmp4 = (font_ptr2[1] & and_val) + add_val; + tmp5 = (font_ptr2[2] & and_val) + add_val; + tmp6 = (font_ptr2[3] & and_val) + add_val; - tmp0 = (tmp0 & ff_val) + palette_add; - tmp1 = (tmp1 & ff_val) + palette_add; - tmp2 = (tmp2 & ff_val) + palette_add; - tmp3 = (tmp3 & ff_val) + palette_add; - tmp4 = (tmp4 & ff_val) + palette_add; - tmp5 = (tmp5 & ff_val) + palette_add; - tmp6 = (tmp6 & ff_val) + palette_add; + tmp0 = (tmp0 & ff_val) + palette_add; + tmp1 = (tmp1 & ff_val) + palette_add; + tmp2 = (tmp2 & ff_val) + palette_add; + tmp3 = (tmp3 & ff_val) + palette_add; + tmp4 = (tmp4 & ff_val) + palette_add; + tmp5 = (tmp5 & ff_val) + palette_add; + tmp6 = (tmp6 & ff_val) + palette_add; - img_ptr[0] = tmp0; - img_ptr[1] = tmp1; - img_ptr[2] = tmp2; - img_ptr[3] = tmp3; - img_ptr[4] = tmp4; - img_ptr[5] = tmp5; - img_ptr[6] = tmp6; + img_ptr[0] = tmp0; + img_ptr[1] = tmp1; + img_ptr[2] = tmp2; + img_ptr[3] = tmp3; + img_ptr[4] = tmp4; + img_ptr[5] = tmp5; + img_ptr[6] = tmp6; - img_ptr2[0] = tmp0; - img_ptr2[1] = tmp1; - img_ptr2[2] = tmp2; - img_ptr2[3] = tmp3; - img_ptr2[4] = tmp4; - img_ptr2[5] = tmp5; - img_ptr2[6] = tmp6; + img_ptr2[0] = tmp0; + img_ptr2[1] = tmp1; + img_ptr2[2] = tmp2; + img_ptr2[3] = tmp3; + img_ptr2[4] = tmp4; + img_ptr2[5] = tmp5; + img_ptr2[6] = tmp6; - img_ptr += (2*pixels_per_line)/4; - img_ptr2 += (2*pixels_per_line)/4; - } + img_ptr += (2*pixels_per_line)/4; + img_ptr2 += (2*pixels_per_line)/4; + } - img_ptr = save_img_ptr + 7; - img_ptr2 = save_img_ptr2 + 7; - } - } - GET_ITIMER(end_time); + img_ptr = save_img_ptr + 7; + img_ptr2 = save_img_ptr2 + 7; + } + } + GET_ITIMER(end_time); - for(i = 0; i < (8 - st_line_mod8); i++) { - g_a2_line_left_edge[st_line + i] = (left*14); - g_a2_line_right_edge[st_line + i] = (right*14); - } + for(i = 0; i < (8 - st_line_mod8); i++) { + g_a2_line_left_edge[st_line + i] = (left*14); + g_a2_line_right_edge[st_line + i] = (right*14); + } - if(left >= right || left < 0 || right < 0) { - printf("line %d, 40: left >= right: %d >= %d\n", - start_line, left, right); - } + if(left >= right || left < 0 || right < 0) { + printf("line %d, 40: left >= right: %d >= %d\n", + start_line, left, right); + } - g_cycs_in_40col += (end_time - start_time); + g_cycs_in_40col += (end_time - start_time); - g_need_redraw = 0; + g_need_redraw = 0; } -void -redraw_changed_text_80(int start_offset, int start_line, int num_lines, - int reparse, byte *screen_data, int altcharset, int bg_val, int fg_val, - int pixels_per_line) -{ - const word32 *font_ptr0, *font_ptr1, *font_ptr2, *font_ptr3; - word32 *ch_ptr; - word32 *img_ptr, *img_ptr2; - word32 *save_img_ptr, *save_img_ptr2; - byte *b_ptr; - byte *slow_mem_ptr; - word32 ch_mask; - word32 ch_tmp; - word32 mask_per_line; - word32 mem_ptr; - word32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; - word32 diff_val; - word32 add_val, and_val, ff_val; - word32 palette_add; - word32 line_mask; - word32 val0, val1, val2, val3; - int st_line_mod8, st_line; - int flash_state; - int y; - int x1, x2; - int i; - int ch_bitpos; - int bits_per_line; - int ch_shift_amount; - int shift_per; - int left, right; +void redraw_changed_text_80(int start_offset, int start_line, int num_lines, + int reparse, byte *screen_data, int altcharset, int bg_val, int fg_val, + int pixels_per_line) { + const word32 *font_ptr0, *font_ptr1, *font_ptr2, *font_ptr3; + word32 *ch_ptr; + word32 *img_ptr, *img_ptr2; + word32 *save_img_ptr, *save_img_ptr2; + byte *b_ptr; + byte *slow_mem_ptr; + word32 ch_mask; + word32 ch_tmp; + word32 mask_per_line; + word32 mem_ptr; + word32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + word32 diff_val; + word32 add_val, and_val, ff_val; + word32 palette_add; + word32 line_mask; + word32 val0, val1, val2, val3; + int st_line_mod8, st_line; + int flash_state; + int y; + int x1, x2; + int i; + int ch_bitpos; + int bits_per_line; + int ch_shift_amount; + int shift_per; + int left, right; - st_line_mod8 = start_line & 7; - st_line = start_line; + st_line_mod8 = start_line & 7; + st_line = start_line; - start_line = start_line >> 3; + start_line = start_line >> 3; - y = start_line; - line_mask = 1 << (y); - mem_ptr = 0x400 + g_screen_index[y] + start_offset; - if(mem_ptr < 0x400 || mem_ptr >= 0xc00) { - halt_printf("redraw_changed_text: mem_ptr: %08x\n", mem_ptr); - } + y = start_line; + line_mask = 1 << (y); + mem_ptr = 0x400 + g_screen_index[y] + start_offset; + if(mem_ptr < 0x400 || mem_ptr >= 0xc00) { + halt_printf("redraw_changed_text: mem_ptr: %08x\n", mem_ptr); + } - CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, (st_line_mod8 == 0)); + CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, (st_line_mod8 == 0)); - if(ch_mask == 0) { - return; - } + if(ch_mask == 0) { + return; + } - shift_per = (1 << SHIFT_PER_CHANGE); + shift_per = (1 << SHIFT_PER_CHANGE); - g_a2_screen_buffer_changed |= line_mask; + g_a2_screen_buffer_changed |= line_mask; - palette_add = (g_a2vid_palette << 4); - palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + - (palette_add << 24); + palette_add = (g_a2vid_palette << 4); + palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + + (palette_add << 24); - left = 40; - right = 0; + left = 40; + right = 0; - diff_val = (fg_val - bg_val) & 0xf; - add_val = bg_val + (bg_val << 8) + (bg_val << 16) + (bg_val << 24); - and_val = diff_val + (diff_val << 8) + (diff_val << 16) +(diff_val<<24); - ff_val = 0x0f0f0f0f; + diff_val = (fg_val - bg_val) & 0xf; + add_val = bg_val + (bg_val << 8) + (bg_val << 16) + (bg_val << 24); + and_val = diff_val + (diff_val << 8) + (diff_val << 16) +(diff_val<<24); + ff_val = 0x0f0f0f0f; - flash_state = (g_cur_a2_stat & ALL_STAT_FLASH_STATE); + flash_state = (g_cur_a2_stat & ALL_STAT_FLASH_STATE); - for(x1 = 0; x1 < 40; x1 += shift_per) { - CH_LOOP_A2_VID(ch_mask, ch_tmp); + for(x1 = 0; x1 < 40; x1 += shift_per) { + CH_LOOP_A2_VID(ch_mask, ch_tmp); - left = MIN(x1, left); - right = MAX(x1 + shift_per, right); + left = MIN(x1, left); + right = MAX(x1 + shift_per, right); - slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); - b_ptr = &screen_data[(y*8 + st_line_mod8)*2*pixels_per_line + - x1*14]; - img_ptr = (word32 *)b_ptr; - img_ptr2 = (word32 *)(b_ptr + pixels_per_line); + slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); + b_ptr = &screen_data[(y*8 + st_line_mod8)*2*pixels_per_line + + x1*14]; + img_ptr = (word32 *)b_ptr; + img_ptr2 = (word32 *)(b_ptr + pixels_per_line); - for(x2 = 0; x2 < shift_per; x2 += 2) { - /* do 4 chars at once! */ + for(x2 = 0; x2 < shift_per; x2 += 2) { + /* do 4 chars at once! */ - val1 = slow_mem_ptr[0]; - val3 = slow_mem_ptr[1]; - val0 = slow_mem_ptr[0x10000]; - val2 = slow_mem_ptr[0x10001]; - slow_mem_ptr += 2; + val1 = slow_mem_ptr[0]; + val3 = slow_mem_ptr[1]; + val0 = slow_mem_ptr[0x10000]; + val2 = slow_mem_ptr[0x10001]; + slow_mem_ptr += 2; - if(!altcharset) { - if(val0 >= 0x40 && val0 < 0x80) { - if(flash_state) { - val0 += 0x40; - } else { - val0 -= 0x40; - } - } - if(val1 >= 0x40 && val1 < 0x80) { - if(flash_state) { - val1 += 0x40; - } else { - val1 -= 0x40; - } - } - if(val2 >= 0x40 && val2 < 0x80) { - if(flash_state) { - val2 += 0x40; - } else { - val2 -= 0x40; - } - } - if(val3 >= 0x40 && val3 < 0x80) { - if(flash_state) { - val3 += 0x40; - } else { - val3 -= 0x40; - } - } - } - save_img_ptr = img_ptr; - save_img_ptr2 = img_ptr2; + if(!altcharset) { + if(val0 >= 0x40 && val0 < 0x80) { + if(flash_state) { + val0 += 0x40; + } else { + val0 -= 0x40; + } + } + if(val1 >= 0x40 && val1 < 0x80) { + if(flash_state) { + val1 += 0x40; + } else { + val1 -= 0x40; + } + } + if(val2 >= 0x40 && val2 < 0x80) { + if(flash_state) { + val2 += 0x40; + } else { + val2 -= 0x40; + } + } + if(val3 >= 0x40 && val3 < 0x80) { + if(flash_state) { + val3 += 0x40; + } else { + val3 -= 0x40; + } + } + } + save_img_ptr = img_ptr; + save_img_ptr2 = img_ptr2; - for(i = st_line_mod8; i < 8; i++) { - font_ptr0 = &(g_font80_off0_bits[val0][i][0]); - tmp0 = (font_ptr0[0] & and_val) + add_val; + for(i = st_line_mod8; i < 8; i++) { + font_ptr0 = &(g_font80_off0_bits[val0][i][0]); + tmp0 = (font_ptr0[0] & and_val) + add_val; - font_ptr3 = &(g_font80_off3_bits[val1][i][0]); - tmp1 = ((font_ptr0[1]+font_ptr3[0]) & and_val)+ - add_val; - /* 3 bytes from ptr0, 1 from ptr3 */ - tmp2 = (font_ptr3[1] & and_val) + add_val; + font_ptr3 = &(g_font80_off3_bits[val1][i][0]); + tmp1 = ((font_ptr0[1]+font_ptr3[0]) & and_val)+ + add_val; + /* 3 bytes from ptr0, 1 from ptr3 */ + tmp2 = (font_ptr3[1] & and_val) + add_val; - font_ptr2 = &(g_font80_off2_bits[val2][i][0]); - tmp3 = ((font_ptr3[2]+font_ptr2[0]) & and_val)+ - add_val; - /* 2 bytes from ptr3, 2 from ptr2*/ - tmp4 = (font_ptr2[1] & and_val) + add_val; + font_ptr2 = &(g_font80_off2_bits[val2][i][0]); + tmp3 = ((font_ptr3[2]+font_ptr2[0]) & and_val)+ + add_val; + /* 2 bytes from ptr3, 2 from ptr2*/ + tmp4 = (font_ptr2[1] & and_val) + add_val; - font_ptr1 = &(g_font80_off1_bits[val3][i][0]); - tmp5 = ((font_ptr2[2]+font_ptr1[0]) & and_val)+ - add_val; - /* 1 byte from ptr2, 3 from ptr1 */ - tmp6 = (font_ptr1[1] & and_val) + add_val; + font_ptr1 = &(g_font80_off1_bits[val3][i][0]); + tmp5 = ((font_ptr2[2]+font_ptr1[0]) & and_val)+ + add_val; + /* 1 byte from ptr2, 3 from ptr1 */ + tmp6 = (font_ptr1[1] & and_val) + add_val; - tmp0 = (tmp0 & ff_val) + palette_add; - tmp1 = (tmp1 & ff_val) + palette_add; - tmp2 = (tmp2 & ff_val) + palette_add; - tmp3 = (tmp3 & ff_val) + palette_add; - tmp4 = (tmp4 & ff_val) + palette_add; - tmp5 = (tmp5 & ff_val) + palette_add; - tmp6 = (tmp6 & ff_val) + palette_add; + tmp0 = (tmp0 & ff_val) + palette_add; + tmp1 = (tmp1 & ff_val) + palette_add; + tmp2 = (tmp2 & ff_val) + palette_add; + tmp3 = (tmp3 & ff_val) + palette_add; + tmp4 = (tmp4 & ff_val) + palette_add; + tmp5 = (tmp5 & ff_val) + palette_add; + tmp6 = (tmp6 & ff_val) + palette_add; - img_ptr[0] = tmp0; - img_ptr[1] = tmp1; - img_ptr[2] = tmp2; - img_ptr[3] = tmp3; - img_ptr[4] = tmp4; - img_ptr[5] = tmp5; - img_ptr[6] = tmp6; + img_ptr[0] = tmp0; + img_ptr[1] = tmp1; + img_ptr[2] = tmp2; + img_ptr[3] = tmp3; + img_ptr[4] = tmp4; + img_ptr[5] = tmp5; + img_ptr[6] = tmp6; - img_ptr2[0] = tmp0; - img_ptr2[1] = tmp1; - img_ptr2[2] = tmp2; - img_ptr2[3] = tmp3; - img_ptr2[4] = tmp4; - img_ptr2[5] = tmp5; - img_ptr2[6] = tmp6; + img_ptr2[0] = tmp0; + img_ptr2[1] = tmp1; + img_ptr2[2] = tmp2; + img_ptr2[3] = tmp3; + img_ptr2[4] = tmp4; + img_ptr2[5] = tmp5; + img_ptr2[6] = tmp6; - img_ptr += (2*pixels_per_line)/4; - img_ptr2 += (2*pixels_per_line)/4; - } + img_ptr += (2*pixels_per_line)/4; + img_ptr2 += (2*pixels_per_line)/4; + } - img_ptr = save_img_ptr + 7; - img_ptr2 = save_img_ptr2 + 7; + img_ptr = save_img_ptr + 7; + img_ptr2 = save_img_ptr2 + 7; - } - } + } + } - for(i = 0; i < (8 - st_line_mod8); i++) { - g_a2_line_left_edge[st_line + i] = (left*14); - g_a2_line_right_edge[st_line + i] = (right*14); - } + for(i = 0; i < (8 - st_line_mod8); i++) { + g_a2_line_left_edge[st_line + i] = (left*14); + g_a2_line_right_edge[st_line + i] = (right*14); + } - if(left >= right || left < 0 || right < 0) { - printf("line %d, 80: left >= right: %d >= %d\n", - start_line, left, right); - } + if(left >= right || left < 0 || right < 0) { + printf("line %d, 80: left >= right: %d >= %d\n", + start_line, left, right); + } - g_need_redraw = 0; + g_need_redraw = 0; } -void -redraw_changed_gr(int start_offset, int start_line, int num_lines, int reparse, - byte *screen_data, int pixels_per_line) -{ - word32 *img_ptr; - word32 *save_img_ptr; - word32 *ch_ptr; - byte *b_ptr; - byte *slow_mem_ptr; - word32 mask_per_line; - word32 ch_mask; - word32 ch_tmp; - word32 mem_ptr; - word32 line_mask; - word32 val0, val1; - word32 val0_wd, val1_wd; - word32 val01_wd; - word32 val_even, val_odd; - word32 palette_add; - int half; - int x1, x2; - int y; - int y2; - int ch_bitpos; - int bits_per_line; - int ch_shift_amount; - int shift_per; - int left, right; - int st_line_mod8, st_line, eff_line, end_line; - int i; +void redraw_changed_gr(int start_offset, int start_line, int num_lines, int reparse, + byte *screen_data, int pixels_per_line) { + word32 *img_ptr; + word32 *save_img_ptr; + word32 *ch_ptr; + byte *b_ptr; + byte *slow_mem_ptr; + word32 mask_per_line; + word32 ch_mask; + word32 ch_tmp; + word32 mem_ptr; + word32 line_mask; + word32 val0, val1; + word32 val0_wd, val1_wd; + word32 val01_wd; + word32 val_even, val_odd; + word32 palette_add; + int half; + int x1, x2; + int y; + int y2; + int ch_bitpos; + int bits_per_line; + int ch_shift_amount; + int shift_per; + int left, right; + int st_line_mod8, st_line, eff_line, end_line; + int i; - st_line_mod8 = start_line & 7; - st_line = start_line; - end_line = 8; // st_line_mod8 + num_lines; + st_line_mod8 = start_line & 7; + st_line = start_line; + end_line = 8; // st_line_mod8 + num_lines; - start_line = start_line >> 3; + start_line = start_line >> 3; - y = start_line; - line_mask = 1 << y; - mem_ptr = 0x400 + g_screen_index[y] + start_offset; - if(mem_ptr < 0x400 || mem_ptr >= 0xc00) { - printf("redraw_changed_gr: mem_ptr: %08x\n", mem_ptr); - } + y = start_line; + line_mask = 1 << y; + mem_ptr = 0x400 + g_screen_index[y] + start_offset; + if(mem_ptr < 0x400 || mem_ptr >= 0xc00) { + printf("redraw_changed_gr: mem_ptr: %08x\n", mem_ptr); + } - CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, (st_line_mod8 == 0)); + CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, (st_line_mod8 == 0)); - if(ch_mask == 0) { - return; - } + if(ch_mask == 0) { + return; + } - shift_per = (1 << SHIFT_PER_CHANGE); + shift_per = (1 << SHIFT_PER_CHANGE); - g_a2_screen_buffer_changed |= line_mask; + g_a2_screen_buffer_changed |= line_mask; - palette_add = (g_a2vid_palette << 4); - palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + - (palette_add << 24); + palette_add = (g_a2vid_palette << 4); + palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + + (palette_add << 24); - left = 40; - right = 0; + left = 40; + right = 0; - for(x1 = 0; x1 < 40; x1 += shift_per) { - CH_LOOP_A2_VID(ch_mask, ch_tmp); + for(x1 = 0; x1 < 40; x1 += shift_per) { + CH_LOOP_A2_VID(ch_mask, ch_tmp); - left = MIN(x1, left); - right = MAX(x1 + shift_per, right); + left = MIN(x1, left); + right = MAX(x1 + shift_per, right); - slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); - b_ptr = &screen_data[(y*8 + st_line_mod8)*2*pixels_per_line + - x1*14]; - img_ptr = (word32 *)b_ptr; + slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); + b_ptr = &screen_data[(y*8 + st_line_mod8)*2*pixels_per_line + + x1*14]; + img_ptr = (word32 *)b_ptr; - for(x2 = 0; x2 < shift_per; x2 += 2) { - val_even = *slow_mem_ptr++; - val_odd = *slow_mem_ptr++; + for(x2 = 0; x2 < shift_per; x2 += 2) { + val_even = *slow_mem_ptr++; + val_odd = *slow_mem_ptr++; - save_img_ptr = img_ptr; + save_img_ptr = img_ptr; - for(half = 0; half < 2; half++) { - val0 = val_even & 0xf; - val1 = val_odd & 0xf; - val0_wd = (val0 << 24) + (val0 << 16) + - (val0 << 8) + val0; - val1_wd = (val1 << 24) + (val1 << 16) + - (val1 << 8) + val1; + for(half = 0; half < 2; half++) { + val0 = val_even & 0xf; + val1 = val_odd & 0xf; + val0_wd = (val0 << 24) + (val0 << 16) + + (val0 << 8) + val0; + val1_wd = (val1 << 24) + (val1 << 16) + + (val1 << 8) + val1; #if defined(GSPLUS_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) // OSX needs to calculate endianness mid-compilation, can't be passed on compile command - val01_wd = (val1_wd << 16) + (val0_wd & 0xffff); + val01_wd = (val1_wd << 16) + (val0_wd & 0xffff); #else - val01_wd = (val0_wd << 16) + (val1_wd & 0xffff); + val01_wd = (val0_wd << 16) + (val1_wd & 0xffff); #endif - for(y2 = 0; y2 < 8; y2++) { - eff_line = half*4 + (y2 >> 1); - if((eff_line < st_line_mod8) || - (eff_line > end_line)) { - continue; - } + for(y2 = 0; y2 < 8; y2++) { + eff_line = half*4 + (y2 >> 1); + if((eff_line < st_line_mod8) || + (eff_line > end_line)) { + continue; + } - img_ptr[0] = val0_wd + palette_add; - img_ptr[1] = val0_wd + palette_add; - img_ptr[2] = val0_wd + palette_add; - img_ptr[3] = val01_wd + palette_add; - img_ptr[4] = val1_wd + palette_add; - img_ptr[5] = val1_wd + palette_add; - img_ptr[6] = val1_wd + palette_add; - img_ptr += (pixels_per_line)/4; - } + img_ptr[0] = val0_wd + palette_add; + img_ptr[1] = val0_wd + palette_add; + img_ptr[2] = val0_wd + palette_add; + img_ptr[3] = val01_wd + palette_add; + img_ptr[4] = val1_wd + palette_add; + img_ptr[5] = val1_wd + palette_add; + img_ptr[6] = val1_wd + palette_add; + img_ptr += (pixels_per_line)/4; + } - val_even = val_even >> 4; - val_odd = val_odd >> 4; - } + val_even = val_even >> 4; + val_odd = val_odd >> 4; + } - img_ptr = save_img_ptr + 7; - } - } + img_ptr = save_img_ptr + 7; + } + } - for(i = 0; i < (8 - st_line_mod8); i++) { - g_a2_line_left_edge[st_line + i] = (left*14); - g_a2_line_right_edge[st_line + i] = (right*14); - } + for(i = 0; i < (8 - st_line_mod8); i++) { + g_a2_line_left_edge[st_line + i] = (left*14); + g_a2_line_right_edge[st_line + i] = (right*14); + } - g_need_redraw = 0; + g_need_redraw = 0; } -void -redraw_changed_dbl_gr(int start_offset, int start_line, int num_lines, - int reparse, byte *screen_data, int pixels_per_line) -{ - word32 *img_ptr; - word32 *save_img_ptr; - word32 *ch_ptr; - byte *b_ptr; - byte *slow_mem_ptr; - word32 mask_per_line; - word32 ch_mask; - word32 ch_tmp; - word32 mem_ptr; - word32 line_mask; - word32 val0, val1, val2, val3; - word32 val0_wd, val1_wd, val2_wd, val3_wd; - word32 val01_wd, val12_wd, val23_wd; - word32 val_even_main, val_odd_main; - word32 val_even_aux, val_odd_aux; - word32 palette_add; - int half; - int x1, x2; - int y; - int y2; - int ch_bitpos; - int bits_per_line; - int ch_shift_amount; - int shift_per; - int left, right; - int st_line_mod8, st_line, eff_line, end_line; - int i; +void redraw_changed_dbl_gr(int start_offset, int start_line, int num_lines, + int reparse, byte *screen_data, int pixels_per_line) { + word32 *img_ptr; + word32 *save_img_ptr; + word32 *ch_ptr; + byte *b_ptr; + byte *slow_mem_ptr; + word32 mask_per_line; + word32 ch_mask; + word32 ch_tmp; + word32 mem_ptr; + word32 line_mask; + word32 val0, val1, val2, val3; + word32 val0_wd, val1_wd, val2_wd, val3_wd; + word32 val01_wd, val12_wd, val23_wd; + word32 val_even_main, val_odd_main; + word32 val_even_aux, val_odd_aux; + word32 palette_add; + int half; + int x1, x2; + int y; + int y2; + int ch_bitpos; + int bits_per_line; + int ch_shift_amount; + int shift_per; + int left, right; + int st_line_mod8, st_line, eff_line, end_line; + int i; - st_line_mod8 = start_line & 7; - end_line = 8; // st_line_mod8 + num_lines - st_line = start_line; + st_line_mod8 = start_line & 7; + end_line = 8; // st_line_mod8 + num_lines + st_line = start_line; - start_line = start_line >> 3; + start_line = start_line >> 3; - y = start_line; - line_mask = 1 << y; - mem_ptr = 0x400 + g_screen_index[y] + start_offset; - if(mem_ptr < 0x400 || mem_ptr >= 0xc00) { - printf("redraw_changed_dbl_gr: mem_ptr: %08x\n", mem_ptr); - } + y = start_line; + line_mask = 1 << y; + mem_ptr = 0x400 + g_screen_index[y] + start_offset; + if(mem_ptr < 0x400 || mem_ptr >= 0xc00) { + printf("redraw_changed_dbl_gr: mem_ptr: %08x\n", mem_ptr); + } - CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, (st_line_mod8 == 0)); + CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, (st_line_mod8 == 0)); - if(ch_mask == 0) { - return; - } + if(ch_mask == 0) { + return; + } - shift_per = (1 << SHIFT_PER_CHANGE); + shift_per = (1 << SHIFT_PER_CHANGE); - g_a2_screen_buffer_changed |= line_mask; + g_a2_screen_buffer_changed |= line_mask; - palette_add = (g_a2vid_palette << 4); - palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + - (palette_add << 24); + palette_add = (g_a2vid_palette << 4); + palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + + (palette_add << 24); - left = 40; - right = 0; + left = 40; + right = 0; - for(x1 = 0; x1 < 40; x1 += shift_per) { - CH_LOOP_A2_VID(ch_mask, ch_tmp); + for(x1 = 0; x1 < 40; x1 += shift_per) { + CH_LOOP_A2_VID(ch_mask, ch_tmp); - left = MIN(x1, left); - right = MAX(x1 + shift_per, right); + left = MIN(x1, left); + right = MAX(x1 + shift_per, right); - slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); - b_ptr = &screen_data[(y*8 + st_line_mod8)*2*pixels_per_line + - x1*14]; - img_ptr = (word32 *)b_ptr; + slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); + b_ptr = &screen_data[(y*8 + st_line_mod8)*2*pixels_per_line + + x1*14]; + img_ptr = (word32 *)b_ptr; - for(x2 = 0; x2 < shift_per; x2 += 2) { - val_even_main = slow_mem_ptr[0]; - val_odd_main = slow_mem_ptr[1]; - val_even_aux = slow_mem_ptr[0x10000]; - val_odd_aux = slow_mem_ptr[0x10001]; - slow_mem_ptr += 2; + for(x2 = 0; x2 < shift_per; x2 += 2) { + val_even_main = slow_mem_ptr[0]; + val_odd_main = slow_mem_ptr[1]; + val_even_aux = slow_mem_ptr[0x10000]; + val_odd_aux = slow_mem_ptr[0x10001]; + slow_mem_ptr += 2; - save_img_ptr = img_ptr; + save_img_ptr = img_ptr; - for(half = 0; half < 2; half++) { - val0 = val_even_aux & 0xf; - val1 = val_even_main & 0xf; - val2 = val_odd_aux & 0xf; - val3 = val_odd_main & 0xf; + for(half = 0; half < 2; half++) { + val0 = val_even_aux & 0xf; + val1 = val_even_main & 0xf; + val2 = val_odd_aux & 0xf; + val3 = val_odd_main & 0xf; - /* Handle funny pattern of dbl gr aux mem */ - val0 = ((val0 << 1) & 0xf) + (val0 >> 3); - val2 = ((val2 << 1) & 0xf) + (val2 >> 3); + /* Handle funny pattern of dbl gr aux mem */ + val0 = ((val0 << 1) & 0xf) + (val0 >> 3); + val2 = ((val2 << 1) & 0xf) + (val2 >> 3); - val0_wd = (val0 << 24) + (val0 << 16) + - (val0 << 8) + val0; - val1_wd = (val1 << 24) + (val1 << 16) + - (val1 << 8) + val1; - val2_wd = (val2 << 24) + (val2 << 16) + - (val2 << 8) + val2; - val3_wd = (val3 << 24) + (val3 << 16) + - (val3 << 8) + val3; + val0_wd = (val0 << 24) + (val0 << 16) + + (val0 << 8) + val0; + val1_wd = (val1 << 24) + (val1 << 16) + + (val1 << 8) + val1; + val2_wd = (val2 << 24) + (val2 << 16) + + (val2 << 8) + val2; + val3_wd = (val3 << 24) + (val3 << 16) + + (val3 << 8) + val3; #if defined(GSPLUS_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) // OSX needs to calculate endianness mid-compilation, can't be passed on compile command - val01_wd = (val1_wd << 24) + (val0_wd&0xffffff); - val12_wd = (val2_wd << 16) + (val1_wd & 0xffff); - val23_wd = (val3_wd << 8) + (val2_wd & 0xff); + val01_wd = (val1_wd << 24) + (val0_wd&0xffffff); + val12_wd = (val2_wd << 16) + (val1_wd & 0xffff); + val23_wd = (val3_wd << 8) + (val2_wd & 0xff); #else - val01_wd = (val0_wd << 8) + (val1_wd & 0xff); - val12_wd = (val1_wd << 16) + (val2_wd & 0xffff); - val23_wd = (val2_wd << 24) + (val3_wd&0xffffff); + val01_wd = (val0_wd << 8) + (val1_wd & 0xff); + val12_wd = (val1_wd << 16) + (val2_wd & 0xffff); + val23_wd = (val2_wd << 24) + (val3_wd&0xffffff); #endif - for(y2 = 0; y2 < 8; y2++) { - eff_line = half*4 + (y2 >> 1); - if((eff_line < st_line_mod8) || - (eff_line > end_line)) { - continue; - } - img_ptr[0] = val0_wd + palette_add; - img_ptr[1] = val01_wd + palette_add; - img_ptr[2] = val1_wd + palette_add; - img_ptr[3] = val12_wd + palette_add; - img_ptr[4] = val2_wd + palette_add; - img_ptr[5] = val23_wd + palette_add; - img_ptr[6] = val3_wd + palette_add; - img_ptr += (pixels_per_line)/4; - } + for(y2 = 0; y2 < 8; y2++) { + eff_line = half*4 + (y2 >> 1); + if((eff_line < st_line_mod8) || + (eff_line > end_line)) { + continue; + } + img_ptr[0] = val0_wd + palette_add; + img_ptr[1] = val01_wd + palette_add; + img_ptr[2] = val1_wd + palette_add; + img_ptr[3] = val12_wd + palette_add; + img_ptr[4] = val2_wd + palette_add; + img_ptr[5] = val23_wd + palette_add; + img_ptr[6] = val3_wd + palette_add; + img_ptr += (pixels_per_line)/4; + } - val_even_aux = val_even_aux >> 4; - val_even_main = val_even_main >> 4; - val_odd_aux = val_odd_aux >> 4; - val_odd_main = val_odd_main >> 4; - } + val_even_aux = val_even_aux >> 4; + val_even_main = val_even_main >> 4; + val_odd_aux = val_odd_aux >> 4; + val_odd_main = val_odd_main >> 4; + } - img_ptr = save_img_ptr + 7; - } - } + img_ptr = save_img_ptr + 7; + } + } - for(i = 0; i < (8 - st_line_mod8); i++) { - g_a2_line_left_edge[st_line + i] = (left*14); - g_a2_line_right_edge[st_line + i] = (right*14); - } + for(i = 0; i < (8 - st_line_mod8); i++) { + g_a2_line_left_edge[st_line + i] = (left*14); + g_a2_line_right_edge[st_line + i] = (right*14); + } - g_need_redraw = 0; + g_need_redraw = 0; } -void -redraw_changed_hires(int start_offset, int start_line, int num_lines, - int color, int reparse, byte *screen_data, int pixels_per_line) -{ - if(!color) { - redraw_changed_hires_color(start_offset, start_line, num_lines, - reparse, screen_data, pixels_per_line); - } else { - redraw_changed_hires_bw(start_offset, start_line, num_lines, - reparse, screen_data, pixels_per_line); - } +void redraw_changed_hires(int start_offset, int start_line, int num_lines, + int color, int reparse, byte *screen_data, int pixels_per_line) { + if(!color) { + redraw_changed_hires_color(start_offset, start_line, num_lines, + reparse, screen_data, pixels_per_line); + } else { + redraw_changed_hires_bw(start_offset, start_line, num_lines, + reparse, screen_data, pixels_per_line); + } } -void -redraw_changed_hires_bw(int start_offset, int start_line, int num_lines, - int reparse, byte *screen_data, int pixels_per_line) -{ - word32 *img_ptr, *img_ptr2; - word32 *ch_ptr; - byte *b_ptr; - byte *slow_mem_ptr; - word32 mask_per_line; - word32 ch_mask; - word32 ch_tmp; - word32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; - word32 mem_ptr; - word32 val0, val1; - word32 val_whole; - word32 line_mask; - word32 palette_add; - int y; - int x1, x2; - int ch_bitpos; - int bits_per_line; - int ch_shift_amount; - int shift_per; - int left, right; - int st_line; - int i; +void redraw_changed_hires_bw(int start_offset, int start_line, int num_lines, + int reparse, byte *screen_data, int pixels_per_line) { + word32 *img_ptr, *img_ptr2; + word32 *ch_ptr; + byte *b_ptr; + byte *slow_mem_ptr; + word32 mask_per_line; + word32 ch_mask; + word32 ch_tmp; + word32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + word32 mem_ptr; + word32 val0, val1; + word32 val_whole; + word32 line_mask; + word32 palette_add; + int y; + int x1, x2; + int ch_bitpos; + int bits_per_line; + int ch_shift_amount; + int shift_per; + int left, right; + int st_line; + int i; - st_line = start_line; - start_line = start_line >> 3; + st_line = start_line; + start_line = start_line >> 3; - palette_add = (g_a2vid_palette << 4); - palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + - (palette_add << 24); + palette_add = (g_a2vid_palette << 4); + palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + + (palette_add << 24); - left = 40; - right = 0; + left = 40; + right = 0; - for(y = st_line; y < (st_line + num_lines); y++) { - line_mask = 1 << (y >> 3); - mem_ptr = 0x2000 + (((y & 7) * 0x400) + - g_screen_index[y >> 3]) + start_offset; + for(y = st_line; y < (st_line + num_lines); y++) { + line_mask = 1 << (y >> 3); + mem_ptr = 0x2000 + (((y & 7) * 0x400) + + g_screen_index[y >> 3]) + start_offset; - CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, 1); + CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, 1); - if(ch_mask == 0) { - continue; - } + if(ch_mask == 0) { + continue; + } - /* Hires depends on adjacent bits, so also reparse adjacent */ - /* regions so that if bits on the edge change, redrawing is */ - /* correct */ - ch_mask = ch_mask | (ch_mask >> 1) | (ch_mask << 1); + /* Hires depends on adjacent bits, so also reparse adjacent */ + /* regions so that if bits on the edge change, redrawing is */ + /* correct */ + ch_mask = ch_mask | (ch_mask >> 1) | (ch_mask << 1); - shift_per = (1 << SHIFT_PER_CHANGE); + shift_per = (1 << SHIFT_PER_CHANGE); - g_a2_screen_buffer_changed |= line_mask; + g_a2_screen_buffer_changed |= line_mask; - for(x1 = 0; x1 < 40; x1 += shift_per) { - CH_LOOP_A2_VID(ch_mask, ch_tmp); + for(x1 = 0; x1 < 40; x1 += shift_per) { + CH_LOOP_A2_VID(ch_mask, ch_tmp); - left = MIN(x1, left); - right = MAX(x1 + shift_per, right); + left = MIN(x1, left); + right = MAX(x1 + shift_per, right); - slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); - b_ptr = &screen_data[(y*2)*pixels_per_line + x1*14]; - img_ptr = (word32 *)b_ptr; - img_ptr2 = (word32 *)(b_ptr + pixels_per_line); + slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); + b_ptr = &screen_data[(y*2)*pixels_per_line + x1*14]; + img_ptr = (word32 *)b_ptr; + img_ptr2 = (word32 *)(b_ptr + pixels_per_line); - for(x2 = 0; x2 < shift_per; x2 += 2) { - val0 = *slow_mem_ptr++; - val1 = *slow_mem_ptr++; + for(x2 = 0; x2 < shift_per; x2 += 2) { + val0 = *slow_mem_ptr++; + val1 = *slow_mem_ptr++; - val_whole = ((val1 & 0x7f) << 7) +(val0 & 0x7f); + val_whole = ((val1 & 0x7f) << 7) +(val0 & 0x7f); - tmp0 = g_bw_hires_convert[val_whole & 3]; - val_whole = val_whole >> 2; - tmp1 = g_bw_hires_convert[val_whole & 3]; - val_whole = val_whole >> 2; - tmp2 = g_bw_hires_convert[val_whole & 3]; - val_whole = val_whole >> 2; - tmp3 = g_bw_hires_convert[val_whole & 3]; - val_whole = val_whole >> 2; - tmp4 = g_bw_hires_convert[val_whole & 3]; - val_whole = val_whole >> 2; - tmp5 = g_bw_hires_convert[val_whole & 3]; - val_whole = val_whole >> 2; - tmp6 = g_bw_hires_convert[val_whole & 3]; + tmp0 = g_bw_hires_convert[val_whole & 3]; + val_whole = val_whole >> 2; + tmp1 = g_bw_hires_convert[val_whole & 3]; + val_whole = val_whole >> 2; + tmp2 = g_bw_hires_convert[val_whole & 3]; + val_whole = val_whole >> 2; + tmp3 = g_bw_hires_convert[val_whole & 3]; + val_whole = val_whole >> 2; + tmp4 = g_bw_hires_convert[val_whole & 3]; + val_whole = val_whole >> 2; + tmp5 = g_bw_hires_convert[val_whole & 3]; + val_whole = val_whole >> 2; + tmp6 = g_bw_hires_convert[val_whole & 3]; - img_ptr[0] = tmp0 + palette_add; - img_ptr[1] = tmp1 + palette_add; - img_ptr[2] = tmp2 + palette_add; - img_ptr[3] = tmp3 + palette_add; - img_ptr[4] = tmp4 + palette_add; - img_ptr[5] = tmp5 + palette_add; - img_ptr[6] = tmp6 + palette_add; + img_ptr[0] = tmp0 + palette_add; + img_ptr[1] = tmp1 + palette_add; + img_ptr[2] = tmp2 + palette_add; + img_ptr[3] = tmp3 + palette_add; + img_ptr[4] = tmp4 + palette_add; + img_ptr[5] = tmp5 + palette_add; + img_ptr[6] = tmp6 + palette_add; - img_ptr2[0] = tmp0 + palette_add; - img_ptr2[1] = tmp1 + palette_add; - img_ptr2[2] = tmp2 + palette_add; - img_ptr2[3] = tmp3 + palette_add; - img_ptr2[4] = tmp4 + palette_add; - img_ptr2[5] = tmp5 + palette_add; - img_ptr2[6] = tmp6 + palette_add; + img_ptr2[0] = tmp0 + palette_add; + img_ptr2[1] = tmp1 + palette_add; + img_ptr2[2] = tmp2 + palette_add; + img_ptr2[3] = tmp3 + palette_add; + img_ptr2[4] = tmp4 + palette_add; + img_ptr2[5] = tmp5 + palette_add; + img_ptr2[6] = tmp6 + palette_add; - img_ptr += 7; - img_ptr2 += 7; - } - } - } + img_ptr += 7; + img_ptr2 += 7; + } + } + } - for(i = 0; i < num_lines; i++) { - g_a2_line_left_edge[st_line + i] = (left*14); - g_a2_line_right_edge[st_line + i] = (right*14); - } + for(i = 0; i < num_lines; i++) { + g_a2_line_left_edge[st_line + i] = (left*14); + g_a2_line_right_edge[st_line + i] = (right*14); + } - g_need_redraw = 0; + g_need_redraw = 0; } -void -redraw_changed_hires_color(int start_offset, int start_line, int num_lines, - int reparse, byte *screen_data, int pixels_per_line) -{ - word32 *img_ptr, *img_ptr2; - word32 *ch_ptr; - byte *b_ptr; - byte *slow_mem_ptr; - word32 mask_per_line; - word32 ch_mask; - word32 ch_tmp; - word32 mem_ptr; - word32 val0, val1; - word32 val_whole; - word32 pix_val; - word32 line_mask; - word32 prev_pixel; - word32 prev_hi; - word32 loc_hi; - word32 val_hi; - word32 tmp_val; - word32 palette_add; - int y; - int x1, x2; - int ch_bitpos; - int bits_per_line; - int ch_shift_amount; - int shift_per; - int left, right; - int st_line; - int i, j; +void redraw_changed_hires_color(int start_offset, int start_line, int num_lines, + int reparse, byte *screen_data, int pixels_per_line) { + word32 *img_ptr, *img_ptr2; + word32 *ch_ptr; + byte *b_ptr; + byte *slow_mem_ptr; + word32 mask_per_line; + word32 ch_mask; + word32 ch_tmp; + word32 mem_ptr; + word32 val0, val1; + word32 val_whole; + word32 pix_val; + word32 line_mask; + word32 prev_pixel; + word32 prev_hi; + word32 loc_hi; + word32 val_hi; + word32 tmp_val; + word32 palette_add; + int y; + int x1, x2; + int ch_bitpos; + int bits_per_line; + int ch_shift_amount; + int shift_per; + int left, right; + int st_line; + int i, j; - st_line = start_line; + st_line = start_line; - start_line = start_line >> 3; + start_line = start_line >> 3; - palette_add = (g_a2vid_palette << 4); - palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + - (palette_add << 24); + palette_add = (g_a2vid_palette << 4); + palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + + (palette_add << 24); - left = 40; - right = 0; + left = 40; + right = 0; - for(y = st_line; y < (st_line + num_lines); y++) { - line_mask = 1 << (y >> 3); - mem_ptr = 0x2000 + (((y & 7) * 0x400) + - g_screen_index[y >> 3]) + start_offset; + for(y = st_line; y < (st_line + num_lines); y++) { + line_mask = 1 << (y >> 3); + mem_ptr = 0x2000 + (((y & 7) * 0x400) + + g_screen_index[y >> 3]) + start_offset; - CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, 1); + CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, 1); - if(ch_mask == 0) { - continue; - } + if(ch_mask == 0) { + continue; + } - /* Hires depends on adjacent bits, so also reparse adjacent */ - /* regions so that if bits on the edge change, redrawing is */ - /* correct */ - ch_mask = ch_mask | (ch_mask >> 1) | (ch_mask << 1); + /* Hires depends on adjacent bits, so also reparse adjacent */ + /* regions so that if bits on the edge change, redrawing is */ + /* correct */ + ch_mask = ch_mask | (ch_mask >> 1) | (ch_mask << 1); - shift_per = (1 << SHIFT_PER_CHANGE); + shift_per = (1 << SHIFT_PER_CHANGE); - g_a2_screen_buffer_changed |= line_mask; + g_a2_screen_buffer_changed |= line_mask; - for(x1 = 0; x1 < 40; x1 += shift_per) { + for(x1 = 0; x1 < 40; x1 += shift_per) { - CH_LOOP_A2_VID(ch_mask, ch_tmp); + CH_LOOP_A2_VID(ch_mask, ch_tmp); - left = MIN(x1, left); - right = MAX(x1 + shift_per, right); + left = MIN(x1, left); + right = MAX(x1 + shift_per, right); - slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); - b_ptr = &screen_data[(y*2)*pixels_per_line + x1*14]; - img_ptr = (word32 *)b_ptr; - img_ptr2 = (word32 *)(b_ptr + pixels_per_line); + slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); + b_ptr = &screen_data[(y*2)*pixels_per_line + x1*14]; + img_ptr = (word32 *)b_ptr; + img_ptr2 = (word32 *)(b_ptr + pixels_per_line); - prev_pixel = 0; - prev_hi = 0; + prev_pixel = 0; + prev_hi = 0; - if(x1 > 0) { - tmp_val = slow_mem_ptr[-1]; - prev_pixel = (tmp_val >> 6) & 1; - prev_hi = (tmp_val >> 7) & 0x1; - } + if(x1 > 0) { + tmp_val = slow_mem_ptr[-1]; + prev_pixel = (tmp_val >> 6) & 1; + prev_hi = (tmp_val >> 7) & 0x1; + } - for(x2 = 0; x2 < shift_per; x2 += 2) { - val0 = *slow_mem_ptr++; - val1 = *slow_mem_ptr++; + for(x2 = 0; x2 < shift_per; x2 += 2) { + val0 = *slow_mem_ptr++; + val1 = *slow_mem_ptr++; - val_whole = ((val1 & 0x7f) << 8) + - ((val0 & 0x7f) << 1) + - prev_pixel; + val_whole = ((val1 & 0x7f) << 8) + + ((val0 & 0x7f) << 1) + + prev_pixel; - loc_hi = prev_hi; - if(((val1 >> 7) & 1) != 0) { - loc_hi += 0x7f00; - } - if(((val0 >> 7) & 1) != 0) { - loc_hi += 0xfe; - } + loc_hi = prev_hi; + if(((val1 >> 7) & 1) != 0) { + loc_hi += 0x7f00; + } + if(((val0 >> 7) & 1) != 0) { + loc_hi += 0xfe; + } - prev_pixel = (val1 >> 6) & 1; - prev_hi = (val1 >> 7) & 1; - if((x1 + x2 + 2) < 40) { - tmp_val = slow_mem_ptr[0]; - if(tmp_val & 1) { - val_whole |= 0x8000; - } - if(tmp_val & 0x80) { - loc_hi |= 0x8000; - } - } + prev_pixel = (val1 >> 6) & 1; + prev_hi = (val1 >> 7) & 1; + if((x1 + x2 + 2) < 40) { + tmp_val = slow_mem_ptr[0]; + if(tmp_val & 1) { + val_whole |= 0x8000; + } + if(tmp_val & 0x80) { + loc_hi |= 0x8000; + } + } - loc_hi = loc_hi >> 1; + loc_hi = loc_hi >> 1; - for(j = 0; j < 7; j++) { - tmp_val = val_whole & 0xf; - val_hi = loc_hi & 0x3; + for(j = 0; j < 7; j++) { + tmp_val = val_whole & 0xf; + val_hi = loc_hi & 0x3; - pix_val = g_hires_convert[(val_hi<<4) + - tmp_val]; - *img_ptr++ = pix_val + palette_add; - *img_ptr2++ = pix_val + palette_add; - val_whole = val_whole >> 2; - loc_hi = loc_hi >> 2; - } - } - } - } + pix_val = g_hires_convert[(val_hi<<4) + + tmp_val]; + *img_ptr++ = pix_val + palette_add; + *img_ptr2++ = pix_val + palette_add; + val_whole = val_whole >> 2; + loc_hi = loc_hi >> 2; + } + } + } + } - for(i = 0; i < num_lines; i++) { - g_a2_line_left_edge[st_line + i] = (left*14); - g_a2_line_right_edge[st_line + i] = (right*14); - } + for(i = 0; i < num_lines; i++) { + g_a2_line_left_edge[st_line + i] = (left*14); + g_a2_line_right_edge[st_line + i] = (right*14); + } - g_need_redraw = 0; + g_need_redraw = 0; } -void -redraw_changed_dbl_hires(int start_offset, int start_line, int num_lines, - int color, int reparse, byte *screen_data, int pixels_per_line) -{ - if(!color) { - redraw_changed_dbl_hires_color(start_offset, start_line, - num_lines, reparse, screen_data, pixels_per_line); - } else { - redraw_changed_dbl_hires_bw(start_offset, start_line, - num_lines, reparse, screen_data, pixels_per_line); - } +void redraw_changed_dbl_hires(int start_offset, int start_line, int num_lines, + int color, int reparse, byte *screen_data, int pixels_per_line) { + if(!color) { + redraw_changed_dbl_hires_color(start_offset, start_line, + num_lines, reparse, screen_data, pixels_per_line); + } else { + redraw_changed_dbl_hires_bw(start_offset, start_line, + num_lines, reparse, screen_data, pixels_per_line); + } } -void -redraw_changed_dbl_hires_bw(int start_offset, int start_line, int num_lines, - int reparse, byte *screen_data, int pixels_per_line) -{ - word32 *img_ptr, *img_ptr2; - word32 *ch_ptr; - byte *b_ptr; - byte *slow_mem_ptr; - word32 mask_per_line; - word32 ch_mask; - word32 ch_tmp; - word32 mem_ptr; - word32 val0, val1, val2, val3; - word32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; - word32 val_whole; - word32 line_mask; - word32 palette_add; - int y; - int x1, x2; - int ch_bitpos; - int bits_per_line; - int ch_shift_amount; - int shift_per; - int left, right; - int st_line; - int i; +void redraw_changed_dbl_hires_bw(int start_offset, int start_line, int num_lines, + int reparse, byte *screen_data, int pixels_per_line) { + word32 *img_ptr, *img_ptr2; + word32 *ch_ptr; + byte *b_ptr; + byte *slow_mem_ptr; + word32 mask_per_line; + word32 ch_mask; + word32 ch_tmp; + word32 mem_ptr; + word32 val0, val1, val2, val3; + word32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + word32 val_whole; + word32 line_mask; + word32 palette_add; + int y; + int x1, x2; + int ch_bitpos; + int bits_per_line; + int ch_shift_amount; + int shift_per; + int left, right; + int st_line; + int i; - st_line = start_line; - start_line = start_line >> 3; + st_line = start_line; + start_line = start_line >> 3; - palette_add = (g_a2vid_palette << 4); - palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + - (palette_add << 24); + palette_add = (g_a2vid_palette << 4); + palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + + (palette_add << 24); - left = 40; - right = 0; + left = 40; + right = 0; - for(y = st_line; y < (st_line + num_lines); y++) { - line_mask = 1 << (y >> 3); - mem_ptr = 0x2000 + (((y & 7) * 0x400) + g_screen_index[y >> 3] + - start_offset); + for(y = st_line; y < (st_line + num_lines); y++) { + line_mask = 1 << (y >> 3); + mem_ptr = 0x2000 + (((y & 7) * 0x400) + g_screen_index[y >> 3] + + start_offset); - CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, 1); + CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, 1); - if(ch_mask == 0) { - continue; - } + if(ch_mask == 0) { + continue; + } - shift_per = (1 << SHIFT_PER_CHANGE); + shift_per = (1 << SHIFT_PER_CHANGE); - g_a2_screen_buffer_changed |= line_mask; + g_a2_screen_buffer_changed |= line_mask; - for(x1 = 0; x1 < 40; x1 += shift_per) { + for(x1 = 0; x1 < 40; x1 += shift_per) { - CH_LOOP_A2_VID(ch_mask, ch_tmp); + CH_LOOP_A2_VID(ch_mask, ch_tmp); - left = MIN(x1, left); - right = MAX(x1 + shift_per, right); + left = MIN(x1, left); + right = MAX(x1 + shift_per, right); - slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); - b_ptr = &screen_data[(y*2)*pixels_per_line + x1*14]; - img_ptr = (word32 *)b_ptr; - img_ptr2 = (word32 *)(b_ptr + pixels_per_line); + slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); + b_ptr = &screen_data[(y*2)*pixels_per_line + x1*14]; + img_ptr = (word32 *)b_ptr; + img_ptr2 = (word32 *)(b_ptr + pixels_per_line); - for(x2 = 0; x2 < shift_per; x2 += 2) { - val0 = slow_mem_ptr[0x10000]; - val1 = slow_mem_ptr[0]; - val2 = slow_mem_ptr[0x10001]; - val3 = slow_mem_ptr[1]; - slow_mem_ptr += 2; + for(x2 = 0; x2 < shift_per; x2 += 2) { + val0 = slow_mem_ptr[0x10000]; + val1 = slow_mem_ptr[0]; + val2 = slow_mem_ptr[0x10001]; + val3 = slow_mem_ptr[1]; + slow_mem_ptr += 2; - val_whole = ((val3 & 0x7f) << 21) + - ((val2 & 0x7f) << 14) + - ((val1 & 0x7f) << 7) + - (val0 & 0x7f); + val_whole = ((val3 & 0x7f) << 21) + + ((val2 & 0x7f) << 14) + + ((val1 & 0x7f) << 7) + + (val0 & 0x7f); - tmp0 = g_bw_dhires_convert[val_whole & 0xf]; - val_whole = val_whole >> 4; - tmp1 = g_bw_dhires_convert[val_whole & 0xf]; - val_whole = val_whole >> 4; - tmp2 = g_bw_dhires_convert[val_whole & 0xf]; - val_whole = val_whole >> 4; - tmp3 = g_bw_dhires_convert[val_whole & 0xf]; - val_whole = val_whole >> 4; - tmp4 = g_bw_dhires_convert[val_whole & 0xf]; - val_whole = val_whole >> 4; - tmp5 = g_bw_dhires_convert[val_whole & 0xf]; - val_whole = val_whole >> 4; - tmp6 = g_bw_dhires_convert[val_whole & 0xf]; + tmp0 = g_bw_dhires_convert[val_whole & 0xf]; + val_whole = val_whole >> 4; + tmp1 = g_bw_dhires_convert[val_whole & 0xf]; + val_whole = val_whole >> 4; + tmp2 = g_bw_dhires_convert[val_whole & 0xf]; + val_whole = val_whole >> 4; + tmp3 = g_bw_dhires_convert[val_whole & 0xf]; + val_whole = val_whole >> 4; + tmp4 = g_bw_dhires_convert[val_whole & 0xf]; + val_whole = val_whole >> 4; + tmp5 = g_bw_dhires_convert[val_whole & 0xf]; + val_whole = val_whole >> 4; + tmp6 = g_bw_dhires_convert[val_whole & 0xf]; - img_ptr[0] = tmp0 + palette_add; - img_ptr[1] = tmp1 + palette_add; - img_ptr[2] = tmp2 + palette_add; - img_ptr[3] = tmp3 + palette_add; - img_ptr[4] = tmp4 + palette_add; - img_ptr[5] = tmp5 + palette_add; - img_ptr[6] = tmp6 + palette_add; + img_ptr[0] = tmp0 + palette_add; + img_ptr[1] = tmp1 + palette_add; + img_ptr[2] = tmp2 + palette_add; + img_ptr[3] = tmp3 + palette_add; + img_ptr[4] = tmp4 + palette_add; + img_ptr[5] = tmp5 + palette_add; + img_ptr[6] = tmp6 + palette_add; - img_ptr2[0] = tmp0 + palette_add; - img_ptr2[1] = tmp1 + palette_add; - img_ptr2[2] = tmp2 + palette_add; - img_ptr2[3] = tmp3 + palette_add; - img_ptr2[4] = tmp4 + palette_add; - img_ptr2[5] = tmp5 + palette_add; - img_ptr2[6] = tmp6 + palette_add; + img_ptr2[0] = tmp0 + palette_add; + img_ptr2[1] = tmp1 + palette_add; + img_ptr2[2] = tmp2 + palette_add; + img_ptr2[3] = tmp3 + palette_add; + img_ptr2[4] = tmp4 + palette_add; + img_ptr2[5] = tmp5 + palette_add; + img_ptr2[6] = tmp6 + palette_add; - img_ptr += 7; - img_ptr2 += 7; - } - } - } + img_ptr += 7; + img_ptr2 += 7; + } + } + } - for(i = 0; i < num_lines; i++) { - g_a2_line_left_edge[st_line + i] = (left*14); - g_a2_line_right_edge[st_line + i] = (right*14); - } + for(i = 0; i < num_lines; i++) { + g_a2_line_left_edge[st_line + i] = (left*14); + g_a2_line_right_edge[st_line + i] = (right*14); + } - g_need_redraw = 0; + g_need_redraw = 0; } -void -redraw_changed_dbl_hires_color(int start_offset, int start_line, int num_lines, - int reparse, byte *screen_data, int pixels_per_line) -{ - word32 *ch_ptr; - word32 *img_ptr, *img_ptr2; - byte *slow_mem_ptr; - byte *b_ptr; - word32 mask_per_line; - word32 ch_mask; - word32 ch_tmp; - word32 mem_ptr; - word32 val0, val1, val2, val3; - word32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; - word32 val_whole; - word32 prev_val; - word32 line_mask; - word32 palette_add; - int y; - int x1, x2; - int ch_bitpos; - int bits_per_line; - int ch_shift_amount; - int shift_per; - int left, right; - int st_line; - int i; +void redraw_changed_dbl_hires_color(int start_offset, int start_line, int num_lines, + int reparse, byte *screen_data, int pixels_per_line) { + word32 *ch_ptr; + word32 *img_ptr, *img_ptr2; + byte *slow_mem_ptr; + byte *b_ptr; + word32 mask_per_line; + word32 ch_mask; + word32 ch_tmp; + word32 mem_ptr; + word32 val0, val1, val2, val3; + word32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6; + word32 val_whole; + word32 prev_val; + word32 line_mask; + word32 palette_add; + int y; + int x1, x2; + int ch_bitpos; + int bits_per_line; + int ch_shift_amount; + int shift_per; + int left, right; + int st_line; + int i; - st_line = start_line; - start_line = start_line >> 3; + st_line = start_line; + start_line = start_line >> 3; - palette_add = (g_a2vid_palette << 4); - palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + - (palette_add << 24); + palette_add = (g_a2vid_palette << 4); + palette_add = palette_add + (palette_add << 8) + (palette_add << 16) + + (palette_add << 24); - left = 40; - right = 0; + left = 40; + right = 0; - for(y = st_line; y < (st_line + num_lines); y++) { - line_mask = 1 << (y >> 3); - mem_ptr = 0x2000 + (((y & 7) * 0x400) + g_screen_index[y >> 3] + - start_offset); + for(y = st_line; y < (st_line + num_lines); y++) { + line_mask = 1 << (y >> 3); + mem_ptr = 0x2000 + (((y & 7) * 0x400) + g_screen_index[y >> 3] + + start_offset); - CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, 1); + CH_SETUP_A2_VID(mem_ptr, ch_mask, reparse, 1); - if(ch_mask == 0) { - continue; - } + if(ch_mask == 0) { + continue; + } - /* dbl-hires also depends on adjacent bits, so reparse */ - /* adjacent regions so that if bits on the edge change, */ - /* redrawing is correct */ - ch_mask = ch_mask | (ch_mask >> 1) | (ch_mask << 1); - ch_mask = -1; + /* dbl-hires also depends on adjacent bits, so reparse */ + /* adjacent regions so that if bits on the edge change, */ + /* redrawing is correct */ + ch_mask = ch_mask | (ch_mask >> 1) | (ch_mask << 1); + ch_mask = -1; - shift_per = (1 << SHIFT_PER_CHANGE); + shift_per = (1 << SHIFT_PER_CHANGE); - g_a2_screen_buffer_changed |= line_mask; + g_a2_screen_buffer_changed |= line_mask; - for(x1 = 0; x1 < 40; x1 += shift_per) { + for(x1 = 0; x1 < 40; x1 += shift_per) { - CH_LOOP_A2_VID(ch_mask, ch_tmp); + CH_LOOP_A2_VID(ch_mask, ch_tmp); - left = MIN(x1, left); - right = MAX(x1 + shift_per, right); + left = MIN(x1, left); + right = MAX(x1 + shift_per, right); - slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); - b_ptr = &screen_data[(y*2)*pixels_per_line + x1*14]; - img_ptr = (word32 *)b_ptr; - img_ptr2 = (word32 *)(b_ptr + pixels_per_line); + slow_mem_ptr = &(g_slow_memory_ptr[mem_ptr + x1]); + b_ptr = &screen_data[(y*2)*pixels_per_line + x1*14]; + img_ptr = (word32 *)b_ptr; + img_ptr2 = (word32 *)(b_ptr + pixels_per_line); - for(x2 = 0; x2 < shift_per; x2 += 2) { - val0 = slow_mem_ptr[0x10000]; - val1 = slow_mem_ptr[0]; - val2 = slow_mem_ptr[0x10001]; - val3 = slow_mem_ptr[1]; + for(x2 = 0; x2 < shift_per; x2 += 2) { + val0 = slow_mem_ptr[0x10000]; + val1 = slow_mem_ptr[0]; + val2 = slow_mem_ptr[0x10001]; + val3 = slow_mem_ptr[1]; - prev_val = 0; - if((x1 + x2) > 0) { - prev_val = (slow_mem_ptr[-1] >> 3) &0xf; - } - val_whole = ((val3 & 0x7f) << 25) + - ((val2 & 0x7f) << 18) + - ((val1 & 0x7f) << 11) + - ((val0 & 0x7f) << 4) + prev_val; + prev_val = 0; + if((x1 + x2) > 0) { + prev_val = (slow_mem_ptr[-1] >> 3) &0xf; + } + val_whole = ((val3 & 0x7f) << 25) + + ((val2 & 0x7f) << 18) + + ((val1 & 0x7f) << 11) + + ((val0 & 0x7f) << 4) + prev_val; - tmp0 = g_dhires_convert[val_whole & 0xfff]; - val_whole = val_whole >> 4; - tmp1 = g_dhires_convert[val_whole & 0xfff]; - val_whole = val_whole >> 4; - tmp2 = g_dhires_convert[val_whole & 0xfff]; - val_whole = val_whole >> 4; - tmp3 = g_dhires_convert[val_whole & 0xfff]; - val_whole = val_whole >> 4; - tmp4 = g_dhires_convert[val_whole & 0xfff]; - val_whole = val_whole >> 4; - tmp5 = g_dhires_convert[val_whole & 0xfff]; - val_whole = val_whole >> 4; - if((x1 + x2 + 2) < 40) { - val_whole += (slow_mem_ptr[0x10002]<<8); - } - tmp6 = g_dhires_convert[val_whole & 0xfff]; + tmp0 = g_dhires_convert[val_whole & 0xfff]; + val_whole = val_whole >> 4; + tmp1 = g_dhires_convert[val_whole & 0xfff]; + val_whole = val_whole >> 4; + tmp2 = g_dhires_convert[val_whole & 0xfff]; + val_whole = val_whole >> 4; + tmp3 = g_dhires_convert[val_whole & 0xfff]; + val_whole = val_whole >> 4; + tmp4 = g_dhires_convert[val_whole & 0xfff]; + val_whole = val_whole >> 4; + tmp5 = g_dhires_convert[val_whole & 0xfff]; + val_whole = val_whole >> 4; + if((x1 + x2 + 2) < 40) { + val_whole += (slow_mem_ptr[0x10002]<<8); + } + tmp6 = g_dhires_convert[val_whole & 0xfff]; - img_ptr[0] = tmp0 + palette_add; - img_ptr[1] = tmp1 + palette_add; - img_ptr[2] = tmp2 + palette_add; - img_ptr[3] = tmp3 + palette_add; - img_ptr[4] = tmp4 + palette_add; - img_ptr[5] = tmp5 + palette_add; - img_ptr[6] = tmp6 + palette_add; + img_ptr[0] = tmp0 + palette_add; + img_ptr[1] = tmp1 + palette_add; + img_ptr[2] = tmp2 + palette_add; + img_ptr[3] = tmp3 + palette_add; + img_ptr[4] = tmp4 + palette_add; + img_ptr[5] = tmp5 + palette_add; + img_ptr[6] = tmp6 + palette_add; - img_ptr2[0] = tmp0 + palette_add; - img_ptr2[1] = tmp1 + palette_add; - img_ptr2[2] = tmp2 + palette_add; - img_ptr2[3] = tmp3 + palette_add; - img_ptr2[4] = tmp4 + palette_add; - img_ptr2[5] = tmp5 + palette_add; - img_ptr2[6] = tmp6 + palette_add; + img_ptr2[0] = tmp0 + palette_add; + img_ptr2[1] = tmp1 + palette_add; + img_ptr2[2] = tmp2 + palette_add; + img_ptr2[3] = tmp3 + palette_add; + img_ptr2[4] = tmp4 + palette_add; + img_ptr2[5] = tmp5 + palette_add; + img_ptr2[6] = tmp6 + palette_add; - slow_mem_ptr += 2; - img_ptr += 7; - img_ptr2 += 7; - } - } - } + slow_mem_ptr += 2; + img_ptr += 7; + img_ptr2 += 7; + } + } + } - for(i = 0; i < num_lines; i++) { - g_a2_line_left_edge[st_line + i] = (left*14); - g_a2_line_right_edge[st_line + i] = (right*14); - } + for(i = 0; i < num_lines; i++) { + g_a2_line_left_edge[st_line + i] = (left*14); + g_a2_line_right_edge[st_line + i] = (right*14); + } - g_need_redraw = 0; + g_need_redraw = 0; } -int -video_rebuild_super_hires_palette(word32 scan_info, int line, int reparse) -{ - word32 *word_ptr; - word32 *ch_ptr; - byte *byte_ptr; - word32 ch_mask, mask_per_line; - word32 tmp; - word32 scan, old_scan; - int palette_changed; - int diff0, diff1, diff2; - int val0, val1, val2; - int diffs; - int low_delta, low_color; - int delta; - int full; - int ch_bit_offset, ch_word_offset; - int bits_per_line; - int palette; - int j, k; +int video_rebuild_super_hires_palette(word32 scan_info, int line, int reparse) { + word32 *word_ptr; + word32 *ch_ptr; + byte *byte_ptr; + word32 ch_mask, mask_per_line; + word32 tmp; + word32 scan, old_scan; + int palette_changed; + int diff0, diff1, diff2; + int val0, val1, val2; + int diffs; + int low_delta, low_color; + int delta; + int full; + int ch_bit_offset, ch_word_offset; + int bits_per_line; + int palette; + int j, k; - palette_changed = 0; - palette = scan_info & 0xf; + palette_changed = 0; + palette = scan_info & 0xf; - ch_ptr = &(slow_mem_changed[0x9e00 >> CHANGE_SHIFT]); - ch_bit_offset = (palette << 5) >> SHIFT_PER_CHANGE; - ch_word_offset = ch_bit_offset >> 5; - ch_bit_offset = ch_bit_offset & 0x1f; - bits_per_line = (0x20 >> SHIFT_PER_CHANGE); - mask_per_line = -(1 << (32 - bits_per_line)); - mask_per_line = mask_per_line >> ch_bit_offset; + ch_ptr = &(slow_mem_changed[0x9e00 >> CHANGE_SHIFT]); + ch_bit_offset = (palette << 5) >> SHIFT_PER_CHANGE; + ch_word_offset = ch_bit_offset >> 5; + ch_bit_offset = ch_bit_offset & 0x1f; + bits_per_line = (0x20 >> SHIFT_PER_CHANGE); + mask_per_line = -(1 << (32 - bits_per_line)); + mask_per_line = mask_per_line >> ch_bit_offset; - ch_mask = ch_ptr[ch_word_offset] & mask_per_line; - ch_ptr[ch_word_offset] &= ~mask_per_line; /* clear the bits */ + ch_mask = ch_ptr[ch_word_offset] & mask_per_line; + ch_ptr[ch_word_offset] &= ~mask_per_line; /* clear the bits */ - old_scan = g_superhires_scan_save[line]; - scan = (scan_info & 0xfaf) + (g_palette_change_cnt[palette] << 12); - g_superhires_scan_save[line] = scan; + old_scan = g_superhires_scan_save[line]; + scan = (scan_info & 0xfaf) + (g_palette_change_cnt[palette] << 12); + g_superhires_scan_save[line] = scan; #if 0 - if(line == 1) { - word_ptr = (word32 *)&(g_slow_memory_ptr[0x19e00+palette*0x20]); - printf("y1vrshp, ch:%08x, s:%08x,os:%08x %d = %08x %08x %08x %08x %08x %08x %08x %08x\n", - ch_mask, scan, old_scan, reparse, - word_ptr[0], word_ptr[1], word_ptr[2], word_ptr[3], - word_ptr[4], word_ptr[5], word_ptr[6], word_ptr[7]); - } + if(line == 1) { + word_ptr = (word32 *)&(g_slow_memory_ptr[0x19e00+palette*0x20]); + printf("y1vrshp, ch:%08x, s:%08x,os:%08x %d = %08x %08x %08x %08x %08x %08x %08x %08x\n", + ch_mask, scan, old_scan, reparse, + word_ptr[0], word_ptr[1], word_ptr[2], word_ptr[3], + word_ptr[4], word_ptr[5], word_ptr[6], word_ptr[7]); + } #endif - diffs = reparse | ((scan ^ old_scan) & 0xf0f); - /* we must do full reparse if palette changed for this line */ + diffs = reparse | ((scan ^ old_scan) & 0xf0f); + /* we must do full reparse if palette changed for this line */ - if(!diffs && (ch_mask == 0) && (((scan ^ old_scan) & (~0xf0)) == 0)) { - /* nothing changed, get out fast */ - return 0; - } + if(!diffs && (ch_mask == 0) && (((scan ^ old_scan) & (~0xf0)) == 0)) { + /* nothing changed, get out fast */ + return 0; + } - if(ch_mask) { - /* indicates the palette has changed, and other scan lines */ - /* using this palette need to do a full 32-byte compare to */ - /* decide if they need to update or not */ - g_palette_change_cnt[palette]++; - } + if(ch_mask) { + /* indicates the palette has changed, and other scan lines */ + /* using this palette need to do a full 32-byte compare to */ + /* decide if they need to update or not */ + g_palette_change_cnt[palette]++; + } - word_ptr = (word32 *)&(g_slow_memory_ptr[0x19e00 + palette*0x20]); - for(j = 0; j < 8; j++) { - if(word_ptr[j] != g_saved_line_palettes[line][j]) { - diffs = 1; - break; - } - } + word_ptr = (word32 *)&(g_slow_memory_ptr[0x19e00 + palette*0x20]); + for(j = 0; j < 8; j++) { + if(word_ptr[j] != g_saved_line_palettes[line][j]) { + diffs = 1; + break; + } + } - if(diffs == 0) { - return 0; - } + if(diffs == 0) { + return 0; + } - /* first, save this word_ptr into saved_line_palettes */ - byte_ptr = (byte *)word_ptr; - for(j = 0; j < 8; j++) { - g_saved_line_palettes[line][j] = word_ptr[j]; - } + /* first, save this word_ptr into saved_line_palettes */ + byte_ptr = (byte *)word_ptr; + for(j = 0; j < 8; j++) { + g_saved_line_palettes[line][j] = word_ptr[j]; + } - full = g_installed_full_superhires_colormap; + full = g_installed_full_superhires_colormap; - if(!full && palette == g_a2vid_palette) { - /* construct new color approximations from lores */ - for(j = 0; j < 16; j++) { - tmp = *byte_ptr++; - val2 = (*byte_ptr++) & 0xf; - val0 = tmp & 0xf; - val1 = (tmp >> 4) & 0xf; - low_delta = 0x1000; - low_color = 0x0; - for(k = 0; k < 16; k++) { - diff0 = g_expanded_col_0[k] - val0; - diff1 = g_expanded_col_1[k] - val1; - diff2 = g_expanded_col_2[k] - val2; - if(diff0 < 0) { - diff0 = -diff0; - } - if(diff1 < 0) { - diff1 = -diff1; - } - if(diff2 < 0) { - diff2 = -diff2; - } - delta = diff0 + diff1 + diff2; - if(delta < low_delta) { - low_delta = delta; - low_color = k; - } - } + if(!full && palette == g_a2vid_palette) { + /* construct new color approximations from lores */ + for(j = 0; j < 16; j++) { + tmp = *byte_ptr++; + val2 = (*byte_ptr++) & 0xf; + val0 = tmp & 0xf; + val1 = (tmp >> 4) & 0xf; + low_delta = 0x1000; + low_color = 0x0; + for(k = 0; k < 16; k++) { + diff0 = g_expanded_col_0[k] - val0; + diff1 = g_expanded_col_1[k] - val1; + diff2 = g_expanded_col_2[k] - val2; + if(diff0 < 0) { + diff0 = -diff0; + } + if(diff1 < 0) { + diff1 = -diff1; + } + if(diff2 < 0) { + diff2 = -diff2; + } + delta = diff0 + diff1 + diff2; + if(delta < low_delta) { + low_delta = delta; + low_color = k; + } + } - g_a2vid_palette_remap[j] = low_color; - } - } + g_a2vid_palette_remap[j] = low_color; + } + } - byte_ptr = (byte *)word_ptr; - /* this palette has changed */ - for(j = 0; j < 16; j++) { - val0 = *byte_ptr++; - val1 = *byte_ptr++; - video_update_color_array(palette*16 + j, (val1<<8) + val0); - } + byte_ptr = (byte *)word_ptr; + /* this palette has changed */ + for(j = 0; j < 16; j++) { + val0 = *byte_ptr++; + val1 = *byte_ptr++; + video_update_color_array(palette*16 + j, (val1<<8) + val0); + } - g_palette_change_summary = 1; + g_palette_change_summary = 1; - return 1; + return 1; } -#define SUPER_TYPE redraw_changed_super_hires_oneline_nofill_8 -#define SUPER_FILL 0 -#define SUPER_PIXEL_SIZE 8 +#define SUPER_TYPE redraw_changed_super_hires_oneline_nofill_8 +#define SUPER_FILL 0 +#define SUPER_PIXEL_SIZE 8 #include "superhires.h" -#undef SUPER_TYPE -#undef SUPER_FILL -#undef SUPER_PIXEL_SIZE +#undef SUPER_TYPE +#undef SUPER_FILL +#undef SUPER_PIXEL_SIZE -#define SUPER_TYPE redraw_changed_super_hires_oneline_nofill_16 -#define SUPER_FILL 0 -#define SUPER_PIXEL_SIZE 16 +#define SUPER_TYPE redraw_changed_super_hires_oneline_nofill_16 +#define SUPER_FILL 0 +#define SUPER_PIXEL_SIZE 16 #include "superhires.h" -#undef SUPER_TYPE -#undef SUPER_FILL -#undef SUPER_PIXEL_SIZE +#undef SUPER_TYPE +#undef SUPER_FILL +#undef SUPER_PIXEL_SIZE -#define SUPER_TYPE redraw_changed_super_hires_oneline_nofill_32 -#define SUPER_FILL 0 -#define SUPER_PIXEL_SIZE 32 +#define SUPER_TYPE redraw_changed_super_hires_oneline_nofill_32 +#define SUPER_FILL 0 +#define SUPER_PIXEL_SIZE 32 #include "superhires.h" -#undef SUPER_TYPE -#undef SUPER_FILL -#undef SUPER_PIXEL_SIZE +#undef SUPER_TYPE +#undef SUPER_FILL +#undef SUPER_PIXEL_SIZE -#define SUPER_TYPE redraw_changed_super_hires_oneline_fill_8 -#define SUPER_FILL 1 -#define SUPER_PIXEL_SIZE 8 +#define SUPER_TYPE redraw_changed_super_hires_oneline_fill_8 +#define SUPER_FILL 1 +#define SUPER_PIXEL_SIZE 8 #include "superhires.h" -#undef SUPER_TYPE -#undef SUPER_FILL -#undef SUPER_PIXEL_SIZE +#undef SUPER_TYPE +#undef SUPER_FILL +#undef SUPER_PIXEL_SIZE -#define SUPER_TYPE redraw_changed_super_hires_oneline_fill_16 -#define SUPER_FILL 1 -#define SUPER_PIXEL_SIZE 16 +#define SUPER_TYPE redraw_changed_super_hires_oneline_fill_16 +#define SUPER_FILL 1 +#define SUPER_PIXEL_SIZE 16 #include "superhires.h" -#undef SUPER_TYPE -#undef SUPER_FILL -#undef SUPER_PIXEL_SIZE +#undef SUPER_TYPE +#undef SUPER_FILL +#undef SUPER_PIXEL_SIZE -#define SUPER_TYPE redraw_changed_super_hires_oneline_fill_32 -#define SUPER_FILL 1 -#define SUPER_PIXEL_SIZE 32 +#define SUPER_TYPE redraw_changed_super_hires_oneline_fill_32 +#define SUPER_FILL 1 +#define SUPER_PIXEL_SIZE 32 #include "superhires.h" -#undef SUPER_TYPE -#undef SUPER_FILL -#undef SUPER_PIXEL_SIZE +#undef SUPER_TYPE +#undef SUPER_FILL +#undef SUPER_PIXEL_SIZE -void -redraw_changed_super_hires(int start_offset, int start_line, int num_lines, - int in_reparse, byte *screen_data) -{ - word32 *ch_ptr; - word32 mask_per_line; - word32 all_checks; - word32 check0, check1, mask0, mask1; - word32 this_check; - word32 tmp; - word32 line_mask; - word32 pal; - word32 scan, old_scan; - word32 kd_tmp_debug; - int y; - int bits_per_line; - int a2vid_palette; - int type; - int left, right; - int st_line; - int check_bit_pos, check_word_off; - int pixel_size, pixel_size_type; - int use_a2vid_palette, mode_640; - int pixels_per_line; - int ret; - int i; +void redraw_changed_super_hires(int start_offset, int start_line, int num_lines, + int in_reparse, byte *screen_data) { + word32 *ch_ptr; + word32 mask_per_line; + word32 all_checks; + word32 check0, check1, mask0, mask1; + word32 this_check; + word32 tmp; + word32 line_mask; + word32 pal; + word32 scan, old_scan; + word32 kd_tmp_debug; + int y; + int bits_per_line; + int a2vid_palette; + int type; + int left, right; + int st_line; + int check_bit_pos, check_word_off; + int pixel_size, pixel_size_type; + int use_a2vid_palette, mode_640; + int pixels_per_line; + int ret; + int i; - st_line = start_line; - start_line = start_line >> 3; + st_line = start_line; + start_line = start_line >> 3; - pixel_size = g_kimage_superhires.mdepth; - pixels_per_line = g_kimage_superhires.width_act; + pixel_size = g_kimage_superhires.mdepth; + pixels_per_line = g_kimage_superhires.width_act; - pixel_size_type = (pixel_size >> 3) - 1; - /* pixel_size_type is now: 0=8bit, 1=16bit, 3=32bit */ - if(pixel_size_type >= 3) { - pixel_size_type = 2; - } + pixel_size_type = (pixel_size >> 3) - 1; + /* pixel_size_type is now: 0=8bit, 1=16bit, 3=32bit */ + if(pixel_size_type >= 3) { + pixel_size_type = 2; + } - kd_tmp_debug = g_a2_screen_buffer_changed; + kd_tmp_debug = g_a2_screen_buffer_changed; - line_mask = 1 << (start_line); + line_mask = 1 << (start_line); - ch_ptr = &(slow_mem_changed[(0x2000) >> CHANGE_SHIFT]); - bits_per_line = 160 >> SHIFT_PER_CHANGE; - mask_per_line = -(1 << (32 - bits_per_line)); + ch_ptr = &(slow_mem_changed[(0x2000) >> CHANGE_SHIFT]); + bits_per_line = 160 >> SHIFT_PER_CHANGE; + mask_per_line = -(1 << (32 - bits_per_line)); - if(SHIFT_PER_CHANGE != 3) { - halt_printf("SHIFT_PER_CHANGE must be 3!\n"); - return; - } + if(SHIFT_PER_CHANGE != 3) { + halt_printf("SHIFT_PER_CHANGE must be 3!\n"); + return; + } - a2vid_palette = g_a2vid_palette; - if(g_installed_full_superhires_colormap) { - a2vid_palette = -1; - } else { - /* handle palette counting for finding least-used palette */ - if(pixel_size == 8) { - for(y = 8*start_line; y < 8*(start_line + 1); y++) { - scan = g_slow_memory_ptr[0x19d00 + y]; - pal = scan & 0xf; - g_shr_palette_used[pal]++; - } - } - } + a2vid_palette = g_a2vid_palette; + if(g_installed_full_superhires_colormap) { + a2vid_palette = -1; + } else { + /* handle palette counting for finding least-used palette */ + if(pixel_size == 8) { + for(y = 8*start_line; y < 8*(start_line + 1); y++) { + scan = g_slow_memory_ptr[0x19d00 + y]; + pal = scan & 0xf; + g_shr_palette_used[pal]++; + } + } + } - all_checks = 0; - check0 = 0; - check1 = 0; - for(y = st_line; y < (st_line + num_lines); y++) { - scan = g_slow_memory_ptr[0x19d00 + y]; - check_bit_pos = bits_per_line * y; - check_word_off = check_bit_pos >> 5; /* 32 bits per word */ - check_bit_pos = check_bit_pos & 0x1f; /* 5-bit bit_pos */ - check0 = ch_ptr[check_word_off]; - check1 = ch_ptr[check_word_off+1]; - mask0 = mask_per_line >> check_bit_pos; - mask1 = 0; - this_check = check0 << check_bit_pos; - /* move indicated bit to MSbit position */ - if((check_bit_pos + bits_per_line) > 32) { - this_check |= (check1 >> (32 - check_bit_pos)); - mask1 = mask_per_line << (32 - check_bit_pos); - } + all_checks = 0; + check0 = 0; + check1 = 0; + for(y = st_line; y < (st_line + num_lines); y++) { + scan = g_slow_memory_ptr[0x19d00 + y]; + check_bit_pos = bits_per_line * y; + check_word_off = check_bit_pos >> 5; /* 32 bits per word */ + check_bit_pos = check_bit_pos & 0x1f; /* 5-bit bit_pos */ + check0 = ch_ptr[check_word_off]; + check1 = ch_ptr[check_word_off+1]; + mask0 = mask_per_line >> check_bit_pos; + mask1 = 0; + this_check = check0 << check_bit_pos; + /* move indicated bit to MSbit position */ + if((check_bit_pos + bits_per_line) > 32) { + this_check |= (check1 >> (32 - check_bit_pos)); + mask1 = mask_per_line << (32 - check_bit_pos); + } - ch_ptr[check_word_off] = check0 & ~mask0; - ch_ptr[check_word_off+1] = check1 & ~mask1; + ch_ptr[check_word_off] = check0 & ~mask0; + ch_ptr[check_word_off+1] = check1 & ~mask1; - this_check = this_check & mask_per_line; - old_scan = g_superhires_scan_save[y]; - use_a2vid_palette = ((scan & 0xf) == (word32)a2vid_palette); - scan = (scan + (a2vid_palette << 8)) & 0xfff; + this_check = this_check & mask_per_line; + old_scan = g_superhires_scan_save[y]; + use_a2vid_palette = ((scan & 0xf) == (word32)a2vid_palette); + scan = (scan + (a2vid_palette << 8)) & 0xfff; - ret = video_rebuild_super_hires_palette(scan, y, in_reparse); + ret = video_rebuild_super_hires_palette(scan, y, in_reparse); #if 0 - if(y == 1) { - printf("y1, ch:%08x, ret:%d, scan:%03x, os:%03x\n", - this_check, ret, scan, old_scan); - } + if(y == 1) { + printf("y1, ch:%08x, ret:%d, scan:%03x, os:%03x\n", + this_check, ret, scan, old_scan); + } #endif - if(ret || in_reparse || ((scan ^ old_scan) & 0xa0)) { - /* 0x80 == mode640, 0x20 = fill */ - this_check = -1; - } + if(ret || in_reparse || ((scan ^ old_scan) & 0xa0)) { + /* 0x80 == mode640, 0x20 = fill */ + this_check = -1; + } - if(this_check == 0) { - continue; - } + if(this_check == 0) { + continue; + } - mode_640 = (scan & 0x80); - if(mode_640) { - g_num_lines_superhires640++; - } + mode_640 = (scan & 0x80); + if(mode_640) { + g_num_lines_superhires640++; + } - type = ((scan >> 5) & 1) + (pixel_size_type << 1); - if(type & 1) { - /* fill mode--redraw whole line */ - this_check = -1; - } + type = ((scan >> 5) & 1) + (pixel_size_type << 1); + if(type & 1) { + /* fill mode--redraw whole line */ + this_check = -1; + } - all_checks |= this_check; + all_checks |= this_check; - g_a2_screen_buffer_changed |= line_mask; + g_a2_screen_buffer_changed |= line_mask; - switch(type) { - case 0: /* nofill, 8 bit pixels */ - redraw_changed_super_hires_oneline_nofill_8( - screen_data, pixels_per_line, y, scan, - this_check, use_a2vid_palette, mode_640); - break; - case 1: /* fill, 8 bit pixels */ - redraw_changed_super_hires_oneline_fill_8( - screen_data, pixels_per_line, y, scan, - this_check, use_a2vid_palette, mode_640); - break; - case 2: /* nofill, 16 bit pixels */ - redraw_changed_super_hires_oneline_nofill_16( - screen_data, pixels_per_line, y, scan, - this_check, use_a2vid_palette, mode_640); - break; - case 3: /* fill, 16 bit pixels */ - redraw_changed_super_hires_oneline_fill_16( - screen_data, pixels_per_line, y, scan, - this_check, use_a2vid_palette, mode_640); - break; - case 4: /* nofill, 32 bit pixels */ - redraw_changed_super_hires_oneline_nofill_32( - screen_data, pixels_per_line, y, scan, - this_check, use_a2vid_palette, mode_640); - break; - case 5: /* fill, 32 byte pixels */ - redraw_changed_super_hires_oneline_fill_32( - screen_data, pixels_per_line, y, scan, - this_check, use_a2vid_palette, mode_640); - break; - default: - halt_printf("type: %d bad!\n", type); - } - } + switch(type) { + case 0: /* nofill, 8 bit pixels */ + redraw_changed_super_hires_oneline_nofill_8( + screen_data, pixels_per_line, y, scan, + this_check, use_a2vid_palette, mode_640); + break; + case 1: /* fill, 8 bit pixels */ + redraw_changed_super_hires_oneline_fill_8( + screen_data, pixels_per_line, y, scan, + this_check, use_a2vid_palette, mode_640); + break; + case 2: /* nofill, 16 bit pixels */ + redraw_changed_super_hires_oneline_nofill_16( + screen_data, pixels_per_line, y, scan, + this_check, use_a2vid_palette, mode_640); + break; + case 3: /* fill, 16 bit pixels */ + redraw_changed_super_hires_oneline_fill_16( + screen_data, pixels_per_line, y, scan, + this_check, use_a2vid_palette, mode_640); + break; + case 4: /* nofill, 32 bit pixels */ + redraw_changed_super_hires_oneline_nofill_32( + screen_data, pixels_per_line, y, scan, + this_check, use_a2vid_palette, mode_640); + break; + case 5: /* fill, 32 byte pixels */ + redraw_changed_super_hires_oneline_fill_32( + screen_data, pixels_per_line, y, scan, + this_check, use_a2vid_palette, mode_640); + break; + default: + halt_printf("type: %d bad!\n", type); + } + } - left = 4*40; - right = 0; + left = 4*40; + right = 0; - tmp = all_checks; - if(all_checks) { - for(i = 0; i < 160; i += 8) { - if(tmp & 0x80000000) { - left = MIN(i, left); - right = MAX(i + 8, right); - } - tmp = tmp << 1; - } - } + tmp = all_checks; + if(all_checks) { + for(i = 0; i < 160; i += 8) { + if(tmp & 0x80000000) { + left = MIN(i, left); + right = MAX(i + 8, right); + } + tmp = tmp << 1; + } + } - for(i = 0; i < num_lines; i++) { - g_a2_line_left_edge[st_line + i] = 4*left; - g_a2_line_right_edge[st_line + i] = 4*right; - } + for(i = 0; i < num_lines; i++) { + g_a2_line_left_edge[st_line + i] = 4*left; + g_a2_line_right_edge[st_line + i] = 4*right; + } #if 0 - if((g_a2_screen_buffer_changed & (1 << start_line)) != 0) { - if(((g_full_refresh_needed & (1 << start_line)) == 0) && - left >= right) { - halt_printf("shr: line: %d, left: %d, right:%d\n", - start_line, left, right); - printf("mask_per_line: %08x, all_checks: %08x\n", - mask_per_line, all_checks); - printf("check0,1 = %08x,%08x\n", check0, check1); - printf("a2_screen_chang: %08x\n", kd_tmp_debug); + if((g_a2_screen_buffer_changed & (1 << start_line)) != 0) { + if(((g_full_refresh_needed & (1 << start_line)) == 0) && + left >= right) { + halt_printf("shr: line: %d, left: %d, right:%d\n", + start_line, left, right); + printf("mask_per_line: %08x, all_checks: %08x\n", + mask_per_line, all_checks); + printf("check0,1 = %08x,%08x\n", check0, check1); + printf("a2_screen_chang: %08x\n", kd_tmp_debug); #ifdef HPUX - U_STACK_TRACE(); + U_STACK_TRACE(); #endif - } - } + } + } #endif - g_need_redraw = 0; + g_need_redraw = 0; } -void -display_screen() -{ - video_update_through_line(262); +void display_screen() { + video_update_through_line(262); } -void -video_update_event_line(int line) -{ - int new_line; +void video_update_event_line(int line) { + int new_line; - video_update_through_line(line); + video_update_through_line(line); - new_line = line + g_line_ref_amt; - if(new_line < 200) { - if(!g_config_control_panel) { - add_event_vid_upd(new_line); - } - } else if(line >= 262) { - video_update_through_line(0); - if(!g_config_control_panel) { - add_event_vid_upd(1); /* add event for new screen */ - } - } + new_line = line + g_line_ref_amt; + if(new_line < 200) { + if(!g_config_control_panel) { + add_event_vid_upd(new_line); + } + } else if(line >= 262) { + video_update_through_line(0); + if(!g_config_control_panel) { + add_event_vid_upd(1); /* add event for new screen */ + } + } - if(g_video_extra_check_inputs) { - if(g_video_dcycs_check_input < g_cur_dcycs) { - video_check_input_events(); - } - } + if(g_video_extra_check_inputs) { + if(g_video_dcycs_check_input < g_cur_dcycs) { + video_check_input_events(); + } + } } -void -video_check_input_events() -{ - word32 start_time, end_time; +void video_check_input_events() { + word32 start_time, end_time; - g_video_dcycs_check_input = g_cur_dcycs + 4000.0; + g_video_dcycs_check_input = g_cur_dcycs + 4000.0; - GET_ITIMER(start_time); - check_input_events(); - GET_ITIMER(end_time); + GET_ITIMER(start_time); + check_input_events(); + GET_ITIMER(end_time); - g_cycs_in_check_input += (end_time - start_time); + g_cycs_in_check_input += (end_time - start_time); } -void -video_update_through_line(int line) -{ - register word32 start_time; - register word32 end_time; - int *mode_ptr; - word32 mask; - int last_line, num_lines; - int must_reparse; - int new_all_stat, prev_all_stat; - int new_stat, prev_stat; - int i; +void video_update_through_line(int line) { + register word32 start_time; + register word32 end_time; + int *mode_ptr; + word32 mask; + int last_line, num_lines; + int must_reparse; + int new_all_stat, prev_all_stat; + int new_stat, prev_stat; + int i; #if 0 - vid_printf("\nvideo_upd for line %d, lines: %06x\n", line, - get_lines_since_vbl(g_cur_dcycs)); + vid_printf("\nvideo_upd for line %d, lines: %06x\n", line, + get_lines_since_vbl(g_cur_dcycs)); #endif - GET_ITIMER(start_time); + GET_ITIMER(start_time); - video_update_all_stat_through_line(line); + video_update_all_stat_through_line(line); - i = g_vid_update_last_line; + i = g_vid_update_last_line; - last_line = MIN(200, line+1); /* go through line, but not past 200 */ + last_line = MIN(200, line+1); /* go through line, but not past 200 */ - prev_stat = -2; - prev_all_stat = -2; - num_lines = 0; - must_reparse = 0; - for(i = g_vid_update_last_line; i < last_line; i++) { - new_all_stat = g_a2_new_all_stat[i]; - if(new_all_stat != g_a2_cur_all_stat[i]) { - /* regen line_stat for this line */ - g_a2_cur_all_stat[i] = new_all_stat; - if(new_all_stat == prev_all_stat) { - /* save a lookup */ - new_stat = prev_stat; - } else { - new_stat = video_all_stat_to_line_stat(i, - new_all_stat); - } - if(new_stat != g_a2_line_stat[i]) { - /* status changed */ - g_a2_line_stat[i] = new_stat; - mode_ptr = video_update_kimage_ptr(i, new_stat); - if(mode_ptr[i] != new_stat) { - must_reparse = 1; - mode_ptr[i] = new_stat; - } - mask = 1 << (line >> 3); - g_full_refresh_needed |= mask; - g_a2_screen_buffer_changed |= mask; - } - } + prev_stat = -2; + prev_all_stat = -2; + num_lines = 0; + must_reparse = 0; + for(i = g_vid_update_last_line; i < last_line; i++) { + new_all_stat = g_a2_new_all_stat[i]; + if(new_all_stat != g_a2_cur_all_stat[i]) { + /* regen line_stat for this line */ + g_a2_cur_all_stat[i] = new_all_stat; + if(new_all_stat == prev_all_stat) { + /* save a lookup */ + new_stat = prev_stat; + } else { + new_stat = video_all_stat_to_line_stat(i, + new_all_stat); + } + if(new_stat != g_a2_line_stat[i]) { + /* status changed */ + g_a2_line_stat[i] = new_stat; + mode_ptr = video_update_kimage_ptr(i, new_stat); + if(mode_ptr[i] != new_stat) { + must_reparse = 1; + mode_ptr[i] = new_stat; + } + mask = 1 << (line >> 3); + g_full_refresh_needed |= mask; + g_a2_screen_buffer_changed |= mask; + } + } - new_stat = g_a2_line_stat[i]; + new_stat = g_a2_line_stat[i]; - if( ((new_stat == prev_stat) && ((i & 7) != 0)) || - (num_lines == 0) ) { - /* merge prev and this together */ - prev_stat = new_stat; - num_lines++; - continue; - } + if( ((new_stat == prev_stat) && ((i & 7) != 0)) || + (num_lines == 0) ) { + /* merge prev and this together */ + prev_stat = new_stat; + num_lines++; + continue; + } - /* else, we must call refresh */ - video_refresh_lines(i - num_lines, num_lines, must_reparse); - num_lines = 1; - prev_all_stat = -1; - prev_stat = new_stat; - must_reparse = 0; - } - if(num_lines > 0) { - video_refresh_lines(i - num_lines, num_lines, must_reparse); - } + /* else, we must call refresh */ + video_refresh_lines(i - num_lines, num_lines, must_reparse); + num_lines = 1; + prev_all_stat = -1; + prev_stat = new_stat; + must_reparse = 0; + } + if(num_lines > 0) { + video_refresh_lines(i - num_lines, num_lines, must_reparse); + } - g_vid_update_last_line = last_line; + g_vid_update_last_line = last_line; - /* deal with border */ - if(line >= 262) { - if(g_num_lines_prev_superhires != g_num_lines_superhires) { - /* switched in/out from superhires--refresh borders */ - g_border_sides_refresh_needed = 1; - } - refresh_border(); + /* deal with border */ + if(line >= 262) { + if(g_num_lines_prev_superhires != g_num_lines_superhires) { + /* switched in/out from superhires--refresh borders */ + g_border_sides_refresh_needed = 1; + } + refresh_border(); - if(g_status_refresh_needed) { - g_status_refresh_needed = 0; - x_redraw_status_lines(); - } - } - GET_ITIMER(end_time); + if(g_status_refresh_needed) { + g_status_refresh_needed = 0; + x_redraw_status_lines(); + } + } + GET_ITIMER(end_time); - g_cycs_in_refresh_line += (end_time - start_time); + g_cycs_in_refresh_line += (end_time - start_time); - if(line >= 262) { - GET_ITIMER(start_time); - if(g_palette_change_summary) { - g_palette_change_summary = 0; - video_update_colormap(); - } + if(line >= 262) { + GET_ITIMER(start_time); + if(g_palette_change_summary) { + g_palette_change_summary = 0; + video_update_colormap(); + } - video_push_kimages(); - GET_ITIMER(end_time); - g_cycs_in_refresh_ximage += (end_time - start_time); + video_push_kimages(); + GET_ITIMER(end_time); + g_cycs_in_refresh_ximage += (end_time - start_time); - g_num_lines_prev_superhires = g_num_lines_superhires; - g_num_lines_prev_superhires640 = g_num_lines_superhires640; - g_num_lines_superhires = 0; - g_num_lines_superhires640 = 0; - } + g_num_lines_prev_superhires = g_num_lines_superhires; + g_num_lines_prev_superhires640 = g_num_lines_superhires640; + g_num_lines_superhires = 0; + g_num_lines_superhires640 = 0; + } } -void -video_refresh_lines(int st_line, int num_lines, int must_reparse) -{ - byte *ptr; - int line; - int stat; - int mode; - int dbl, page, color; - int altchar, bg_color, text_color; - int pixels_per_line; - int i; +void video_refresh_lines(int st_line, int num_lines, int must_reparse) { + byte *ptr; + int line; + int stat; + int mode; + int dbl, page, color; + int altchar, bg_color, text_color; + int pixels_per_line; + int i; - line = st_line; + line = st_line; - /* do some basic checking, num_lines should be 1-8, and */ - /* st_line+num_lines-1 cannot roll over 8 */ - if((num_lines < 1) || (num_lines > 8) || - (((st_line & 7) + num_lines) > 8) ) { - halt_printf("video_refresh_lines called with %d, %d\n", - st_line, num_lines); - return; - } + /* do some basic checking, num_lines should be 1-8, and */ + /* st_line+num_lines-1 cannot roll over 8 */ + if((num_lines < 1) || (num_lines > 8) || + (((st_line & 7) + num_lines) > 8) ) { + halt_printf("video_refresh_lines called with %d, %d\n", + st_line, num_lines); + return; + } - stat = g_a2_line_stat[line]; - ptr = g_a2_line_kimage[line]->data_ptr; - pixels_per_line = g_a2_line_kimage[line]->width_act; + stat = g_a2_line_stat[line]; + ptr = g_a2_line_kimage[line]->data_ptr; + pixels_per_line = g_a2_line_kimage[line]->width_act; - /* do not zero g_a2_line_left/right_edge here since text/gr routs */ - /* need to leave stale values around for drawing to work correctly */ - /* all routs force in new left/right when there are screen changes */ + /* do not zero g_a2_line_left/right_edge here since text/gr routs */ + /* need to leave stale values around for drawing to work correctly */ + /* all routs force in new left/right when there are screen changes */ - dbl = stat & 1; - color = (stat >> 1) & 1; - page = (stat >> 2) & 1; - mode = (stat >> 4) & 7; + dbl = stat & 1; + color = (stat >> 1) & 1; + page = (stat >> 2) & 1; + mode = (stat >> 4) & 7; #if 0 - printf("refresh line: %d, stat: %04x\n", line, stat); + printf("refresh line: %d, stat: %04x\n", line, stat); #endif - switch(mode) { - case MODE_TEXT: - altchar = (stat >> 7) & 1; - bg_color = (stat >> 8) & 0xf; - text_color = (stat >> 12) & 0xf; - if(dbl) { - redraw_changed_text_80(0x000 + page*0x400, st_line, - num_lines, must_reparse, ptr, altchar, bg_color, - text_color, pixels_per_line); - } else { - redraw_changed_text_40(0x000 + page*0x400, st_line, - num_lines, must_reparse, ptr, altchar, bg_color, - text_color, pixels_per_line); - } - break; - case MODE_GR: - if(dbl) { - redraw_changed_dbl_gr(0x000 + page*0x400, st_line, - num_lines, must_reparse, ptr, pixels_per_line); - } else { - redraw_changed_gr(0x000 + page*0x400, st_line, - num_lines, must_reparse, ptr, pixels_per_line); - } - break; - case MODE_HGR: - if(dbl) { - redraw_changed_dbl_hires(0x000 + page*0x2000, st_line, - num_lines, color, must_reparse, ptr, - pixels_per_line); - } else { - redraw_changed_hires(0x000 + page*0x2000, st_line, - num_lines, color, must_reparse, ptr, - pixels_per_line); - } - break; - case MODE_SUPER_HIRES: - g_num_lines_superhires++; - redraw_changed_super_hires(0, st_line, num_lines, - must_reparse, ptr); - break; - case MODE_BORDER: - if(line < 192) { - halt_printf("Border line not 192: %d\n", line); - } - for(i = 0; i < num_lines; i++) { - g_a2_line_left_edge[line + i] = 0; - g_a2_line_right_edge[line + i] = 560; - } - if(g_border_line24_refresh_needed) { - g_border_line24_refresh_needed = 0; - g_a2_screen_buffer_changed |= (1 << 24); - } - break; - default: - halt_printf("refresh screen: mode: 0x%02x unknown!\n", mode); - exit(7); - } + switch(mode) { + case MODE_TEXT: + altchar = (stat >> 7) & 1; + bg_color = (stat >> 8) & 0xf; + text_color = (stat >> 12) & 0xf; + if(dbl) { + redraw_changed_text_80(0x000 + page*0x400, st_line, + num_lines, must_reparse, ptr, altchar, bg_color, + text_color, pixels_per_line); + } else { + redraw_changed_text_40(0x000 + page*0x400, st_line, + num_lines, must_reparse, ptr, altchar, bg_color, + text_color, pixels_per_line); + } + break; + case MODE_GR: + if(dbl) { + redraw_changed_dbl_gr(0x000 + page*0x400, st_line, + num_lines, must_reparse, ptr, pixels_per_line); + } else { + redraw_changed_gr(0x000 + page*0x400, st_line, + num_lines, must_reparse, ptr, pixels_per_line); + } + break; + case MODE_HGR: + if(dbl) { + redraw_changed_dbl_hires(0x000 + page*0x2000, st_line, + num_lines, color, must_reparse, ptr, + pixels_per_line); + } else { + redraw_changed_hires(0x000 + page*0x2000, st_line, + num_lines, color, must_reparse, ptr, + pixels_per_line); + } + break; + case MODE_SUPER_HIRES: + g_num_lines_superhires++; + redraw_changed_super_hires(0, st_line, num_lines, + must_reparse, ptr); + break; + case MODE_BORDER: + if(line < 192) { + halt_printf("Border line not 192: %d\n", line); + } + for(i = 0; i < num_lines; i++) { + g_a2_line_left_edge[line + i] = 0; + g_a2_line_right_edge[line + i] = 560; + } + if(g_border_line24_refresh_needed) { + g_border_line24_refresh_needed = 0; + g_a2_screen_buffer_changed |= (1 << 24); + } + break; + default: + halt_printf("refresh screen: mode: 0x%02x unknown!\n", mode); + exit(7); + } } -void -refresh_border() -{ - /**ZZZZ***/ +void refresh_border() { + /**ZZZZ***/ } // OG Added video_release_kimages proto void video_release_kimages(); -void -end_screen() -{ - glog("Shutting down display"); +void end_screen() { + glog("Shutting down display"); - // OG Free up allocated images - video_release_kimages(); - xdriver_end(); + // OG Free up allocated images + video_release_kimages(); + xdriver_end(); } byte g_font_array[256][8] = { #include "gsportfont.h" }; -void -read_a2_font() -{ - byte *f40_e_ptr; - byte *f40_o_ptr; - byte *f80_0_ptr, *f80_1_ptr, *f80_2_ptr, *f80_3_ptr; - int char_num; - int j, k; - int val0; - int mask; - int pix; +void read_a2_font() { + byte *f40_e_ptr; + byte *f40_o_ptr; + byte *f80_0_ptr, *f80_1_ptr, *f80_2_ptr, *f80_3_ptr; + int char_num; + int j, k; + int val0; + int mask; + int pix; - for(char_num = 0; char_num < 0x100; char_num++) { - for(j = 0; j < 8; j++) { - val0 = g_font_array[char_num][j]; + for(char_num = 0; char_num < 0x100; char_num++) { + for(j = 0; j < 8; j++) { + val0 = g_font_array[char_num][j]; - mask = 0x80; + mask = 0x80; - for(k = 0; k < 3; k++) { - g_font80_off0_bits[char_num][j][k] = 0; - g_font80_off1_bits[char_num][j][k] = 0; - g_font80_off2_bits[char_num][j][k] = 0; - g_font80_off3_bits[char_num][j][k] = 0; - g_font40_even_bits[char_num][j][k] = 0; - g_font40_odd_bits[char_num][j][k] = 0; - } - g_font40_even_bits[char_num][j][3] = 0; - g_font40_odd_bits[char_num][j][3] = 0; + for(k = 0; k < 3; k++) { + g_font80_off0_bits[char_num][j][k] = 0; + g_font80_off1_bits[char_num][j][k] = 0; + g_font80_off2_bits[char_num][j][k] = 0; + g_font80_off3_bits[char_num][j][k] = 0; + g_font40_even_bits[char_num][j][k] = 0; + g_font40_odd_bits[char_num][j][k] = 0; + } + g_font40_even_bits[char_num][j][3] = 0; + g_font40_odd_bits[char_num][j][3] = 0; - f40_e_ptr = (byte *)&g_font40_even_bits[char_num][j][0]; - f40_o_ptr = (byte *)&g_font40_odd_bits[char_num][j][0]; + f40_e_ptr = (byte *)&g_font40_even_bits[char_num][j][0]; + f40_o_ptr = (byte *)&g_font40_odd_bits[char_num][j][0]; - f80_0_ptr = (byte *)&g_font80_off0_bits[char_num][j][0]; - f80_1_ptr = (byte *)&g_font80_off1_bits[char_num][j][0]; - f80_2_ptr = (byte *)&g_font80_off2_bits[char_num][j][0]; - f80_3_ptr = (byte *)&g_font80_off3_bits[char_num][j][0]; + f80_0_ptr = (byte *)&g_font80_off0_bits[char_num][j][0]; + f80_1_ptr = (byte *)&g_font80_off1_bits[char_num][j][0]; + f80_2_ptr = (byte *)&g_font80_off2_bits[char_num][j][0]; + f80_3_ptr = (byte *)&g_font80_off3_bits[char_num][j][0]; - for(k = 0; k < 7; k++) { - pix = 0; - if(val0 & mask) { - pix = 0xf; - } + for(k = 0; k < 7; k++) { + pix = 0; + if(val0 & mask) { + pix = 0xf; + } - f40_e_ptr[2*k] = pix; - f40_e_ptr[2*k+1] = pix; + f40_e_ptr[2*k] = pix; + f40_e_ptr[2*k+1] = pix; - f40_o_ptr[2*k+2] = pix; - f40_o_ptr[2*k+3] = pix; + f40_o_ptr[2*k+2] = pix; + f40_o_ptr[2*k+3] = pix; - f80_0_ptr[k] = pix; - f80_1_ptr[k+1] = pix; - f80_2_ptr[k+2] = pix; - f80_3_ptr[k+3] = pix; + f80_0_ptr[k] = pix; + f80_1_ptr[k+1] = pix; + f80_2_ptr[k+2] = pix; + f80_3_ptr[k+3] = pix; - mask = mask >> 1; - } - } - } + mask = mask >> 1; + } + } + } } /* Helper routine for the *driver.c files */ -void -video_get_kimage(Kimage *kimage_ptr, int extend_info, int depth, int mdepth) -{ - int width; - int height; +void video_get_kimage(Kimage *kimage_ptr, int extend_info, int depth, int mdepth) { + int width; + int height; - width = A2_WINDOW_WIDTH; - height = A2_WINDOW_HEIGHT; - if(extend_info & 1) { - /* Border at top and bottom of screen */ - width = X_A2_WINDOW_WIDTH; - height = X_A2_WINDOW_HEIGHT - A2_WINDOW_HEIGHT + 2*8; - } - if(extend_info & 2) { - /* Border at sides of screen */ - width = BORDER_WIDTH + EFF_BORDER_WIDTH; - height = A2_WINDOW_HEIGHT; - } + width = A2_WINDOW_WIDTH; + height = A2_WINDOW_HEIGHT; + if(extend_info & 1) { + /* Border at top and bottom of screen */ + width = X_A2_WINDOW_WIDTH; + height = X_A2_WINDOW_HEIGHT - A2_WINDOW_HEIGHT + 2*8; + } + if(extend_info & 2) { + /* Border at sides of screen */ + width = BORDER_WIDTH + EFF_BORDER_WIDTH; + height = A2_WINDOW_HEIGHT; + } - kimage_ptr->dev_handle = 0; - kimage_ptr->dev_handle2 = 0; - kimage_ptr->data_ptr = 0; - kimage_ptr->width_req = width; - kimage_ptr->width_act = width; - kimage_ptr->height = height; - kimage_ptr->depth = depth; - kimage_ptr->mdepth = mdepth; - kimage_ptr->aux_info = 0; + kimage_ptr->dev_handle = 0; + kimage_ptr->dev_handle2 = 0; + kimage_ptr->data_ptr = 0; + kimage_ptr->width_req = width; + kimage_ptr->width_act = width; + kimage_ptr->height = height; + kimage_ptr->depth = depth; + kimage_ptr->mdepth = mdepth; + kimage_ptr->aux_info = 0; - x_get_kimage(kimage_ptr); + x_get_kimage(kimage_ptr); } -void -video_get_kimages() -{ - video_get_kimage(&g_kimage_text[0], 0, 8, 8); - video_get_kimage(&g_kimage_text[1], 0, 8, 8); - video_get_kimage(&g_kimage_hires[0], 0, 8, 8); - video_get_kimage(&g_kimage_hires[1], 0, 8, 8); - video_get_kimage(&g_kimage_superhires, 0, g_screen_depth, - g_screen_mdepth); - video_get_kimage(&g_kimage_border_special, 1, g_screen_depth, - g_screen_mdepth); - video_get_kimage(&g_kimage_border_sides, 2, g_screen_depth, - g_screen_mdepth); +void video_get_kimages() { + video_get_kimage(&g_kimage_text[0], 0, 8, 8); + video_get_kimage(&g_kimage_text[1], 0, 8, 8); + video_get_kimage(&g_kimage_hires[0], 0, 8, 8); + video_get_kimage(&g_kimage_hires[1], 0, 8, 8); + video_get_kimage(&g_kimage_superhires, 0, g_screen_depth, + g_screen_mdepth); + video_get_kimage(&g_kimage_border_special, 1, g_screen_depth, + g_screen_mdepth); + video_get_kimage(&g_kimage_border_sides, 2, g_screen_depth, + g_screen_mdepth); } // OG Added video_release_kimages (to match video_get_kimages) -void video_release_kimages() -{ - extern void x_release_kimage(Kimage *kimage_ptr); +void video_release_kimages() { + extern void x_release_kimage(Kimage *kimage_ptr); - x_release_kimage(&g_kimage_text[0]); - x_release_kimage(&g_kimage_text[1]); - x_release_kimage(&g_kimage_hires[0]); - x_release_kimage(&g_kimage_hires[1]); - x_release_kimage(&g_kimage_superhires); - x_release_kimage(&g_kimage_border_special); - x_release_kimage(&g_kimage_border_sides); + x_release_kimage(&g_kimage_text[0]); + x_release_kimage(&g_kimage_text[1]); + x_release_kimage(&g_kimage_hires[0]); + x_release_kimage(&g_kimage_hires[1]); + x_release_kimage(&g_kimage_superhires); + x_release_kimage(&g_kimage_border_special); + x_release_kimage(&g_kimage_border_sides); } -void -video_convert_kimage_depth(Kimage *kim_in, Kimage *kim_out, int startx, - int starty, int width, int height) -{ - byte *indata, *inptr; - word32 *outdata32, *outptr32; - word16 *outdata16, *outptr16; - word32 *palptr; - int out_width, in_width; - int x, y; +void video_convert_kimage_depth(Kimage *kim_in, Kimage *kim_out, int startx, + int starty, int width, int height) { + byte *indata, *inptr; + word32 *outdata32, *outptr32; + word16 *outdata16, *outptr16; + word32 *palptr; + int out_width, in_width; + int x, y; - indata = (byte *)kim_in->data_ptr; - outdata32 = (word32 *)kim_out->data_ptr; - outdata16 = (word16 *)kim_out->data_ptr; + indata = (byte *)kim_in->data_ptr; + outdata32 = (word32 *)kim_out->data_ptr; + outdata16 = (word16 *)kim_out->data_ptr; - if(kim_in == &g_kimage_superhires) { - palptr = &(g_palette_8to1624[0]); - } else { - palptr = &(g_a2palette_8to1624[0]); - } - if(kim_in->depth != 8) { - printf("x_convert_kimage_depth from non-8 bit depth: %p\n", - kim_in); - exit(1); - } + if(kim_in == &g_kimage_superhires) { + palptr = &(g_palette_8to1624[0]); + } else { + palptr = &(g_a2palette_8to1624[0]); + } + if(kim_in->depth != 8) { + printf("x_convert_kimage_depth from non-8 bit depth: %p\n", + kim_in); + exit(1); + } - out_width = kim_out->width_act; - in_width = kim_in->width_act; - indata += (starty * in_width + startx); - outdata32 += (starty * out_width + startx); - outdata16 += (starty * out_width + startx); - if(kim_out->mdepth == 16) { - for(y = 0; y < height; y++) { - outptr16 = outdata16; - inptr = indata; - for(x = 0; x < width; x++) { - *outptr16++ = palptr[*inptr++]; - } - outdata16 += out_width; - indata += in_width; - } - } else { - /* 32-bit depth */ - for(y = 0; y < height; y++) { - outptr32 = outdata32; - inptr = indata; - for(x = 0; x < width; x++) { - *outptr32++ = palptr[*inptr++]; - } - outdata32 += out_width; - indata += in_width; - } - } + out_width = kim_out->width_act; + in_width = kim_in->width_act; + indata += (starty * in_width + startx); + outdata32 += (starty * out_width + startx); + outdata16 += (starty * out_width + startx); + if(kim_out->mdepth == 16) { + for(y = 0; y < height; y++) { + outptr16 = outdata16; + inptr = indata; + for(x = 0; x < width; x++) { + *outptr16++ = palptr[*inptr++]; + } + outdata16 += out_width; + indata += in_width; + } + } else { + /* 32-bit depth */ + for(y = 0; y < height; y++) { + outptr32 = outdata32; + inptr = indata; + for(x = 0; x < width; x++) { + *outptr32++ = palptr[*inptr++]; + } + outdata32 += out_width; + indata += in_width; + } + } } -void -video_push_lines(Kimage *kimage_ptr, int start_line, int end_line, int left_pix, - int right_pix) -{ - int mdepth_mismatch; - int srcy; - int center = 0; // OG added variable to center screen +void video_push_lines(Kimage *kimage_ptr, int start_line, int end_line, int left_pix, + int right_pix) { + int mdepth_mismatch; + int srcy; + int center = 0; // OG added variable to center screen - //OG add null pointer check when emulator is restarted - if (!kimage_ptr) - { - printf("warning : video_push_lines(kimage_ptr=null)\n"); - return ; - } + //OG add null pointer check when emulator is restarted + if (!kimage_ptr) + { + printf("warning : video_push_lines(kimage_ptr=null)\n"); + return; + } - if(left_pix >= right_pix || left_pix < 0 || right_pix <= 0) { - halt_printf("video_push_lines: lines %d to %d, pix %d to %d\n", - start_line, end_line, left_pix, right_pix); - printf("a2_screen_buf_ch:%08x, g_full_refr:%08x\n", - g_a2_screen_buffer_changed, g_full_refresh_needed); - } + if(left_pix >= right_pix || left_pix < 0 || right_pix <= 0) { + halt_printf("video_push_lines: lines %d to %d, pix %d to %d\n", + start_line, end_line, left_pix, right_pix); + printf("a2_screen_buf_ch:%08x, g_full_refr:%08x\n", + g_a2_screen_buffer_changed, g_full_refresh_needed); + } - srcy = 2*start_line; + srcy = 2*start_line; - mdepth_mismatch = (kimage_ptr->mdepth != g_screen_mdepth); - if(mdepth_mismatch) { - /* translate from 8-bit pseudo to correct visual */ - video_convert_kimage_depth(kimage_ptr, &g_mainwin_kimage, - left_pix, srcy, (right_pix - left_pix), - 2*(end_line - start_line)); - kimage_ptr = &g_mainwin_kimage; - } - g_refresh_bytes_xfer += 2*(end_line - start_line) * - (right_pix - left_pix); + mdepth_mismatch = (kimage_ptr->mdepth != g_screen_mdepth); + if(mdepth_mismatch) { + /* translate from 8-bit pseudo to correct visual */ + video_convert_kimage_depth(kimage_ptr, &g_mainwin_kimage, + left_pix, srcy, (right_pix - left_pix), + 2*(end_line - start_line)); + kimage_ptr = &g_mainwin_kimage; + } + g_refresh_bytes_xfer += 2*(end_line - start_line) * + (right_pix - left_pix); - // OG Calculating new center - if (g_cur_a2_stat & ALL_STAT_SUPER_HIRES) - center=0; - else - center=EFF_BORDER_WIDTH - BORDER_WIDTH; + // OG Calculating new center + if (g_cur_a2_stat & ALL_STAT_SUPER_HIRES) + center=0; + else + center=EFF_BORDER_WIDTH - BORDER_WIDTH; - // OG shifting image to the center - x_push_kimage(kimage_ptr, g_video_act_margin_left + left_pix + center, - g_video_act_margin_top + srcy, left_pix, srcy, - (right_pix - left_pix), 2*(end_line - start_line)); + // OG shifting image to the center + x_push_kimage(kimage_ptr, g_video_act_margin_left + left_pix + center, + g_video_act_margin_top + srcy, left_pix, srcy, + (right_pix - left_pix), 2*(end_line - start_line)); } -void -video_push_border_sides_lines(int src_x, int dest_x, int width, int start_line, - int end_line) -{ - Kimage *kimage_ptr; - int srcy; +void video_push_border_sides_lines(int src_x, int dest_x, int width, int start_line, + int end_line) { + Kimage *kimage_ptr; + int srcy; - if(start_line < 0 || width < 0) { - return; - } + if(start_line < 0 || width < 0) { + return; + } #if 0 - printf("push_border_sides lines:%d-%d from %d to %d\n", - start_line, end_line, end_x - width, end_x); + printf("push_border_sides lines:%d-%d from %d to %d\n", + start_line, end_line, end_x - width, end_x); #endif - kimage_ptr = &g_kimage_border_sides; - g_refresh_bytes_xfer += 2 * (end_line - start_line) * width; + kimage_ptr = &g_kimage_border_sides; + g_refresh_bytes_xfer += 2 * (end_line - start_line) * width; - srcy = 2 * start_line; + srcy = 2 * start_line; - // Adjust dext_x to accound for changed margins - dest_x = dest_x + g_video_act_margin_left - BASE_MARGIN_LEFT; - if(dest_x < BASE_MARGIN_LEFT) { - src_x = src_x + g_video_act_margin_left - BASE_MARGIN_LEFT; - // Don't adjust src_x if doing right border - } - if(dest_x < 0) { - width = width + dest_x; - src_x = src_x - dest_x; - dest_x = 0; - } - if(src_x < 0) { - width = width + src_x; - dest_x = dest_x - src_x; - src_x = 0; - } - if(dest_x + width > g_video_act_width) { - width = g_video_act_width - dest_x; - } - if(width > 0) { - x_push_kimage(kimage_ptr, dest_x, g_video_act_margin_top + srcy, - src_x, srcy, width, 2*(end_line - start_line)); - } + // Adjust dext_x to accound for changed margins + dest_x = dest_x + g_video_act_margin_left - BASE_MARGIN_LEFT; + if(dest_x < BASE_MARGIN_LEFT) { + src_x = src_x + g_video_act_margin_left - BASE_MARGIN_LEFT; + // Don't adjust src_x if doing right border + } + if(dest_x < 0) { + width = width + dest_x; + src_x = src_x - dest_x; + dest_x = 0; + } + if(src_x < 0) { + width = width + src_x; + dest_x = dest_x - src_x; + src_x = 0; + } + if(dest_x + width > g_video_act_width) { + width = g_video_act_width - dest_x; + } + if(width > 0) { + x_push_kimage(kimage_ptr, dest_x, g_video_act_margin_top + srcy, + src_x, srcy, width, 2*(end_line - start_line)); + } } -void -video_push_border_sides() -{ - int old_width; - int prev_line; - int width; - int mode; - int i; +void video_push_border_sides() { + int old_width; + int prev_line; + int width; + int mode; + int i; #if 0 - printf("refresh border sides!\n"); + printf("refresh border sides!\n"); #endif - /* redraw left sides */ - // OG Left side can alos be "jagged" as a2 screen is now being centered + /* redraw left sides */ + // OG Left side can alos be "jagged" as a2 screen is now being centered - //video_push_border_sides_lines(0, 0, BORDER_WIDTH, 0, 200); + //video_push_border_sides_lines(0, 0, BORDER_WIDTH, 0, 200); - prev_line = -1; - old_width = -1; - for(i = 0; i < 200; i++) { - mode = (g_a2_line_stat[i] >> 4) & 7; - width = EFF_BORDER_WIDTH; - if(mode == MODE_SUPER_HIRES) { - width = BORDER_WIDTH; - } - if(width != old_width) { - video_push_border_sides_lines(BORDER_WIDTH, - 0, old_width, - prev_line, i); - prev_line = i; - old_width = width; - } - } - video_push_border_sides_lines(0/*BORDER_WIDTH*/, - 0, old_width, prev_line, 200); + prev_line = -1; + old_width = -1; + for(i = 0; i < 200; i++) { + mode = (g_a2_line_stat[i] >> 4) & 7; + width = EFF_BORDER_WIDTH; + if(mode == MODE_SUPER_HIRES) { + width = BORDER_WIDTH; + } + if(width != old_width) { + video_push_border_sides_lines(BORDER_WIDTH, + 0, old_width, + prev_line, i); + prev_line = i; + old_width = width; + } + } + video_push_border_sides_lines(0 /*BORDER_WIDTH*/, + 0, old_width, prev_line, 200); - /* right side--can be "jagged" */ - prev_line = -1; - old_width = -1; - for(i = 0; i < 200; i++) { - mode = (g_a2_line_stat[i] >> 4) & 7; - width = EFF_BORDER_WIDTH; - if(mode == MODE_SUPER_HIRES) { - width = BORDER_WIDTH; - } - if(width != old_width) { - video_push_border_sides_lines(BORDER_WIDTH, - X_A2_WINDOW_WIDTH - old_width, old_width, - prev_line, i); - prev_line = i; - old_width = width; - } - } + /* right side--can be "jagged" */ + prev_line = -1; + old_width = -1; + for(i = 0; i < 200; i++) { + mode = (g_a2_line_stat[i] >> 4) & 7; + width = EFF_BORDER_WIDTH; + if(mode == MODE_SUPER_HIRES) { + width = BORDER_WIDTH; + } + if(width != old_width) { + video_push_border_sides_lines(BORDER_WIDTH, + X_A2_WINDOW_WIDTH - old_width, old_width, + prev_line, i); + prev_line = i; + old_width = width; + } + } - video_push_border_sides_lines(0/*BORDER_WIDTH*/, - X_A2_WINDOW_WIDTH - old_width, old_width, prev_line, 200); + video_push_border_sides_lines(0 /*BORDER_WIDTH*/, + X_A2_WINDOW_WIDTH - old_width, old_width, prev_line, 200); } -void -video_push_border_special() -{ - Kimage *kimage_ptr; - int width, height; - int src_x, src_y; - int dest_x, dest_y; +void video_push_border_special() { + Kimage *kimage_ptr; + int width, height; + int src_x, src_y; + int dest_x, dest_y; - kimage_ptr = &g_kimage_border_special; - width = g_video_act_width; - g_refresh_bytes_xfer += width * (BASE_MARGIN_TOP + BASE_MARGIN_BOTTOM); + kimage_ptr = &g_kimage_border_special; + width = g_video_act_width; + g_refresh_bytes_xfer += width * (BASE_MARGIN_TOP + BASE_MARGIN_BOTTOM); - // First do bottom border: dest_x from 0 to 640+MARGIN_LEFT+MARGIN_RIGHT - // and dest_y of BASE_MARGIN_BOTTOM starting at TOP+A2_HEIGHT - // src_x is dest_x, and src_y is 0. - dest_y = g_video_act_margin_top + A2_WINDOW_HEIGHT; - height = g_video_act_margin_bottom; - src_y = BASE_MARGIN_BOTTOM - height; + // First do bottom border: dest_x from 0 to 640+MARGIN_LEFT+MARGIN_RIGHT + // and dest_y of BASE_MARGIN_BOTTOM starting at TOP+A2_HEIGHT + // src_x is dest_x, and src_y is 0. + dest_y = g_video_act_margin_top + A2_WINDOW_HEIGHT; + height = g_video_act_margin_bottom; + src_y = BASE_MARGIN_BOTTOM - height; - dest_x = 0; - src_x = BASE_MARGIN_LEFT - g_video_act_margin_left; + dest_x = 0; + src_x = BASE_MARGIN_LEFT - g_video_act_margin_left; /* - glogf("width: %d", kimage_ptr->width_act); - for (int i = 600; i< 700; i++) { - kimage_ptr->data_ptr[i*4] = 0xFF; - kimage_ptr->data_ptr[i*4+1] = 0xFF; - kimage_ptr->data_ptr[i*4+2] = 0x00; - } -*/ - if(width > 0 && height > 0) { - #ifdef WINSDL_BORDERHACK - x_push_kimage(kimage_ptr, dest_x+72, dest_y, src_x, src_y, width, height); - #endif - x_push_kimage(kimage_ptr, dest_x, dest_y, src_x, src_y, width, height); + glogf("width: %d", kimage_ptr->width_act); + for (int i = 600; i< 700; i++) { + kimage_ptr->data_ptr[i*4] = 0xFF; + kimage_ptr->data_ptr[i*4+1] = 0xFF; + kimage_ptr->data_ptr[i*4+2] = 0x00; + } + */ + if(width > 0 && height > 0) { + #ifdef WINSDL_BORDERHACK + x_push_kimage(kimage_ptr, dest_x+72, dest_y, src_x, src_y, width, height); + #endif + x_push_kimage(kimage_ptr, dest_x, dest_y, src_x, src_y, width, height); // glogf("X:%d Y: %d SX:%d SY:%D W:%d H:%d\n",dest_x, dest_y, src_x, src_y, width, height); - } + } - // Then fix top border: dest_x from 0 to 640+LEFT+RIGHT and - // dest_y from 0 to TOP. src_x is dest_x, but src_y is - // BOTTOM to BOTTOM+TOP - // Just use src_x and dest_x from earlier. - height = g_video_act_margin_top; - dest_y = 0; - src_y = BASE_MARGIN_BOTTOM; - if(width > 0 && height > 0) { - #ifdef WINSDL_BORDERHACK - x_push_kimage(kimage_ptr, dest_x+72, dest_y, src_x, src_y, width, height); - #endif - x_push_kimage(kimage_ptr, dest_x, dest_y, src_x, src_y, width, height); - } + // Then fix top border: dest_x from 0 to 640+LEFT+RIGHT and + // dest_y from 0 to TOP. src_x is dest_x, but src_y is + // BOTTOM to BOTTOM+TOP + // Just use src_x and dest_x from earlier. + height = g_video_act_margin_top; + dest_y = 0; + src_y = BASE_MARGIN_BOTTOM; + if(width > 0 && height > 0) { + #ifdef WINSDL_BORDERHACK + x_push_kimage(kimage_ptr, dest_x+72, dest_y, src_x, src_y, width, height); + #endif + x_push_kimage(kimage_ptr, dest_x, dest_y, src_x, src_y, width, height); + } } // OG Added window ratio support extern int x_calc_ratio(float ratiox,float ratioy); -void -video_push_kimages() -{ - register word32 start_time; - register word32 end_time; - Kimage *last_kim, *cur_kim; - word32 mask; - int start; - int line; - int left_pix, right_pix; - int left, right; - int line_div8; - float ratiox = 0,ratioy = 0; +void video_push_kimages() { + register word32 start_time; + register word32 end_time; + Kimage *last_kim, *cur_kim; + word32 mask; + int start; + int line; + int left_pix, right_pix; + int left, right; + int line_div8; + float ratiox = 0,ratioy = 0; - if(g_border_sides_refresh_needed) { - g_border_sides_refresh_needed = 0; - video_push_border_sides(); - } - if(g_border_special_refresh_needed) { - g_border_special_refresh_needed = 0; - video_push_border_special(); - } + if(g_border_sides_refresh_needed) { + g_border_sides_refresh_needed = 0; + video_push_border_sides(); + } + if(g_border_special_refresh_needed) { + g_border_special_refresh_needed = 0; + video_push_border_special(); + } - if(g_a2_screen_buffer_changed == 0) { - return; - } + if(g_a2_screen_buffer_changed == 0) { + return; + } - GET_ITIMER(start_time); + GET_ITIMER(start_time); - if (x_calc_ratio(ratiox,ratioy)) - { - line = 0; - while (1) - { - start = line; - cur_kim = g_a2_line_kimage[line]; - while(line < 200 && g_a2_line_kimage[line] == cur_kim) line++; - if (cur_kim == &g_kimage_superhires) - right = 640; - else - right = 560; + if (x_calc_ratio(ratiox,ratioy)) + { + line = 0; + while (1) + { + start = line; + cur_kim = g_a2_line_kimage[line]; + while(line < 200 && g_a2_line_kimage[line] == cur_kim) line++; + if (cur_kim == &g_kimage_superhires) + right = 640; + else + right = 560; - video_push_lines(cur_kim, start, line,0,right); - if (line==200) break; - } + video_push_lines(cur_kim, start, line,0,right); + if (line==200) break; + } - } - else - { - start = -1; - last_kim = (Kimage *)-1; - cur_kim = (Kimage *)0; + } + else + { + start = -1; + last_kim = (Kimage *)-1; + cur_kim = (Kimage *)0; - left_pix = 640; - right_pix = 0; + left_pix = 640; + right_pix = 0; - for(line = 0; line < 200; line++) { - line_div8 = line >> 3; - mask = 1 << (line_div8); - cur_kim = g_a2_line_kimage[line]; - if((g_full_refresh_needed & mask) != 0) { - left = 0; - right = 560; - if(cur_kim == &g_kimage_superhires) { - right = 640; - } - } else { - left = g_a2_line_left_edge[line]; - right = g_a2_line_right_edge[line]; - } + for(line = 0; line < 200; line++) { + line_div8 = line >> 3; + mask = 1 << (line_div8); + cur_kim = g_a2_line_kimage[line]; + if((g_full_refresh_needed & mask) != 0) { + left = 0; + right = 560; + if(cur_kim == &g_kimage_superhires) { + right = 640; + } + } else { + left = g_a2_line_left_edge[line]; + right = g_a2_line_right_edge[line]; + } - if(!(g_a2_screen_buffer_changed & mask) || (left > right)) { - /* No need to update this line */ - /* Refresh previous chunks of lines, if any */ - if(start >= 0) { - video_push_lines(last_kim, start, line, - left_pix, right_pix); - start = -1; - left_pix = 640; - right_pix = 0; - } - } else { - /* Need to update this line */ - if(start < 0) { - start = line; - last_kim = cur_kim; - } - if(cur_kim != last_kim) { - /* do the refresh */ - video_push_lines(last_kim, start, line, - left_pix, right_pix); - last_kim = cur_kim; - start = line; - left_pix = left; - right_pix = right; - } - left_pix = MIN(left, left_pix); - right_pix = MAX(right, right_pix); - } - } + if(!(g_a2_screen_buffer_changed & mask) || (left > right)) { + /* No need to update this line */ + /* Refresh previous chunks of lines, if any */ + if(start >= 0) { + video_push_lines(last_kim, start, line, + left_pix, right_pix); + start = -1; + left_pix = 640; + right_pix = 0; + } + } else { + /* Need to update this line */ + if(start < 0) { + start = line; + last_kim = cur_kim; + } + if(cur_kim != last_kim) { + /* do the refresh */ + video_push_lines(last_kim, start, line, + left_pix, right_pix); + last_kim = cur_kim; + start = line; + left_pix = left; + right_pix = right; + } + left_pix = MIN(left, left_pix); + right_pix = MAX(right, right_pix); + } + } - if(start >= 0) { - video_push_lines(last_kim, start, 200, left_pix, right_pix); - } - } + if(start >= 0) { + video_push_lines(last_kim, start, 200, left_pix, right_pix); + } + } - g_a2_screen_buffer_changed = 0; - g_full_refresh_needed = 0; + g_a2_screen_buffer_changed = 0; + g_full_refresh_needed = 0; - x_push_done(); + x_push_done(); - GET_ITIMER(end_time); + GET_ITIMER(end_time); - g_cycs_in_xredraw += (end_time - start_time); + g_cycs_in_xredraw += (end_time - start_time); } -void -video_update_color_raw(int col_num, int a2_color) -{ - word32 tmp; - int red, green, blue; - int newred, newgreen, newblue; +void video_update_color_raw(int col_num, int a2_color) { + word32 tmp; + int red, green, blue; + int newred, newgreen, newblue; - red = (a2_color >> 8) & 0xf; - green = (a2_color >> 4) & 0xf; - blue = (a2_color) & 0xf; - red = ((red << 4) + red); - green = ((green << 4) + green); - blue = ((blue << 4) + blue); + red = (a2_color >> 8) & 0xf; + green = (a2_color >> 4) & 0xf; + blue = (a2_color) & 0xf; + red = ((red << 4) + red); + green = ((green << 4) + green); + blue = ((blue << 4) + blue); - newred = red >> g_red_right_shift; - newgreen = green >> g_green_right_shift; - newblue = blue >> g_blue_right_shift; + newred = red >> g_red_right_shift; + newgreen = green >> g_green_right_shift; + newblue = blue >> g_blue_right_shift; - tmp = ((newred & g_red_mask) << g_red_left_shift) + - ((newgreen & g_green_mask) << g_green_left_shift) + - ((newblue & g_blue_mask) << g_blue_left_shift); - g_palette_8to1624[col_num] = tmp; + tmp = ((newred & g_red_mask) << g_red_left_shift) + + ((newgreen & g_green_mask) << g_green_left_shift) + + ((newblue & g_blue_mask) << g_blue_left_shift); + g_palette_8to1624[col_num] = tmp; - x_update_color(col_num, red, green, blue, tmp); + x_update_color(col_num, red, green, blue, tmp); } -void -video_update_color_array(int col_num, int a2_color) -{ - int palette; - int full; +void video_update_color_array(int col_num, int a2_color) { + int palette; + int full; - if(col_num >= 256 || col_num < 0) { - halt_printf("video_update_color_array: col: %03x\n", col_num); - return; - } + if(col_num >= 256 || col_num < 0) { + halt_printf("video_update_color_array: col: %03x\n", col_num); + return; + } - full = g_installed_full_superhires_colormap; + full = g_installed_full_superhires_colormap; - palette = col_num >> 4; - if(!full && palette == g_a2vid_palette) { - return; - } + palette = col_num >> 4; + if(!full && palette == g_a2vid_palette) { + return; + } #if 0 - if(g_screen_depth != 8) { - /* redraw whole superhires for now */ - g_full_refresh_needed = -1; - } + if(g_screen_depth != 8) { + /* redraw whole superhires for now */ + g_full_refresh_needed = -1; + } #endif - video_update_color_raw(col_num, a2_color); + video_update_color_raw(col_num, a2_color); } -void -video_update_colormap() -{ - int palette; - int full; - int i; +void video_update_colormap() { + int palette; + int full; + int i; - full = g_installed_full_superhires_colormap; + full = g_installed_full_superhires_colormap; - if(!full) { - palette = g_a2vid_palette << 4; - for(i = 0; i < 16; i++) { - video_update_color_raw(palette + i, g_lores_colors[i]); - } - x_update_physical_colormap(); - } + if(!full) { + palette = g_a2vid_palette << 4; + for(i = 0; i < 16; i++) { + video_update_color_raw(palette + i, g_lores_colors[i]); + } + x_update_physical_colormap(); + } } -void -video_update_status_line(int line, const char *string) -{ - char *buf; - const char *ptr; - int i; +void video_update_status_line(int line, const char *string) { + char *buf; + const char *ptr; + int i; - if(line >= MAX_STATUS_LINES || line < 0) { - printf("update_status_line: line: %d!\n", line); - exit(1); - } + if(line >= MAX_STATUS_LINES || line < 0) { + printf("update_status_line: line: %d!\n", line); + exit(1); + } - ptr = string; - buf = &(g_status_buf[line][0]); - g_status_ptrs[line] = buf; - for(i = 0; i < STATUS_LINE_LENGTH; i++) { - if(*ptr) { - buf[i] = *ptr++; - } else { - buf[i] = ' '; - } - } + ptr = string; + buf = &(g_status_buf[line][0]); + g_status_ptrs[line] = buf; + for(i = 0; i < STATUS_LINE_LENGTH; i++) { + if(*ptr) { + buf[i] = *ptr++; + } else { + buf[i] = ' '; + } + } - buf[STATUS_LINE_LENGTH] = 0; + buf[STATUS_LINE_LENGTH] = 0; } -void -video_show_debug_info() -{ - word32 tmp1; +void video_show_debug_info() { + word32 tmp1; - printf("g_cur_dcycs: %f, last_vbl: %f\n", g_cur_dcycs, - g_last_vbl_dcycs); - tmp1 = get_lines_since_vbl(g_cur_dcycs); - printf("lines since vbl: %06x\n", tmp1); - printf("Last line updated: %d\n", g_vid_update_last_line); + printf("g_cur_dcycs: %f, last_vbl: %f\n", g_cur_dcycs, + g_last_vbl_dcycs); + tmp1 = get_lines_since_vbl(g_cur_dcycs); + printf("lines since vbl: %06x\n", tmp1); + printf("Last line updated: %d\n", g_vid_update_last_line); } -word32 -float_bus(double dcycs) -{ - word32 val; - int lines_since_vbl; - int line, eff_line, line24; - int all_stat; - int byte_offset; - int hires, page2; - int addr; +word32 float_bus(double dcycs) { + word32 val; + int lines_since_vbl; + int line, eff_line, line24; + int all_stat; + int byte_offset; + int hires, page2; + int addr; - lines_since_vbl = get_lines_since_vbl(dcycs); + lines_since_vbl = get_lines_since_vbl(dcycs); /* For floating bus, model hires style: Visible lines 0-191 are simply the */ /* data being displayed at that time. Lines 192-255 are lines 0 - 63 again */ @@ -3649,46 +3553,46 @@ float_bus(double dcycs) /* nor during veritical blanking. The data seems to be 0 or related to */ /* the instruction fetches on a real IIgs during blankings */ - line = lines_since_vbl >> 8; - byte_offset = lines_since_vbl & 0xff; - /* byte offset is from 0 to 65, where the visible screen is drawn */ - /* from 25 to 65 */ + line = lines_since_vbl >> 8; + byte_offset = lines_since_vbl & 0xff; + /* byte offset is from 0 to 65, where the visible screen is drawn */ + /* from 25 to 65 */ - eff_line = line; - if(line >= 192) { - eff_line = line - 192; - if(line >= 256) { - eff_line = line - 262 + 64; - } - } - all_stat = g_cur_a2_stat; - hires = all_stat & ALL_STAT_HIRES; - if((all_stat & ALL_STAT_MIX_T_GR) && (line >= 160)) { - hires = 0; - } - page2 = EXTRU(all_stat, 31 - BIT_ALL_STAT_PAGE2, 1); - if(all_stat & ALL_STAT_ST80) { - page2 = 0; - } + eff_line = line; + if(line >= 192) { + eff_line = line - 192; + if(line >= 256) { + eff_line = line - 262 + 64; + } + } + all_stat = g_cur_a2_stat; + hires = all_stat & ALL_STAT_HIRES; + if((all_stat & ALL_STAT_MIX_T_GR) && (line >= 160)) { + hires = 0; + } + page2 = EXTRU(all_stat, 31 - BIT_ALL_STAT_PAGE2, 1); + if(all_stat & ALL_STAT_ST80) { + page2 = 0; + } - line24 = (eff_line >> 3) & 0x1f; - addr = g_screen_index[line24] & 0x3ff; - addr = (addr & 0x380) + (((addr & 0x7f) - 25 + byte_offset) & 0x7f); - if(hires) { - addr = 0x2000 + addr + ((eff_line & 7) << 10) + (page2 << 13); - } else { - addr = 0x400 + addr + (page2 << 10); - } + line24 = (eff_line >> 3) & 0x1f; + addr = g_screen_index[line24] & 0x3ff; + addr = (addr & 0x380) + (((addr & 0x7f) - 25 + byte_offset) & 0x7f); + if(hires) { + addr = 0x2000 + addr + ((eff_line & 7) << 10) + (page2 << 13); + } else { + addr = 0x400 + addr + (page2 << 10); + } - val = g_slow_memory_ptr[addr]; - if(byte_offset < 10) { - /* Bob Bishop's sample program seems to get confused by */ - /* these bytes--so mask some off to prevent seeing some */ - val = 0; - } + val = g_slow_memory_ptr[addr]; + if(byte_offset < 10) { + /* Bob Bishop's sample program seems to get confused by */ + /* these bytes--so mask some off to prevent seeing some */ + val = 0; + } #if 0 - printf("For %04x (%d) addr=%04x, val=%02x, dcycs:%9.2f\n", - lines_since_vbl, eff_line, addr, val, dcycs - g_last_vbl_dcycs); + printf("For %04x (%d) addr=%04x, val=%02x, dcycs:%9.2f\n", + lines_since_vbl, eff_line, addr, val, dcycs - g_last_vbl_dcycs); #endif - return val; + return val; } diff --git a/src/win32_host_fst.c b/src/win32_host_fst.c index b5e2366..07f0b49 100644 --- a/src/win32_host_fst.c +++ b/src/win32_host_fst.c @@ -1,7 +1,11 @@ /* - * host_fst.c + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) */ + #define _WIN32_WINNT 0x0600 // vista+ #include @@ -31,44 +35,44 @@ extern Engine_reg engine; #define global_buffer 0x009a00 enum { - regular_file, - resource_file, - directory_file, + regular_file, + resource_file, + directory_file, }; enum { - translate_none, - translate_crlf, - translate_merlin, + translate_none, + translate_crlf, + translate_merlin, }; struct directory { - int displacement; - int num_entries; - char *entries[1]; + int displacement; + int num_entries; + char *entries[1]; }; struct fd_entry { - struct fd_entry *next; - char *path; - int cookie; - int type; - int access; - HANDLE handle; - int translate; - struct directory *dir; + struct fd_entry *next; + char *path; + int cookie; + int type; + int access; + HANDLE handle; + int translate; + struct directory *dir; }; #pragma pack(push, 2) struct AFP_Info { - uint32_t magic; - uint32_t version; - uint32_t file_id; - uint32_t backup_date; - uint8_t finder_info[32]; - uint16_t prodos_file_type; - uint32_t prodos_aux_type; - uint8_t reserved[6]; + uint32_t magic; + uint32_t version; + uint32_t file_id; + uint32_t backup_date; + uint8_t finder_info[32]; + uint16_t prodos_file_type; + uint32_t prodos_aux_type; + uint8_t reserved[6]; }; #pragma pack(pop) @@ -81,55 +85,55 @@ static int finder_info_to_filetype(const byte *buffer, word16 *file_type, word32 static void afp_init(struct AFP_Info *info, word16 file_type, word32 aux_type) { - //static_assert(sizeof(AFP_Info) == 60, "Incorrect AFP_Info size"); - memset(info, 0, sizeof(*info)); - info->magic = 0x00504641; - info->version = 0x00010000; - info->prodos_file_type = file_type; - info->prodos_aux_type = aux_type; - if (file_type || aux_type) - file_type_to_finder_info(info->finder_info, file_type, aux_type); + //static_assert(sizeof(AFP_Info) == 60, "Incorrect AFP_Info size"); + memset(info, 0, sizeof(*info)); + info->magic = 0x00504641; + info->version = 0x00010000; + info->prodos_file_type = file_type; + info->prodos_aux_type = aux_type; + if (file_type || aux_type) + file_type_to_finder_info(info->finder_info, file_type, aux_type); } static BOOL afp_verify(struct AFP_Info *info) { - if (!info) return 0; + if (!info) return 0; - if (info->magic != 0x00504641) return 0; - if (info->version != 0x00010000) return 0; + if (info->magic != 0x00504641) return 0; + if (info->version != 0x00010000) return 0; - return 1; + return 1; } static int afp_to_filetype(struct AFP_Info *info, word16 *file_type, word32 *aux_type) { - // check for prodos ftype/auxtype... - if (info->prodos_file_type || info->prodos_aux_type) { - *file_type = info->prodos_file_type; - *aux_type = info->prodos_aux_type; - return 0; - } - int ok = finder_info_to_filetype(info->finder_info, file_type, aux_type); - if (ok == 0) { - info->prodos_file_type = *file_type; - info->prodos_aux_type = *aux_type; - } - return 0; + // check for prodos ftype/auxtype... + if (info->prodos_file_type || info->prodos_aux_type) { + *file_type = info->prodos_file_type; + *aux_type = info->prodos_aux_type; + return 0; + } + int ok = finder_info_to_filetype(info->finder_info, file_type, aux_type); + if (ok == 0) { + info->prodos_file_type = *file_type; + info->prodos_aux_type = *aux_type; + } + return 0; } enum { prefer_prodos, prefer_hfs }; static void afp_synchronize(struct AFP_Info *info, int preference) { - // if ftype/auxtype is inconsistent between prodos and finder info, use - // prodos as source of truth. - word16 f; - word32 a; - if (finder_info_to_filetype(info->finder_info, &f, &a) != 0) return; - if (f == info->prodos_file_type && a == info->prodos_aux_type) return; - if (preference == prefer_prodos) - file_type_to_finder_info(info->finder_info, info->prodos_file_type, info->prodos_aux_type); - else { - info->prodos_file_type = f; - info->prodos_aux_type = a; - } + // if ftype/auxtype is inconsistent between prodos and finder info, use + // prodos as source of truth. + word16 f; + word32 a; + if (finder_info_to_filetype(info->finder_info, &f, &a) != 0) return; + if (f == info->prodos_file_type && a == info->prodos_aux_type) return; + if (preference == prefer_prodos) + file_type_to_finder_info(info->finder_info, info->prodos_file_type, info->prodos_aux_type); + else { + info->prodos_file_type = f; + info->prodos_aux_type = a; + } } @@ -138,33 +142,33 @@ static void afp_synchronize(struct AFP_Info *info, int preference) { static word32 cookies[32] = {}; static int alloc_cookie() { - for (int i = 0; i < 32; ++i) { - word32 x = cookies[i]; + for (int i = 0; i < 32; ++i) { + word32 x = cookies[i]; - for (int j = 0; j < 32; ++j, x >>= 1) { - if (x & 0x01) continue; + for (int j = 0; j < 32; ++j, x >>= 1) { + if (x & 0x01) continue; - cookies[i] |= (1 << j); - return COOKIE_BASE + (i * 32 + j); - } - } - return -1; + cookies[i] |= (1 << j); + return COOKIE_BASE + (i * 32 + j); + } + } + return -1; } static int free_cookie(int cookie) { - if (cookie < COOKIE_BASE) return -1; - cookie -= COOKIE_BASE; - if (cookie >= 32 * 32) return -1; + if (cookie < COOKIE_BASE) return -1; + cookie -= COOKIE_BASE; + if (cookie >= 32 * 32) return -1; - int chunk = cookie / 32; - int offset = 1 << (cookie % 32); + int chunk = cookie / 32; + int offset = 1 << (cookie % 32); - word32 x = cookies[chunk]; + word32 x = cookies[chunk]; - if ((x & offset) == 0) return -1; - x &= ~offset; - cookies[chunk] = x; - return 0; + if ((x & offset) == 0) return -1; + x &= ~offset; + cookies[chunk] = x; + return 0; } static struct fd_entry *fd_head = NULL; @@ -188,431 +192,430 @@ int g_cfg_host_merlin = 0; static void *gc[16]; static void **gc_ptr = &gc[0]; static void *gc_malloc(size_t size) { - if (gc_ptr == &gc[16]) { - errno = ENOMEM; - return NULL; - } + if (gc_ptr == &gc[16]) { + errno = ENOMEM; + return NULL; + } - void *ptr = malloc(size); - if (ptr) { - *gc_ptr++ = ptr; - } - return ptr; + void *ptr = malloc(size); + if (ptr) { + *gc_ptr++ = ptr; + } + return ptr; } static void gc_free(void) { - while (gc_ptr > gc) free(*--gc_ptr); + while (gc_ptr > gc) free(*--gc_ptr); } static char *append_path(const char *a, const char *b) { - int aa = strlen(a); - int bb = strlen(b); + int aa = strlen(a); + int bb = strlen(b); - char *cp = gc_malloc(aa + bb + 2); - if (!cp) return NULL; - memcpy(cp, a, aa); - int len = aa; - if (len && cp[len-1] != '/' && b[0] != '/') cp[len++] = '/'; - memcpy(cp + len, b, bb); - len += bb; - cp[len] = 0; - while (len > 2 && cp[len - 1] == '/') cp[--len] = 0; - return cp; + char *cp = gc_malloc(aa + bb + 2); + if (!cp) return NULL; + memcpy(cp, a, aa); + int len = aa; + if (len && cp[len-1] != '/' && b[0] != '/') cp[len++] = '/'; + memcpy(cp + len, b, bb); + len += bb; + cp[len] = 0; + while (len > 2 && cp[len - 1] == '/') cp[--len] = 0; + return cp; } static char *append_string(const char *a, const char *b) { - int aa = strlen(a); - int bb = strlen(b); + int aa = strlen(a); + int bb = strlen(b); - char *cp = gc_malloc(aa + bb + 2); - if (!cp) return NULL; - memcpy(cp, a, aa); - int len = aa; - memcpy(cp + len, b, bb); - len += bb; - cp[len] = 0; - return cp; + char *cp = gc_malloc(aa + bb + 2); + if (!cp) return NULL; + memcpy(cp, a, aa); + int len = aa; + memcpy(cp + len, b, bb); + len += bb; + cp[len] = 0; + return cp; } static char *gc_strdup(const char *src) { - if (!src) return ""; - if (!*src) return ""; - int len = strlen(src) + 1; - char *cp = gc_malloc(len); - memcpy(cp, src, len); - return cp; + if (!src) return ""; + if (!*src) return ""; + int len = strlen(src) + 1; + char *cp = gc_malloc(len); + memcpy(cp, src, len); + return cp; } static word32 map_errno() { - switch(errno) { - case 0: return 0; - case EBADF: - return invalidAccess; + switch(errno) { + case 0: return 0; + case EBADF: + return invalidAccess; #ifdef EDQUOT - case EDQUOT: + case EDQUOT: #endif - case EFBIG: - return volumeFull; - case ENOENT: - return fileNotFound; - case ENOTDIR: - return pathNotFound; - case ENOMEM: - return outOfMem; - case EEXIST: - return dupPathname; - default: - return drvrIOError; - } + case EFBIG: + return volumeFull; + case ENOENT: + return fileNotFound; + case ENOTDIR: + return pathNotFound; + case ENOMEM: + return outOfMem; + case EEXIST: + return dupPathname; + default: + return drvrIOError; + } } static word32 map_last_error() { - DWORD e = GetLastError(); - switch (e) { - case ERROR_NO_MORE_FILES: - return endOfDir; - case ERROR_FILE_NOT_FOUND: - return fileNotFound; - case ERROR_PATH_NOT_FOUND: - return pathNotFound; - case ERROR_INVALID_ACCESS: - return invalidAccess; - case ERROR_FILE_EXISTS: - case ERROR_ALREADY_EXISTS: - return dupPathname; - case ERROR_DISK_FULL: - return volumeFull; - case ERROR_INVALID_PARAMETER: - return paramRangeErr; - case ERROR_DRIVE_LOCKED: - return drvrWrtProt; - case ERROR_NEGATIVE_SEEK: - return outOfRange; - case ERROR_SHARING_VIOLATION: - return fileBusy; // destroy open file, etc. - case ERROR_DIR_NOT_EMPTY: - // ... - default: - fprintf(stderr, "GetLastError: %08x - %d\n", (int)e, (int)e); - return drvrIOError; - } + DWORD e = GetLastError(); + switch (e) { + case ERROR_NO_MORE_FILES: + return endOfDir; + case ERROR_FILE_NOT_FOUND: + return fileNotFound; + case ERROR_PATH_NOT_FOUND: + return pathNotFound; + case ERROR_INVALID_ACCESS: + return invalidAccess; + case ERROR_FILE_EXISTS: + case ERROR_ALREADY_EXISTS: + return dupPathname; + case ERROR_DISK_FULL: + return volumeFull; + case ERROR_INVALID_PARAMETER: + return paramRangeErr; + case ERROR_DRIVE_LOCKED: + return drvrWrtProt; + case ERROR_NEGATIVE_SEEK: + return outOfRange; + case ERROR_SHARING_VIOLATION: + return fileBusy; // destroy open file, etc. + case ERROR_DIR_NOT_EMPTY: + // ... + default: + fprintf(stderr, "GetLastError: %08x - %d\n", (int)e, (int)e); + return drvrIOError; + } } static struct fd_entry *find_fd(int cookie) { - struct fd_entry *head = fd_head; + struct fd_entry *head = fd_head; - while (head) { - if (head->cookie == cookie) return head; - head = head->next; - } - return NULL; + while (head) { + if (head->cookie == cookie) return head; + head = head->next; + } + return NULL; } -static void free_fd(struct fd_entry *e) { - if (!e) return; - if (e->cookie) free_cookie(e->cookie); - if (e->handle != INVALID_HANDLE_VALUE) CloseHandle(e->handle); - if (e->dir) free_directory(e->dir); - free(e->path); - free(e); +static void free_fd(struct fd_entry *e) { + if (!e) return; + if (e->cookie) free_cookie(e->cookie); + if (e->handle != INVALID_HANDLE_VALUE) CloseHandle(e->handle); + if (e->dir) free_directory(e->dir); + free(e->path); + free(e); } static struct fd_entry *alloc_fd() { - struct fd_entry *e = calloc(sizeof(struct fd_entry), 1); - e->handle = INVALID_HANDLE_VALUE; - return e; + struct fd_entry *e = calloc(sizeof(struct fd_entry), 1); + e->handle = INVALID_HANDLE_VALUE; + return e; } static word32 remove_fd(int cookie) { - word32 rv = invalidRefNum; + word32 rv = invalidRefNum; - struct fd_entry *prev = NULL; - struct fd_entry *head = fd_head; - while (head) { - if (head->cookie == cookie) { - if (prev) prev->next = head->next; - else fd_head = head->next; + struct fd_entry *prev = NULL; + struct fd_entry *head = fd_head; + while (head) { + if (head->cookie == cookie) { + if (prev) prev->next = head->next; + else fd_head = head->next; - free_fd(head); - rv = 0; - break; - } - prev = head; - head = head->next; - } - return rv; + free_fd(head); + rv = 0; + break; + } + prev = head; + head = head->next; + } + return rv; } static void cr_to_lf(byte *buffer, size_t size) { - size_t i; - for (i = 0; i < size; ++i) { - if (buffer[i] == '\r') buffer[i] = '\n'; - } + size_t i; + for (i = 0; i < size; ++i) { + if (buffer[i] == '\r') buffer[i] = '\n'; + } } static void lf_to_cr(byte *buffer, size_t size) { - size_t i; - for (i = 0; i < size; ++i) { - if (buffer[i] == '\n') buffer[i] = '\r'; - } + size_t i; + for (i = 0; i < size; ++i) { + if (buffer[i] == '\n') buffer[i] = '\r'; + } } static void merlin_to_text(byte *buffer, size_t size) { - size_t i; - for (i = 0; i < size; ++i) { - byte b = buffer[i]; - if (b == 0xa0) b = '\t'; - b &= 0x7f; - if (b == '\r') b = '\n'; - buffer[i] = b; - } + size_t i; + for (i = 0; i < size; ++i) { + byte b = buffer[i]; + if (b == 0xa0) b = '\t'; + b &= 0x7f; + if (b == '\r') b = '\n'; + buffer[i] = b; + } } static void text_to_merlin(byte *buffer, size_t size) { - size_t i; - for (i = 0; i < size; ++i) { - byte b = buffer[i]; - if (b == '\t') b = 0xa0; - if (b == '\n') b = '\r'; - if (b != ' ') b |= 0x80; - buffer[i] = b; - } + size_t i; + for (i = 0; i < size; ++i) { + byte b = buffer[i]; + if (b == '\t') b = 0xa0; + if (b == '\n') b = '\r'; + if (b != ' ') b |= 0x80; + buffer[i] = b; + } } struct file_info { - FILETIME create_date; - FILETIME modified_date; + FILETIME create_date; + FILETIME modified_date; - word16 access; - word16 storage_type; - word16 file_type; - word32 aux_type; - word32 eof; - word32 blocks; - word32 resource_eof; - word32 resource_blocks; - int has_fi; - //byte finder_info[32]; - struct AFP_Info afp; + word16 access; + word16 storage_type; + word16 file_type; + word32 aux_type; + word32 eof; + word32 blocks; + word32 resource_eof; + word32 resource_blocks; + int has_fi; + //byte finder_info[32]; + struct AFP_Info afp; }; -static int hex(byte c) -{ - if (c >= '0' && c <= '9') return c - '0'; - if (c >= 'a' && c <= 'f') return c + 10 - 'a'; - if (c >= 'A' && c <= 'F') return c + 10 - 'A'; - return 0; +static int hex(byte c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c + 10 - 'a'; + if (c >= 'A' && c <= 'F') return c + 10 - 'A'; + return 0; } static int finder_info_to_filetype(const byte *buffer, word16 *file_type, word32 *aux_type) { - if (!memcmp("pdos", buffer + 4, 4)) - { - if (buffer[0] == 'p') { - *file_type = buffer[1]; - *aux_type = (buffer[2] << 8) | buffer[3]; - return 0; - } - if (!memcmp("PSYS", buffer, 4)) { - *file_type = 0xff; - *aux_type = 0x0000; - return 0; - } - if (!memcmp("PS16", buffer, 4)) { - *file_type = 0xb3; - *aux_type = 0x0000; - return 0; - } + if (!memcmp("pdos", buffer + 4, 4)) + { + if (buffer[0] == 'p') { + *file_type = buffer[1]; + *aux_type = (buffer[2] << 8) | buffer[3]; + return 0; + } + if (!memcmp("PSYS", buffer, 4)) { + *file_type = 0xff; + *aux_type = 0x0000; + return 0; + } + if (!memcmp("PS16", buffer, 4)) { + *file_type = 0xb3; + *aux_type = 0x0000; + return 0; + } - // old mpw method for encoding. - if (!isxdigit(buffer[0]) && isxdigit(buffer[1]) && buffer[2] == ' ' && buffer[3] == ' ') - { - *file_type = (hex(buffer[0]) << 8) | hex(buffer[1]); - *aux_type = 0; - return 0; - } - } - if (!memcmp("TEXT", buffer, 4)) { - *file_type = 0x04; - *aux_type = 0x0000; - return 0; - } - if (!memcmp("BINA", buffer, 4)) { - *file_type = 0x00; - *aux_type = 0x0000; - return 0; - } - if (!memcmp("dImgdCpy", buffer, 8)) { - *file_type = 0xe0; - *aux_type = 0x0005; - return 0; - } + // old mpw method for encoding. + if (!isxdigit(buffer[0]) && isxdigit(buffer[1]) && buffer[2] == ' ' && buffer[3] == ' ') + { + *file_type = (hex(buffer[0]) << 8) | hex(buffer[1]); + *aux_type = 0; + return 0; + } + } + if (!memcmp("TEXT", buffer, 4)) { + *file_type = 0x04; + *aux_type = 0x0000; + return 0; + } + if (!memcmp("BINA", buffer, 4)) { + *file_type = 0x00; + *aux_type = 0x0000; + return 0; + } + if (!memcmp("dImgdCpy", buffer, 8)) { + *file_type = 0xe0; + *aux_type = 0x0005; + return 0; + } - if (!memcmp("MIDI", buffer, 4)) { - *file_type = 0xd7; - *aux_type = 0x0000; - return 0; - } + if (!memcmp("MIDI", buffer, 4)) { + *file_type = 0xd7; + *aux_type = 0x0000; + return 0; + } - if (!memcmp("AIFF", buffer, 4)) { - *file_type = 0xd8; - *aux_type = 0x0000; - return 0; - } + if (!memcmp("AIFF", buffer, 4)) { + *file_type = 0xd8; + *aux_type = 0x0000; + return 0; + } - if (!memcmp("AIFC", buffer, 4)) { - *file_type = 0xd8; - *aux_type = 0x0001; - return 0; - } + if (!memcmp("AIFC", buffer, 4)) { + *file_type = 0xd8; + *aux_type = 0x0001; + return 0; + } - return -1; + return -1; } static int file_type_to_finder_info(byte *buffer, word16 file_type, word32 aux_type) { - if (file_type > 0xff || aux_type > 0xffff) return -1; + if (file_type > 0xff || aux_type > 0xffff) return -1; - if (!file_type && aux_type == 0x0000) { - memcpy(buffer, "BINApdos", 8); - return 0; - } + if (!file_type && aux_type == 0x0000) { + memcpy(buffer, "BINApdos", 8); + return 0; + } - if (file_type == 0x04 && aux_type == 0x0000) { - memcpy(buffer, "TEXTpdos", 8); - return 0; - } + if (file_type == 0x04 && aux_type == 0x0000) { + memcpy(buffer, "TEXTpdos", 8); + return 0; + } - if (file_type == 0xff && aux_type == 0x0000) { - memcpy(buffer, "PSYSpdos", 8); - return 0; - } + if (file_type == 0xff && aux_type == 0x0000) { + memcpy(buffer, "PSYSpdos", 8); + return 0; + } - if (file_type == 0xb3 && aux_type == 0x0000) { - memcpy(buffer, "PS16pdos", 8); - return 0; - } + if (file_type == 0xb3 && aux_type == 0x0000) { + memcpy(buffer, "PS16pdos", 8); + return 0; + } - if (file_type == 0xd7 && aux_type == 0x0000) { - memcpy(buffer, "MIDIpdos", 8); - return 0; - } - if (file_type == 0xd8 && aux_type == 0x0000) { - memcpy(buffer, "AIFFpdos", 8); - return 0; - } - if (file_type == 0xd8 && aux_type == 0x0001) { - memcpy(buffer, "AIFCpdos", 8); - return 0; - } - if (file_type == 0xe0 && aux_type == 0x0005) { - memcpy(buffer, "dImgdCpy", 8); - return 0; - } + if (file_type == 0xd7 && aux_type == 0x0000) { + memcpy(buffer, "MIDIpdos", 8); + return 0; + } + if (file_type == 0xd8 && aux_type == 0x0000) { + memcpy(buffer, "AIFFpdos", 8); + return 0; + } + if (file_type == 0xd8 && aux_type == 0x0001) { + memcpy(buffer, "AIFCpdos", 8); + return 0; + } + if (file_type == 0xe0 && aux_type == 0x0005) { + memcpy(buffer, "dImgdCpy", 8); + return 0; + } - memcpy(buffer, "p pdos", 8); - buffer[1] = (file_type) & 0xff; - buffer[2] = (aux_type >> 8) & 0xff; - buffer[3] = (aux_type) & 0xff; - return 0; + memcpy(buffer, "p pdos", 8); + buffer[1] = (file_type) & 0xff; + buffer[2] = (aux_type >> 8) & 0xff; + buffer[3] = (aux_type) & 0xff; + return 0; } static void get_file_xinfo(const char *path, struct file_info *fi) { - HANDLE h; + HANDLE h; - char *p = append_string(path, ":AFP_Resource"); - LARGE_INTEGER size = { 0 }; + char *p = append_string(path, ":AFP_Resource"); + LARGE_INTEGER size = { 0 }; - h = CreateFile(p, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); - if (h != INVALID_HANDLE_VALUE) { - GetFileSizeEx(h, &size); - CloseHandle(h); - } - fi->resource_eof = size.LowPart; - fi->resource_blocks = (size.LowPart + 511) / 512; + h = CreateFile(p, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (h != INVALID_HANDLE_VALUE) { + GetFileSizeEx(h, &size); + CloseHandle(h); + } + fi->resource_eof = size.LowPart; + fi->resource_blocks = (size.LowPart + 511) / 512; - p = append_string(path, ":AFP_AfpInfo"); + p = append_string(path, ":AFP_AfpInfo"); - h = CreateFile(p, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + h = CreateFile(p, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (h != INVALID_HANDLE_VALUE) { - DWORD read = 0; - if (ReadFile(h, &fi->afp, sizeof(struct AFP_Info), &read, NULL) && read == sizeof(struct AFP_Info)) { - if (afp_verify(&fi->afp)) fi->has_fi = 1; - afp_to_filetype(&fi->afp, &fi->file_type, &fi->aux_type); - } - CloseHandle(h); - } + if (h != INVALID_HANDLE_VALUE) { + DWORD read = 0; + if (ReadFile(h, &fi->afp, sizeof(struct AFP_Info), &read, NULL) && read == sizeof(struct AFP_Info)) { + if (afp_verify(&fi->afp)) fi->has_fi = 1; + afp_to_filetype(&fi->afp, &fi->file_type, &fi->aux_type); + } + CloseHandle(h); + } } static word16 map_attributes(DWORD dwFileAttributes) { - // 0x01 = read enable - // 0x02 = write enable - // 0x04 = invisible - // 0x08 = reserved - // 0x10 = reserved - // 0x20 = backup needed - // 0x40 = rename enable - // 0x80 = destroy enable + // 0x01 = read enable + // 0x02 = write enable + // 0x04 = invisible + // 0x08 = reserved + // 0x10 = reserved + // 0x20 = backup needed + // 0x40 = rename enable + // 0x80 = destroy enable - word16 access = 0; + word16 access = 0; - if (dwFileAttributes & FILE_ATTRIBUTE_READONLY) - access = readEnable; - else - access = readEnable | writeEnable | renameEnable | destroyEnable; + if (dwFileAttributes & FILE_ATTRIBUTE_READONLY) + access = readEnable; + else + access = readEnable | writeEnable | renameEnable | destroyEnable; - if (dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) - access |= fileInvisible; + if (dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) + access |= fileInvisible; - // map FILE_ATTRIBUTE_ARCHIVE to backup needed bit? + // map FILE_ATTRIBUTE_ARCHIVE to backup needed bit? - return access; + return access; } #undef _ #define _(a, b, c) { a, sizeof(a) - 1, b, c } struct ftype_entry { - char *ext; - unsigned length; - unsigned file_type; - unsigned aux_type; + char *ext; + unsigned length; + unsigned file_type; + unsigned aux_type; }; static struct ftype_entry suffixes[] = { - _("c", 0xb0, 0x0008), - _("cc", 0xb0, 0x0008), - _("h", 0xb0, 0x0008), - _("rez", 0xb0, 0x0015), - _("asm", 0xb0, 0x0003), - _("mac", 0xb0, 0x0003), - _("pas", 0xb0, 0x0005), - _("txt", 0x04, 0x0000), - _("text", 0x04, 0x0000), - _("s", 0x04, 0x0000), - { 0, 0, 0, 0} + _("c", 0xb0, 0x0008), + _("cc", 0xb0, 0x0008), + _("h", 0xb0, 0x0008), + _("rez", 0xb0, 0x0015), + _("asm", 0xb0, 0x0003), + _("mac", 0xb0, 0x0003), + _("pas", 0xb0, 0x0005), + _("txt", 0x04, 0x0000), + _("text", 0x04, 0x0000), + _("s", 0x04, 0x0000), + { 0, 0, 0, 0} }; static struct ftype_entry prefixes[] = { - _("m16.", 0xb0, 0x0003), - _("e16.", 0xb0, 0x0003), - { 0, 0, 0, 0} + _("m16.", 0xb0, 0x0003), + _("e16.", 0xb0, 0x0003), + { 0, 0, 0, 0} }; #undef _ @@ -622,172 +625,172 @@ static struct ftype_entry prefixes[] = { static word32 get_file_info(const char *path, struct file_info *fi) { - HANDLE h = CreateFile(path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); - if (h == INVALID_HANDLE_VALUE) return map_last_error(); + HANDLE h = CreateFile(path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (h == INVALID_HANDLE_VALUE) return map_last_error(); - //FILE_BASIC_INFO fbi; - //FILE_STANDARD_INFO fsi; - //FILE_ID_INFO fii; - BY_HANDLE_FILE_INFORMATION info; + //FILE_BASIC_INFO fbi; + //FILE_STANDARD_INFO fsi; + //FILE_ID_INFO fii; + BY_HANDLE_FILE_INFORMATION info; - memset(fi, 0, sizeof(*fi)); - //memset(&fbi, 0, sizeof(fbi)); - //memset(&fsi, 0, sizeof(fsi)); + memset(fi, 0, sizeof(*fi)); + //memset(&fbi, 0, sizeof(fbi)); + //memset(&fsi, 0, sizeof(fsi)); - GetFileInformationByHandle(h, &info); - //GetFileInformationByHandleEx(h, FileBasicInfo, &fbi, sizeof(fbi)); - //GetFileInformationByHandleEx(h, FileStandardInfo, &fsi, sizeof(fsi)); - //GetFileInformationByHandleEx(h, FileIdInfo, &fii, sizeof(fii)); + GetFileInformationByHandle(h, &info); + //GetFileInformationByHandleEx(h, FileBasicInfo, &fbi, sizeof(fbi)); + //GetFileInformationByHandleEx(h, FileStandardInfo, &fsi, sizeof(fsi)); + //GetFileInformationByHandleEx(h, FileIdInfo, &fii, sizeof(fii)); - word32 size = info.nFileSizeLow; + word32 size = info.nFileSizeLow; - fi->eof = size; - fi->blocks = (size + 511) / 512; + fi->eof = size; + fi->blocks = (size + 511) / 512; - fi->create_date = info.ftCreationTime; - fi->modified_date = info.ftLastWriteTime; + fi->create_date = info.ftCreationTime; + fi->modified_date = info.ftLastWriteTime; - if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { - fi->storage_type = directoryFile; - fi->file_type = 0x0f; + if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + fi->storage_type = directoryFile; + fi->file_type = 0x0f; - DWORD id[3] = { info.dwVolumeSerialNumber, info.nFileIndexHigh, info.nFileIndexLow }; - if (memcmp(&id, &root_file_id, sizeof(root_file_id)) == 0) { - fi->storage_type = 0x0f; - } - } else { - fi->file_type = 0x06; - if (size < 0x200) fi->storage_type = seedling; - else if (size < 0x20000) fi->storage_type = sapling; - else fi->storage_type = tree; + DWORD id[3] = { info.dwVolumeSerialNumber, info.nFileIndexHigh, info.nFileIndexLow }; + if (memcmp(&id, &root_file_id, sizeof(root_file_id)) == 0) { + fi->storage_type = 0x0f; + } + } else { + fi->file_type = 0x06; + if (size < 0x200) fi->storage_type = seedling; + else if (size < 0x20000) fi->storage_type = sapling; + else fi->storage_type = tree; - get_file_xinfo(path, fi); - if (fi->resource_eof) fi->storage_type = extendedFile; + get_file_xinfo(path, fi); + if (fi->resource_eof) fi->storage_type = extendedFile; - if (!fi->has_fi) { - /* guess the file type / auxtype based on extension */ - int n; - const char *dot = NULL; - const char *slash = NULL; + if (!fi->has_fi) { + /* guess the file type / auxtype based on extension */ + int n; + const char *dot = NULL; + const char *slash = NULL; - for(n = 0; ; ++n) { - char c = path[n]; - if (c == 0) break; - else if (c == '/') { slash = path + n + 1; dot = NULL; } - else if (c == '.') dot = path + n + 1; - } + for(n = 0;; ++n) { + char c = path[n]; + if (c == 0) break; + else if (c == '/') { slash = path + n + 1; dot = NULL; } + else if (c == '.') dot = path + n + 1; + } - if (dot && *dot) { - for (n = 0; n < sizeof(suffixes) / sizeof(suffixes[0]); ++n) { - if (!suffixes[n].ext) break; - if (!strcasecmp(dot, suffixes[n].ext)) { - fi->file_type = suffixes[n].file_type; - fi->aux_type = suffixes[n].aux_type; - break; - } - } - } - } - } + if (dot && *dot) { + for (n = 0; n < sizeof(suffixes) / sizeof(suffixes[0]); ++n) { + if (!suffixes[n].ext) break; + if (!strcasecmp(dot, suffixes[n].ext)) { + fi->file_type = suffixes[n].file_type; + fi->aux_type = suffixes[n].aux_type; + break; + } + } + } + } + } - // 0x01 = read enable - // 0x02 = write enable - // 0x04 = invisible - // 0x08 = reserved - // 0x10 = reserved - // 0x20 = backup needed - // 0x40 = rename enable - // 0x80 = destroy enable + // 0x01 = read enable + // 0x02 = write enable + // 0x04 = invisible + // 0x08 = reserved + // 0x10 = reserved + // 0x20 = backup needed + // 0x40 = rename enable + // 0x80 = destroy enable - word16 access = 0; + word16 access = 0; - if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) - access = 0x01; - else - access = 0x01 | 0x02 | 0x40 | 0x80; + if (info.dwFileAttributes & FILE_ATTRIBUTE_READONLY) + access = 0x01; + else + access = 0x01 | 0x02 | 0x40 | 0x80; - if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) - access |= 0x04; + if (info.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) + access |= 0x04; - // map FILE_ATTRIBUTE_ARCHIVE to backup needed bit? + // map FILE_ATTRIBUTE_ARCHIVE to backup needed bit? - fi->access = map_attributes(info.dwFileAttributes); + fi->access = map_attributes(info.dwFileAttributes); - CloseHandle(h); - return 0; + CloseHandle(h); + return 0; } static word32 set_file_info(const char *path, struct file_info *fi) { - if (fi->has_fi && fi->storage_type != 0x0d && fi->storage_type != 0x0f) { - char *rpath = append_string(path, ":AFP_AfpInfo"); + if (fi->has_fi && fi->storage_type != 0x0d && fi->storage_type != 0x0f) { + char *rpath = append_string(path, ":AFP_AfpInfo"); - HANDLE h = CreateFile(rpath, GENERIC_WRITE, - FILE_SHARE_READ , NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - if (h == INVALID_HANDLE_VALUE) return map_last_error(); + HANDLE h = CreateFile(rpath, GENERIC_WRITE, + FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (h == INVALID_HANDLE_VALUE) return map_last_error(); - WriteFile(h, &fi->afp, sizeof(struct AFP_Info), NULL, NULL); - CloseHandle(h); - } + WriteFile(h, &fi->afp, sizeof(struct AFP_Info), NULL, NULL); + CloseHandle(h); + } - if (fi->create_date.dwLowDateTime || fi->create_date.dwHighDateTime - || fi->modified_date.dwLowDateTime || fi->modified_date.dwHighDateTime) { - // SetFileInformationByHandle can modify dates. - HANDLE h; - h = CreateFile(path, FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - if (h == INVALID_HANDLE_VALUE) return map_last_error(); + if (fi->create_date.dwLowDateTime || fi->create_date.dwHighDateTime + || fi->modified_date.dwLowDateTime || fi->modified_date.dwHighDateTime) { + // SetFileInformationByHandle can modify dates. + HANDLE h; + h = CreateFile(path, FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (h == INVALID_HANDLE_VALUE) return map_last_error(); - FILE_BASIC_INFO fbi; - FILE_BASIC_INFO newfbi; - memset(&fbi, 0, sizeof(fbi)); - memset(&newfbi, 0, sizeof(newfbi)); + FILE_BASIC_INFO fbi; + FILE_BASIC_INFO newfbi; + memset(&fbi, 0, sizeof(fbi)); + memset(&newfbi, 0, sizeof(newfbi)); - BOOL ok; - ok = GetFileInformationByHandleEx(h, FileBasicInfo, &fbi, sizeof(fbi)); + BOOL ok; + ok = GetFileInformationByHandleEx(h, FileBasicInfo, &fbi, sizeof(fbi)); - int delta = 0; + int delta = 0; - word16 old_access = map_attributes(fbi.FileAttributes); - if (fi->access && fi->access != old_access) { - newfbi.FileAttributes = fbi.FileAttributes; + word16 old_access = map_attributes(fbi.FileAttributes); + if (fi->access && fi->access != old_access) { + newfbi.FileAttributes = fbi.FileAttributes; - if (fi->access & fileInvisible) { - delta = 1; - newfbi.FileAttributes |= FILE_ATTRIBUTE_HIDDEN; - } - // hfs fst only marks it read enable if all are clear. - word16 locked = writeEnable | destroyEnable | renameEnable; - if ((fi->access & locked) == 0) { - delta = 1; - newfbi.FileAttributes |= FILE_ATTRIBUTE_READONLY; - } - } + if (fi->access & fileInvisible) { + delta = 1; + newfbi.FileAttributes |= FILE_ATTRIBUTE_HIDDEN; + } + // hfs fst only marks it read enable if all are clear. + word16 locked = writeEnable | destroyEnable | renameEnable; + if ((fi->access & locked) == 0) { + delta = 1; + newfbi.FileAttributes |= FILE_ATTRIBUTE_READONLY; + } + } - // todo -- compare against nt file time to see if it's actually changed. - // to prevent time stamp truncation. + // todo -- compare against nt file time to see if it's actually changed. + // to prevent time stamp truncation. - if (fi->create_date.dwLowDateTime || fi->create_date.dwHighDateTime) { - delta = 1; - newfbi.CreationTime.LowPart = fi->create_date.dwLowDateTime; - newfbi.CreationTime.HighPart = fi->create_date.dwHighDateTime; - } - if (fi->modified_date.dwLowDateTime || fi->modified_date.dwHighDateTime) { - delta = 1; - newfbi.LastWriteTime.LowPart = fi->modified_date.dwLowDateTime; - newfbi.LastWriteTime.HighPart = fi->modified_date.dwHighDateTime; - //newfbi.ChangeTime.LowPart = fi->modified_date.dwLowDateTime; //? - //newfbi.ChangeTime.HighPart = fi->modified_date.dwHighDateTime; //? - } + if (fi->create_date.dwLowDateTime || fi->create_date.dwHighDateTime) { + delta = 1; + newfbi.CreationTime.LowPart = fi->create_date.dwLowDateTime; + newfbi.CreationTime.HighPart = fi->create_date.dwHighDateTime; + } + if (fi->modified_date.dwLowDateTime || fi->modified_date.dwHighDateTime) { + delta = 1; + newfbi.LastWriteTime.LowPart = fi->modified_date.dwLowDateTime; + newfbi.LastWriteTime.HighPart = fi->modified_date.dwHighDateTime; + //newfbi.ChangeTime.LowPart = fi->modified_date.dwLowDateTime; //? + //newfbi.ChangeTime.HighPart = fi->modified_date.dwHighDateTime; //? + } - if (delta) - ok = SetFileInformationByHandle(h, FileBasicInfo, &newfbi, sizeof(newfbi)); - CloseHandle(h); - } - return 0; + if (delta) + ok = SetFileInformationByHandle(h, FileBasicInfo, &newfbi, sizeof(newfbi)); + CloseHandle(h); + } + return 0; } /* @@ -796,204 +799,203 @@ static word32 set_file_info(const char *path, struct file_info *fi) { */ static const char *check_path(const char *in, word32 *error) { - word32 tmp; - if (!error) error = &tmp; + word32 tmp; + if (!error) error = &tmp; - *error = 0; - if (!in) return ""; + *error = 0; + if (!in) return ""; - /* check for .. */ - const char *cp = in; - do { - while (*cp == '/') ++cp; - if (cp[0] == '.' && cp[1] == '.' && (cp[2] == '/' || cp[2] == 0)) - { - *error = badPathSyntax; - return NULL; - } - cp = strchr(cp, '/'); - } while(cp); + /* check for .. */ + const char *cp = in; + do { + while (*cp == '/') ++cp; + if (cp[0] == '.' && cp[1] == '.' && (cp[2] == '/' || cp[2] == 0)) + { + *error = badPathSyntax; + return NULL; + } + cp = strchr(cp, '/'); + } while(cp); - if (in[0] != '/') return in; + if (in[0] != '/') return in; - if (strncasecmp(in, "/Host", 5) == 0 && (in[5] == '/' || in[5] == 0)) { - in += 5; - while (*in == '/') ++in; - return in; - } - *error = volNotFound; - return NULL; + if (strncasecmp(in, "/Host", 5) == 0 && (in[5] == '/' || in[5] == 0)) { + in += 5; + while (*in == '/') ++in; + return in; + } + *error = volNotFound; + return NULL; } static char * get_gsstr(word32 ptr) { - if (!ptr) return NULL; - int length = get_memory16_c(ptr, 0); - ptr += 2; - char *str = gc_malloc(length + 1); - for (int i = 0; i < length; ++i) { - char c = get_memory_c(ptr+i, 0); - if (c == ':') c = '/'; - str[i] = c; - } - str[length] = 0; - return str; + if (!ptr) return NULL; + int length = get_memory16_c(ptr, 0); + ptr += 2; + char *str = gc_malloc(length + 1); + for (int i = 0; i < length; ++i) { + char c = get_memory_c(ptr+i, 0); + if (c == ':') c = '/'; + str[i] = c; + } + str[length] = 0; + return str; } static char * get_pstr(word32 ptr) { - if (!ptr) return NULL; - int length = get_memory16_c(ptr, 0); - ptr += 2; - char *str = gc_malloc(length + 1); - for (int i = 0; i < length; ++i) { - char c = get_memory_c(ptr+i, 0); - if (c == ':') c = '/'; - str[i] = c; - } - str[length] = 0; - return str; + if (!ptr) return NULL; + int length = get_memory16_c(ptr, 0); + ptr += 2; + char *str = gc_malloc(length + 1); + for (int i = 0; i < length; ++i) { + char c = get_memory_c(ptr+i, 0); + if (c == ':') c = '/'; + str[i] = c; + } + str[length] = 0; + return str; } static word32 set_gsstr(word32 ptr, const char *str) { - if (!ptr) return paramRangeErr; + if (!ptr) return paramRangeErr; - int l = str ? strlen(str) : 0; + int l = str ? strlen(str) : 0; - word32 cap = get_memory16_c(ptr, 0); - ptr += 2; + word32 cap = get_memory16_c(ptr, 0); + ptr += 2; - if (cap < 4) return paramRangeErr; + if (cap < 4) return paramRangeErr; - set_memory16_c(ptr, l, 0); - ptr += 2; + set_memory16_c(ptr, l, 0); + ptr += 2; - if (cap < l + 4) return buffTooSmall; + if (cap < l + 4) return buffTooSmall; - for (int i = 0; i < l; ++i) { - char c = *str++; - if (c == '/') c = ':'; - set_memory_c(ptr++, c, 0); - } - return 0; + for (int i = 0; i < l; ++i) { + char c = *str++; + if (c == '/') c = ':'; + set_memory_c(ptr++, c, 0); + } + return 0; } static word32 set_gsstr_truncate(word32 ptr, const char *str) { - if (!ptr) return paramRangeErr; + if (!ptr) return paramRangeErr; - int l = str ? strlen(str) : 0; + int l = str ? strlen(str) : 0; - word32 cap = get_memory16_c(ptr, 0); - ptr += 2; + word32 cap = get_memory16_c(ptr, 0); + ptr += 2; - if (cap < 4) return paramRangeErr; + if (cap < 4) return paramRangeErr; - set_memory16_c(ptr, l, 0); - ptr += 2; + set_memory16_c(ptr, l, 0); + ptr += 2; - // get dir entry copies data even - // if buffTooSmall... - int rv = 0; - if (cap < l + 4) { - l = cap - 4; - rv = buffTooSmall; - } + // get dir entry copies data even + // if buffTooSmall... + int rv = 0; + if (cap < l + 4) { + l = cap - 4; + rv = buffTooSmall; + } - for (int i = 0; i < l; ++i) { - char c = *str++; - if (c == '/') c = ':'; - set_memory_c(ptr++, c, 0); - } - return rv; + for (int i = 0; i < l; ++i) { + char c = *str++; + if (c == '/') c = ':'; + set_memory_c(ptr++, c, 0); + } + return rv; } static word32 set_pstr(word32 ptr, const char *str) { - if (!ptr) return paramRangeErr; + if (!ptr) return paramRangeErr; - int l = str ? strlen(str) : 0; + int l = str ? strlen(str) : 0; - if (l > 255) return buffTooSmall; - // / is the pascal separator. - set_memory_c(ptr++, l, 0); - for (int i = 0; i < l; ++i) { - set_memory_c(ptr++, *str++, 0); - } - return 0; + if (l > 255) return buffTooSmall; + // / is the pascal separator. + set_memory_c(ptr++, l, 0); + for (int i = 0; i < l; ++i) { + set_memory_c(ptr++, *str++, 0); + } + return 0; } static word32 set_option_list(word32 ptr, word16 fstID, const byte *data, int size) { - if (!ptr) return 0; + if (!ptr) return 0; - // totalSize - word32 cap = get_memory16_c(ptr, 0); - ptr += 2; + // totalSize + word32 cap = get_memory16_c(ptr, 0); + ptr += 2; - if (cap < 4) return paramRangeErr; + if (cap < 4) return paramRangeErr; - // reqSize - set_memory16_c(ptr, size + 2, 0); - ptr += 2; + // reqSize + set_memory16_c(ptr, size + 2, 0); + ptr += 2; - if (cap < size + 6) return buffTooSmall; + if (cap < size + 6) return buffTooSmall; - // fileSysID. - set_memory16_c(ptr, fstID, 0); - ptr += 2; + // fileSysID. + set_memory16_c(ptr, fstID, 0); + ptr += 2; - for (int i = 0; i < size; ++i) - set_memory_c(ptr++, *data++, 0); + for (int i = 0; i < size; ++i) + set_memory_c(ptr++, *data++, 0); - return 0; + return 0; } -static int dayofweek(int y, int m, int d) /* 1 <= m <= 12, y > 1752 (in the U.K.) */ - { - static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; - y -= m < 3; - return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7; - } +static int dayofweek(int y, int m, int d) { /* 1 <= m <= 12, y > 1752 (in the U.K.) */ + static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; + y -= m < 3; + return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7; +} /* * converts time_t to a gs/os readhextime date/time record. */ static void set_date_time_rec(word32 ptr, FILETIME utc) { - if (utc.dwLowDateTime == 0 && utc.dwHighDateTime == 0) { - for (int i = 0; i < 8; ++i) set_memory_c(ptr++, 0, 0); - return; - } + if (utc.dwLowDateTime == 0 && utc.dwHighDateTime == 0) { + for (int i = 0; i < 8; ++i) set_memory_c(ptr++, 0, 0); + return; + } - SYSTEMTIME tmLocal; - SYSTEMTIME tmUTC; + SYSTEMTIME tmLocal; + SYSTEMTIME tmUTC; - FileTimeToSystemTime(&utc, &tmUTC); - SystemTimeToTzSpecificLocalTime(NULL, &tmUTC, &tmLocal); + FileTimeToSystemTime(&utc, &tmUTC); + SystemTimeToTzSpecificLocalTime(NULL, &tmUTC, &tmLocal); - if (tmLocal.wYear < 1900 || tmLocal.wYear > 1900 + 255) { - for (int i = 0; i < 8; ++i) set_memory_c(ptr++, 0, 0); - return; - } - if (tmLocal.wSecond == 60) tmLocal.wSecond = 59; /* leap second */ + if (tmLocal.wYear < 1900 || tmLocal.wYear > 1900 + 255) { + for (int i = 0; i < 8; ++i) set_memory_c(ptr++, 0, 0); + return; + } + if (tmLocal.wSecond == 60) tmLocal.wSecond = 59; /* leap second */ - int dow = dayofweek(tmLocal.wYear, tmLocal.wMonth, tmLocal.wDay); - set_memory_c(ptr++, tmLocal.wSecond, 0); - set_memory_c(ptr++, tmLocal.wMinute, 0); - set_memory_c(ptr++, tmLocal.wHour, 0); - set_memory_c(ptr++, tmLocal.wYear - 1900, 0); - set_memory_c(ptr++, tmLocal.wDay - 1, 0); // 1 = sunday - set_memory_c(ptr++, tmLocal.wMonth - 1, 0); - set_memory_c(ptr++, 0, 0); - set_memory_c(ptr++, dow + 1, 0); + int dow = dayofweek(tmLocal.wYear, tmLocal.wMonth, tmLocal.wDay); + set_memory_c(ptr++, tmLocal.wSecond, 0); + set_memory_c(ptr++, tmLocal.wMinute, 0); + set_memory_c(ptr++, tmLocal.wHour, 0); + set_memory_c(ptr++, tmLocal.wYear - 1900, 0); + set_memory_c(ptr++, tmLocal.wDay - 1, 0); // 1 = sunday + set_memory_c(ptr++, tmLocal.wMonth - 1, 0); + set_memory_c(ptr++, 0, 0); + set_memory_c(ptr++, dow + 1, 0); } /* @@ -1001,114 +1003,114 @@ static void set_date_time_rec(word32 ptr, FILETIME utc) { */ static void set_date_time(word32 ptr, FILETIME utc) { - if (utc.dwLowDateTime == 0 && utc.dwHighDateTime == 0) { - for (int i = 0; i < 4; ++i) set_memory_c(ptr++, 0, 0); - return; - } + if (utc.dwLowDateTime == 0 && utc.dwHighDateTime == 0) { + for (int i = 0; i < 4; ++i) set_memory_c(ptr++, 0, 0); + return; + } - SYSTEMTIME tmLocal; - SYSTEMTIME tmUTC; + SYSTEMTIME tmLocal; + SYSTEMTIME tmUTC; - FileTimeToSystemTime(&utc, &tmUTC); - SystemTimeToTzSpecificLocalTime(NULL, &tmUTC, &tmLocal); + FileTimeToSystemTime(&utc, &tmUTC); + SystemTimeToTzSpecificLocalTime(NULL, &tmUTC, &tmLocal); - if (tmLocal.wYear < 1940 || tmLocal.wYear > 2039) { - for (int i = 0; i < 4; ++i) set_memory_c(ptr++, 0, 0); - return; - } - if (tmLocal.wSecond == 60) tmLocal.wSecond = 59; /* leap second */ + if (tmLocal.wYear < 1940 || tmLocal.wYear > 2039) { + for (int i = 0; i < 4; ++i) set_memory_c(ptr++, 0, 0); + return; + } + if (tmLocal.wSecond == 60) tmLocal.wSecond = 59; /* leap second */ - word16 tmp = 0; - tmp |= (tmLocal.wYear % 100) << 9; - tmp |= tmLocal.wMonth << 5; - tmp |= tmLocal.wDay; + word16 tmp = 0; + tmp |= (tmLocal.wYear % 100) << 9; + tmp |= tmLocal.wMonth << 5; + tmp |= tmLocal.wDay; - set_memory16_c(ptr, tmp, 0); - ptr += 2; + set_memory16_c(ptr, tmp, 0); + ptr += 2; - tmp = 0; - tmp |= tmLocal.wHour << 8; - tmp |= tmLocal.wMinute; - set_memory16_c(ptr, tmp, 0); + tmp = 0; + tmp |= tmLocal.wHour << 8; + tmp |= tmLocal.wMinute; + set_memory16_c(ptr, tmp, 0); } static FILETIME get_date_time(word32 ptr) { - FILETIME utc = {0, 0}; + FILETIME utc = {0, 0}; - word16 a = get_memory16_c(ptr + 0, 0); - word16 b = get_memory16_c(ptr + 2, 0); - if (!a && !b) return utc; + word16 a = get_memory16_c(ptr + 0, 0); + word16 b = get_memory16_c(ptr + 2, 0); + if (!a && !b) return utc; - SYSTEMTIME tmLocal; - SYSTEMTIME tmUTC; - memset(&tmLocal, 0, sizeof(tmLocal)); - memset(&tmUTC, 0, sizeof(tmUTC)); + SYSTEMTIME tmLocal; + SYSTEMTIME tmUTC; + memset(&tmLocal, 0, sizeof(tmLocal)); + memset(&tmUTC, 0, sizeof(tmUTC)); - tmLocal.wYear = ((a >> 9) & 0x7f) + 1900; - tmLocal.wMonth = ((a >> 5) & 0x0f); - tmLocal.wDay = (a >> 0) & 0x1f; + tmLocal.wYear = ((a >> 9) & 0x7f) + 1900; + tmLocal.wMonth = ((a >> 5) & 0x0f); + tmLocal.wDay = (a >> 0) & 0x1f; - tmLocal.wHour = (b >> 8) & 0x1f; - tmLocal.wMinute = (b >> 0) & 0x3f; - tmLocal.wSecond = 0; + tmLocal.wHour = (b >> 8) & 0x1f; + tmLocal.wMinute = (b >> 0) & 0x3f; + tmLocal.wSecond = 0; - // 00 - 39 => 2000-2039 - // 40 - 99 => 1940-1999 - if (tmLocal.wYear < 40) tmLocal.wYear += 100; + // 00 - 39 => 2000-2039 + // 40 - 99 => 1940-1999 + if (tmLocal.wYear < 40) tmLocal.wYear += 100; - TzSpecificLocalTimeToSystemTime(NULL, &tmLocal, &tmUTC); - if (!SystemTimeToFileTime(&tmUTC, &utc)) utc =(FILETIME){0, 0}; + TzSpecificLocalTimeToSystemTime(NULL, &tmLocal, &tmUTC); + if (!SystemTimeToFileTime(&tmUTC, &utc)) utc =(FILETIME){0, 0}; - return utc; + return utc; } static FILETIME get_date_time_rec(word32 ptr) { - FILETIME utc = {0, 0}; + FILETIME utc = {0, 0}; - byte buffer[8]; - for (int i = 0; i < 8; ++i) buffer[i] = get_memory_c(ptr++, 0); + byte buffer[8]; + for (int i = 0; i < 8; ++i) buffer[i] = get_memory_c(ptr++, 0); - if (!memcmp(buffer, "\x00\x00\x00\x00\x00\x00\x00\x00", 8)) return utc; + if (!memcmp(buffer, "\x00\x00\x00\x00\x00\x00\x00\x00", 8)) return utc; - SYSTEMTIME tmLocal; - SYSTEMTIME tmUTC; - memset(&tmLocal, 0, sizeof(tmLocal)); - memset(&tmUTC, 0, sizeof(tmUTC)); + SYSTEMTIME tmLocal; + SYSTEMTIME tmUTC; + memset(&tmLocal, 0, sizeof(tmLocal)); + memset(&tmUTC, 0, sizeof(tmUTC)); - tmLocal.wSecond = buffer[0]; - tmLocal.wMinute = buffer[1]; - tmLocal.wHour = buffer[2]; - tmLocal.wYear = 1900 + buffer[3]; - tmLocal.wDay = buffer[4] + 1; - tmLocal.wMonth = buffer[5] + 1; + tmLocal.wSecond = buffer[0]; + tmLocal.wMinute = buffer[1]; + tmLocal.wHour = buffer[2]; + tmLocal.wYear = 1900 + buffer[3]; + tmLocal.wDay = buffer[4] + 1; + tmLocal.wMonth = buffer[5] + 1; - TzSpecificLocalTimeToSystemTime(NULL, &tmLocal, &tmUTC); - if (!SystemTimeToFileTime(&tmUTC, &utc)) utc =(FILETIME){0, 0}; + TzSpecificLocalTimeToSystemTime(NULL, &tmLocal, &tmUTC); + if (!SystemTimeToFileTime(&tmUTC, &utc)) utc =(FILETIME){0, 0}; - return utc; + return utc; } static char *get_path1(void) { - word32 direct = engine.direct; - word16 flags = get_memory16_c(direct + dp_path_flag, 0); - if (flags & (1 << 14)) - return get_gsstr( get_memory24_c(direct + dp_path1_ptr, 0)); - return NULL; + word32 direct = engine.direct; + word16 flags = get_memory16_c(direct + dp_path_flag, 0); + if (flags & (1 << 14)) + return get_gsstr( get_memory24_c(direct + dp_path1_ptr, 0)); + return NULL; } static char *get_path2(void) { - word32 direct = engine.direct; - word16 flags = get_memory16_c(direct + dp_path_flag, 0); - if (flags & (1 << 6)) - return get_gsstr( get_memory24_c(direct + dp_path2_ptr, 0)); - return NULL; + word32 direct = engine.direct; + word16 flags = get_memory16_c(direct + dp_path_flag, 0); + if (flags & (1 << 6)) + return get_gsstr( get_memory24_c(direct + dp_path2_ptr, 0)); + return NULL; } @@ -1118,431 +1120,431 @@ static char *get_path2(void) { static word32 fst_shutdown(void) { - // close any remaining files. - struct fd_entry *head = fd_head; - while (head) { - struct fd_entry *next = head->next; + // close any remaining files. + struct fd_entry *head = fd_head; + while (head) { + struct fd_entry *next = head->next; - free_fd(head); - head = next; - } - return 0; + free_fd(head); + head = next; + } + return 0; } static word32 fst_startup(void) { - // if restart, close any previous files. + // if restart, close any previous files. - if (!g_cfg_host_path) return invalidFSTop; - if (!*g_cfg_host_path) return invalidFSTop; - if (root) free(root); - root = strdup(g_cfg_host_path); + if (!g_cfg_host_path) return invalidFSTop; + if (!*g_cfg_host_path) return invalidFSTop; + if (root) free(root); + root = strdup(g_cfg_host_path); - read_only = g_cfg_host_read_only; + read_only = g_cfg_host_read_only; - fst_shutdown(); + fst_shutdown(); - memset(&cookies, 0, sizeof(cookies)); + memset(&cookies, 0, sizeof(cookies)); - HANDLE h = CreateFile(root, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); - if (h == INVALID_HANDLE_VALUE) { - fprintf(stderr, "%s does not exist or is not accessible\n", root); - return invalidFSTop; - } - FILE_BASIC_INFO fbi; - BY_HANDLE_FILE_INFORMATION info; + HANDLE h = CreateFile(root, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (h == INVALID_HANDLE_VALUE) { + fprintf(stderr, "%s does not exist or is not accessible\n", root); + return invalidFSTop; + } + FILE_BASIC_INFO fbi; + BY_HANDLE_FILE_INFORMATION info; - GetFileInformationByHandle(h, &info); - GetFileInformationByHandleEx(h, FileBasicInfo, &fbi, sizeof(fbi)); - // can't delete volume root. - CloseHandle(h); + GetFileInformationByHandle(h, &info); + GetFileInformationByHandleEx(h, FileBasicInfo, &fbi, sizeof(fbi)); + // can't delete volume root. + CloseHandle(h); - root_file_id[0] = info.dwVolumeSerialNumber; - root_file_id[1] = info.nFileIndexHigh; - root_file_id[2] = info.nFileIndexLow; + root_file_id[0] = info.dwVolumeSerialNumber; + root_file_id[1] = info.nFileIndexHigh; + root_file_id[2] = info.nFileIndexLow; - if (!(fbi.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { - fprintf(stderr, "%s is not a directory\n", root); - CloseHandle(h); - return invalidFSTop; - } - CloseHandle(h); + if (!(fbi.FileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { + fprintf(stderr, "%s is not a directory\n", root); + CloseHandle(h); + return invalidFSTop; + } + CloseHandle(h); - //root_ino = st.st_ino; - //root_dev = st.st_dev; + //root_ino = st.st_ino; + //root_dev = st.st_dev; - return 0; + return 0; } static word32 fst_create(int class, const char *path) { - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - struct file_info fi; - memset(&fi, 0, sizeof(fi)); + struct file_info fi; + memset(&fi, 0, sizeof(fi)); - word16 pcount = 0; - if (class) { - pcount = get_memory16_c(pb, 0); - if (pcount >= 2) fi.access = get_memory16_c(pb + CreateRecGS_access, 0); - if (pcount >= 3) fi.file_type = get_memory16_c(pb + CreateRecGS_fileType, 0); - if (pcount >= 4) fi.aux_type = get_memory32_c(pb + CreateRecGS_auxType, 0); - if (pcount >= 5) fi.storage_type = get_memory16_c(pb + CreateRecGS_storageType, 0); - if (pcount >= 6) fi.eof = get_memory32_c(pb + CreateRecGS_eof, 0); - if (pcount >= 7) fi.resource_eof = get_memory32_c(pb + CreateRecGS_resourceEOF, 0); + word16 pcount = 0; + if (class) { + pcount = get_memory16_c(pb, 0); + if (pcount >= 2) fi.access = get_memory16_c(pb + CreateRecGS_access, 0); + if (pcount >= 3) fi.file_type = get_memory16_c(pb + CreateRecGS_fileType, 0); + if (pcount >= 4) fi.aux_type = get_memory32_c(pb + CreateRecGS_auxType, 0); + if (pcount >= 5) fi.storage_type = get_memory16_c(pb + CreateRecGS_storageType, 0); + if (pcount >= 6) fi.eof = get_memory32_c(pb + CreateRecGS_eof, 0); + if (pcount >= 7) fi.resource_eof = get_memory32_c(pb + CreateRecGS_resourceEOF, 0); - if (pcount >= 4) { - afp_init(&fi.afp, fi.file_type, fi.aux_type); - fi.has_fi = 1; - } + if (pcount >= 4) { + afp_init(&fi.afp, fi.file_type, fi.aux_type); + fi.has_fi = 1; + } - } else { + } else { - fi.access = get_memory16_c(pb + CreateRec_fAccess, 0); - fi.file_type = get_memory16_c(pb + CreateRec_fileType, 0); - fi.aux_type = get_memory32_c(pb + CreateRec_auxType, 0); - fi.storage_type = get_memory16_c(pb + CreateRec_storageType, 0); - fi.create_date = get_date_time(pb + CreateRec_createDate); + fi.access = get_memory16_c(pb + CreateRec_fAccess, 0); + fi.file_type = get_memory16_c(pb + CreateRec_fileType, 0); + fi.aux_type = get_memory32_c(pb + CreateRec_auxType, 0); + fi.storage_type = get_memory16_c(pb + CreateRec_storageType, 0); + fi.create_date = get_date_time(pb + CreateRec_createDate); - afp_init(&fi.afp, fi.file_type, fi.aux_type); - fi.has_fi = 1; - } - int ok; + afp_init(&fi.afp, fi.file_type, fi.aux_type); + fi.has_fi = 1; + } + int ok; - if (fi.storage_type == 0 && fi.file_type == 0x0f) - fi.storage_type = 0x0d; + if (fi.storage_type == 0 && fi.file_type == 0x0f) + fi.storage_type = 0x0d; - if (fi.storage_type == 0x0d) { - ok = CreateDirectory(path, NULL); - if (!ok) return map_last_error(); + if (fi.storage_type == 0x0d) { + ok = CreateDirectory(path, NULL); + if (!ok) return map_last_error(); - if (class) { - if (pcount >= 5) set_memory16_c(pb + CreateRecGS_storageType, fi.storage_type, 0); - } else { - set_memory16_c(pb + CreateRec_storageType, fi.storage_type, 0); - } + if (class) { + if (pcount >= 5) set_memory16_c(pb + CreateRecGS_storageType, fi.storage_type, 0); + } else { + set_memory16_c(pb + CreateRec_storageType, fi.storage_type, 0); + } - return 0; - } - if (fi.storage_type <= 3 || fi.storage_type == 0x05) { - // normal file. - // 0x05 is an extended/resource file but we don't do anything special. - HANDLE h = CreateFile(path, - GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, - CREATE_NEW, - FILE_ATTRIBUTE_NORMAL, - NULL); - if (h == INVALID_HANDLE_VALUE) return map_last_error(); - // set ftype, auxtype... - set_file_info(path, &fi); // set_file_info_handle(...); - CloseHandle(h); + return 0; + } + if (fi.storage_type <= 3 || fi.storage_type == 0x05) { + // normal file. + // 0x05 is an extended/resource file but we don't do anything special. + HANDLE h = CreateFile(path, + GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, + CREATE_NEW, + FILE_ATTRIBUTE_NORMAL, + NULL); + if (h == INVALID_HANDLE_VALUE) return map_last_error(); + // set ftype, auxtype... + set_file_info(path, &fi); // set_file_info_handle(...); + CloseHandle(h); - fi.storage_type = 1; + fi.storage_type = 1; - if (class) { - if (pcount >= 5) set_memory16_c(pb + CreateRecGS_storageType, fi.storage_type, 0); - } else { - set_memory16_c(pb + CreateRec_storageType, fi.storage_type, 0); - } + if (class) { + if (pcount >= 5) set_memory16_c(pb + CreateRecGS_storageType, fi.storage_type, 0); + } else { + set_memory16_c(pb + CreateRec_storageType, fi.storage_type, 0); + } - return 0; - } + return 0; + } - if (fi.storage_type == 0x8005) { - // convert an existing file to an extended file. - // this checks that the file exists and has a 0-sized resource. - word32 rv = get_file_info(path, &fi); - if (rv) return rv; - if (fi.storage_type == extendedFile) return resExistsErr; - if (fi.storage_type < seedling || fi.storage_type > tree) return resAddErr; - return 0; - } + if (fi.storage_type == 0x8005) { + // convert an existing file to an extended file. + // this checks that the file exists and has a 0-sized resource. + word32 rv = get_file_info(path, &fi); + if (rv) return rv; + if (fi.storage_type == extendedFile) return resExistsErr; + if (fi.storage_type < seedling || fi.storage_type > tree) return resAddErr; + return 0; + } - // other storage types? - return badStoreType; + // other storage types? + return badStoreType; } static word16 storage_type(const char *path, word16 *error) { - if (!path) { - *error = badPathSyntax; - return 0; // ? - } - HANDLE h; - h = CreateFile(path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); - if (h == INVALID_HANDLE_VALUE) { - *error = map_last_error(); - return 0; - } + if (!path) { + *error = badPathSyntax; + return 0; // ? + } + HANDLE h; + h = CreateFile(path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); + if (h == INVALID_HANDLE_VALUE) { + *error = map_last_error(); + return 0; + } - BY_HANDLE_FILE_INFORMATION info; - GetFileInformationByHandle(h, &info); - CloseHandle(h); + BY_HANDLE_FILE_INFORMATION info; + GetFileInformationByHandle(h, &info); + CloseHandle(h); - DWORD id[3] = { info.dwVolumeSerialNumber, info.nFileIndexHigh, info.nFileIndexLow }; - if (memcmp(&id, &root_file_id, sizeof(root_file_id)) == 0) { - return 0x0f; - } + DWORD id[3] = { info.dwVolumeSerialNumber, info.nFileIndexHigh, info.nFileIndexLow }; + if (memcmp(&id, &root_file_id, sizeof(root_file_id)) == 0) { + return 0x0f; + } - if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) - return directoryFile; + if (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + return directoryFile; - return standardFile; + return standardFile; } static word32 fst_destroy(int class, const char *path) { - word16 rv = 0; - BOOL ok = 0; - word16 type = storage_type(path, &rv); - if (rv) return rv; - if (read_only) return drvrWrtProt; + word16 rv = 0; + BOOL ok = 0; + word16 type = storage_type(path, &rv); + if (rv) return rv; + if (read_only) return drvrWrtProt; - switch(type) { - case 0: return fileNotFound; - case 0x0f: return badStoreType; - default: return badStoreType; + switch(type) { + case 0: return fileNotFound; + case 0x0f: return badStoreType; + default: return badStoreType; - case directoryFile: - ok = RemoveDirectory(path); - break; - case sapling: - case seedling: - case tree: - case extendedFile: - ok = DeleteFile(path); - break; - } + case directoryFile: + ok = RemoveDirectory(path); + break; + case sapling: + case seedling: + case tree: + case extendedFile: + ok = DeleteFile(path); + break; + } - if (!ok) return map_last_error(); - return 0; + if (!ok) return map_last_error(); + return 0; } static word32 fst_set_file_info(int class, const char *path) { - word16 rv = 0; - word16 type = storage_type(path, &rv); - if (rv) return rv; - if (read_only) return drvrWrtProt; + word16 rv = 0; + word16 type = storage_type(path, &rv); + if (rv) return rv; + if (read_only) return drvrWrtProt; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - struct file_info fi; - memset(&fi, 0, sizeof(fi)); + struct file_info fi; + memset(&fi, 0, sizeof(fi)); - // load up existing file types / finder info. - get_file_xinfo(path, &fi); + // load up existing file types / finder info. + get_file_xinfo(path, &fi); - word32 option_list = 0; - if (class) { - word16 pcount = get_memory16_c(pb, 0); + word32 option_list = 0; + if (class) { + word16 pcount = get_memory16_c(pb, 0); - if (pcount >= 2) fi.access = get_memory16_c(pb + FileInfoRecGS_access, 0); - if (pcount >= 3) fi.file_type = get_memory16_c(pb + FileInfoRecGS_fileType, 0); - if (pcount >= 4) fi.aux_type = get_memory32_c(pb + FileInfoRecGS_auxType, 0); - // reserved. - //if (pcount >= 5) fi.storage_type = get_memory16_c(pb + FileInfoRecGS_storageType, 0); - if (pcount >= 6) fi.create_date = get_date_time_rec(pb + FileInfoRecGS_createDateTime); - if (pcount >= 7) fi.modified_date = get_date_time_rec(pb + FileInfoRecGS_modDateTime); - if (pcount >= 8) option_list = get_memory24_c(pb + FileInfoRecGS_optionList, 0); - // remainder reserved + if (pcount >= 2) fi.access = get_memory16_c(pb + FileInfoRecGS_access, 0); + if (pcount >= 3) fi.file_type = get_memory16_c(pb + FileInfoRecGS_fileType, 0); + if (pcount >= 4) fi.aux_type = get_memory32_c(pb + FileInfoRecGS_auxType, 0); + // reserved. + //if (pcount >= 5) fi.storage_type = get_memory16_c(pb + FileInfoRecGS_storageType, 0); + if (pcount >= 6) fi.create_date = get_date_time_rec(pb + FileInfoRecGS_createDateTime); + if (pcount >= 7) fi.modified_date = get_date_time_rec(pb + FileInfoRecGS_modDateTime); + if (pcount >= 8) option_list = get_memory24_c(pb + FileInfoRecGS_optionList, 0); + // remainder reserved - if (fi.has_fi) { - if (pcount < 4) fi.aux_type = fi.afp.prodos_aux_type; - if (pcount < 3) fi.file_type = fi.afp.prodos_file_type; - } + if (fi.has_fi) { + if (pcount < 4) fi.aux_type = fi.afp.prodos_aux_type; + if (pcount < 3) fi.file_type = fi.afp.prodos_file_type; + } - } else { - fi.access = get_memory16_c(pb + FileRec_fAccess, 0); - fi.file_type = get_memory16_c(pb + FileRec_fileType, 0); - fi.aux_type = get_memory32_c(pb + FileRec_auxType, 0); - // reserved. - //fi.storage_type = get_memory32_c(pb + FileRec_storageType, 0); - fi.create_date = get_date_time(pb + FileRec_createDate); - fi.modified_date = get_date_time(pb + FileRec_modDate); - } + } else { + fi.access = get_memory16_c(pb + FileRec_fAccess, 0); + fi.file_type = get_memory16_c(pb + FileRec_fileType, 0); + fi.aux_type = get_memory32_c(pb + FileRec_auxType, 0); + // reserved. + //fi.storage_type = get_memory32_c(pb + FileRec_storageType, 0); + fi.create_date = get_date_time(pb + FileRec_createDate); + fi.modified_date = get_date_time(pb + FileRec_modDate); + } - if (fi.has_fi) { - fi.afp.prodos_file_type = fi.file_type; - fi.afp.prodos_aux_type = fi.aux_type; - } - else { - afp_init(&fi.afp, fi.file_type, fi.aux_type); - fi.has_fi = 1; - } + if (fi.has_fi) { + fi.afp.prodos_file_type = fi.file_type; + fi.afp.prodos_aux_type = fi.aux_type; + } + else { + afp_init(&fi.afp, fi.file_type, fi.aux_type); + fi.has_fi = 1; + } - if (option_list) { - // total size, req size, fst id, data... - int total_size = get_memory16_c(option_list + 0, 0); - int req_size = get_memory16_c(option_list + 2, 0); - int fst_id = get_memory16_c(option_list + 4, 0); + if (option_list) { + // total size, req size, fst id, data... + int total_size = get_memory16_c(option_list + 0, 0); + int req_size = get_memory16_c(option_list + 2, 0); + int fst_id = get_memory16_c(option_list + 4, 0); - int size = req_size - 6; - if ((fst_id == proDOSFSID || fst_id == hfsFSID || fst_id == appleShareFSID) && size >= 32) { + int size = req_size - 6; + if ((fst_id == proDOSFSID || fst_id == hfsFSID || fst_id == appleShareFSID) && size >= 32) { - // this should never happen... - if (!fi.has_fi) afp_init(&fi.afp, fi.file_type, fi.aux_type); - fi.has_fi = 1; + // this should never happen... + if (!fi.has_fi) afp_init(&fi.afp, fi.file_type, fi.aux_type); + fi.has_fi = 1; - for (int i = 0; i <32; ++i) - fi.afp.finder_info[i] = get_memory_c(option_list + 6 + i, 0); - } - } + for (int i = 0; i <32; ++i) + fi.afp.finder_info[i] = get_memory_c(option_list + 6 + i, 0); + } + } - // one more check... if ftype/auxtype doesn't match the ftype/auxtype in finder info - // update finder info - if (fi.has_fi) afp_synchronize(&fi.afp, prefer_prodos); + // one more check... if ftype/auxtype doesn't match the ftype/auxtype in finder info + // update finder info + if (fi.has_fi) afp_synchronize(&fi.afp, prefer_prodos); - return set_file_info(path, &fi); + return set_file_info(path, &fi); } static word32 fst_get_file_info(int class, const char *path) { - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - struct file_info fi; - int rv = 0; + struct file_info fi; + int rv = 0; - rv = get_file_info(path, &fi); - if (rv) return rv; + rv = get_file_info(path, &fi); + if (rv) return rv; - if (class) { + if (class) { - word16 pcount = get_memory16_c(pb, 0); + word16 pcount = get_memory16_c(pb, 0); - if (pcount >= 2) set_memory16_c(pb + FileInfoRecGS_access, fi.access, 0); - if (pcount >= 3) set_memory16_c(pb + FileInfoRecGS_fileType, fi.file_type, 0); - if (pcount >= 4) set_memory32_c(pb + FileInfoRecGS_auxType, fi.aux_type, 0); - if (pcount >= 5) set_memory16_c(pb + FileInfoRecGS_storageType, fi.storage_type, 0); + if (pcount >= 2) set_memory16_c(pb + FileInfoRecGS_access, fi.access, 0); + if (pcount >= 3) set_memory16_c(pb + FileInfoRecGS_fileType, fi.file_type, 0); + if (pcount >= 4) set_memory32_c(pb + FileInfoRecGS_auxType, fi.aux_type, 0); + if (pcount >= 5) set_memory16_c(pb + FileInfoRecGS_storageType, fi.storage_type, 0); - if (pcount >= 6) set_date_time_rec(pb + FileInfoRecGS_createDateTime, fi.create_date); - if (pcount >= 7) set_date_time_rec(pb + FileInfoRecGS_modDateTime, fi.modified_date); + if (pcount >= 6) set_date_time_rec(pb + FileInfoRecGS_createDateTime, fi.create_date); + if (pcount >= 7) set_date_time_rec(pb + FileInfoRecGS_modDateTime, fi.modified_date); - if (pcount >= 8) { - word16 fst_id = hfsFSID; - //if (fi.storage_type == 0x0f) fst_id = mfsFSID; - word32 option_list = get_memory24_c(pb + FileInfoRecGS_optionList, 0); - rv = set_option_list(option_list, fst_id, fi.afp.finder_info, fi.has_fi ? 32 : 0); - } - if (pcount >= 9) set_memory32_c(pb + FileInfoRecGS_eof, fi.eof, 0); - if (pcount >= 10) set_memory32_c(pb + FileInfoRecGS_blocksUsed, fi.blocks, 0); - if (pcount >= 11) set_memory32_c(pb + FileInfoRecGS_resourceEOF, fi.resource_eof, 0); - if (pcount >= 12) set_memory32_c(pb + FileInfoRecGS_resourceBlocks, fi.resource_blocks, 0); + if (pcount >= 8) { + word16 fst_id = hfsFSID; + //if (fi.storage_type == 0x0f) fst_id = mfsFSID; + word32 option_list = get_memory24_c(pb + FileInfoRecGS_optionList, 0); + rv = set_option_list(option_list, fst_id, fi.afp.finder_info, fi.has_fi ? 32 : 0); + } + if (pcount >= 9) set_memory32_c(pb + FileInfoRecGS_eof, fi.eof, 0); + if (pcount >= 10) set_memory32_c(pb + FileInfoRecGS_blocksUsed, fi.blocks, 0); + if (pcount >= 11) set_memory32_c(pb + FileInfoRecGS_resourceEOF, fi.resource_eof, 0); + if (pcount >= 12) set_memory32_c(pb + FileInfoRecGS_resourceBlocks, fi.resource_blocks, 0); - } else { + } else { - set_memory16_c(pb + FileRec_fAccess, fi.access, 0); - set_memory16_c(pb + FileRec_fileType, fi.file_type, 0); - set_memory32_c(pb + FileRec_auxType, fi.aux_type, 0); - set_memory16_c(pb + FileRec_storageType, fi.storage_type, 0); + set_memory16_c(pb + FileRec_fAccess, fi.access, 0); + set_memory16_c(pb + FileRec_fileType, fi.file_type, 0); + set_memory32_c(pb + FileRec_auxType, fi.aux_type, 0); + set_memory16_c(pb + FileRec_storageType, fi.storage_type, 0); - set_date_time(pb + FileRec_createDate, fi.create_date); - set_date_time(pb + FileRec_modDate, fi.modified_date); + set_date_time(pb + FileRec_createDate, fi.create_date); + set_date_time(pb + FileRec_modDate, fi.modified_date); - set_memory32_c(pb + FileRec_blocksUsed, fi.blocks, 0); - } + set_memory32_c(pb + FileRec_blocksUsed, fi.blocks, 0); + } - return rv; + return rv; } static word32 fst_judge_name(int class, char *path) { - if (class == 0) return invalidClass; + if (class == 0) return invalidClass; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word16 pcount = get_memory16_c(pb, 0); - word16 name_type = get_memory16_c(pb + JudgeNameRecGS_nameType, 0); - word32 name = pcount >= 5 ? get_memory24_c(pb + JudgeNameRecGS_name, 0) : 0; + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word16 pcount = get_memory16_c(pb, 0); + word16 name_type = get_memory16_c(pb + JudgeNameRecGS_nameType, 0); + word32 name = pcount >= 5 ? get_memory24_c(pb + JudgeNameRecGS_name, 0) : 0; - // 255 max length. - if (pcount >= 4) set_memory16_c(pb + JudgeNameRecGS_maxLen, 255, 0); - if (pcount >= 6) set_memory16_c(pb + JudgeNameRecGS_nameFlags, 0, 0); + // 255 max length. + if (pcount >= 4) set_memory16_c(pb + JudgeNameRecGS_maxLen, 255, 0); + if (pcount >= 6) set_memory16_c(pb + JudgeNameRecGS_nameFlags, 0, 0); - word16 nameFlags = 0; - word16 rv = 0; + word16 nameFlags = 0; + word16 rv = 0; - if (name) { - word16 cap = get_memory16_c(name, 0); - if (cap < 4) return buffTooSmall; - word16 length = get_memory16_c(name + 2, 0); - if (length == 0) { - nameFlags |= 1 << 13; - rv = set_gsstr(name, "A"); - } else { - // if volume name, only allow "Host" ? - if (length > 255) nameFlags |= 1 << 14; - for (int i = 0; i < length; ++i) { - char c = get_memory_c(name + 4 + i, 0); - if (c == 0 || c == ':' || c == '/' || c == '\\') { - nameFlags |= 1 << 15; - set_memory_c(name + 4 + i, '.', 0); - } - } - } - } - if (pcount >= 6) set_memory16_c(pb + JudgeNameRecGS_nameFlags, nameFlags, 0); - return rv; + if (name) { + word16 cap = get_memory16_c(name, 0); + if (cap < 4) return buffTooSmall; + word16 length = get_memory16_c(name + 2, 0); + if (length == 0) { + nameFlags |= 1 << 13; + rv = set_gsstr(name, "A"); + } else { + // if volume name, only allow "Host" ? + if (length > 255) nameFlags |= 1 << 14; + for (int i = 0; i < length; ++i) { + char c = get_memory_c(name + 4 + i, 0); + if (c == 0 || c == ':' || c == '/' || c == '\\') { + nameFlags |= 1 << 15; + set_memory_c(name + 4 + i, '.', 0); + } + } + } + } + if (pcount >= 6) set_memory16_c(pb + JudgeNameRecGS_nameFlags, nameFlags, 0); + return rv; } static word32 fst_volume(int class) { - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word32 rv = 0; - if (class) { - word16 pcount = get_memory16_c(pb, 0); - if (pcount >= 2) rv = set_gsstr(get_memory24_c(pb + VolumeRecGS_volName, 0), ":Host"); - // finder bug -- if used blocks is 0, doesn't display header. - if (pcount >= 3) set_memory32_c(pb + VolumeRecGS_totalBlocks, 0x007fffff, 0); - if (pcount >= 4) set_memory32_c(pb + VolumeRecGS_freeBlocks, 0x007fffff-1, 0); - if (pcount >= 5) set_memory16_c(pb + VolumeRecGS_fileSysID, mfsFSID, 0); - if (pcount >= 6) set_memory16_c(pb + VolumeRecGS_blockSize, 512, 0); - // handled via gs/os - //if (pcount >= 7) set_memory16_c(pb + VolumeRecGS_characteristics, 0, 0); - //if (pcount >= 8) set_memory16_c(pb + VolumeRecGS_deviceID, 0, 0); - } else { + word32 rv = 0; + if (class) { + word16 pcount = get_memory16_c(pb, 0); + if (pcount >= 2) rv = set_gsstr(get_memory24_c(pb + VolumeRecGS_volName, 0), ":Host"); + // finder bug -- if used blocks is 0, doesn't display header. + if (pcount >= 3) set_memory32_c(pb + VolumeRecGS_totalBlocks, 0x007fffff, 0); + if (pcount >= 4) set_memory32_c(pb + VolumeRecGS_freeBlocks, 0x007fffff-1, 0); + if (pcount >= 5) set_memory16_c(pb + VolumeRecGS_fileSysID, mfsFSID, 0); + if (pcount >= 6) set_memory16_c(pb + VolumeRecGS_blockSize, 512, 0); + // handled via gs/os + //if (pcount >= 7) set_memory16_c(pb + VolumeRecGS_characteristics, 0, 0); + //if (pcount >= 8) set_memory16_c(pb + VolumeRecGS_deviceID, 0, 0); + } else { - // prodos 16 uses / sep. - rv = set_pstr(get_memory24_c(pb + VolumeRec_volName, 0), "/Host"); - set_memory32_c(pb + VolumeRec_totalBlocks, 0x007fffff, 0); - set_memory32_c(pb + VolumeRec_freeBlocks, 0x007fffff-1, 0); - set_memory16_c(pb + VolumeRec_fileSysID, mfsFSID, 0); - } + // prodos 16 uses / sep. + rv = set_pstr(get_memory24_c(pb + VolumeRec_volName, 0), "/Host"); + set_memory32_c(pb + VolumeRec_totalBlocks, 0x007fffff, 0); + set_memory32_c(pb + VolumeRec_freeBlocks, 0x007fffff-1, 0); + set_memory16_c(pb + VolumeRec_fileSysID, mfsFSID, 0); + } - return rv; + return rv; } static word32 fst_clear_backup(int class, const char *path) { - word16 rv = 0; - word16 type = storage_type(path, &rv); - if (rv) return rv; - if (read_only) return drvrWrtProt; + word16 rv = 0; + word16 type = storage_type(path, &rv); + if (rv) return rv; + if (read_only) return drvrWrtProt; - return invalidFSTop; + return invalidFSTop; } static void free_directory(struct directory *dd) { - if (!dd) return; - for (int i = 0; i < dd->num_entries; ++i) { - free(dd->entries[i]); - } - free(dd); + if (!dd) return; + for (int i = 0; i < dd->num_entries; ++i) { + free(dd->entries[i]); + } + free(dd); } static int qsort_callback(const void *a, const void *b) { - return strcmp(*(const char **)a, *(const char **)b); + return strcmp(*(const char **)a, *(const char **)b); } /* @@ -1550,155 +1552,155 @@ static int qsort_callback(const void *a, const void *b) { * */ static int filter_directory_entry(const char *name) { - if (!name[0]) return 1; - if (name[0] == '.') { - return 1; - /* - if (!strcmp(name, ".")) return 1; - if (!strcmp(name, "..")) return 1; - if (!strncmp(name, "._", 2)) return 1; // ._ resource fork - if (!strcmp(name, ".fseventsd")) return 1; - */ - } - return 0; + if (!name[0]) return 1; + if (name[0] == '.') { + return 1; + /* + if (!strcmp(name, ".")) return 1; + if (!strcmp(name, "..")) return 1; + if (!strncmp(name, "._", 2)) return 1; // ._ resource fork + if (!strcmp(name, ".fseventsd")) return 1; + */ + } + return 0; } static struct directory *read_directory(const char *path, word16 *error) { - HANDLE h; - WIN32_FIND_DATA data; - struct directory *dd; - int capacity = 100; - int size = sizeof(struct directory) + capacity * sizeof(char *); + HANDLE h; + WIN32_FIND_DATA data; + struct directory *dd; + int capacity = 100; + int size = sizeof(struct directory) + capacity * sizeof(char *); - memset(&data, 0, sizeof(data)); + memset(&data, 0, sizeof(data)); - char *p = append_path(path, "*"); - h = FindFirstFile(p, &data); - if (h == INVALID_HANDLE_VALUE) { - *error = map_last_error(); - return NULL; - } + char *p = append_path(path, "*"); + h = FindFirstFile(p, &data); + if (h == INVALID_HANDLE_VALUE) { + *error = map_last_error(); + return NULL; + } - dd = (struct directory *)malloc(size); - if (!dd) { - FindClose(h); - *error = outOfMem; - return NULL; - } - memset(dd, 0, size); - do { - if (filter_directory_entry(data.cFileName)) continue; - if (dd->num_entries >= capacity) { - capacity += capacity; - size = sizeof(struct directory) + capacity * sizeof(char *); - struct directory * tmp = realloc(dd, size); - if (!tmp) { - *error = map_errno(); - free_directory(dd); - FindClose(h); - return NULL; - } - dd = tmp; - } - dd->entries[dd->num_entries++] = strdup(data.cFileName); - } while (FindNextFile(h, &data) != 0); + dd = (struct directory *)malloc(size); + if (!dd) { + FindClose(h); + *error = outOfMem; + return NULL; + } + memset(dd, 0, size); + do { + if (filter_directory_entry(data.cFileName)) continue; + if (dd->num_entries >= capacity) { + capacity += capacity; + size = sizeof(struct directory) + capacity * sizeof(char *); + struct directory * tmp = realloc(dd, size); + if (!tmp) { + *error = map_errno(); + free_directory(dd); + FindClose(h); + return NULL; + } + dd = tmp; + } + dd->entries[dd->num_entries++] = strdup(data.cFileName); + } while (FindNextFile(h, &data) != 0); - //? - if (GetLastError() != ERROR_NO_MORE_FILES) *error = map_last_error(); + //? + if (GetLastError() != ERROR_NO_MORE_FILES) *error = map_last_error(); - FindClose(h); + FindClose(h); - // sort them.... - qsort(dd->entries, dd->num_entries, sizeof(char *), qsort_callback); + // sort them.... + qsort(dd->entries, dd->num_entries, sizeof(char *), qsort_callback); - return dd; + return dd; } static HANDLE open_data_fork(const char *path, word16 *access, word16 *error) { - HANDLE h = INVALID_HANDLE_VALUE; - for (;;) { + HANDLE h = INVALID_HANDLE_VALUE; + for (;;) { - DWORD wAccess = 0; - DWORD wShare = FILE_SHARE_READ; - DWORD wCreate = OPEN_EXISTING; + DWORD wAccess = 0; + DWORD wShare = FILE_SHARE_READ; + DWORD wCreate = OPEN_EXISTING; - switch(*access) { - case readEnableAllowWrite: - case readWriteEnable: - wAccess = GENERIC_READ | GENERIC_WRITE; - break; - case readEnable: - wAccess = GENERIC_READ; - break; - case writeEnable: - wAccess = GENERIC_WRITE; - break; - } + switch(*access) { + case readEnableAllowWrite: + case readWriteEnable: + wAccess = GENERIC_READ | GENERIC_WRITE; + break; + case readEnable: + wAccess = GENERIC_READ; + break; + case writeEnable: + wAccess = GENERIC_WRITE; + break; + } - h = CreateFile(path, wAccess, wShare, NULL, wCreate, FILE_ATTRIBUTE_NORMAL, NULL); + h = CreateFile(path, wAccess, wShare, NULL, wCreate, FILE_ATTRIBUTE_NORMAL, NULL); - if (*access == readEnableAllowWrite) { - if (h == INVALID_HANDLE_VALUE) { - *access = readEnable; - continue; - } - *access = readWriteEnable; - } - break; - } - if (h == INVALID_HANDLE_VALUE) { - *error = map_last_error(); - } - return h; + if (*access == readEnableAllowWrite) { + if (h == INVALID_HANDLE_VALUE) { + *access = readEnable; + continue; + } + *access = readWriteEnable; + } + break; + } + if (h == INVALID_HANDLE_VALUE) { + *error = map_last_error(); + } + return h; } static HANDLE open_resource_fork(const char *path, word16 *access, word16 *error) { - char *rpath = append_string(path, ":AFP_Resource"); + char *rpath = append_string(path, ":AFP_Resource"); - HANDLE h = INVALID_HANDLE_VALUE; - for (;;) { + HANDLE h = INVALID_HANDLE_VALUE; + for (;;) { - DWORD wAccess = 0; - DWORD wShare = FILE_SHARE_READ; - DWORD wCreate = 0; + DWORD wAccess = 0; + DWORD wShare = FILE_SHARE_READ; + DWORD wCreate = 0; - switch(*access) { - case readEnableAllowWrite: - case readWriteEnable: - wAccess = GENERIC_READ | GENERIC_WRITE; - wCreate = OPEN_ALWAYS; - break; - case readEnable: - wAccess = GENERIC_READ; - wCreate = OPEN_EXISTING; - break; - case writeEnable: - wAccess = GENERIC_WRITE; - wCreate = OPEN_ALWAYS; - break; - } + switch(*access) { + case readEnableAllowWrite: + case readWriteEnable: + wAccess = GENERIC_READ | GENERIC_WRITE; + wCreate = OPEN_ALWAYS; + break; + case readEnable: + wAccess = GENERIC_READ; + wCreate = OPEN_EXISTING; + break; + case writeEnable: + wAccess = GENERIC_WRITE; + wCreate = OPEN_ALWAYS; + break; + } - h = CreateFile(rpath, wAccess, wShare, NULL, wCreate, FILE_ATTRIBUTE_NORMAL, NULL); + h = CreateFile(rpath, wAccess, wShare, NULL, wCreate, FILE_ATTRIBUTE_NORMAL, NULL); - if (*access == readEnableAllowWrite) { - if (h == INVALID_HANDLE_VALUE) { - *access = readEnable; - continue; - } - *access = readWriteEnable; - } - break; - } - if (h == INVALID_HANDLE_VALUE) { - *error = map_last_error(); - } - return h; + if (*access == readEnableAllowWrite) { + if (h == INVALID_HANDLE_VALUE) { + *access = readEnable; + continue; + } + *access = readWriteEnable; + } + break; + } + if (h == INVALID_HANDLE_VALUE) { + *error = map_last_error(); + } + return h; } @@ -1706,497 +1708,497 @@ static HANDLE open_resource_fork(const char *path, word16 *access, word16 *error static word32 fst_open(int class, const char *path) { - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - struct file_info fi; - word16 rv = 0; + struct file_info fi; + word16 rv = 0; - rv = get_file_info(path, &fi); - if (rv) return rv; + rv = get_file_info(path, &fi); + if (rv) return rv; - HANDLE h = INVALID_HANDLE_VALUE; - int type = regular_file; - struct directory *dd = NULL; + HANDLE h = INVALID_HANDLE_VALUE; + int type = regular_file; + struct directory *dd = NULL; - word16 pcount = 0; - word16 request_access = readEnableAllowWrite; - word16 access = 0; - word16 resource_number = 0; - if (class) { - pcount = get_memory16_c(pb, 0); - if (pcount >= 3) request_access = get_memory16_c(pb + OpenRecGS_requestAccess, 0); - if (pcount >= 4) resource_number = get_memory16_c(pb + OpenRecGS_resourceNumber, 0); - } + word16 pcount = 0; + word16 request_access = readEnableAllowWrite; + word16 access = 0; + word16 resource_number = 0; + if (class) { + pcount = get_memory16_c(pb, 0); + if (pcount >= 3) request_access = get_memory16_c(pb + OpenRecGS_requestAccess, 0); + if (pcount >= 4) resource_number = get_memory16_c(pb + OpenRecGS_resourceNumber, 0); + } - if (resource_number) { - if (resource_number > 1) return paramRangeErr; - type = resource_file; - } + if (resource_number) { + if (resource_number > 1) return paramRangeErr; + type = resource_file; + } - if (access > 3) return paramRangeErr; + if (access > 3) return paramRangeErr; - // special access checks for directories. - if (fi.storage_type == 0x0f || fi.storage_type == 0x0d) { - if (resource_number) return resForkNotFound; - switch (request_access) { - case readEnableAllowWrite: - request_access = readEnable; - break; - case writeEnable: - case readWriteEnable: - return invalidAccess; - } - type = directory_file; - } + // special access checks for directories. + if (fi.storage_type == 0x0f || fi.storage_type == 0x0d) { + if (resource_number) return resForkNotFound; + switch (request_access) { + case readEnableAllowWrite: + request_access = readEnable; + break; + case writeEnable: + case readWriteEnable: + return invalidAccess; + } + type = directory_file; + } - if (read_only) { - switch (request_access) { - case readEnableAllowWrite: - request_access = readEnable; - break; - case readWriteEnable: - case writeEnable: - return invalidAccess; - break; - } - } + if (read_only) { + switch (request_access) { + case readEnableAllowWrite: + request_access = readEnable; + break; + case readWriteEnable: + case writeEnable: + return invalidAccess; + break; + } + } - access = request_access; - switch(type) { - case regular_file: - h = open_data_fork(path, &access, &rv); - break; - case resource_file: - h = open_resource_fork(path, &access, &rv); - break; - case directory_file: - dd = read_directory(path, &rv); - break; - } + access = request_access; + switch(type) { + case regular_file: + h = open_data_fork(path, &access, &rv); + break; + case resource_file: + h = open_resource_fork(path, &access, &rv); + break; + case directory_file: + dd = read_directory(path, &rv); + break; + } - if (rv) return rv; + if (rv) return rv; - if (class) { - if (pcount >= 5) set_memory16_c(pb + OpenRecGS_access, access, 0); - if (pcount >= 6) set_memory16_c(pb + OpenRecGS_fileType, fi.file_type, 0); - if (pcount >= 7) set_memory32_c(pb + OpenRecGS_auxType, fi.aux_type, 0); - if (pcount >= 8) set_memory16_c(pb + OpenRecGS_storageType, fi. storage_type, 0); + if (class) { + if (pcount >= 5) set_memory16_c(pb + OpenRecGS_access, access, 0); + if (pcount >= 6) set_memory16_c(pb + OpenRecGS_fileType, fi.file_type, 0); + if (pcount >= 7) set_memory32_c(pb + OpenRecGS_auxType, fi.aux_type, 0); + if (pcount >= 8) set_memory16_c(pb + OpenRecGS_storageType, fi.storage_type, 0); - if (pcount >= 9) set_date_time_rec(pb + OpenRecGS_createDateTime, fi.create_date); - if (pcount >= 10) set_date_time_rec(pb + OpenRecGS_modDateTime, fi.modified_date); + if (pcount >= 9) set_date_time_rec(pb + OpenRecGS_createDateTime, fi.create_date); + if (pcount >= 10) set_date_time_rec(pb + OpenRecGS_modDateTime, fi.modified_date); - if (pcount >= 11) { - word16 fst_id = hfsFSID; - //if (fi.storage_type == 0x0f) fst_id = mfsFSID; + if (pcount >= 11) { + word16 fst_id = hfsFSID; + //if (fi.storage_type == 0x0f) fst_id = mfsFSID; - word32 option_list = get_memory24_c(pb + OpenRecGS_optionList, 0); - word32 tmp = set_option_list(option_list, fst_id, fi.afp.finder_info, fi.has_fi ? 32 : 0); - if (!rv) rv = tmp; - } + word32 option_list = get_memory24_c(pb + OpenRecGS_optionList, 0); + word32 tmp = set_option_list(option_list, fst_id, fi.afp.finder_info, fi.has_fi ? 32 : 0); + if (!rv) rv = tmp; + } - if (pcount >= 12) set_memory32_c(pb + OpenRecGS_eof, fi.eof, 0); - if (pcount >= 13) set_memory32_c(pb + OpenRecGS_blocksUsed, fi.blocks, 0); - if (pcount >= 14) set_memory32_c(pb + OpenRecGS_resourceEOF, fi.resource_eof, 0); - if (pcount >= 15) set_memory32_c(pb + OpenRecGS_resourceBlocks, fi.resource_blocks, 0); + if (pcount >= 12) set_memory32_c(pb + OpenRecGS_eof, fi.eof, 0); + if (pcount >= 13) set_memory32_c(pb + OpenRecGS_blocksUsed, fi.blocks, 0); + if (pcount >= 14) set_memory32_c(pb + OpenRecGS_resourceEOF, fi.resource_eof, 0); + if (pcount >= 15) set_memory32_c(pb + OpenRecGS_resourceBlocks, fi.resource_blocks, 0); - } - // prodos 16 doesn't return anything in the parameter block. + } + // prodos 16 doesn't return anything in the parameter block. - struct fd_entry *e = alloc_fd(); - if (!e) { - if (h != INVALID_HANDLE_VALUE) CloseHandle(h); - if (dd) free_directory(dd); - return outOfMem; - } - e->handle = h; - e->dir = dd; + struct fd_entry *e = alloc_fd(); + if (!e) { + if (h != INVALID_HANDLE_VALUE) CloseHandle(h); + if (dd) free_directory(dd); + return outOfMem; + } + e->handle = h; + e->dir = dd; - e->cookie = alloc_cookie(); - if (!e->cookie) { - free_fd(e); - return tooManyFilesOpen; - } + e->cookie = alloc_cookie(); + if (!e->cookie) { + free_fd(e); + return tooManyFilesOpen; + } - if (type == regular_file){ + if (type == regular_file) { - if (g_cfg_host_crlf) { - if (fi.file_type == 0x04 || fi.file_type == 0xb0) - e->translate = translate_crlf; - } + if (g_cfg_host_crlf) { + if (fi.file_type == 0x04 || fi.file_type == 0xb0) + e->translate = translate_crlf; + } - if (g_cfg_host_merlin && fi.file_type == 0x04) { - int n = strlen(path); - if (n >= 3 && path[n-1] == 'S' && path[n-2] == '.') - e->translate = translate_merlin; - } - } + if (g_cfg_host_merlin && fi.file_type == 0x04) { + int n = strlen(path); + if (n >= 3 && path[n-1] == 'S' && path[n-2] == '.') + e->translate = translate_merlin; + } + } - e->access = access; - e->path = strdup(path); - e->type = type; + e->access = access; + e->path = strdup(path); + e->type = type; - // insert it in the linked list. - e->next = fd_head; - fd_head = e; + // insert it in the linked list. + e->next = fd_head; + fd_head = e; - engine.xreg = e->cookie; - engine.yreg = access; // actual access, needed in fcr. + engine.xreg = e->cookie; + engine.yreg = access; // actual access, needed in fcr. - return rv; + return rv; } static word32 fst_read(int class) { - int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); + int cookie = engine.yreg; + struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + if (!e) return invalidRefNum; - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - if (!(e->access & readEnable)) - return invalidAccess; + if (!(e->access & readEnable)) + return invalidAccess; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word32 data_buffer = 0; - word32 request_count = 0; - word32 transfer_count = 0; + word32 data_buffer = 0; + word32 request_count = 0; + word32 transfer_count = 0; - if (class) { - data_buffer = get_memory24_c(pb + IORecGS_dataBuffer, 0); - request_count = get_memory24_c(pb + IORecGS_requestCount, 0); - // pre-zero transfer count - set_memory32_c(pb + IORecGS_transferCount, 0, 0); - } else { - data_buffer = get_memory24_c(pb + FileIORec_dataBuffer, 0); - request_count = get_memory24_c(pb + FileIORec_requestCount, 0); - set_memory32_c(pb + FileIORec_transferCount, 0, 0); - } + if (class) { + data_buffer = get_memory24_c(pb + IORecGS_dataBuffer, 0); + request_count = get_memory24_c(pb + IORecGS_requestCount, 0); + // pre-zero transfer count + set_memory32_c(pb + IORecGS_transferCount, 0, 0); + } else { + data_buffer = get_memory24_c(pb + FileIORec_dataBuffer, 0); + request_count = get_memory24_c(pb + FileIORec_requestCount, 0); + set_memory32_c(pb + FileIORec_transferCount, 0, 0); + } - if (request_count == 0) return 0; + if (request_count == 0) return 0; - word16 newline_mask; - word32 rv = 0; - int ok; + word16 newline_mask; + word32 rv = 0; + int ok; - newline_mask = get_memory16_c(global_buffer, 0); - if (newline_mask) { - byte newline_table[256]; - for (int i = 0; i < 256; ++i) - newline_table[i] = get_memory_c(global_buffer + 2 + i, 0); + newline_mask = get_memory16_c(global_buffer, 0); + if (newline_mask) { + byte newline_table[256]; + for (int i = 0; i < 256; ++i) + newline_table[i] = get_memory_c(global_buffer + 2 + i, 0); - for (word32 i = 0 ; i < request_count; ++i) { - byte b; - DWORD read_count; - ok = ReadFile(e->handle, &b, 1, &read_count, NULL); - if (!ok) return map_last_error(); - if (read_count == 0) break; - transfer_count++; + for (word32 i = 0; i < request_count; ++i) { + byte b; + DWORD read_count; + ok = ReadFile(e->handle, &b, 1, &read_count, NULL); + if (!ok) return map_last_error(); + if (read_count == 0) break; + transfer_count++; - switch(e->translate) { - case translate_crlf: - lf_to_cr(&b, 1); - break; - case translate_merlin: - text_to_merlin(&b, 1); - break; - } + switch(e->translate) { + case translate_crlf: + lf_to_cr(&b, 1); + break; + case translate_merlin: + text_to_merlin(&b, 1); + break; + } - set_memory_c(data_buffer++, b, 0); - if (newline_table[b & newline_mask]) break; - } - if (transfer_count == 0) rv = eofEncountered; - } - else { - DWORD read_count; - byte *data = gc_malloc(request_count); - if (!data) return outOfMem; + set_memory_c(data_buffer++, b, 0); + if (newline_table[b & newline_mask]) break; + } + if (transfer_count == 0) rv = eofEncountered; + } + else { + DWORD read_count; + byte *data = gc_malloc(request_count); + if (!data) return outOfMem; - ok = ReadFile(e->handle, data, request_count, &read_count, NULL); - if (!ok) rv = map_last_error(); - else if (read_count == 0) rv = eofEncountered; - if (read_count > 0) { - transfer_count = read_count; - switch(e->translate) { - case translate_crlf: - lf_to_cr(data, transfer_count); - break; - case translate_merlin: - text_to_merlin(data, transfer_count); - break; - } - for (size_t i = 0; i < transfer_count; ++i) { - set_memory_c(data_buffer + i, data[i], 0); - } - } - } + ok = ReadFile(e->handle, data, request_count, &read_count, NULL); + if (!ok) rv = map_last_error(); + else if (read_count == 0) rv = eofEncountered; + if (read_count > 0) { + transfer_count = read_count; + switch(e->translate) { + case translate_crlf: + lf_to_cr(data, transfer_count); + break; + case translate_merlin: + text_to_merlin(data, transfer_count); + break; + } + for (size_t i = 0; i < transfer_count; ++i) { + set_memory_c(data_buffer + i, data[i], 0); + } + } + } - if (transfer_count) { - if (class) - set_memory32_c(pb + IORecGS_transferCount, transfer_count, 0); - else - set_memory32_c(pb + FileIORec_transferCount, transfer_count, 0); - } + if (transfer_count) { + if (class) + set_memory32_c(pb + IORecGS_transferCount, transfer_count, 0); + else + set_memory32_c(pb + FileIORec_transferCount, transfer_count, 0); + } - return rv; + return rv; } static word32 fst_write(int class) { - int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); + int cookie = engine.yreg; + struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + if (!e) return invalidRefNum; - switch (e->type) { - case directory_file: - return badStoreType; - } - if (read_only) return drvrWrtProt; + switch (e->type) { + case directory_file: + return badStoreType; + } + if (read_only) return drvrWrtProt; - if (!(e->access & writeEnable)) - return invalidAccess; + if (!(e->access & writeEnable)) + return invalidAccess; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word32 data_buffer = 0; - word32 request_count = 0; + word32 data_buffer = 0; + word32 request_count = 0; - if (class) { - data_buffer = get_memory24_c(pb + IORecGS_dataBuffer, 0); - request_count = get_memory24_c(pb + IORecGS_requestCount, 0); - // pre-zero transfer count - set_memory32_c(pb + IORecGS_transferCount, 0, 0); - } else { - data_buffer = get_memory24_c(pb + FileIORec_dataBuffer, 0); - request_count = get_memory24_c(pb + FileIORec_requestCount, 0); - set_memory32_c(pb + FileIORec_transferCount, 0, 0); - } + if (class) { + data_buffer = get_memory24_c(pb + IORecGS_dataBuffer, 0); + request_count = get_memory24_c(pb + IORecGS_requestCount, 0); + // pre-zero transfer count + set_memory32_c(pb + IORecGS_transferCount, 0, 0); + } else { + data_buffer = get_memory24_c(pb + FileIORec_dataBuffer, 0); + request_count = get_memory24_c(pb + FileIORec_requestCount, 0); + set_memory32_c(pb + FileIORec_transferCount, 0, 0); + } - if (request_count == 0) return 0; - byte *data = gc_malloc(request_count); - if (!data) return outOfMem; + if (request_count == 0) return 0; + byte *data = gc_malloc(request_count); + if (!data) return outOfMem; - for (word32 i = 0; i < request_count; ++i) { - data[i] = get_memory_c(data_buffer + i,0); - } + for (word32 i = 0; i < request_count; ++i) { + data[i] = get_memory_c(data_buffer + i,0); + } - switch (e->translate) { - case translate_crlf: - cr_to_lf(data, request_count); - break; - case translate_merlin: - merlin_to_text(data, request_count); - break; - } - word32 rv = 0; - DWORD write_count = 0; - int ok = WriteFile(e->handle, data, request_count, &write_count, NULL); - if (!ok) rv = map_last_error(); - if (write_count > 0) { - if (class) - set_memory32_c(pb + IORecGS_transferCount, write_count, 0); - else - set_memory32_c(pb + FileIORec_transferCount, write_count, 0); - } - return rv; + switch (e->translate) { + case translate_crlf: + cr_to_lf(data, request_count); + break; + case translate_merlin: + merlin_to_text(data, request_count); + break; + } + word32 rv = 0; + DWORD write_count = 0; + int ok = WriteFile(e->handle, data, request_count, &write_count, NULL); + if (!ok) rv = map_last_error(); + if (write_count > 0) { + if (class) + set_memory32_c(pb + IORecGS_transferCount, write_count, 0); + else + set_memory32_c(pb + FileIORec_transferCount, write_count, 0); + } + return rv; } static word32 fst_close(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - return remove_fd(cookie); + return remove_fd(cookie); } static word32 fst_flush(int class) { - int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); + int cookie = engine.yreg; + struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + if (!e) return invalidRefNum; - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - if (!FlushFileBuffers(e->handle)) return map_last_error(); - return 0; + if (!FlushFileBuffers(e->handle)) return map_last_error(); + return 0; } static word16 win_seek(HANDLE h, word16 base, word32 displacement, LARGE_INTEGER *position) { - LARGE_INTEGER d; - int m = 0; - switch (base) { - case startPlus: - m = FILE_BEGIN; - d.QuadPart = displacement; - break; - case markPlus: - m = FILE_CURRENT; - d.QuadPart = displacement; - break; - case markMinus: - m = FILE_CURRENT; - d.QuadPart = -displacement; - break; - case eofMinus: - m = FILE_END; - d.QuadPart = -displacement; - break; + LARGE_INTEGER d; + int m = 0; + switch (base) { + case startPlus: + m = FILE_BEGIN; + d.QuadPart = displacement; + break; + case markPlus: + m = FILE_CURRENT; + d.QuadPart = displacement; + break; + case markMinus: + m = FILE_CURRENT; + d.QuadPart = -displacement; + break; + case eofMinus: + m = FILE_END; + d.QuadPart = -displacement; + break; - default: - return paramRangeErr; + default: + return paramRangeErr; - } - if (!SetFilePointerEx(h, d, position, m)) return map_last_error(); - return 0; + } + if (!SetFilePointerEx(h, d, position, m)) return map_last_error(); + return 0; } static word32 fst_set_mark(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + struct fd_entry *e = find_fd(cookie); + if (!e) return invalidRefNum; - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word16 base = 0; - word32 displacement = 0; - if (class) { - base = get_memory16_c(pb + SetPositionRecGS_base, 0); - displacement = get_memory32_c(pb + SetPositionRecGS_displacement, 0); - } else { - displacement = get_memory32_c(pb + MarkRec_position, 0); - } - if (base > markMinus) return paramRangeErr; + word16 base = 0; + word32 displacement = 0; + if (class) { + base = get_memory16_c(pb + SetPositionRecGS_base, 0); + displacement = get_memory32_c(pb + SetPositionRecGS_displacement, 0); + } else { + displacement = get_memory32_c(pb + MarkRec_position, 0); + } + if (base > markMinus) return paramRangeErr; - return win_seek(e->handle, base, displacement, NULL); + return win_seek(e->handle, base, displacement, NULL); } static word32 fst_set_eof(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + struct fd_entry *e = find_fd(cookie); + if (!e) return invalidRefNum; - switch (e->type) { - case directory_file: - return badStoreType; - } - if (read_only) return drvrWrtProt; + switch (e->type) { + case directory_file: + return badStoreType; + } + if (read_only) return drvrWrtProt; - if (!(e->access & writeEnable)) - return invalidAccess; + if (!(e->access & writeEnable)) + return invalidAccess; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word16 base = 0; - word32 displacement = 0; - if (class) { - base = get_memory16_c(pb + SetPositionRecGS_base, 0); - displacement = get_memory32_c(pb + SetPositionRecGS_displacement, 0); - } else { - displacement = get_memory32_c(pb + EOFRec_eofPosition, 0); - } + word16 base = 0; + word32 displacement = 0; + if (class) { + base = get_memory16_c(pb + SetPositionRecGS_base, 0); + displacement = get_memory32_c(pb + SetPositionRecGS_displacement, 0); + } else { + displacement = get_memory32_c(pb + EOFRec_eofPosition, 0); + } - if (base > markMinus) return paramRangeErr; + if (base > markMinus) return paramRangeErr; - // get the current mark - LARGE_INTEGER mark; - LARGE_INTEGER zero; zero.QuadPart = 0; - SetFilePointerEx(e->handle, zero, &mark, FILE_CURRENT); + // get the current mark + LARGE_INTEGER mark; + LARGE_INTEGER zero; zero.QuadPart = 0; + SetFilePointerEx(e->handle, zero, &mark, FILE_CURRENT); - word16 rv = win_seek(e->handle, base, displacement, NULL); - if (rv) return rv; + word16 rv = win_seek(e->handle, base, displacement, NULL); + if (rv) return rv; - // SetEndOfFile sets the current positions as eof. - if (!SetEndOfFile(e->handle)) return map_last_error(); + // SetEndOfFile sets the current positions as eof. + if (!SetEndOfFile(e->handle)) return map_last_error(); - // restore old mark. ??? - SetFilePointerEx(e->handle, mark, NULL, FILE_BEGIN); - return 0; + // restore old mark. ??? + SetFilePointerEx(e->handle, mark, NULL, FILE_BEGIN); + return 0; } static word32 fst_get_mark(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + struct fd_entry *e = find_fd(cookie); + if (!e) return invalidRefNum; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - // get the current mark - LARGE_INTEGER pos; - LARGE_INTEGER zero; zero.QuadPart = 0; - if (!SetFilePointerEx(e->handle, zero, &pos, FILE_CURRENT)) return map_last_error(); + // get the current mark + LARGE_INTEGER pos; + LARGE_INTEGER zero; zero.QuadPart = 0; + if (!SetFilePointerEx(e->handle, zero, &pos, FILE_CURRENT)) return map_last_error(); - if (class) { - set_memory32_c(pb + PositionRecGS_position, pos.LowPart, 0); - } else { - set_memory32_c(pb + MarkRec_position, pos.LowPart, 0); - } + if (class) { + set_memory32_c(pb + PositionRecGS_position, pos.LowPart, 0); + } else { + set_memory32_c(pb + MarkRec_position, pos.LowPart, 0); + } - return 0; + return 0; } static word32 fst_get_eof(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + struct fd_entry *e = find_fd(cookie); + if (!e) return invalidRefNum; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - switch (e->type) { - case directory_file: - return badStoreType; - } + switch (e->type) { + case directory_file: + return badStoreType; + } - LARGE_INTEGER eof; + LARGE_INTEGER eof; - if (!GetFileSizeEx(e->handle, &eof)) return map_last_error(); + if (!GetFileSizeEx(e->handle, &eof)) return map_last_error(); - // what if > 32 bits? + // what if > 32 bits? - if (class) { - set_memory32_c(pb + PositionRecGS_position, eof.LowPart, 0); - } else { - set_memory32_c(pb + MarkRec_position, eof.LowPart, 0); - } + if (class) { + set_memory32_c(pb + PositionRecGS_position, eof.LowPart, 0); + } else { + set_memory32_c(pb + MarkRec_position, eof.LowPart, 0); + } - return 0; + return 0; } @@ -2204,428 +2206,428 @@ static word32 fst_get_eof(int class) { static word32 fst_get_dir_entry(int class) { - int cookie = engine.yreg; + int cookie = engine.yreg; - struct fd_entry *e = find_fd(cookie); - if (!e) return invalidRefNum; + struct fd_entry *e = find_fd(cookie); + if (!e) return invalidRefNum; - if (e->type != directory_file) return badFileFormat; + if (e->type != directory_file) return badFileFormat; - word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); + word32 pb = get_memory24_c(engine.direct + dp_param_blk_ptr, 0); - word16 base = 0; - word16 pcount = 0; - word32 displacement = 0; - word32 name = 0; + word16 base = 0; + word16 pcount = 0; + word32 displacement = 0; + word32 name = 0; - if (class) { - pcount = get_memory16_c(pb, 0); - base = get_memory16_c(pb + DirEntryRecGS_base, 0); - displacement = get_memory16_c(pb + DirEntryRecGS_displacement, 0); - name = get_memory24_c(pb + DirEntryRecGS_name, 0); - } else { - base = get_memory16_c(pb + DirEntryRec_base, 0); - displacement = get_memory16_c(pb + DirEntryRec_displacement, 0); - name = get_memory24_c(pb + DirEntryRec_nameBuffer, 0); - } + if (class) { + pcount = get_memory16_c(pb, 0); + base = get_memory16_c(pb + DirEntryRecGS_base, 0); + displacement = get_memory16_c(pb + DirEntryRecGS_displacement, 0); + name = get_memory24_c(pb + DirEntryRecGS_name, 0); + } else { + base = get_memory16_c(pb + DirEntryRec_base, 0); + displacement = get_memory16_c(pb + DirEntryRec_displacement, 0); + name = get_memory24_c(pb + DirEntryRec_nameBuffer, 0); + } - if (base == 0 && displacement == 0) { - // count them up. - int count = e->dir->num_entries; - e->dir->displacement = 0; + if (base == 0 && displacement == 0) { + // count them up. + int count = e->dir->num_entries; + e->dir->displacement = 0; - if (class) { - if (pcount >= 6) set_memory16_c(pb + DirEntryRecGS_entryNum, count, 0); - } - else { - set_memory16_c(pb + DirEntryRec_entryNum, count, 0); - } + if (class) { + if (pcount >= 6) set_memory16_c(pb + DirEntryRecGS_entryNum, count, 0); + } + else { + set_memory16_c(pb + DirEntryRec_entryNum, count, 0); + } - return 0; - } + return 0; + } - int dir_displacement = e->dir->displacement; - switch (base) { - case 0: // displacement is absolute entry number. - break; - case 1: // displacement is added to the current displacement. - displacement = dir_displacement + displacement; - break; - case 2: // displacement is substracted from current displacement. - displacement = dir_displacement - displacement; - break; - default: - return paramRangeErr; - } - //if (displacement) --displacement; - --displacement; - if (displacement < 0) return endOfDir; - if (displacement >= e->dir->num_entries) return endOfDir; + int dir_displacement = e->dir->displacement; + switch (base) { + case 0: // displacement is absolute entry number. + break; + case 1: // displacement is added to the current displacement. + displacement = dir_displacement + displacement; + break; + case 2: // displacement is substracted from current displacement. + displacement = dir_displacement - displacement; + break; + default: + return paramRangeErr; + } + //if (displacement) --displacement; + --displacement; + if (displacement < 0) return endOfDir; + if (displacement >= e->dir->num_entries) return endOfDir; - word32 rv = 0; - const char *dname = e->dir->entries[displacement++]; - e->dir->displacement = displacement; - char *fullpath = append_path(e->path, dname); - struct file_info fi; - rv = get_file_info(fullpath, &fi); + word32 rv = 0; + const char *dname = e->dir->entries[displacement++]; + e->dir->displacement = displacement; + char *fullpath = append_path(e->path, dname); + struct file_info fi; + rv = get_file_info(fullpath, &fi); - if (dname) fprintf(stderr, " - %s", dname); + if (dname) fprintf(stderr, " - %s", dname); - // p16 and gs/os both use truncating c1 output string. - rv = set_gsstr_truncate(name, dname); + // p16 and gs/os both use truncating c1 output string. + rv = set_gsstr_truncate(name, dname); - if (class) { + if (class) { - if (pcount > 2) set_memory16_c(pb + DirEntryRecGS_flags, fi.storage_type == 0x05 ? 0x8000 : 0, 0); + if (pcount > 2) set_memory16_c(pb + DirEntryRecGS_flags, fi.storage_type == 0x05 ? 0x8000 : 0, 0); - if (pcount >= 6) set_memory16_c(pb + DirEntryRecGS_entryNum, displacement, 0); - if (pcount >= 7) set_memory16_c(pb + DirEntryRecGS_fileType, fi.file_type, 0); - if (pcount >= 8) set_memory32_c(pb + DirEntryRecGS_eof, fi.eof, 0); - if (pcount >= 9) set_memory32_c(pb + DirEntryRecGS_blockCount, fi.blocks, 0); + if (pcount >= 6) set_memory16_c(pb + DirEntryRecGS_entryNum, displacement, 0); + if (pcount >= 7) set_memory16_c(pb + DirEntryRecGS_fileType, fi.file_type, 0); + if (pcount >= 8) set_memory32_c(pb + DirEntryRecGS_eof, fi.eof, 0); + if (pcount >= 9) set_memory32_c(pb + DirEntryRecGS_blockCount, fi.blocks, 0); - if (pcount >= 10) set_date_time_rec(pb + DirEntryRecGS_createDateTime, fi.create_date); - if (pcount >= 11) set_date_time_rec(pb + DirEntryRecGS_modDateTime, fi.modified_date); + if (pcount >= 10) set_date_time_rec(pb + DirEntryRecGS_createDateTime, fi.create_date); + if (pcount >= 11) set_date_time_rec(pb + DirEntryRecGS_modDateTime, fi.modified_date); - if (pcount >= 12) set_memory16_c(pb + DirEntryRecGS_access, fi.access, 0); - if (pcount >= 13) set_memory32_c(pb + DirEntryRecGS_auxType, fi.aux_type, 0); - if (pcount >= 14) set_memory16_c(pb + DirEntryRecGS_fileSysID, mfsFSID, 0); + if (pcount >= 12) set_memory16_c(pb + DirEntryRecGS_access, fi.access, 0); + if (pcount >= 13) set_memory32_c(pb + DirEntryRecGS_auxType, fi.aux_type, 0); + if (pcount >= 14) set_memory16_c(pb + DirEntryRecGS_fileSysID, mfsFSID, 0); - if (pcount >= 15) { - word16 fst_id = hfsFSID; - //if (fi.storage_type == 0x0f) fst_id = mfsFSID; - word32 option_list = get_memory24_c(pb + DirEntryRecGS_optionList, 0); - word32 tmp = set_option_list(option_list, fst_id, fi.afp.finder_info, fi.has_fi ? 32 : 0); - if (!rv) rv = tmp; - } + if (pcount >= 15) { + word16 fst_id = hfsFSID; + //if (fi.storage_type == 0x0f) fst_id = mfsFSID; + word32 option_list = get_memory24_c(pb + DirEntryRecGS_optionList, 0); + word32 tmp = set_option_list(option_list, fst_id, fi.afp.finder_info, fi.has_fi ? 32 : 0); + if (!rv) rv = tmp; + } - if (pcount >= 16) set_memory32_c(pb + DirEntryRecGS_resourceEOF, fi.resource_eof, 0); - if (pcount >= 17) set_memory32_c(pb + DirEntryRecGS_resourceBlocks, fi.resource_blocks, 0); - } - else { - set_memory16_c(pb + DirEntryRec_flags, fi.storage_type == 0x05 ? 0x8000 : 0, 0); + if (pcount >= 16) set_memory32_c(pb + DirEntryRecGS_resourceEOF, fi.resource_eof, 0); + if (pcount >= 17) set_memory32_c(pb + DirEntryRecGS_resourceBlocks, fi.resource_blocks, 0); + } + else { + set_memory16_c(pb + DirEntryRec_flags, fi.storage_type == 0x05 ? 0x8000 : 0, 0); - set_memory16_c(pb + DirEntryRec_entryNum, displacement, 0); - set_memory16_c(pb + DirEntryRec_fileType, fi.file_type, 0); - set_memory32_c(pb + DirEntryRec_endOfFile, fi.eof, 0); - set_memory32_c(pb + DirEntryRec_blockCount, fi.blocks, 0); + set_memory16_c(pb + DirEntryRec_entryNum, displacement, 0); + set_memory16_c(pb + DirEntryRec_fileType, fi.file_type, 0); + set_memory32_c(pb + DirEntryRec_endOfFile, fi.eof, 0); + set_memory32_c(pb + DirEntryRec_blockCount, fi.blocks, 0); - set_date_time_rec(pb + DirEntryRec_createTime, fi.create_date); - set_date_time_rec(pb + DirEntryRec_modTime, fi.modified_date); + set_date_time_rec(pb + DirEntryRec_createTime, fi.create_date); + set_date_time_rec(pb + DirEntryRec_modTime, fi.modified_date); - set_memory16_c(pb + DirEntryRec_access, fi.access, 0); - set_memory32_c(pb + DirEntryRec_auxType, fi.aux_type, 0); - set_memory16_c(pb + DirEntryRec_fileSysID, mfsFSID, 0); + set_memory16_c(pb + DirEntryRec_access, fi.access, 0); + set_memory32_c(pb + DirEntryRec_auxType, fi.aux_type, 0); + set_memory16_c(pb + DirEntryRec_fileSysID, mfsFSID, 0); - } + } - return rv; + return rv; } static word32 fst_change_path(int class, const char *path1, const char *path2) { - /* make sure they're not trying to rename the volume... */ - BOOL ok = 0; + /* make sure they're not trying to rename the volume... */ + BOOL ok = 0; - word16 rv = 0; - word16 type = storage_type(path1, &rv); - if (rv) return rv; - if (type == 0x0f) return badStoreType; - if (read_only) return drvrWrtProt; + word16 rv = 0; + word16 type = storage_type(path1, &rv); + if (rv) return rv; + if (type == 0x0f) return badStoreType; + if (read_only) return drvrWrtProt; - if (!MoveFile(path1, path2)) return map_last_error(); - return 0; + if (!MoveFile(path1, path2)) return map_last_error(); + return 0; } static word32 fst_format(int class) { - return notBlockDev; + return notBlockDev; } static word32 fst_erase(int class) { - return notBlockDev; + return notBlockDev; } static const char *call_name(word16 call) { - static char* class1[] = { - // 0x00 - "", - "CreateGS", - "DestroyGS", - "", - "ChangePathGS", - "SetFileInfoGS", - "GetFileInfoGS", - "JudgeNameGS", - "VolumeGS", - "", - "", - "ClearBackupGS", - "", - "", - "", - "", + static char* class1[] = { + // 0x00 + "", + "CreateGS", + "DestroyGS", + "", + "ChangePathGS", + "SetFileInfoGS", + "GetFileInfoGS", + "JudgeNameGS", + "VolumeGS", + "", + "", + "ClearBackupGS", + "", + "", + "", + "", - // 0x10 - "OpenGS", - "", - "ReadGS", - "WriteGS", - "CloseGS", - "FlushGS", - "SetMarkGS", - "GetMarkGS", - "SetEOFGS", - "GetEOFGS", - "", - "", - "GetDirEntryGS", - "", - "", - "", + // 0x10 + "OpenGS", + "", + "ReadGS", + "WriteGS", + "CloseGS", + "FlushGS", + "SetMarkGS", + "GetMarkGS", + "SetEOFGS", + "GetEOFGS", + "", + "", + "GetDirEntryGS", + "", + "", + "", - // 0x20 - "", - "", - "", - "", - "FormatGS", - "EraseDiskGS", - }; + // 0x20 + "", + "", + "", + "", + "FormatGS", + "EraseDiskGS", + }; - static char* class0[] = { - // 0x00 - "", - "CREATE", - "DESTROY", - "", - "CHANGE_PATH", - "SET_FILE_INFO", - "GET_FILE_INFO", - "", - "VOLUME", - "", - "", - "CLEAR_BACKUP_BIT", - "", - "", - "", - "", + static char* class0[] = { + // 0x00 + "", + "CREATE", + "DESTROY", + "", + "CHANGE_PATH", + "SET_FILE_INFO", + "GET_FILE_INFO", + "", + "VOLUME", + "", + "", + "CLEAR_BACKUP_BIT", + "", + "", + "", + "", - // 0X10 - "OPEN", - "", - "READ", - "WRITE", - "CLOSE", - "FLUSH", - "SET_MARK", - "GET_MARK", - "SET_EOF", - "GET_EOF", - "", - "", - "GET_DIR_ENTRY", - "", - "", - "", + // 0X10 + "OPEN", + "", + "READ", + "WRITE", + "CLOSE", + "FLUSH", + "SET_MARK", + "GET_MARK", + "SET_EOF", + "GET_EOF", + "", + "", + "GET_DIR_ENTRY", + "", + "", + "", - // 0X20 - "", - "", - "", - "", - "FORMAT", - "ERASE_DISK", - }; + // 0X20 + "", + "", + "", + "", + "FORMAT", + "ERASE_DISK", + }; - if (call & 0x8000) { - static char *sys[] = { - "", - "fst_startup", - "fst_shutdown", - "fst_remove_vcr", - "fst_deferred_flush" - }; - call &= ~0x8000; + if (call & 0x8000) { + static char *sys[] = { + "", + "fst_startup", + "fst_shutdown", + "fst_remove_vcr", + "fst_deferred_flush" + }; + call &= ~0x8000; - if (call < sizeof(sys) / sizeof(sys[0])) - return sys[call]; + if (call < sizeof(sys) / sizeof(sys[0])) + return sys[call]; - return ""; - } + return ""; + } - int class = call >> 13; - call &= 0x1fff; - switch(class) { - case 0: - if (call < sizeof(class0) / sizeof(class0[0])) - return class0[call]; - break; - case 1: - if (call < sizeof(class1) / sizeof(class1[0])) - return class1[call]; - break; + int class = call >> 13; + call &= 0x1fff; + switch(class) { + case 0: + if (call < sizeof(class0) / sizeof(class0[0])) + return class0[call]; + break; + case 1: + if (call < sizeof(class1) / sizeof(class1[0])) + return class1[call]; + break; - } - return ""; + } + return ""; } static const char *error_name(word16 error) { - static char *errors[] = { - "", - "badSystemCall", - "", - "", - "invalidPcount", - "", - "", - "gsosActive", - "", - "", - "", - "", - "", - "", - "", - "", - // 0x10 - "devNotFound", - "invalidDevNum", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - // 0x20 - "drvrBadReq", - "drvrBadCode", - "drvrBadParm", - "drvrNotOpen", - "drvrPriorOpen", - "irqTableFull", - "drvrNoResrc", - "drvrIOError", - "drvrNoDevice", - "drvrBusy", - "", - "drvrWrtProt", - "drvrBadCount", - "drvrBadBlock", - "drvrDiskSwitch", - "drvrOffLine", - // 0x30 - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - // 0x40 - "badPathSyntax", - "", - "tooManyFilesOpen", - "invalidRefNum", - "pathNotFound", - "volNotFound", - "fileNotFound", - "dupPathname", - "volumeFull", - "volDirFull", - "badFileFormat", - "badStoreType", - "eofEncountered", - "outOfRange", - "invalidAccess", - "buffTooSmall", - // 0x50 - "fileBusy", - "dirError", - "unknownVol", - "paramRangeErr", - "outOfMem", - "", - "", - "dupVolume", - "notBlockDev", - "invalidLevel", - "damagedBitMap", - "badPathNames", - "notSystemFile", - "osUnsupported", - "", - "stackOverflow", - // 0x60 - "dataUnavail", - "endOfDir", - "invalidClass", - "resForkNotFound", - "invalidFSTID", - "invalidFSTop", - "fstCaution", - "devNameErr", - "defListFull", - "supListFull", - "fstError", - "", - "", - "", - "", - "", - //0x70 - "resExistsErr", - "resAddErr", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - //0x80 - "", - "", - "", - "", - "", - "", - "", - "", - "networkError" - }; + static char *errors[] = { + "", + "badSystemCall", + "", + "", + "invalidPcount", + "", + "", + "gsosActive", + "", + "", + "", + "", + "", + "", + "", + "", + // 0x10 + "devNotFound", + "invalidDevNum", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + // 0x20 + "drvrBadReq", + "drvrBadCode", + "drvrBadParm", + "drvrNotOpen", + "drvrPriorOpen", + "irqTableFull", + "drvrNoResrc", + "drvrIOError", + "drvrNoDevice", + "drvrBusy", + "", + "drvrWrtProt", + "drvrBadCount", + "drvrBadBlock", + "drvrDiskSwitch", + "drvrOffLine", + // 0x30 + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + // 0x40 + "badPathSyntax", + "", + "tooManyFilesOpen", + "invalidRefNum", + "pathNotFound", + "volNotFound", + "fileNotFound", + "dupPathname", + "volumeFull", + "volDirFull", + "badFileFormat", + "badStoreType", + "eofEncountered", + "outOfRange", + "invalidAccess", + "buffTooSmall", + // 0x50 + "fileBusy", + "dirError", + "unknownVol", + "paramRangeErr", + "outOfMem", + "", + "", + "dupVolume", + "notBlockDev", + "invalidLevel", + "damagedBitMap", + "badPathNames", + "notSystemFile", + "osUnsupported", + "", + "stackOverflow", + // 0x60 + "dataUnavail", + "endOfDir", + "invalidClass", + "resForkNotFound", + "invalidFSTID", + "invalidFSTop", + "fstCaution", + "devNameErr", + "defListFull", + "supListFull", + "fstError", + "", + "", + "", + "", + "", + //0x70 + "resExistsErr", + "resAddErr", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + //0x80 + "", + "", + "", + "", + "", + "", + "", + "", + "networkError" + }; - if (error < sizeof(errors) / sizeof(errors[0])) - return errors[error]; - return ""; + if (error < sizeof(errors) / sizeof(errors[0])) + return errors[error]; + return ""; } #ifdef __CYGWIN__ @@ -2634,13 +2636,13 @@ static const char *error_name(word16 error) { static char *expand_path(const char *path, word32 *error) { - char buffer[PATH_MAX]; - ssize_t ok = cygwin_conv_path(CCP_POSIX_TO_WIN_A, path, buffer, sizeof(buffer)); - if (ok < 0) { - *error = fstError; - return NULL; - } - return gc_strdup(buffer); + char buffer[PATH_MAX]; + ssize_t ok = cygwin_conv_path(CCP_POSIX_TO_WIN_A, path, buffer, sizeof(buffer)); + if (ok < 0) { + *error = fstError; + return NULL; + } + return gc_strdup(buffer); } @@ -2650,213 +2652,213 @@ static char *expand_path(const char *path, word32 *error) { void host_fst(void) { - - /* - * input: - * c = set - * a = default error code - * x = gs/os callnum - * y = [varies] - * - * output: - * c = set/clear - * a = error code/0 - * x = varies - * y = varies - */ + + /* + * input: + * c = set + * a = default error code + * x = gs/os callnum + * y = [varies] + * + * output: + * c = set/clear + * a = error code/0 + * x = varies + * y = varies + */ - word32 acc = 0; - word16 call = engine.xreg; + word32 acc = 0; + word16 call = engine.xreg; - fprintf(stderr, "Host FST: %04x %s", call, call_name(call)); + fprintf(stderr, "Host FST: %04x %s", call, call_name(call)); - if (call & 0x8000) { - fputs("\n", stderr); - // system level. - switch(call) { - case 0x8001: - acc = fst_startup(); - break; - case 0x8002: - acc = fst_shutdown(); - break; - default: - acc = badSystemCall; - break; - } - } else { + if (call & 0x8000) { + fputs("\n", stderr); + // system level. + switch(call) { + case 0x8001: + acc = fst_startup(); + break; + case 0x8002: + acc = fst_shutdown(); + break; + default: + acc = badSystemCall; + break; + } + } else { - if (!root) { - acc = networkError; - engine.acc = acc; - SEC(); - fprintf(stderr, " %02x %s\n", acc, error_name(acc)); + if (!root) { + acc = networkError; + engine.acc = acc; + SEC(); + fprintf(stderr, " %02x %s\n", acc, error_name(acc)); - return; - } + return; + } - int class = call >> 13; - call &= 0x1fff; + int class = call >> 13; + call &= 0x1fff; - if (class > 1) { - acc = invalidClass; - engine.acc = acc; - SEC(); - fprintf(stderr, " %02x %s\n", acc, error_name(acc)); + if (class > 1) { + acc = invalidClass; + engine.acc = acc; + SEC(); + fprintf(stderr, " %02x %s\n", acc, error_name(acc)); - return; - } + return; + } - char *path1 = NULL; - char *path2 = NULL; - char *path3 = NULL; - char *path4 = NULL; - const char *cp; + char *path1 = NULL; + char *path2 = NULL; + char *path3 = NULL; + char *path4 = NULL; + const char *cp; - switch(call & 0xff) { - case 0x01: - case 0x02: - case 0x05: - case 0x06: - case 0x0b: - case 0x10: - path1 = get_path1(); - break; - case 0x04: - path1 = get_path1(); - path2 = get_path2(); - break; - } + switch(call & 0xff) { + case 0x01: + case 0x02: + case 0x05: + case 0x06: + case 0x0b: + case 0x10: + path1 = get_path1(); + break; + case 0x04: + path1 = get_path1(); + path2 = get_path2(); + break; + } - if (path1) fprintf(stderr, " - %s", path1); - if (path2) fprintf(stderr, " - %s", path2); + if (path1) fprintf(stderr, " - %s", path1); + if (path2) fprintf(stderr, " - %s", path2); - switch(call & 0xff) { - case 0x01: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); - path3 = expand_path(path3, &acc); - if (acc) break; + switch(call & 0xff) { + case 0x01: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); + path3 = expand_path(path3, &acc); + if (acc) break; - acc = fst_create(class, path3); - break; - case 0x02: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); - path3 = expand_path(path3, &acc); - if (acc) break; + acc = fst_create(class, path3); + break; + case 0x02: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); + path3 = expand_path(path3, &acc); + if (acc) break; - acc = fst_destroy(class, path3); - break; - case 0x04: + acc = fst_destroy(class, path3); + break; + case 0x04: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); - path3 = expand_path(path3, &acc); - if (acc) break; + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); + path3 = expand_path(path3, &acc); + if (acc) break; - cp = check_path(path2, &acc); - if (acc) break; - path4 = append_path(root, cp); - path4 = expand_path(path4, &acc); - if (acc) break; + cp = check_path(path2, &acc); + if (acc) break; + path4 = append_path(root, cp); + path4 = expand_path(path4, &acc); + if (acc) break; - acc = fst_change_path(class, path3, path4); - break; - case 0x05: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); - path3 = expand_path(path3, &acc); - if (acc) break; + acc = fst_change_path(class, path3, path4); + break; + case 0x05: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); + path3 = expand_path(path3, &acc); + if (acc) break; - acc = fst_set_file_info(class, path3); - break; - case 0x06: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); - path3 = expand_path(path3, &acc); - if (acc) break; + acc = fst_set_file_info(class, path3); + break; + case 0x06: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); + path3 = expand_path(path3, &acc); + if (acc) break; - acc = fst_get_file_info(class, path3); - break; - case 0x07: - acc = fst_judge_name(class, path1); - break; - case 0x08: - acc = fst_volume(class); - break; - case 0x0b: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); - path3 = expand_path(path3, &acc); - if (acc) break; + acc = fst_get_file_info(class, path3); + break; + case 0x07: + acc = fst_judge_name(class, path1); + break; + case 0x08: + acc = fst_volume(class); + break; + case 0x0b: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); + path3 = expand_path(path3, &acc); + if (acc) break; - acc = fst_clear_backup(class, path3); - break; - case 0x10: - cp = check_path(path1, &acc); - if (acc) break; - path3 = append_path(root, cp); - path3 = expand_path(path3, &acc); - if (acc) break; + acc = fst_clear_backup(class, path3); + break; + case 0x10: + cp = check_path(path1, &acc); + if (acc) break; + path3 = append_path(root, cp); + path3 = expand_path(path3, &acc); + if (acc) break; - acc = fst_open(class, path3); - break; - case 0x012: - acc = fst_read(class); - break; - case 0x013: - acc = fst_write(class); - break; - case 0x14: - acc = fst_close(class); - break; - case 0x15: - acc = fst_flush(class); - break; - case 0x16: - acc = fst_set_mark(class); - break; - case 0x17: - acc = fst_get_mark(class); - break; - case 0x18: - acc = fst_set_eof(class); - break; - case 0x19: - acc = fst_get_eof(class); - break; - case 0x1c: - acc = fst_get_dir_entry(class); - break; - case 0x24: - acc = fst_format(class); - break; - case 0x25: - acc = fst_erase(class); - break; + acc = fst_open(class, path3); + break; + case 0x012: + acc = fst_read(class); + break; + case 0x013: + acc = fst_write(class); + break; + case 0x14: + acc = fst_close(class); + break; + case 0x15: + acc = fst_flush(class); + break; + case 0x16: + acc = fst_set_mark(class); + break; + case 0x17: + acc = fst_get_mark(class); + break; + case 0x18: + acc = fst_set_eof(class); + break; + case 0x19: + acc = fst_get_eof(class); + break; + case 0x1c: + acc = fst_get_dir_entry(class); + break; + case 0x24: + acc = fst_format(class); + break; + case 0x25: + acc = fst_erase(class); + break; - default: - acc = invalidFSTop; - break; - } - fputs("\n", stderr); - } + default: + acc = invalidFSTop; + break; + } + fputs("\n", stderr); + } - if (acc) fprintf(stderr, " %02x %s\n", acc, error_name(acc)); + if (acc) fprintf(stderr, " %02x %s\n", acc, error_name(acc)); - gc_free(); + gc_free(); - engine.acc = acc; - if (acc) SEC(); - else CLC(); + engine.acc = acc; + if (acc) SEC(); + else CLC(); } diff --git a/src/win32snd_driver.c b/src/win32snd_driver.c index 1cc6d5a..3e6067b 100644 --- a/src/win32snd_driver.c +++ b/src/win32snd_driver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ #include "defc.h" #include "sound.h" @@ -18,215 +18,201 @@ extern int g_audio_rate; unsigned int __stdcall child_sound_loop_win32(void *param); void check_wave_error(int res, char *str); -#define NUM_WAVE_HEADERS 8 +#define NUM_WAVE_HEADERS 8 -HWAVEOUT g_wave_handle = NULL; // OG Default value must be set +HWAVEOUT g_wave_handle = NULL; // OG Default value must be set WAVEHDR g_wavehdr[NUM_WAVE_HEADERS]; extern int g_audio_enable; extern word32 *g_sound_shm_addr; extern int g_preferred_rate; -int g_win32snd_buflen = 0x1000; +int g_win32snd_buflen = 0x1000; -void -win32snd_init(word32 *shmaddr) -{ - printf("win32snd_init\n"); - child_sound_loop(-1, -1, shmaddr); +void win32snd_init(word32 *shmaddr) { + printf("win32snd_init\n"); + child_sound_loop(-1, -1, shmaddr); - return; + return; } // OG Added global to free the dedicated win32 sound memory -byte *bptr = NULL; +byte *bptr = NULL; // OG shut win32 sound resources -void -win32snd_shutdown() -{ +void win32snd_shutdown() { - if (g_wave_handle) - { - MMRESULT res = waveOutReset(g_wave_handle); - if (res!=MMSYSERR_NOERROR ) - printf("waveOutReset Failed"); + if (g_wave_handle) + { + MMRESULT res = waveOutReset(g_wave_handle); + if (res!=MMSYSERR_NOERROR ) + printf("waveOutReset Failed"); - res = waveOutClose(g_wave_handle); - if (res!=MMSYSERR_NOERROR ) - printf("waveOutClose Failed"); - g_wave_handle=NULL; -} - // OG Free dedicated sound memory - if (bptr) - { - free(bptr); - bptr = NULL; - } + res = waveOutClose(g_wave_handle); + if (res!=MMSYSERR_NOERROR ) + printf("waveOutClose Failed"); + g_wave_handle=NULL; + } + // OG Free dedicated sound memory + if (bptr) + { + free(bptr); + bptr = NULL; + } } -void CALLBACK -handle_wav_snd(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, - DWORD_PTR dwParam2) -{ - LPWAVEHDR lpwavehdr; +void CALLBACK handle_wav_snd(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, + DWORD_PTR dwParam2) { + LPWAVEHDR lpwavehdr; - /* Only service "buffer done playing messages */ - if(uMsg == WOM_DONE) { - lpwavehdr = (LPWAVEHDR)dwParam1; - if(lpwavehdr->dwFlags == (WHDR_DONE | WHDR_PREPARED)) { - lpwavehdr->dwUser = FALSE; - } - } + /* Only service "buffer done playing messages */ + if(uMsg == WOM_DONE) { + lpwavehdr = (LPWAVEHDR)dwParam1; + if(lpwavehdr->dwFlags == (WHDR_DONE | WHDR_PREPARED)) { + lpwavehdr->dwUser = FALSE; + } + } - return; + return; } -void -check_wave_error(int res, char *str) -{ - TCHAR buf[256]; +void check_wave_error(int res, char *str) { + TCHAR buf[256]; - if(res == MMSYSERR_NOERROR) { - return; - } + if(res == MMSYSERR_NOERROR) { + return; + } - waveOutGetErrorText(res, &buf[0], sizeof(buf)); - printf("%s: %s\n", str, buf); - exit(1); + waveOutGetErrorText(res, &buf[0], sizeof(buf)); + printf("%s: %s\n", str, buf); + exit(1); } -void -child_sound_init_win32() -{ - WAVEFORMATEX wavefmt; - WAVEOUTCAPS caps; +void child_sound_init_win32() { + WAVEFORMATEX wavefmt; + WAVEOUTCAPS caps; // OG Moved as global variable (to rename) // byte *bptr; - int bits_per_sample, channels, block_align; - int blen; - int res; - int i; + int bits_per_sample, channels, block_align; + int blen; + int res; + int i; - memset(&wavefmt, 0, sizeof(WAVEFORMATEX)); + memset(&wavefmt, 0, sizeof(WAVEFORMATEX)); - wavefmt.wFormatTag = WAVE_FORMAT_PCM; + wavefmt.wFormatTag = WAVE_FORMAT_PCM; #ifndef UNDER_CE - bits_per_sample = 16; - wavefmt.nSamplesPerSec = g_audio_rate; + bits_per_sample = 16; + wavefmt.nSamplesPerSec = g_audio_rate; #else - bits_per_sample = 16; - wavefmt.nSamplesPerSec = 12000; + bits_per_sample = 16; + wavefmt.nSamplesPerSec = 12000; #endif - channels = 2; - wavefmt.wBitsPerSample = bits_per_sample; - wavefmt.nChannels = channels; - block_align = channels * (bits_per_sample / 8); - wavefmt.nBlockAlign = block_align; - wavefmt.nAvgBytesPerSec = block_align * g_audio_rate; + channels = 2; + wavefmt.wBitsPerSample = bits_per_sample; + wavefmt.nChannels = channels; + block_align = channels * (bits_per_sample / 8); + wavefmt.nBlockAlign = block_align; + wavefmt.nAvgBytesPerSec = block_align * g_audio_rate; - res = waveOutOpen(&g_wave_handle, WAVE_MAPPER, &wavefmt, 0, 0, - WAVE_FORMAT_QUERY); + res = waveOutOpen(&g_wave_handle, WAVE_MAPPER, &wavefmt, 0, 0, + WAVE_FORMAT_QUERY); - if(res != MMSYSERR_NOERROR) { - printf("Cannot open audio device\n"); - g_audio_enable = 0; - return; - } + if(res != MMSYSERR_NOERROR) { + printf("Cannot open audio device\n"); + g_audio_enable = 0; + return; + } - res = waveOutOpen(&g_wave_handle, WAVE_MAPPER, &wavefmt, - (DWORD_PTR)handle_wav_snd, 0, CALLBACK_FUNCTION | WAVE_ALLOWSYNC); + res = waveOutOpen(&g_wave_handle, WAVE_MAPPER, &wavefmt, + (DWORD_PTR)handle_wav_snd, 0, CALLBACK_FUNCTION | WAVE_ALLOWSYNC); - if(res != MMSYSERR_NOERROR) { - printf("Cannot register audio\n"); - g_audio_enable = 0; - return; - } + if(res != MMSYSERR_NOERROR) { + printf("Cannot register audio\n"); + g_audio_enable = 0; + return; + } - g_audio_rate = wavefmt.nSamplesPerSec; + g_audio_rate = wavefmt.nSamplesPerSec; - blen = (SOUND_SHM_SAMP_SIZE * 4 * 2) / NUM_WAVE_HEADERS; - g_win32snd_buflen = blen; - bptr = (byte*)malloc(blen * NUM_WAVE_HEADERS); // OG Added cast - if(bptr == NULL) { - printf("Unabled to allocate sound buffer\n"); - exit(1); - } + blen = (SOUND_SHM_SAMP_SIZE * 4 * 2) / NUM_WAVE_HEADERS; + g_win32snd_buflen = blen; + bptr = (byte*)malloc(blen * NUM_WAVE_HEADERS); // OG Added cast + if(bptr == NULL) { + printf("Unabled to allocate sound buffer\n"); + exit(1); + } - for(i = 0; i < NUM_WAVE_HEADERS; i++) { - memset(&g_wavehdr[i], 0, sizeof(WAVEHDR)); - g_wavehdr[i].dwUser = FALSE; - g_wavehdr[i].lpData = (LPSTR)&(bptr[i*blen]); // OG Added cast - g_wavehdr[i].dwBufferLength = blen; - g_wavehdr[i].dwFlags = 0; - g_wavehdr[i].dwLoops = 0; - res = waveOutPrepareHeader(g_wave_handle, &g_wavehdr[i], - sizeof(WAVEHDR)); - check_wave_error(res, "waveOutPrepareHeader"); - } + for(i = 0; i < NUM_WAVE_HEADERS; i++) { + memset(&g_wavehdr[i], 0, sizeof(WAVEHDR)); + g_wavehdr[i].dwUser = FALSE; + g_wavehdr[i].lpData = (LPSTR)&(bptr[i*blen]); // OG Added cast + g_wavehdr[i].dwBufferLength = blen; + g_wavehdr[i].dwFlags = 0; + g_wavehdr[i].dwLoops = 0; + res = waveOutPrepareHeader(g_wave_handle, &g_wavehdr[i], + sizeof(WAVEHDR)); + check_wave_error(res, "waveOutPrepareHeader"); + } - res = waveOutGetDevCaps((UINT_PTR)g_wave_handle, &caps, sizeof(caps)); - check_wave_error(res, "waveOutGetDevCaps"); - printf("Using %s\n", caps.szPname); - printf(" Bits per Sample = %d. Channels = %d\n", - wavefmt.wBitsPerSample, wavefmt.nChannels); - printf(" Sampling rate = %d, avg_bytes_per_sec = %d\n", - (int)wavefmt.nSamplesPerSec, (int)wavefmt.nAvgBytesPerSec); + res = waveOutGetDevCaps((UINT_PTR)g_wave_handle, &caps, sizeof(caps)); + check_wave_error(res, "waveOutGetDevCaps"); + printf("Using %s\n", caps.szPname); + printf(" Bits per Sample = %d. Channels = %d\n", + wavefmt.wBitsPerSample, wavefmt.nChannels); + printf(" Sampling rate = %d, avg_bytes_per_sec = %d\n", + (int)wavefmt.nSamplesPerSec, (int)wavefmt.nAvgBytesPerSec); - set_audio_rate(g_audio_rate); + set_audio_rate(g_audio_rate); } -void -win32_send_audio2(byte *ptr, int size) -{ - int found; - int res; - int i; +void win32_send_audio2(byte *ptr, int size) { + int found; + int res; + int i; - found = 0; - for(i = 0; i < NUM_WAVE_HEADERS; i++) { - if(g_wavehdr[i].dwUser == FALSE) { - found = 1; - break; - } - } + found = 0; + for(i = 0; i < NUM_WAVE_HEADERS; i++) { + if(g_wavehdr[i].dwUser == FALSE) { + found = 1; + break; + } + } - if(!found) { - /* all audio buffers busy, just get out */ - return; - } + if(!found) { + /* all audio buffers busy, just get out */ + return; + } - memcpy(g_wavehdr[i].lpData, ptr, size); - g_wavehdr[i].dwBufferLength = size; - g_wavehdr[i].dwUser = TRUE; + memcpy(g_wavehdr[i].lpData, ptr, size); + g_wavehdr[i].dwBufferLength = size; + g_wavehdr[i].dwUser = TRUE; - res = waveOutWrite(g_wave_handle, &g_wavehdr[i], sizeof(g_wavehdr)); - check_wave_error(res, "waveOutWrite"); + res = waveOutWrite(g_wave_handle, &g_wavehdr[i], sizeof(g_wavehdr)); + check_wave_error(res, "waveOutWrite"); - return; + return; } -int -win32_send_audio(byte *ptr, int in_size) -{ - int size; - int tmpsize; +int win32_send_audio(byte *ptr, int in_size) { + int size; + int tmpsize; - size = in_size; - while(size > 0) { - tmpsize = size; - if(size > g_win32snd_buflen) { - tmpsize = g_win32snd_buflen; - } - win32_send_audio2(ptr, tmpsize); - ptr += tmpsize; - size = size - tmpsize; - } + size = in_size; + while(size > 0) { + tmpsize = size; + if(size > g_win32snd_buflen) { + tmpsize = g_win32snd_buflen; + } + win32_send_audio2(ptr, tmpsize); + ptr += tmpsize; + size = size - tmpsize; + } - return in_size; + return in_size; } diff --git a/src/win_console.c b/src/win_console.c index 4b3c9aa..000aed6 100644 --- a/src/win_console.c +++ b/src/win_console.c @@ -1,12 +1,12 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ -#define WIN32_LEAN_AND_MEAN /* Tell windows we want less header gunk */ -#define STRICT /* Tell Windows we want compile type checks */ +#define WIN32_LEAN_AND_MEAN /* Tell windows we want less header gunk */ +#define STRICT /* Tell Windows we want compile type checks */ #include #include @@ -21,206 +21,188 @@ extern void gsportshut(); extern HWND g_hwnd_main; extern char *g_status_ptrs[MAX_STATUS_LINES]; -extern int g_win_status_debug; -extern int g_win_status_debug_request; -extern int g_win_fullscreen_state; +extern int g_win_status_debug; +extern int g_win_status_debug_request; +extern int g_win_fullscreen_state; -int -win_nonblock_read_stdin(int fd, char *bufptr, int len) -{ - DWORD charsRead = 0; - ReadConsole(GetStdHandle(STD_INPUT_HANDLE), bufptr, len, &charsRead, NULL); - - if (charsRead == 0) - { - errno = EAGAIN; - return -1; - } - else - { - DWORD charsWritten = 0; - WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), bufptr, charsRead, &charsWritten, NULL); - return charsRead; - } +int win_nonblock_read_stdin(int fd, char *bufptr, int len) { + DWORD charsRead = 0; + ReadConsole(GetStdHandle(STD_INPUT_HANDLE), bufptr, len, &charsRead, NULL); + + if (charsRead == 0) + { + errno = EAGAIN; + return -1; + } + else + { + DWORD charsWritten = 0; + WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), bufptr, charsRead, &charsWritten, NULL); + return charsRead; + } } -void get_cwd(LPTSTR buffer, int size) -{ - HMODULE hSelf; - hSelf = GetModuleHandle(NULL); - GetModuleFileName(hSelf,buffer,size); - PathRemoveFileSpec(buffer); - //printf("Local directory: [%s]\n",buffer); +void get_cwd(LPTSTR buffer, int size) { + HMODULE hSelf; + hSelf = GetModuleHandle(NULL); + GetModuleFileName(hSelf,buffer,size); + PathRemoveFileSpec(buffer); + //printf("Local directory: [%s]\n",buffer); } -void -x_dialog_create_gsport_conf(const char *str) -{ - // Just write the config file already... - config_write_config_gsplus_file(); +void x_dialog_create_gsport_conf(const char *str) { + // Just write the config file already... + config_write_config_gsplus_file(); } -int -x_show_alert(int is_fatal, const char *str) -{ - return 0; +int x_show_alert(int is_fatal, const char *str) { + return 0; } -void get_default_window_size(LPSIZE size) -{ - // Calculate the window client dimensions. - RECT rect; - rect.left = 0; - rect.top = 0; - rect.bottom = X_A2_WINDOW_HEIGHT; - if (g_win_status_debug) - rect.bottom += (MAX_STATUS_LINES * 16); - rect.right = X_A2_WINDOW_WIDTH; +void get_default_window_size(LPSIZE size) { + // Calculate the window client dimensions. + RECT rect; + rect.left = 0; + rect.top = 0; + rect.bottom = X_A2_WINDOW_HEIGHT; + if (g_win_status_debug) + rect.bottom += (MAX_STATUS_LINES * 16); + rect.right = X_A2_WINDOW_WIDTH; - // Calculate the window rectangle, which is the client area plus non-client area (e.g. frame and caption). - AdjustWindowRect(&rect, WS_TILED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, FALSE); - - // Return the window size. - size->cx = rect.right - rect.left; - size->cy = rect.bottom - rect.top; + // Calculate the window rectangle, which is the client area plus non-client area (e.g. frame and caption). + AdjustWindowRect(&rect, WS_TILED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, FALSE); + + // Return the window size. + size->cx = rect.right - rect.left; + size->cy = rect.bottom - rect.top; } -void x_toggle_status_lines() -{ - SIZE size; +void x_toggle_status_lines() { + SIZE size; - if (!g_win_fullscreen_state) - { - g_win_status_debug = !g_win_status_debug; - g_win_status_debug_request = g_win_status_debug; + if (!g_win_fullscreen_state) + { + g_win_status_debug = !g_win_status_debug; + g_win_status_debug_request = g_win_status_debug; - get_default_window_size(&size); - SetWindowPos(g_hwnd_main, NULL, 0, 0, size.cx, size.cy, SWP_NOMOVE | SWP_NOZORDER); - x_redraw_status_lines(); - } + get_default_window_size(&size); + SetWindowPos(g_hwnd_main, NULL, 0, 0, size.cx, size.cy, SWP_NOMOVE | SWP_NOZORDER); + x_redraw_status_lines(); + } } -void x_show_console(int show) -{ - HWND hWnd = GetConsoleWindow(); - if (hWnd) - ShowWindow(hWnd, show ? SW_SHOW : SW_HIDE); +void x_show_console(int show) { + HWND hWnd = GetConsoleWindow(); + if (hWnd) + ShowWindow(hWnd, show ? SW_SHOW : SW_HIDE); - if (g_hwnd_main) - SetFocus(g_hwnd_main); + if (g_hwnd_main) + SetFocus(g_hwnd_main); } -int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) -{ - return main(0,0); +int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { + return main(0,0); } -int -main(int argc, char **argv) -{ - // Hide the console initially to reduce window flashing. We'll show the console later if needed. - x_show_console(0); +int main(int argc, char **argv) { + // Hide the console initially to reduce window flashing. We'll show the console later if needed. + x_show_console(0); - // Register the window class. - WNDCLASS wndclass; - SIZE size; - RECT rect; + // Register the window class. + WNDCLASS wndclass; + SIZE size; + RECT rect; - wndclass.style = 0; - wndclass.lpfnWndProc = (WNDPROC)win_event_handler; - wndclass.cbClsExtra = 0; - wndclass.cbWndExtra = 0; - wndclass.hInstance = GetModuleHandle(NULL); - wndclass.hIcon = LoadIcon(wndclass.hInstance, MAKEINTRESOURCE(IDC_GSPORT32)); - wndclass.hCursor = LoadCursor((HINSTANCE) NULL, IDC_ARROW); - wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); - wndclass.lpszMenuName = NULL; - wndclass.lpszClassName = "gsport"; + wndclass.style = 0; + wndclass.lpfnWndProc = (WNDPROC)win_event_handler; + wndclass.cbClsExtra = 0; + wndclass.cbWndExtra = 0; + wndclass.hInstance = GetModuleHandle(NULL); + wndclass.hIcon = LoadIcon(wndclass.hInstance, MAKEINTRESOURCE(IDC_GSPORT32)); + wndclass.hCursor = LoadCursor((HINSTANCE) NULL, IDC_ARROW); + wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wndclass.lpszMenuName = NULL; + wndclass.lpszClassName = "gsport"; - if(!RegisterClass(&wndclass)) { - printf("Registering window failed\n"); - exit(1); - } + if(!RegisterClass(&wndclass)) { + printf("Registering window failed\n"); + exit(1); + } - // Create the window. - get_default_window_size(&size); + // Create the window. + get_default_window_size(&size); - HWND hwnd = CreateWindowEx(WS_EX_ACCEPTFILES, "gsport", "GSplus - Apple //gs Emulator", - WS_TILED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, - CW_USEDEFAULT, CW_USEDEFAULT, - size.cx, size.cy, - NULL, NULL, GetModuleHandle(NULL), NULL); + HWND hwnd = CreateWindowEx(WS_EX_ACCEPTFILES, "gsport", "GSplus - Apple //gs Emulator", + WS_TILED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, + CW_USEDEFAULT, CW_USEDEFAULT, + size.cx, size.cy, + NULL, NULL, GetModuleHandle(NULL), NULL); - printf("g_hwnd_main = %p, height = %d\n", hwnd, size.cx); - GetWindowRect(hwnd, &rect); - printf("...rect is: %ld, %ld, %ld, %ld\n", rect.left, rect.top, - rect.right, rect.bottom); - - // Enable non-blocking, character-at-a-time console I/O. - // win_nonblock_read_stdin() expects this behavior. - DWORD mode; - GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode); - mode &= ~ENABLE_LINE_INPUT; - SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode); + printf("g_hwnd_main = %p, height = %d\n", hwnd, size.cx); + GetWindowRect(hwnd, &rect); + printf("...rect is: %ld, %ld, %ld, %ld\n", rect.left, rect.top, + rect.right, rect.bottom); + + // Enable non-blocking, character-at-a-time console I/O. + // win_nonblock_read_stdin() expects this behavior. + DWORD mode; + GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &mode); + mode &= ~ENABLE_LINE_INPUT; + SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), mode); - gsportinit(hwnd); - int ret = gsplusmain(argc, argv); - - UnregisterClass(wndclass.lpszClassName,GetModuleHandle(NULL)); + gsportinit(hwnd); + int ret = gsplusmain(argc, argv); - gsportshut(); - return ret; + UnregisterClass(wndclass.lpszClassName,GetModuleHandle(NULL)); + + gsportshut(); + return ret; } -void x_check_input_events() -{ - MSG msg; +void x_check_input_events() { + MSG msg; - while(PeekMessage(&msg, g_hwnd_main, 0, 0, PM_NOREMOVE)) { - if(GetMessage(&msg, g_hwnd_main, 0, 0) > 0) { - TranslateMessage(&msg); - DispatchMessage(&msg); - } else { - printf("GetMessage returned <= 0\n"); - my_exit(2); - } - } + while(PeekMessage(&msg, g_hwnd_main, 0, 0, PM_NOREMOVE)) { + if(GetMessage(&msg, g_hwnd_main, 0, 0) > 0) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } else { + printf("GetMessage returned <= 0\n"); + my_exit(2); + } + } } -void -x_redraw_status_lines() -{ - COLORREF oldtextcolor, oldbkcolor; - char *buf; - int line; - int len; - int height; - int margin; +void x_redraw_status_lines() { + COLORREF oldtextcolor, oldbkcolor; + char *buf; + int line; + int len; + int height; + int margin; - height = 16; - margin = 0; - if (g_win_status_debug) - { - HDC localdc = GetDC(g_hwnd_main); - oldtextcolor = SetTextColor(localdc, RGB(255, 255, 255)); - oldbkcolor = SetBkColor(localdc, RGB(0, 0, 0)); - for(line = 0; line < MAX_STATUS_LINES; line++) { - buf = g_status_ptrs[line]; - if(buf != 0) { - len = strlen(buf); - TextOut(localdc, 10, X_A2_WINDOW_HEIGHT + - height*line + margin, buf, len); - } - } - SetTextColor(localdc, oldtextcolor); - SetBkColor(localdc, oldbkcolor); - ReleaseDC(g_hwnd_main,localdc); - } + height = 16; + margin = 0; + if (g_win_status_debug) + { + HDC localdc = GetDC(g_hwnd_main); + oldtextcolor = SetTextColor(localdc, RGB(255, 255, 255)); + oldbkcolor = SetBkColor(localdc, RGB(0, 0, 0)); + for(line = 0; line < MAX_STATUS_LINES; line++) { + buf = g_status_ptrs[line]; + if(buf != 0) { + len = strlen(buf); + TextOut(localdc, 10, X_A2_WINDOW_HEIGHT + + height*line + margin, buf, len); + } + } + SetTextColor(localdc, oldtextcolor); + SetBkColor(localdc, oldbkcolor); + ReleaseDC(g_hwnd_main,localdc); + } } -int -x_calc_ratio(float ratiox,float ratioy) -{ - return 0; // not stretched +int x_calc_ratio(float ratiox,float ratioy) { + return 0; // not stretched } diff --git a/src/win_generic.c b/src/win_generic.c index b054ebc..f27570f 100644 --- a/src/win_generic.c +++ b/src/win_generic.c @@ -1,12 +1,12 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ -#define WIN32_LEAN_AND_MEAN /* Tell windows we want less header gunk */ -#define STRICT /* Tell Windows we want compile type checks */ +#define WIN32_LEAN_AND_MEAN /* Tell windows we want less header gunk */ +#define STRICT /* Tell Windows we want compile type checks */ #include #include @@ -33,66 +33,64 @@ extern void x_toggle_status_lines(); extern void x_show_console(int show); extern void x_check_input_events(); -int g_win_capslock_down = 0; +int g_win_capslock_down = 0; int g_screen_mdepth = 0; int g_num_a2_keycodes = 0; int g_win_button_states = 0; -int g_use_shmem = 1; +int g_use_shmem = 1; int g_screenshot_requested = 0; // DB to know if we want to save a screenshot -HDC g_main_cdc; -HWND g_hwnd_main; +HDC g_main_cdc; +HWND g_hwnd_main; BITMAPINFO *g_bmapinfo_ptr = 0; volatile BITMAPINFOHEADER *g_bmaphdr_ptr = 0; // KEGS32 specific customisations -int g_win_status_debug = 1; // Current visibility of status lines. -int g_win_status_debug_request = 1; // Desired visibility of status lines. -int g_win_show_console = 0; // Current visibility of console. -int g_win_show_console_request = 1; // Desired visibility of console. -RECT g_main_window_saved_rect; -int g_main_window_saved_style; -int g_main_window_saved_exstyle; -int g_win_fullscreen_state = 0; +int g_win_status_debug = 1; // Current visibility of status lines. +int g_win_status_debug_request = 1; // Desired visibility of status lines. +int g_win_show_console = 0; // Current visibility of console. +int g_win_show_console_request = 1; // Desired visibility of console. +RECT g_main_window_saved_rect; +int g_main_window_saved_style; +int g_main_window_saved_exstyle; +int g_win_fullscreen_state = 0; #if 0 // enable non-integral full-screen scaling - #define FULLSCREEN_SCALE_TYPE float + #define FULLSCREEN_SCALE_TYPE float #else - #define FULLSCREEN_SCALE_TYPE int + #define FULLSCREEN_SCALE_TYPE int #endif -FULLSCREEN_SCALE_TYPE g_win_fullscreen_scale = 1; -int g_win_fullscreen_offsetx = 0; -int g_win_fullscreen_offsety = 0; +FULLSCREEN_SCALE_TYPE g_win_fullscreen_scale = 1; +int g_win_fullscreen_offsetx = 0; +int g_win_fullscreen_offsety = 0; -LPSTR g_clipboard; -size_t g_clipboard_pos; +LPSTR g_clipboard; +size_t g_clipboard_pos; int x_calc_ratio(float ratiox, float ratioy); -int -win_update_mouse(int x, int y, int button_states, int buttons_valid) -{ - int buttons_changed; +int win_update_mouse(int x, int y, int button_states, int buttons_valid) { + int buttons_changed; #ifdef ACTIVEGS - if (g_config_control_panel) // OG ignore input events while in debugger - return 0; + if (g_config_control_panel) // OG ignore input events while in debugger + return 0; - buttons_valid &= 1; // filter out middle & right button + buttons_valid &= 1; // filter out middle & right button #endif - buttons_changed = ((g_win_button_states & buttons_valid) != - button_states); - g_win_button_states = (g_win_button_states & ~buttons_valid) | - (button_states & buttons_valid); - if(g_warp_pointer && (x == A2_WINDOW_WIDTH/2) && - (y == A2_WINDOW_HEIGHT/2) && (!buttons_changed) ) { - /* tell adb routs to recenter but ignore this motion */ - update_mouse(x, y, 0, -1); - return 0; - } - return update_mouse(x, y, button_states, buttons_valid & 7); + buttons_changed = ((g_win_button_states & buttons_valid) != + button_states); + g_win_button_states = (g_win_button_states & ~buttons_valid) | + (button_states & buttons_valid); + if(g_warp_pointer && (x == A2_WINDOW_WIDTH/2) && + (y == A2_WINDOW_HEIGHT/2) && (!buttons_changed) ) { + /* tell adb routs to recenter but ignore this motion */ + update_mouse(x, y, 0, -1); + return 0; + } + return update_mouse(x, y, button_states, buttons_valid & 7); } @@ -100,28 +98,28 @@ win_update_mouse(int x, int y, int button_states, int buttons_valid) #define ASYNCEVENT #ifndef ASYNCEVENT -#define WIN_EVENT_MOUSE win_event_mouse -#define WIN_EVENT_KEY win_event_key +#define WIN_EVENT_MOUSE win_event_mouse +#define WIN_EVENT_KEY win_event_key #else extern void add_event_mouse(int umsg,WPARAM wParam, LPARAM lParam); extern void add_event_key(HWND hwnd, UINT raw_vk, BOOL down, int repeat, UINT flags); -#define WIN_EVENT_MOUSE add_event_mouse -#define WIN_EVENT_KEY add_event_key +#define WIN_EVENT_MOUSE add_event_mouse +#define WIN_EVENT_KEY add_event_key #endif struct win32_mouse { - int umsg; - int wparam; - int lparam; + int umsg; + int wparam; + int lparam; }; struct win32_key { - UINT raw_vk; - BOOL down; - int repeat; - UINT flags; + UINT raw_vk; + BOOL down; + int repeat; + UINT flags; }; int last_win32_mouse=0; @@ -135,587 +133,541 @@ struct win32_key win32_keys[MAX_EVENT]; // OG Push Mouse Event -void add_event_mouse(int umsg,WPARAM wParam, LPARAM lParam) -{ - +void add_event_mouse(int umsg,WPARAM wParam, LPARAM lParam) { + #ifdef ACTIVEGS - if (g_config_control_panel) // OG ignore input events while in debugger - return; + if (g_config_control_panel) // OG ignore input events while in debugger + return; #endif - win32_mouses[next_win32_mouse].umsg = umsg; - win32_mouses[next_win32_mouse].wparam = wParam; - win32_mouses[next_win32_mouse].lparam = lParam; - next_win32_mouse = (next_win32_mouse+1)%MAX_EVENT; + win32_mouses[next_win32_mouse].umsg = umsg; + win32_mouses[next_win32_mouse].wparam = wParam; + win32_mouses[next_win32_mouse].lparam = lParam; + next_win32_mouse = (next_win32_mouse+1)%MAX_EVENT; // ASSERT (next_win32_mouse==last_win32_mouse) } // OG Push Key Event -void -add_event_key(HWND hwnd, UINT raw_vk, BOOL down, int repeat, UINT flags) -{ +void add_event_key(HWND hwnd, UINT raw_vk, BOOL down, int repeat, UINT flags) { #ifdef ACTIVEGS - if (g_config_control_panel) // OG ignore input events while in debugger - return; + if (g_config_control_panel) // OG ignore input events while in debugger + return; #endif - if (nb_win32_key>=MAX_EVENT) - return; + if (nb_win32_key>=MAX_EVENT) + return; - win32_keys[nb_win32_key].raw_vk = raw_vk; - win32_keys[nb_win32_key].down = down; - win32_keys[nb_win32_key].repeat = repeat; - win32_keys[nb_win32_key].flags = flags; - - nb_win32_key++; + win32_keys[nb_win32_key].raw_vk = raw_vk; + win32_keys[nb_win32_key].down = down; + win32_keys[nb_win32_key].repeat = repeat; + win32_keys[nb_win32_key].flags = flags; + + nb_win32_key++; } -void -win_event_mouse(int umsg,WPARAM wParam, LPARAM lParam) -{ - POINT pt; - word32 flags; - int buttons; - int x, y; - int motion; +void win_event_mouse(int umsg,WPARAM wParam, LPARAM lParam) { + POINT pt; + word32 flags; + int buttons; + int x, y; + int motion; - x = LOWORD(lParam); - y = HIWORD(lParam); + x = LOWORD(lParam); + y = HIWORD(lParam); - // OG Reformat the mouse coordinates - float ratiox = 0.0, ratioy = 0.0; - if (!g_warp_pointer && x_calc_ratio(ratiox,ratioy)) - { - x = (int)((float)(x)/ratiox); - y = (int)((float)(y)/ratioy); - } + // OG Reformat the mouse coordinates + float ratiox = 0.0, ratioy = 0.0; + if (!g_warp_pointer && x_calc_ratio(ratiox,ratioy)) + { + x = (int)((float)(x)/ratiox); + y = (int)((float)(y)/ratioy); + } - flags = wParam; - x -= BASE_MARGIN_LEFT; - y -= BASE_MARGIN_TOP; + flags = wParam; + x -= BASE_MARGIN_LEFT; + y -= BASE_MARGIN_TOP; - buttons = (flags & 1) + - (((flags >> 1) & 1) << 2) + - (((flags >> 4) & 1) << 1); + buttons = (flags & 1) + + (((flags >> 1) & 1) << 2) + + (((flags >> 4) & 1) << 1); #if 0 - if (umsg!=WM_MOUSEMOVE) - printf("Mouse at %d, %d fl: %08x, but: %d\n", x, y, flags, buttons); + if (umsg!=WM_MOUSEMOVE) + printf("Mouse at %d, %d fl: %08x, but: %d\n", x, y, flags, buttons); #endif - motion = win_update_mouse(x, y, buttons, 7); + motion = win_update_mouse(x, y, buttons, 7); - if(motion && g_warp_pointer) { - /* move mouse to center of screen */ - pt.x = BASE_MARGIN_LEFT + A2_WINDOW_WIDTH/2; - pt.y = BASE_MARGIN_TOP + A2_WINDOW_HEIGHT/2; - ClientToScreen(g_hwnd_main, &pt); - SetCursorPos(pt.x, pt.y); - } + if(motion && g_warp_pointer) { + /* move mouse to center of screen */ + pt.x = BASE_MARGIN_LEFT + A2_WINDOW_WIDTH/2; + pt.y = BASE_MARGIN_TOP + A2_WINDOW_HEIGHT/2; + ClientToScreen(g_hwnd_main, &pt); + SetCursorPos(pt.x, pt.y); + } } -void -win_event_key(HWND hwnd, UINT raw_vk, BOOL down, int repeat, UINT flags) -{ - word32 vk; - int a2code; - int is_up; - int capslock_down; - int i; +void win_event_key(HWND hwnd, UINT raw_vk, BOOL down, int repeat, UINT flags) { + word32 vk; + int a2code; + int is_up; + int capslock_down; + int i; - if((flags & 0x4000) && down) { - /* auto-repeating, just ignore it */ - return; - } + if((flags & 0x4000) && down) { + /* auto-repeating, just ignore it */ + return; + } - vk = raw_vk + (flags & 0x100); + vk = raw_vk + (flags & 0x100); #if 0 - printf("Key event, vk=%04x, down:%d, repeat: %d, flags: %08x\n", - vk, down, repeat, flags); + printf("Key event, vk=%04x, down:%d, repeat: %d, flags: %08x\n", + vk, down, repeat, flags); #endif - /* remap a few keys here.. sigh */ - if((vk & 0xff) == VK_APPS) { - /* remap to command */ - vk = VK_MENU; - } + /* remap a few keys here.. sigh */ + if((vk & 0xff) == VK_APPS) { + /* remap to command */ + vk = VK_MENU; + } - if((vk & 0xff) == VK_CAPITAL) { - // Windows gives us up-and-down events of the actual key - // Use GetKeyState to get the true toggle state, and pass - // that on to the adb interface - capslock_down = GetKeyState(VK_CAPITAL) & 0x01; - if(capslock_down != g_win_capslock_down) { - g_win_capslock_down = capslock_down; - adb_physical_key_update(0x39, !capslock_down); - } + if((vk & 0xff) == VK_CAPITAL) { + // Windows gives us up-and-down events of the actual key + // Use GetKeyState to get the true toggle state, and pass + // that on to the adb interface + capslock_down = GetKeyState(VK_CAPITAL) & 0x01; + if(capslock_down != g_win_capslock_down) { + g_win_capslock_down = capslock_down; + adb_physical_key_update(0x39, !capslock_down); + } - return; // Do no more processing! - } + return; // Do no more processing! + } - /* search a2key_to_wsym to find wsym in col 1 or 2 */ - i = 0; - is_up = !down; - for(i = g_num_a2_keycodes-1; i >= 0; i--) { - a2code = g_a2_key_to_wsym[i][0]; - if((vk == g_a2_key_to_wsym[i][1]) || - (vk == g_a2_key_to_wsym[i][2])) { - vid_printf("Found vk:%04x = %02x\n", vk, a2code); - adb_physical_key_update(a2code, is_up); - return; - } - } - printf("VK: %04x unknown\n", vk); + /* search a2key_to_wsym to find wsym in col 1 or 2 */ + i = 0; + is_up = !down; + for(i = g_num_a2_keycodes-1; i >= 0; i--) { + a2code = g_a2_key_to_wsym[i][0]; + if((vk == g_a2_key_to_wsym[i][1]) || + (vk == g_a2_key_to_wsym[i][2])) { + vid_printf("Found vk:%04x = %02x\n", vk, a2code); + adb_physical_key_update(a2code, is_up); + return; + } + } + printf("VK: %04x unknown\n", vk); } -void -win_event_quit(HWND hwnd) -{ - quitEmulator(); +void win_event_quit(HWND hwnd) { + quitEmulator(); } -extern int g_needfullrefreshfornextframe ; +extern int g_needfullrefreshfornextframe; -void -win_event_redraw() -{ - g_needfullrefreshfornextframe = 1; +void win_event_redraw() { + g_needfullrefreshfornextframe = 1; } -LRESULT CALLBACK -win_event_handler(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) -{ - int i; - int numDraggedFiles; - int szFilename; - LPTSTR lpszFile; +LRESULT CALLBACK win_event_handler(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam) { + int i; + int numDraggedFiles; + int szFilename; + LPTSTR lpszFile; - switch(umsg) { - case WM_MOUSEMOVE: - case WM_LBUTTONDOWN: - case WM_LBUTTONUP: - case WM_MBUTTONDOWN: - case WM_MBUTTONUP: - case WM_RBUTTONDOWN: - case WM_RBUTTONUP: - case WM_LBUTTONDBLCLK: // OG Added dblclk as WM_LBUTTONDOWN - WIN_EVENT_MOUSE(umsg,wParam, lParam); - return 0; - case WM_PAINT: - win_event_redraw(); - break; - case WM_DROPFILES: - numDraggedFiles = DragQueryFile((HDROP)wParam, 0xFFFFFFFF, NULL, 0); - for (i = 0; i < numDraggedFiles; i++) { - szFilename = DragQueryFile((HDROP)wParam, i, NULL, 0); - lpszFile = (LPTSTR)malloc(szFilename + 1); - szFilename = DragQueryFile((HDROP)wParam, i, lpszFile, szFilename + 1); - cfg_inspect_maybe_insert_file(lpszFile, 0); - free(lpszFile); - } - DragFinish((HDROP)wParam); - break; - } - switch(umsg) { - HANDLE_MSG(hwnd, WM_KEYUP, WIN_EVENT_KEY); - HANDLE_MSG(hwnd, WM_KEYDOWN, WIN_EVENT_KEY); - HANDLE_MSG(hwnd, WM_SYSKEYUP, WIN_EVENT_KEY); - HANDLE_MSG(hwnd, WM_SYSKEYDOWN, WIN_EVENT_KEY); - HANDLE_MSG(hwnd, WM_DESTROY, win_event_quit); - } + switch(umsg) { + case WM_MOUSEMOVE: + case WM_LBUTTONDOWN: + case WM_LBUTTONUP: + case WM_MBUTTONDOWN: + case WM_MBUTTONUP: + case WM_RBUTTONDOWN: + case WM_RBUTTONUP: + case WM_LBUTTONDBLCLK: // OG Added dblclk as WM_LBUTTONDOWN + WIN_EVENT_MOUSE(umsg,wParam, lParam); + return 0; + case WM_PAINT: + win_event_redraw(); + break; + case WM_DROPFILES: + numDraggedFiles = DragQueryFile((HDROP)wParam, 0xFFFFFFFF, NULL, 0); + for (i = 0; i < numDraggedFiles; i++) { + szFilename = DragQueryFile((HDROP)wParam, i, NULL, 0); + lpszFile = (LPTSTR)malloc(szFilename + 1); + szFilename = DragQueryFile((HDROP)wParam, i, lpszFile, szFilename + 1); + cfg_inspect_maybe_insert_file(lpszFile, 0); + free(lpszFile); + } + DragFinish((HDROP)wParam); + break; + } + switch(umsg) { + HANDLE_MSG(hwnd, WM_KEYUP, WIN_EVENT_KEY); + HANDLE_MSG(hwnd, WM_KEYDOWN, WIN_EVENT_KEY); + HANDLE_MSG(hwnd, WM_SYSKEYUP, WIN_EVENT_KEY); + HANDLE_MSG(hwnd, WM_SYSKEYDOWN, WIN_EVENT_KEY); + HANDLE_MSG(hwnd, WM_DESTROY, win_event_quit); + } - return DefWindowProc(hwnd, umsg, wParam, lParam); + return DefWindowProc(hwnd, umsg, wParam, lParam); } -void gsportinit(HWND _hwnd) -{ - g_hwnd_main = _hwnd; - - HDC localdc = GetDC(g_hwnd_main); - SetTextColor(localdc, RGB(0, 0, 0)); - SetBkColor(localdc, RGB(255, 255, 255)); +void gsportinit(HWND _hwnd) { + g_hwnd_main = _hwnd; - g_main_cdc = CreateCompatibleDC(localdc); - ReleaseDC(g_hwnd_main,localdc); + HDC localdc = GetDC(g_hwnd_main); + SetTextColor(localdc, RGB(0, 0, 0)); + SetBkColor(localdc, RGB(255, 255, 255)); + + g_main_cdc = CreateCompatibleDC(localdc); + ReleaseDC(g_hwnd_main,localdc); } -void gsportshut() -{ - if (g_main_cdc) - { - DeleteDC(g_main_cdc); - g_main_cdc = NULL; - } - g_hwnd_main = NULL; +void gsportshut() { + if (g_main_cdc) + { + DeleteDC(g_main_cdc); + g_main_cdc = NULL; + } + g_hwnd_main = NULL; } -void -check_input_events() -{ - x_check_input_events(); +void check_input_events() { + x_check_input_events(); - // OG Unstack Mouse Events - int i=last_win32_mouse; - while(i!=next_win32_mouse) - { - win_event_mouse(win32_mouses[i].umsg,win32_mouses[i].wparam,win32_mouses[i].lparam); - int lb = win32_mouses[i].wparam&1; - i = (i+1)%MAX_EVENT; - if (lb != (win32_mouses[i].wparam&1)) - break; // don't send up & down event at the same time - } - last_win32_mouse=i; - - for(i=0;idev_handle != -1) - { - DeleteObject(kimage_ptr->dev_handle); - kimage_ptr->dev_handle = (void*)-1; - } - else - if (kimage_ptr->data_ptr) - { - free(kimage_ptr->data_ptr); - kimage_ptr->data_ptr = NULL; - } +void x_release_kimage(Kimage *kimage_ptr) { + if ((int)kimage_ptr->dev_handle != -1) + { + DeleteObject(kimage_ptr->dev_handle); + kimage_ptr->dev_handle = (void*)-1; + } + else + if (kimage_ptr->data_ptr) + { + free(kimage_ptr->data_ptr); + kimage_ptr->data_ptr = NULL; + } } -// OG Free video global memory -void -xdriver_end() // Should be renamed to dev_video_shut() ??? -{ +// OG Free video global memory +void xdriver_end() { // Should be renamed to dev_video_shut() ??? - x_release_kimage(&g_mainwin_kimage); - - GlobalFree(g_bmapinfo_ptr); // allocated in dev_video_init - g_bmapinfo_ptr = 0; + x_release_kimage(&g_mainwin_kimage); - printf("win32 video driver end\n"); + GlobalFree(g_bmapinfo_ptr); // allocated in dev_video_init + g_bmapinfo_ptr = 0; + + printf("win32 video driver end\n"); } -void -x_get_kimage(Kimage *kimage_ptr) -{ - byte *ptr; - int width; - int height; - int depth, mdepth; - int size; +void x_get_kimage(Kimage *kimage_ptr) { + byte *ptr; + int width; + int height; + int depth, mdepth; + int size; - width = kimage_ptr->width_req; - height = kimage_ptr->height; - depth = kimage_ptr->depth; - mdepth = kimage_ptr->mdepth; + width = kimage_ptr->width_req; + height = kimage_ptr->height; + depth = kimage_ptr->depth; + mdepth = kimage_ptr->mdepth; - size = 0; - if(depth == g_screen_depth) { - /* Use g_bmapinfo_ptr, adjusting width, height */ - g_bmaphdr_ptr->biWidth = width; - g_bmaphdr_ptr->biHeight = -height; + size = 0; + if(depth == g_screen_depth) { + /* Use g_bmapinfo_ptr, adjusting width, height */ + g_bmaphdr_ptr->biWidth = width; + g_bmaphdr_ptr->biHeight = -height; - HDC localdc = GetDC(g_hwnd_main); // OG Use on the fly DC - kimage_ptr->dev_handle = CreateDIBSection(localdc, - g_bmapinfo_ptr, DIB_RGB_COLORS, - (VOID **)&(kimage_ptr->data_ptr), NULL, 0); - ReleaseDC(g_hwnd_main,localdc); - } else { - /* allocate buffers for video.c to draw into */ + HDC localdc = GetDC(g_hwnd_main); // OG Use on the fly DC + kimage_ptr->dev_handle = CreateDIBSection(localdc, + g_bmapinfo_ptr, DIB_RGB_COLORS, + (VOID **)&(kimage_ptr->data_ptr), NULL, 0); + ReleaseDC(g_hwnd_main,localdc); + } else { + /* allocate buffers for video.c to draw into */ - size = (width*height*mdepth) >> 3; - ptr = (byte *)malloc(size); + size = (width*height*mdepth) >> 3; + ptr = (byte *)malloc(size); - if(ptr == 0) { - printf("malloc for data failed, mdepth: %d\n", mdepth); - exit(2); - } + if(ptr == 0) { + printf("malloc for data failed, mdepth: %d\n", mdepth); + exit(2); + } - kimage_ptr->data_ptr = ptr; + kimage_ptr->data_ptr = ptr; - kimage_ptr->dev_handle = (void *)-1; + kimage_ptr->dev_handle = (void *)-1; - } - printf("kim: %p, dev:%p data: %p, size: %08x\n", kimage_ptr, - kimage_ptr->dev_handle, kimage_ptr->data_ptr, size); + } + printf("kim: %p, dev:%p data: %p, size: %08x\n", kimage_ptr, + kimage_ptr->dev_handle, kimage_ptr->data_ptr, size); - return; + return; } -void -dev_video_init() -{ - int extra_size; - int lores_col; - int i; +void dev_video_init() { + int extra_size; + int lores_col; + int i; - printf("Preparing graphics system\n"); + printf("Preparing graphics system\n"); - // OG fix g_num_a2_keycodes identification - g_num_a2_keycodes = sizeof(g_a2_key_to_wsym)/3/sizeof(int); - g_num_a2_keycodes--; // last entry=-1 + // OG fix g_num_a2_keycodes identification + g_num_a2_keycodes = sizeof(g_a2_key_to_wsym)/3/sizeof(int); + g_num_a2_keycodes--; // last entry=-1 - /* - g_num_a2_keycodes = 0; - for(i = 0; i < maxcode; i++) { - int a2code = g_a2_key_to_wsym[i][0]; - if(a2code < 0) { - g_num_a2_keycodes = i; - } - } - */ + /* + g_num_a2_keycodes = 0; + for(i = 0; i < maxcode; i++) { + int a2code = g_a2_key_to_wsym[i][0]; + if(a2code < 0) { + g_num_a2_keycodes = i; + } + } + */ - // OG Forcing 16bits depth for WiNCE + // OG Forcing 16bits depth for WiNCE #ifndef UNDER_CE - g_screen_depth = 24; - g_screen_mdepth = 32; + g_screen_depth = 24; + g_screen_mdepth = 32; #else - g_screen_depth = 16; - g_screen_mdepth = 16; - - -extern word32 g_red_mask ; -extern word32 g_green_mask ; -extern word32 g_blue_mask ; -extern int g_red_left_shift; -extern int g_green_left_shift; -extern int g_blue_left_shift ; -extern int g_red_right_shift ; -extern int g_green_right_shift ; -extern int g_blue_right_shift ; + g_screen_depth = 16; + g_screen_mdepth = 16; - g_red_mask = 0xff; - g_green_mask = 0xff; - g_blue_mask = 0xff; - g_red_left_shift = 10; - g_green_left_shift = 5; - g_blue_left_shift = 0; - g_red_right_shift = 3; - g_green_right_shift = 3; - g_blue_right_shift = 3; + extern word32 g_red_mask; + extern word32 g_green_mask; + extern word32 g_blue_mask; + extern int g_red_left_shift; + extern int g_green_left_shift; + extern int g_blue_left_shift; + extern int g_red_right_shift; + extern int g_green_right_shift; + extern int g_blue_right_shift; + + + g_red_mask = 0xff; + g_green_mask = 0xff; + g_blue_mask = 0xff; + g_red_left_shift = 10; + g_green_left_shift = 5; + g_blue_left_shift = 0; + g_red_right_shift = 3; + g_green_right_shift = 3; + g_blue_right_shift = 3; #endif - - extra_size = sizeof(RGBQUAD); - if(g_screen_depth == 8) { - extra_size = 256 * sizeof(RGBQUAD); - } - g_bmapinfo_ptr = (BITMAPINFO *)GlobalAlloc(GPTR, - sizeof(BITMAPINFOHEADER) + extra_size); - g_bmaphdr_ptr = (BITMAPINFOHEADER *)g_bmapinfo_ptr; - g_bmaphdr_ptr->biSize = sizeof(BITMAPINFOHEADER); - g_bmaphdr_ptr->biWidth = A2_WINDOW_WIDTH; - g_bmaphdr_ptr->biHeight = -A2_WINDOW_HEIGHT; - g_bmaphdr_ptr->biPlanes = 1; - g_bmaphdr_ptr->biBitCount = g_screen_mdepth; - g_bmaphdr_ptr->biCompression = BI_RGB; - g_bmaphdr_ptr->biClrUsed = 0; + extra_size = sizeof(RGBQUAD); + if(g_screen_depth == 8) { + extra_size = 256 * sizeof(RGBQUAD); + } + g_bmapinfo_ptr = (BITMAPINFO *)GlobalAlloc(GPTR, + sizeof(BITMAPINFOHEADER) + extra_size); - video_get_kimages(); + g_bmaphdr_ptr = (BITMAPINFOHEADER *)g_bmapinfo_ptr; + g_bmaphdr_ptr->biSize = sizeof(BITMAPINFOHEADER); + g_bmaphdr_ptr->biWidth = A2_WINDOW_WIDTH; + g_bmaphdr_ptr->biHeight = -A2_WINDOW_HEIGHT; + g_bmaphdr_ptr->biPlanes = 1; + g_bmaphdr_ptr->biBitCount = g_screen_mdepth; + g_bmaphdr_ptr->biCompression = BI_RGB; + g_bmaphdr_ptr->biClrUsed = 0; - if(g_screen_depth != 8) { - // Allocate g_mainwin_kimage - video_get_kimage(&g_mainwin_kimage, 0, g_screen_depth, - g_screen_mdepth); - } + video_get_kimages(); - for(i = 0; i < 256; i++) { - lores_col = g_lores_colors[i & 0xf]; - video_update_color_raw(i, lores_col); - g_a2palette_8to1624[i] = g_palette_8to1624[i]; - } + if(g_screen_depth != 8) { + // Allocate g_mainwin_kimage + video_get_kimage(&g_mainwin_kimage, 0, g_screen_depth, + g_screen_mdepth); + } - g_installed_full_superhires_colormap = 1; + for(i = 0; i < 256; i++) { + lores_col = g_lores_colors[i & 0xf]; + video_update_color_raw(i, lores_col); + g_a2palette_8to1624[i] = g_palette_8to1624[i]; + } + + g_installed_full_superhires_colormap = 1; #ifndef UNDER_CE - ShowWindow(g_hwnd_main, SW_SHOWDEFAULT); + ShowWindow(g_hwnd_main, SW_SHOWDEFAULT); #else - ShowWindow(g_hwnd_main, SW_SHOW); + ShowWindow(g_hwnd_main, SW_SHOW); #endif - UpdateWindow(g_hwnd_main); + UpdateWindow(g_hwnd_main); - printf("Done with dev_video_init\n"); - fflush(stdout); + printf("Done with dev_video_init\n"); + fflush(stdout); } -void -x_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, - int width, int height) -{ - void *bitm_old; +void x_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, + int width, int height) { + void *bitm_old; - HDC localdc = GetDC(g_hwnd_main); - HDC localcdc = g_main_cdc; + HDC localdc = GetDC(g_hwnd_main); + HDC localcdc = g_main_cdc; - bitm_old = SelectObject(localcdc, kimage_ptr->dev_handle); + bitm_old = SelectObject(localcdc, kimage_ptr->dev_handle); - if (g_win_fullscreen_scale == 1) - BitBlt(localdc, destx + g_win_fullscreen_offsetx, desty + g_win_fullscreen_offsety, width, height, localcdc, srcx, srcy, SRCCOPY); - else - { - FULLSCREEN_SCALE_TYPE xdest = destx * g_win_fullscreen_scale + g_win_fullscreen_offsetx; - FULLSCREEN_SCALE_TYPE ydest = desty * g_win_fullscreen_scale + g_win_fullscreen_offsety; - FULLSCREEN_SCALE_TYPE wdest = width * g_win_fullscreen_scale; - FULLSCREEN_SCALE_TYPE hdest = height * g_win_fullscreen_scale; + if (g_win_fullscreen_scale == 1) + BitBlt(localdc, destx + g_win_fullscreen_offsetx, desty + g_win_fullscreen_offsety, width, height, localcdc, srcx, srcy, SRCCOPY); + else + { + FULLSCREEN_SCALE_TYPE xdest = destx * g_win_fullscreen_scale + g_win_fullscreen_offsetx; + FULLSCREEN_SCALE_TYPE ydest = desty * g_win_fullscreen_scale + g_win_fullscreen_offsety; + FULLSCREEN_SCALE_TYPE wdest = width * g_win_fullscreen_scale; + FULLSCREEN_SCALE_TYPE hdest = height * g_win_fullscreen_scale; - StretchBlt(localdc, (int)xdest, (int)ydest, (int)wdest, (int)hdest, localcdc, srcx, srcy, width, height, SRCCOPY); - } + StretchBlt(localdc, (int)xdest, (int)ydest, (int)wdest, (int)hdest, localcdc, srcx, srcy, width, height, SRCCOPY); + } - SelectObject(localcdc, bitm_old); - ReleaseDC(g_hwnd_main,localdc); + SelectObject(localcdc, bitm_old); + ReleaseDC(g_hwnd_main,localdc); } -void -x_push_done() -{ +void x_push_done() { } -void -x_auto_repeat_on(int must) -{ +void x_auto_repeat_on(int must) { } -void -x_auto_repeat_off(int must) -{ +void x_auto_repeat_off(int must) { } -void -x_hide_pointer(int do_hide) -{ - if(do_hide) { - ShowCursor(0); - } else { - ShowCursor(1); - } +void x_hide_pointer(int do_hide) { + if(do_hide) { + ShowCursor(0); + } else { + ShowCursor(1); + } } -void -x_full_screen(int do_full) -{ - MONITORINFO monitor_info; - FULLSCREEN_SCALE_TYPE width, height, scalex, scaley; - int top, left; +void x_full_screen(int do_full) { + MONITORINFO monitor_info; + FULLSCREEN_SCALE_TYPE width, height, scalex, scaley; + int top, left; - g_win_status_debug = 1 - do_full; - if (do_full && !g_win_fullscreen_state) { - g_main_window_saved_style = GetWindowLong(g_hwnd_main, GWL_STYLE); - g_main_window_saved_exstyle = GetWindowLong(g_hwnd_main, GWL_EXSTYLE); - GetWindowRect(g_hwnd_main, &g_main_window_saved_rect); - SetWindowLong(g_hwnd_main, GWL_STYLE, g_main_window_saved_style & ~(WS_CAPTION | WS_THICKFRAME)); - SetWindowLong(g_hwnd_main, GWL_EXSTYLE, g_main_window_saved_exstyle & ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE)); + g_win_status_debug = 1 - do_full; + if (do_full && !g_win_fullscreen_state) { + g_main_window_saved_style = GetWindowLong(g_hwnd_main, GWL_STYLE); + g_main_window_saved_exstyle = GetWindowLong(g_hwnd_main, GWL_EXSTYLE); + GetWindowRect(g_hwnd_main, &g_main_window_saved_rect); + SetWindowLong(g_hwnd_main, GWL_STYLE, g_main_window_saved_style & ~(WS_CAPTION | WS_THICKFRAME)); + SetWindowLong(g_hwnd_main, GWL_EXSTYLE, g_main_window_saved_exstyle & ~(WS_EX_DLGMODALFRAME | WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE)); - monitor_info.cbSize = sizeof(monitor_info); - GetMonitorInfo(MonitorFromWindow(g_hwnd_main, MONITOR_DEFAULTTONEAREST), &monitor_info); - left = monitor_info.rcMonitor.left; - top = monitor_info.rcMonitor.top; - width = (FULLSCREEN_SCALE_TYPE)(monitor_info.rcMonitor.right - monitor_info.rcMonitor.left); - height = (FULLSCREEN_SCALE_TYPE)(monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top); - scalex = width / X_A2_WINDOW_WIDTH; - scaley = height / X_A2_WINDOW_HEIGHT; - g_win_fullscreen_scale = (scalex <= scaley) ? scalex : scaley; - g_win_fullscreen_offsetx = ((int)width - (int)(g_win_fullscreen_scale * X_A2_WINDOW_WIDTH)) / 2; - g_win_fullscreen_offsety = ((int)height - (int)(g_win_fullscreen_scale * X_A2_WINDOW_HEIGHT)) / 2; - SetWindowPos(g_hwnd_main, NULL, left, top, (int)width, (int)height, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); + monitor_info.cbSize = sizeof(monitor_info); + GetMonitorInfo(MonitorFromWindow(g_hwnd_main, MONITOR_DEFAULTTONEAREST), &monitor_info); + left = monitor_info.rcMonitor.left; + top = monitor_info.rcMonitor.top; + width = (FULLSCREEN_SCALE_TYPE)(monitor_info.rcMonitor.right - monitor_info.rcMonitor.left); + height = (FULLSCREEN_SCALE_TYPE)(monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top); + scalex = width / X_A2_WINDOW_WIDTH; + scaley = height / X_A2_WINDOW_HEIGHT; + g_win_fullscreen_scale = (scalex <= scaley) ? scalex : scaley; + g_win_fullscreen_offsetx = ((int)width - (int)(g_win_fullscreen_scale * X_A2_WINDOW_WIDTH)) / 2; + g_win_fullscreen_offsety = ((int)height - (int)(g_win_fullscreen_scale * X_A2_WINDOW_HEIGHT)) / 2; + SetWindowPos(g_hwnd_main, NULL, left, top, (int)width, (int)height, SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); - g_win_fullscreen_state = 1; - RedrawWindow(g_hwnd_main, NULL, NULL, RDW_INVALIDATE); + g_win_fullscreen_state = 1; + RedrawWindow(g_hwnd_main, NULL, NULL, RDW_INVALIDATE); - } else { - if (g_win_fullscreen_state) { - g_win_fullscreen_offsetx = 0; - g_win_fullscreen_offsety = 0; - g_win_fullscreen_scale = 1; - SetWindowLong(g_hwnd_main, GWL_STYLE, g_main_window_saved_style); - SetWindowLong(g_hwnd_main, GWL_EXSTYLE, g_main_window_saved_exstyle); - SetWindowPos(g_hwnd_main, NULL, - g_main_window_saved_rect.left, - g_main_window_saved_rect.top, - g_main_window_saved_rect.right - g_main_window_saved_rect.left, - g_main_window_saved_rect.bottom - g_main_window_saved_rect.top, - SWP_SHOWWINDOW); + } else { + if (g_win_fullscreen_state) { + g_win_fullscreen_offsetx = 0; + g_win_fullscreen_offsety = 0; + g_win_fullscreen_scale = 1; + SetWindowLong(g_hwnd_main, GWL_STYLE, g_main_window_saved_style); + SetWindowLong(g_hwnd_main, GWL_EXSTYLE, g_main_window_saved_exstyle); + SetWindowPos(g_hwnd_main, NULL, + g_main_window_saved_rect.left, + g_main_window_saved_rect.top, + g_main_window_saved_rect.right - g_main_window_saved_rect.left, + g_main_window_saved_rect.bottom - g_main_window_saved_rect.top, + SWP_SHOWWINDOW); - g_win_fullscreen_state = 0; - RedrawWindow(g_hwnd_main, NULL, NULL, RDW_INVALIDATE); - } - } - return; + g_win_fullscreen_state = 0; + RedrawWindow(g_hwnd_main, NULL, NULL, RDW_INVALIDATE); + } + } + return; } -void -clipboard_paste() -{ - if (!IsClipboardFormatAvailable(CF_TEXT)) - return; - if (!OpenClipboard(g_hwnd_main)) - return; - { - HGLOBAL hclipboard = GetClipboardData(CF_TEXT); - if (!hclipboard) { - CloseClipboard(); - return; - } - { - LPSTR clipboard = (LPSTR)GlobalLock(hclipboard); - if (!clipboard) { - CloseClipboard(); - return; - } - if (g_clipboard) { - free(g_clipboard); - g_clipboard_pos = 0; - } - g_clipboard = strdup(clipboard); - } - GlobalUnlock(hclipboard); - } - CloseClipboard(); +void clipboard_paste() { + if (!IsClipboardFormatAvailable(CF_TEXT)) + return; + if (!OpenClipboard(g_hwnd_main)) + return; + { + HGLOBAL hclipboard = GetClipboardData(CF_TEXT); + if (!hclipboard) { + CloseClipboard(); + return; + } + { + LPSTR clipboard = (LPSTR)GlobalLock(hclipboard); + if (!clipboard) { + CloseClipboard(); + return; + } + if (g_clipboard) { + free(g_clipboard); + g_clipboard_pos = 0; + } + g_clipboard = strdup(clipboard); + } + GlobalUnlock(hclipboard); + } + CloseClipboard(); } -int -clipboard_get_char() -{ - if (!g_clipboard) - return 0; - if (g_clipboard[g_clipboard_pos] == '\n') - g_clipboard_pos++; - if (g_clipboard[g_clipboard_pos] == '\0') - return 0; - return g_clipboard[g_clipboard_pos++] | 0x80; +int clipboard_get_char() { + if (!g_clipboard) + return 0; + if (g_clipboard[g_clipboard_pos] == '\n') + g_clipboard_pos++; + if (g_clipboard[g_clipboard_pos] == '\0') + return 0; + return g_clipboard[g_clipboard_pos++] | 0x80; } diff --git a/src/xdriver.c b/src/xdriver.c index c2bd3fe..8a50eca 100644 --- a/src/xdriver.c +++ b/src/xdriver.c @@ -1,9 +1,9 @@ /* - GSPLUS - Advanced Apple IIGS Emulator Environment - Based on the KEGS emulator written by Kent Dickey - See COPYRIGHT.txt for Copyright information - See LICENSE.txt for license (GPL v2) -*/ + GSPLUS - Advanced Apple IIGS Emulator Environment + Based on the KEGS emulator written by Kent Dickey + See COPYRIGHT.txt for Copyright information + See LICENSE.txt for license (GPL v2) + */ # if !defined(__CYGWIN__) && !defined(__POWERPC__) /* No shared memory on Cygwin */ @@ -29,7 +29,7 @@ int XShmQueryExtension(Display *display); #include "defc.h" #include "protos_xdriver.h" -#define FONT_NAME_STATUS "8x13" +#define FONT_NAME_STATUS "8x13" extern int Verbose; @@ -44,23 +44,23 @@ extern int g_send_sound_to_file; extern int g_quit_sim_now; -int g_has_focus = 0; -int g_auto_repeat_on = -1; -int g_x_shift_control_state = 0; +int g_has_focus = 0; +int g_auto_repeat_on = -1; +int g_x_shift_control_state = 0; int g_screenshot_requested = 0; // DB to know if we want to save a screenshot Display *g_display = 0; -Visual *g_vis = 0; +Visual *g_vis = 0; Window g_a2_win; GC g_a2_winGC; -Atom WM_DELETE_WINDOW; +Atom WM_DELETE_WINDOW; XFontStruct *g_text_FontSt; Colormap g_a2_colormap = 0; Colormap g_default_colormap = 0; -int g_needs_cmap = 0; -int g_win_status_debug = 0; // Current visibility of status lines. -int g_win_status_debug_request = 0; // Desired visibility of status lines. +int g_needs_cmap = 0; +int g_win_status_debug = 0; // Current visibility of status lines. +int g_win_status_debug_request = 0; // Desired visibility of status lines. extern word32 g_red_mask; extern word32 g_green_mask; @@ -87,8 +87,8 @@ XColor g_xcolor_a2vid_array[256]; extern word32 g_palette_8to1624[256]; extern word32 g_a2palette_8to1624[256]; -int g_alt_left_up = 1; -int g_alt_right_up = 1; +int g_alt_left_up = 1; +int g_alt_right_up = 1; extern word32 g_full_refresh_needed; @@ -109,1301 +109,1236 @@ extern word32 g_a2_screen_buffer_changed; extern char *g_status_ptrs[MAX_STATUS_LINES]; -Cursor g_cursor; -Pixmap g_cursor_shape; -Pixmap g_cursor_mask; +Cursor g_cursor; +Pixmap g_cursor_shape; +Pixmap g_cursor_mask; -XColor g_xcolor_black = { 0, 0x0000, 0x0000, 0x0000, DoRed|DoGreen|DoBlue, 0 }; -XColor g_xcolor_white = { 0, 0xffff, 0xffff, 0xffff, DoRed|DoGreen|DoBlue, 0 }; +XColor g_xcolor_black = { 0, 0x0000, 0x0000, 0x0000, DoRed|DoGreen|DoBlue, 0 }; +XColor g_xcolor_white = { 0, 0xffff, 0xffff, 0xffff, DoRed|DoGreen|DoBlue, 0 }; int g_depth_attempt_list[] = { 16, 24, 15, 8 }; -#define X_EVENT_LIST_ALL_WIN \ - (ExposureMask | ButtonPressMask | ButtonReleaseMask | \ - OwnerGrabButtonMask | KeyPressMask | KeyReleaseMask | \ - KeymapStateMask | ColormapChangeMask | FocusChangeMask) +#define X_EVENT_LIST_ALL_WIN \ + (ExposureMask | ButtonPressMask | ButtonReleaseMask | \ + OwnerGrabButtonMask | KeyPressMask | KeyReleaseMask | \ + KeymapStateMask | ColormapChangeMask | FocusChangeMask) -#define X_BASE_WIN_EVENT_LIST \ - (X_EVENT_LIST_ALL_WIN | PointerMotionMask | ButtonMotionMask) +#define X_BASE_WIN_EVENT_LIST \ + (X_EVENT_LIST_ALL_WIN | PointerMotionMask | ButtonMotionMask) -#define X_A2_WIN_EVENT_LIST \ - (X_BASE_WIN_EVENT_LIST) +#define X_A2_WIN_EVENT_LIST \ + (X_BASE_WIN_EVENT_LIST) -int g_num_a2_keycodes = 0; +int g_num_a2_keycodes = 0; int a2_key_to_xsym[][3] = { - { 0x35, XK_Escape, 0 }, - { 0x7a, XK_F1, 0 }, - { 0x78, XK_F2, 0 }, - { 0x63, XK_F3, 0 }, - { 0x76, XK_F4, 0 }, - { 0x60, XK_F5, 0 }, - { 0x61, XK_F6, 0 }, - { 0x62, XK_F7, 0 }, - { 0x64, XK_F8, 0 }, - { 0x65, XK_F9, 0 }, - { 0x6d, XK_F10, 0 }, - { 0x67, XK_F11, 0 }, - { 0x6f, XK_F12, 0 }, - { 0x69, XK_F13, 0 }, - { 0x6b, XK_F14, 0 }, - { 0x71, XK_F15, 0 }, - { 0x7f, XK_Pause, XK_Break }, - { 0x32, '`', '~' }, /* Key number 18? */ - { 0x12, '1', '!' }, - { 0x13, '2', '@' }, - { 0x14, '3', '#' }, - { 0x15, '4', '$' }, - { 0x17, '5', '%' }, - { 0x16, '6', '^' }, - { 0x1a, '7', '&' }, - { 0x1c, '8', '*' }, - { 0x19, '9', '(' }, - { 0x1d, '0', ')' }, - { 0x1b, '-', '_' }, - { 0x18, '=', '+' }, - { 0x33, XK_BackSpace, 0 }, - { 0x72, XK_Insert, XK_Help }, /* Help? */ + { 0x35, XK_Escape, 0 }, + { 0x7a, XK_F1, 0 }, + { 0x78, XK_F2, 0 }, + { 0x63, XK_F3, 0 }, + { 0x76, XK_F4, 0 }, + { 0x60, XK_F5, 0 }, + { 0x61, XK_F6, 0 }, + { 0x62, XK_F7, 0 }, + { 0x64, XK_F8, 0 }, + { 0x65, XK_F9, 0 }, + { 0x6d, XK_F10, 0 }, + { 0x67, XK_F11, 0 }, + { 0x6f, XK_F12, 0 }, + { 0x69, XK_F13, 0 }, + { 0x6b, XK_F14, 0 }, + { 0x71, XK_F15, 0 }, + { 0x7f, XK_Pause, XK_Break }, + { 0x32, '`', '~' }, /* Key number 18? */ + { 0x12, '1', '!' }, + { 0x13, '2', '@' }, + { 0x14, '3', '#' }, + { 0x15, '4', '$' }, + { 0x17, '5', '%' }, + { 0x16, '6', '^' }, + { 0x1a, '7', '&' }, + { 0x1c, '8', '*' }, + { 0x19, '9', '(' }, + { 0x1d, '0', ')' }, + { 0x1b, '-', '_' }, + { 0x18, '=', '+' }, + { 0x33, XK_BackSpace, 0 }, + { 0x72, XK_Insert, XK_Help }, /* Help? */ /* { 0x73, XK_Home, 0 }, alias XK_Home to be XK_KP_Equal! */ - { 0x74, XK_Page_Up, 0 }, - { 0x47, XK_Num_Lock, XK_Clear }, /* Clear */ - { 0x51, XK_KP_Equal, XK_Home }, /* Note XK_Home alias! */ - { 0x4b, XK_KP_Divide, 0 }, - { 0x43, XK_KP_Multiply, 0 }, + { 0x74, XK_Page_Up, 0 }, + { 0x47, XK_Num_Lock, XK_Clear }, /* Clear */ + { 0x51, XK_KP_Equal, XK_Home }, /* Note XK_Home alias! */ + { 0x4b, XK_KP_Divide, 0 }, + { 0x43, XK_KP_Multiply, 0 }, - { 0x30, XK_Tab, 0 }, - { 0x0c, 'q', 'Q' }, - { 0x0d, 'w', 'W' }, - { 0x0e, 'e', 'E' }, - { 0x0f, 'r', 'R' }, - { 0x11, 't', 'T' }, - { 0x10, 'y', 'Y' }, - { 0x20, 'u', 'U' }, - { 0x22, 'i', 'I' }, - { 0x1f, 'o', 'O' }, - { 0x23, 'p', 'P' }, - { 0x21, '[', '{' }, - { 0x1e, ']', '}' }, - { 0x2a, 0x5c, '|' }, /* backslash, bar */ - { 0x75, XK_Delete, 0 }, - { 0x77, XK_End, 0 }, - { 0x79, XK_Page_Down, 0 }, - { 0x59, XK_KP_7, XK_KP_Home }, - { 0x5b, XK_KP_8, XK_KP_Up }, - { 0x5c, XK_KP_9, XK_KP_Page_Up }, - { 0x4e, XK_KP_Subtract, 0 }, + { 0x30, XK_Tab, 0 }, + { 0x0c, 'q', 'Q' }, + { 0x0d, 'w', 'W' }, + { 0x0e, 'e', 'E' }, + { 0x0f, 'r', 'R' }, + { 0x11, 't', 'T' }, + { 0x10, 'y', 'Y' }, + { 0x20, 'u', 'U' }, + { 0x22, 'i', 'I' }, + { 0x1f, 'o', 'O' }, + { 0x23, 'p', 'P' }, + { 0x21, '[', '{' }, + { 0x1e, ']', '}' }, + { 0x2a, 0x5c, '|' }, /* backslash, bar */ + { 0x75, XK_Delete, 0 }, + { 0x77, XK_End, 0 }, + { 0x79, XK_Page_Down, 0 }, + { 0x59, XK_KP_7, XK_KP_Home }, + { 0x5b, XK_KP_8, XK_KP_Up }, + { 0x5c, XK_KP_9, XK_KP_Page_Up }, + { 0x4e, XK_KP_Subtract, 0 }, - { 0x39, XK_Caps_Lock, 0 }, - { 0x00, 'a', 'A' }, - { 0x01, 's', 'S' }, - { 0x02, 'd', 'D' }, - { 0x03, 'f', 'F' }, - { 0x05, 'g', 'G' }, - { 0x04, 'h', 'H' }, - { 0x26, 'j', 'J' }, - { 0x28, 'k', 'K' }, - { 0x25, 'l', 'L' }, - { 0x29, ';', ':' }, - { 0x27, 0x27, '"' }, /* single quote */ - { 0x24, XK_Return, 0 }, - { 0x56, XK_KP_4, XK_KP_Left }, - { 0x57, XK_KP_5, 0 }, - { 0x58, XK_KP_6, XK_KP_Right }, - { 0x45, XK_KP_Add, 0 }, + { 0x39, XK_Caps_Lock, 0 }, + { 0x00, 'a', 'A' }, + { 0x01, 's', 'S' }, + { 0x02, 'd', 'D' }, + { 0x03, 'f', 'F' }, + { 0x05, 'g', 'G' }, + { 0x04, 'h', 'H' }, + { 0x26, 'j', 'J' }, + { 0x28, 'k', 'K' }, + { 0x25, 'l', 'L' }, + { 0x29, ';', ':' }, + { 0x27, 0x27, '"' }, /* single quote */ + { 0x24, XK_Return, 0 }, + { 0x56, XK_KP_4, XK_KP_Left }, + { 0x57, XK_KP_5, 0 }, + { 0x58, XK_KP_6, XK_KP_Right }, + { 0x45, XK_KP_Add, 0 }, - { 0x38, XK_Shift_L, XK_Shift_R }, - { 0x06, 'z', 'Z' }, - { 0x07, 'x', 'X' }, - { 0x08, 'c', 'C' }, - { 0x09, 'v', 'V' }, - { 0x0b, 'b', 'B' }, - { 0x2d, 'n', 'N' }, - { 0x2e, 'm', 'M' }, - { 0x2b, ',', '<' }, - { 0x2f, '.', '>' }, - { 0x2c, '/', '?' }, - { 0x3e, XK_Up, 0 }, - { 0x53, XK_KP_1, XK_KP_End }, - { 0x54, XK_KP_2, XK_KP_Down }, - { 0x55, XK_KP_3, XK_KP_Page_Down }, + { 0x38, XK_Shift_L, XK_Shift_R }, + { 0x06, 'z', 'Z' }, + { 0x07, 'x', 'X' }, + { 0x08, 'c', 'C' }, + { 0x09, 'v', 'V' }, + { 0x0b, 'b', 'B' }, + { 0x2d, 'n', 'N' }, + { 0x2e, 'm', 'M' }, + { 0x2b, ',', '<' }, + { 0x2f, '.', '>' }, + { 0x2c, '/', '?' }, + { 0x3e, XK_Up, 0 }, + { 0x53, XK_KP_1, XK_KP_End }, + { 0x54, XK_KP_2, XK_KP_Down }, + { 0x55, XK_KP_3, XK_KP_Page_Down }, - { 0x36, XK_Control_L, XK_Control_R }, - { 0x3a, XK_Print, XK_Sys_Req }, /* Option */ - { 0x37, XK_Scroll_Lock, 0 }, /* Command */ - { 0x31, ' ', 0 }, - { 0x3b, XK_Left, 0 }, - { 0x3d, XK_Down, 0 }, - { 0x3c, XK_Right, 0 }, - { 0x52, XK_KP_0, XK_KP_Insert }, - { 0x41, XK_KP_Decimal, XK_KP_Separator }, - { 0x4c, XK_KP_Enter, 0 }, - { -1, -1, -1 } + { 0x36, XK_Control_L, XK_Control_R }, + { 0x3a, XK_Print, XK_Sys_Req }, /* Option */ + { 0x37, XK_Scroll_Lock, 0 }, /* Command */ + { 0x31, ' ', 0 }, + { 0x3b, XK_Left, 0 }, + { 0x3d, XK_Down, 0 }, + { 0x3c, XK_Right, 0 }, + { 0x52, XK_KP_0, XK_KP_Insert }, + { 0x41, XK_KP_Decimal, XK_KP_Separator }, + { 0x4c, XK_KP_Enter, 0 }, + { -1, -1, -1 } }; -int -main(int argc, char **argv) -{ - return gsplusmain(argc, argv); +int main(int argc, char **argv) { + return gsplusmain(argc, argv); } -void -x_dialog_create_gsport_conf(const char *str) -{ - // Just write the config file already... - config_write_config_gsplus_file(); +void x_dialog_create_gsport_conf(const char *str) { + // Just write the config file already... + config_write_config_gsplus_file(); } -int -x_show_alert(int is_fatal, const char *str) -{ - /* Not implemented yet */ - adb_all_keys_up(); +int x_show_alert(int is_fatal, const char *str) { + /* Not implemented yet */ + adb_all_keys_up(); - clear_fatal_logs(); - return 0; + clear_fatal_logs(); + return 0; } -#define MAKE_2(val) ( (val << 8) + val) +#define MAKE_2(val) ( (val << 8) + val) -void -x_update_color(int col_num, int red, int green, int blue, word32 rgb) -{ - XColor *xcol; +void x_update_color(int col_num, int red, int green, int blue, word32 rgb) { + XColor *xcol; - xcol = &(g_xcolor_a2vid_array[col_num]); - xcol->red = MAKE_2(red); - xcol->green = MAKE_2(green); - xcol->blue = MAKE_2(blue); - xcol->flags = DoRed | DoGreen | DoBlue; + xcol = &(g_xcolor_a2vid_array[col_num]); + xcol->red = MAKE_2(red); + xcol->green = MAKE_2(green); + xcol->blue = MAKE_2(blue); + xcol->flags = DoRed | DoGreen | DoBlue; } -void -x_update_physical_colormap() -{ - if(g_needs_cmap) { - XStoreColors(g_display, g_a2_colormap, - &g_xcolor_a2vid_array[0], Max_color_size); - } +void x_update_physical_colormap() { + if(g_needs_cmap) { + XStoreColors(g_display, g_a2_colormap, + &g_xcolor_a2vid_array[0], Max_color_size); + } } -void -show_xcolor_array() -{ - int i; +void show_xcolor_array() { + int i; - for(i = 0; i < 256; i++) { - printf("%02x: %08x\n", i, g_palette_8to1624[i]); - -#if 0 - printf("%02x: %04x %04x %04x, %02x %x\n", - i, xcolor_array[i].red, xcolor_array[i].green, - xcolor_array[i].blue, (word32)xcolor_array[i].pixel, - xcolor_array[i].flags); -#endif - } -} - - -int -my_error_handler(Display *display, XErrorEvent *ev) -{ - char msg[1024]; - XGetErrorText(display, ev->error_code, msg, 1000); - printf("X Error code %s\n", msg); - fflush(stdout); - - return 0; -} - -void -xdriver_end() -{ - - printf("xdriver_end\n"); - if(g_display) { - x_auto_repeat_on(1); - XFlush(g_display); - } -} - -void -show_colormap(char *str, Colormap cmap, int index1, int index2, int index3) -{ - XColor xcol; - int i; - int pix; - - printf("Show colormap: %08x = %s, cmap cells: %d,%d,%d\n", - (int)cmap, str, index1, index2, index3); - for(i = 0; i < index1 + index2 + index3; i++) { - pix = i; - if(i >= index1) { - pix = (i-index1)*index1; - if(i >= (index1 + index2)) { - pix = (i - index1 - index2)*index2*index1; - } - } - if(i == 0 && index1 < 250) { - pix = 0x842; - } - xcol.pixel = pix; - XQueryColor(g_display, cmap, &xcol); - printf("Cell %03x: pix: %03x, R:%04x, G:%04x, B:%04x\n", - i, (int)xcol.pixel, xcol.red, xcol.green, xcol.blue); - } -} - -void -x_badpipe(int signum) -{ - /* restore normal sigpipe handling */ - signal(SIGPIPE, SIG_DFL); - - /* attempt to xset r */ - system("xset r"); - my_exit(5); -} - -void -dev_video_init() -{ - int tmp_array[0x80]; - XGCValues new_gc; - XSetWindowAttributes win_attr; - XSizeHints my_winSizeHints; - XClassHint my_winClassHint; - XTextProperty my_winText; - XVisualInfo *visualList; - char **font_ptr; - char cursor_data; - word32 create_win_list; - int depth; - int len; - int cmap_alloc_amt; - int cnt; - int font_height; - int base_height; - int screen_num; - char *myTextString[1]; - word32 lores_col; - int ret; - int i; - int keycode; - - printf("Preparing X Windows graphics system\n"); - ret = 0; - - signal(SIGPIPE, x_badpipe); - - g_num_a2_keycodes = 0; - for(i = 0; i <= 0x7f; i++) { - tmp_array[i] = 0; - } - for(i = 0; i < 0x7f; i++) { - keycode = a2_key_to_xsym[i][0]; - if(keycode < 0) { - g_num_a2_keycodes = i; - break; - } else if(keycode > 0x7f) { - printf("a2_key_to_xsym[%d] = %02x!\n", i, keycode); - exit(2); - } else { - if(tmp_array[keycode]) { - printf("a2_key_to_x[%d] = %02x used by %d\n", - i, keycode, tmp_array[keycode] - 1); - } - tmp_array[keycode] = i + 1; - } - } + for(i = 0; i < 256; i++) { + printf("%02x: %08x\n", i, g_palette_8to1624[i]); #if 0 - printf("Setting _Xdebug = 1, makes X synchronous\n"); - _Xdebug = 1; + printf("%02x: %04x %04x %04x, %02x %x\n", + i, xcolor_array[i].red, xcolor_array[i].green, + xcolor_array[i].blue, (word32)xcolor_array[i].pixel, + xcolor_array[i].flags); +#endif + } +} + + +int my_error_handler(Display *display, XErrorEvent *ev) { + char msg[1024]; + XGetErrorText(display, ev->error_code, msg, 1000); + printf("X Error code %s\n", msg); + fflush(stdout); + + return 0; +} + +void xdriver_end() { + + printf("xdriver_end\n"); + if(g_display) { + x_auto_repeat_on(1); + XFlush(g_display); + } +} + +void show_colormap(char *str, Colormap cmap, int index1, int index2, int index3) { + XColor xcol; + int i; + int pix; + + printf("Show colormap: %08x = %s, cmap cells: %d,%d,%d\n", + (int)cmap, str, index1, index2, index3); + for(i = 0; i < index1 + index2 + index3; i++) { + pix = i; + if(i >= index1) { + pix = (i-index1)*index1; + if(i >= (index1 + index2)) { + pix = (i - index1 - index2)*index2*index1; + } + } + if(i == 0 && index1 < 250) { + pix = 0x842; + } + xcol.pixel = pix; + XQueryColor(g_display, cmap, &xcol); + printf("Cell %03x: pix: %03x, R:%04x, G:%04x, B:%04x\n", + i, (int)xcol.pixel, xcol.red, xcol.green, xcol.blue); + } +} + +void x_badpipe(int signum) { + /* restore normal sigpipe handling */ + signal(SIGPIPE, SIG_DFL); + + /* attempt to xset r */ + system("xset r"); + my_exit(5); +} + +void dev_video_init() { + int tmp_array[0x80]; + XGCValues new_gc; + XSetWindowAttributes win_attr; + XSizeHints my_winSizeHints; + XClassHint my_winClassHint; + XTextProperty my_winText; + XVisualInfo *visualList; + char **font_ptr; + char cursor_data; + word32 create_win_list; + int depth; + int len; + int cmap_alloc_amt; + int cnt; + int font_height; + int base_height; + int screen_num; + char *myTextString[1]; + word32 lores_col; + int ret; + int i; + int keycode; + + printf("Preparing X Windows graphics system\n"); + ret = 0; + + signal(SIGPIPE, x_badpipe); + + g_num_a2_keycodes = 0; + for(i = 0; i <= 0x7f; i++) { + tmp_array[i] = 0; + } + for(i = 0; i < 0x7f; i++) { + keycode = a2_key_to_xsym[i][0]; + if(keycode < 0) { + g_num_a2_keycodes = i; + break; + } else if(keycode > 0x7f) { + printf("a2_key_to_xsym[%d] = %02x!\n", i, keycode); + exit(2); + } else { + if(tmp_array[keycode]) { + printf("a2_key_to_x[%d] = %02x used by %d\n", + i, keycode, tmp_array[keycode] - 1); + } + tmp_array[keycode] = i + 1; + } + } + +#if 0 + printf("Setting _Xdebug = 1, makes X synchronous\n"); + _Xdebug = 1; #endif - g_display = XOpenDisplay(NULL); - if(g_display == NULL) { - fprintf(stderr, "Can't open display\n"); - exit(1); - } + g_display = XOpenDisplay(NULL); + if(g_display == NULL) { + fprintf(stderr, "Can't open display\n"); + exit(1); + } - vid_printf("Just opened display = %p\n", g_display); - fflush(stdout); + vid_printf("Just opened display = %p\n", g_display); + fflush(stdout); - screen_num = DefaultScreen(g_display); + screen_num = DefaultScreen(g_display); - len = sizeof(g_depth_attempt_list)/sizeof(int); - if(g_force_depth > 0) { - /* Only use the requested user depth */ - len = 1; - g_depth_attempt_list[0] = g_force_depth; - } - g_vis = 0; - for(i = 0; i < len; i++) { - depth = g_depth_attempt_list[i]; + len = sizeof(g_depth_attempt_list)/sizeof(int); + if(g_force_depth > 0) { + /* Only use the requested user depth */ + len = 1; + g_depth_attempt_list[0] = g_force_depth; + } + g_vis = 0; + for(i = 0; i < len; i++) { + depth = g_depth_attempt_list[i]; - g_vis = x_try_find_visual(depth, screen_num, - &visualList); - if(g_vis != 0) { - break; - } - } - if(g_vis == 0) { - fprintf(stderr, "Couldn't find any visuals at any depth!\n"); - exit(2); - } + g_vis = x_try_find_visual(depth, screen_num, + &visualList); + if(g_vis != 0) { + break; + } + } + if(g_vis == 0) { + fprintf(stderr, "Couldn't find any visuals at any depth!\n"); + exit(2); + } - g_default_colormap = XDefaultColormap(g_display, screen_num); - if(!g_default_colormap) { - printf("g_default_colormap == 0!\n"); - exit(4); - } + g_default_colormap = XDefaultColormap(g_display, screen_num); + if(!g_default_colormap) { + printf("g_default_colormap == 0!\n"); + exit(4); + } - g_a2_colormap = -1; - cmap_alloc_amt = AllocNone; - if(g_needs_cmap) { - cmap_alloc_amt = AllocAll; - } - g_a2_colormap = XCreateColormap(g_display, - RootWindow(g_display,screen_num), g_vis, - cmap_alloc_amt); + g_a2_colormap = -1; + cmap_alloc_amt = AllocNone; + if(g_needs_cmap) { + cmap_alloc_amt = AllocAll; + } + g_a2_colormap = XCreateColormap(g_display, + RootWindow(g_display,screen_num), g_vis, + cmap_alloc_amt); - vid_printf("g_a2_colormap: %08x, main: %08x\n", - (word32)g_a2_colormap, (word32)g_default_colormap); + vid_printf("g_a2_colormap: %08x, main: %08x\n", + (word32)g_a2_colormap, (word32)g_default_colormap); - if(g_needs_cmap && g_a2_colormap == g_default_colormap) { - printf("A2_colormap = default colormap!\n"); - exit(4); - } + if(g_needs_cmap && g_a2_colormap == g_default_colormap) { + printf("A2_colormap = default colormap!\n"); + exit(4); + } - /* and define cursor */ - cursor_data = 0; - g_cursor_shape = XCreatePixmapFromBitmapData(g_display, - RootWindow(g_display,screen_num), &cursor_data, 1, 1, 1, 0, 1); - g_cursor_mask = XCreatePixmapFromBitmapData(g_display, - RootWindow(g_display,screen_num), &cursor_data, 1, 1, 1, 0, 1); + /* and define cursor */ + cursor_data = 0; + g_cursor_shape = XCreatePixmapFromBitmapData(g_display, + RootWindow(g_display,screen_num), &cursor_data, 1, 1, 1, 0, 1); + g_cursor_mask = XCreatePixmapFromBitmapData(g_display, + RootWindow(g_display,screen_num), &cursor_data, 1, 1, 1, 0, 1); - g_cursor = XCreatePixmapCursor(g_display, g_cursor_shape, - g_cursor_mask, &g_xcolor_black, &g_xcolor_white, 0, 0); + g_cursor = XCreatePixmapCursor(g_display, g_cursor_shape, + g_cursor_mask, &g_xcolor_black, &g_xcolor_white, 0, 0); - XFreePixmap(g_display, g_cursor_shape); - XFreePixmap(g_display, g_cursor_mask); + XFreePixmap(g_display, g_cursor_shape); + XFreePixmap(g_display, g_cursor_mask); - XFlush(g_display); + XFlush(g_display); - win_attr.event_mask = X_A2_WIN_EVENT_LIST; - win_attr.colormap = g_a2_colormap; - win_attr.backing_store = WhenMapped; - win_attr.border_pixel = 1; - win_attr.background_pixel = 0; - if(g_warp_pointer) { - win_attr.cursor = g_cursor; - } else { - win_attr.cursor = None; - } + win_attr.event_mask = X_A2_WIN_EVENT_LIST; + win_attr.colormap = g_a2_colormap; + win_attr.backing_store = WhenMapped; + win_attr.border_pixel = 1; + win_attr.background_pixel = 0; + if(g_warp_pointer) { + win_attr.cursor = g_cursor; + } else { + win_attr.cursor = None; + } - vid_printf("About to a2_win, depth: %d\n", g_screen_depth); - fflush(stdout); + vid_printf("About to a2_win, depth: %d\n", g_screen_depth); + fflush(stdout); - create_win_list = CWEventMask | CWBackingStore | CWCursor; - create_win_list |= CWColormap | CWBorderPixel | CWBackPixel; + create_win_list = CWEventMask | CWBackingStore | CWCursor; + create_win_list |= CWColormap | CWBorderPixel | CWBackPixel; - base_height = X_A2_WINDOW_HEIGHT; - if (g_win_status_debug) - base_height += MAX_STATUS_LINES * 13; - - g_a2_win = XCreateWindow(g_display, RootWindow(g_display, screen_num), - 0, 0, BASE_WINDOW_WIDTH, base_height, - 0, g_screen_depth, InputOutput, g_vis, - create_win_list, &win_attr); + base_height = X_A2_WINDOW_HEIGHT; + if (g_win_status_debug) + base_height += MAX_STATUS_LINES * 13; - XSetWindowColormap(g_display, g_a2_win, g_a2_colormap); + g_a2_win = XCreateWindow(g_display, RootWindow(g_display, screen_num), + 0, 0, BASE_WINDOW_WIDTH, base_height, + 0, g_screen_depth, InputOutput, g_vis, + create_win_list, &win_attr); - XFlush(g_display); + XSetWindowColormap(g_display, g_a2_win, g_a2_colormap); + + XFlush(g_display); /* Check for XShm */ #ifdef X_SHARED_MEM - if(g_use_shmem) { - ret = XShmQueryExtension(g_display); - if(ret == 0) { - printf("XShmQueryExt ret: %d\n", ret); - printf("not using shared memory\n"); - g_use_shmem = 0; - } else { - printf("Will use shared memory for X\n"); - } - } + if(g_use_shmem) { + ret = XShmQueryExtension(g_display); + if(ret == 0) { + printf("XShmQueryExt ret: %d\n", ret); + printf("not using shared memory\n"); + g_use_shmem = 0; + } else { + printf("Will use shared memory for X\n"); + } + } #endif - video_get_kimages(); - if(g_screen_depth != 8) { - video_get_kimage(&g_mainwin_kimage, 0, g_screen_depth, - g_screen_mdepth); - } + video_get_kimages(); + if(g_screen_depth != 8) { + video_get_kimage(&g_mainwin_kimage, 0, g_screen_depth, + g_screen_mdepth); + } - if(!g_use_shmem) { - if(g_screen_redraw_skip_amt < 0) { - g_screen_redraw_skip_amt = 3; - } - printf("Not using shared memory, setting skip_amt = %d\n", - g_screen_redraw_skip_amt); - } + if(!g_use_shmem) { + if(g_screen_redraw_skip_amt < 0) { + g_screen_redraw_skip_amt = 3; + } + printf("Not using shared memory, setting skip_amt = %d\n", + g_screen_redraw_skip_amt); + } - /* Done with visualList now */ - XFree(visualList); + /* Done with visualList now */ + XFree(visualList); - for(i = 0; i < 256; i++) { - g_xcolor_a2vid_array[i].pixel = i; - lores_col = g_lores_colors[i & 0xf]; - video_update_color_raw(i, lores_col); - g_a2palette_8to1624[i] = g_palette_8to1624[i]; - } + for(i = 0; i < 256; i++) { + g_xcolor_a2vid_array[i].pixel = i; + lores_col = g_lores_colors[i & 0xf]; + video_update_color_raw(i, lores_col); + g_a2palette_8to1624[i] = g_palette_8to1624[i]; + } - x_update_physical_colormap(); + x_update_physical_colormap(); - g_installed_full_superhires_colormap = !g_needs_cmap; - - myTextString[0] = "GSport"; + g_installed_full_superhires_colormap = !g_needs_cmap; - XStringListToTextProperty(myTextString, 1, &my_winText); + myTextString[0] = "GSport"; - my_winSizeHints.flags = PSize | PMinSize | PMaxSize; - my_winSizeHints.width = BASE_WINDOW_WIDTH; - my_winSizeHints.height = base_height; - my_winSizeHints.min_width = BASE_WINDOW_WIDTH; - my_winSizeHints.min_height = base_height; - my_winSizeHints.max_width = BASE_WINDOW_WIDTH; - my_winSizeHints.max_height = base_height; - my_winClassHint.res_name = "GSport"; - my_winClassHint.res_class = "GSport"; + XStringListToTextProperty(myTextString, 1, &my_winText); - XSetWMProperties(g_display, g_a2_win, &my_winText, &my_winText, 0, - 0, &my_winSizeHints, 0, &my_winClassHint); - - WM_DELETE_WINDOW = XInternAtom(g_display, "WM_DELETE_WINDOW", False); - XSetWMProtocols(g_display, g_a2_win, &WM_DELETE_WINDOW, 1); - XMapRaised(g_display, g_a2_win); + my_winSizeHints.flags = PSize | PMinSize | PMaxSize; + my_winSizeHints.width = BASE_WINDOW_WIDTH; + my_winSizeHints.height = base_height; + my_winSizeHints.min_width = BASE_WINDOW_WIDTH; + my_winSizeHints.min_height = base_height; + my_winSizeHints.max_width = BASE_WINDOW_WIDTH; + my_winSizeHints.max_height = base_height; + my_winClassHint.res_name = "GSport"; + my_winClassHint.res_class = "GSport"; - XSync(g_display, False); + XSetWMProperties(g_display, g_a2_win, &my_winText, &my_winText, 0, + 0, &my_winSizeHints, 0, &my_winClassHint); - g_a2_winGC = XCreateGC(g_display, g_a2_win, 0, (XGCValues *) 0); - font_ptr = XListFonts(g_display, FONT_NAME_STATUS, 4, &cnt); + WM_DELETE_WINDOW = XInternAtom(g_display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(g_display, g_a2_win, &WM_DELETE_WINDOW, 1); + XMapRaised(g_display, g_a2_win); - vid_printf("act_cnt of fonts: %d\n", cnt); - for(i = 0; i < cnt; i++) { - vid_printf("Font %d: %s\n", i, font_ptr[i]); - } - fflush(stdout); - g_text_FontSt = XLoadQueryFont(g_display, FONT_NAME_STATUS); - if (g_text_FontSt == NULL) { - glog("Could not load font, possibly missing xorg-x11-fonts-misc"); - exit(1); - } - vid_printf("font # returned: %08x\n", (word32)(g_text_FontSt->fid)); - font_height = g_text_FontSt->ascent + g_text_FontSt->descent; - vid_printf("font_height: %d\n", font_height); + XSync(g_display, False); - vid_printf("widest width: %d\n", g_text_FontSt->max_bounds.width); + g_a2_winGC = XCreateGC(g_display, g_a2_win, 0, (XGCValues *) 0); + font_ptr = XListFonts(g_display, FONT_NAME_STATUS, 4, &cnt); - new_gc.font = g_text_FontSt->fid; - new_gc.fill_style = FillSolid; - XChangeGC(g_display, g_a2_winGC, GCFillStyle | GCFont, &new_gc); + vid_printf("act_cnt of fonts: %d\n", cnt); + for(i = 0; i < cnt; i++) { + vid_printf("Font %d: %s\n", i, font_ptr[i]); + } + fflush(stdout); + g_text_FontSt = XLoadQueryFont(g_display, FONT_NAME_STATUS); + if (g_text_FontSt == NULL) { + glog("Could not load font, possibly missing xorg-x11-fonts-misc"); + exit(1); + } + vid_printf("font # returned: %08x\n", (word32)(g_text_FontSt->fid)); + font_height = g_text_FontSt->ascent + g_text_FontSt->descent; + vid_printf("font_height: %d\n", font_height); - /* XSync(g_display, False); */ + vid_printf("widest width: %d\n", g_text_FontSt->max_bounds.width); + + new_gc.font = g_text_FontSt->fid; + new_gc.fill_style = FillSolid; + XChangeGC(g_display, g_a2_winGC, GCFillStyle | GCFont, &new_gc); + + /* XSync(g_display, False); */ #if 0 /* MkLinux for Powermac depth 15 has bugs--this was to try to debug them */ - if(g_screen_depth == 15) { - /* draw phony screen */ - ptr16 = (word16 *)dint_main_win; - for(i = 0; i < 320*400; i++) { - ptr16[i] = 0; - } - for(i = 0; i < 400; i++) { - for(j = 0; j < 640; j++) { - sh = (j / 20) & 0x1f; - val = sh; - val = val; - *ptr16++ = val; - } - } - XPutImage(g_display, g_a2_win, g_a2_winGC, xint_main_win, - 0, 0, - BASE_MARGIN_LEFT, BASE_MARGIN_TOP, - 640, 400); - XFlush(g_display); - } + if(g_screen_depth == 15) { + /* draw phony screen */ + ptr16 = (word16 *)dint_main_win; + for(i = 0; i < 320*400; i++) { + ptr16[i] = 0; + } + for(i = 0; i < 400; i++) { + for(j = 0; j < 640; j++) { + sh = (j / 20) & 0x1f; + val = sh; + val = val; + *ptr16++ = val; + } + } + XPutImage(g_display, g_a2_win, g_a2_winGC, xint_main_win, + 0, 0, + BASE_MARGIN_LEFT, BASE_MARGIN_TOP, + 640, 400); + XFlush(g_display); + } #endif - XFlush(g_display); - fflush(stdout); + XFlush(g_display); + fflush(stdout); } -Visual * -x_try_find_visual(int depth, int screen_num, XVisualInfo **visual_list_ptr) -{ - XVisualInfo *visualList; - XVisualInfo *v_chosen; - XVisualInfo vTemplate; - int visualsMatched; - int mdepth; - int needs_cmap; - int visual_chosen; - int match8, match24; - int i; +Visual *x_try_find_visual(int depth, int screen_num, XVisualInfo **visual_list_ptr) { + XVisualInfo *visualList; + XVisualInfo *v_chosen; + XVisualInfo vTemplate; + int visualsMatched; + int mdepth; + int needs_cmap; + int visual_chosen; + int match8, match24; + int i; - vTemplate.screen = screen_num; - vTemplate.depth = depth; + vTemplate.screen = screen_num; + vTemplate.depth = depth; - visualList = XGetVisualInfo(g_display, - (VisualScreenMask | VisualDepthMask), - &vTemplate, &visualsMatched); + visualList = XGetVisualInfo(g_display, + (VisualScreenMask | VisualDepthMask), + &vTemplate, &visualsMatched); - vid_printf("visuals matched: %d\n", visualsMatched); - if(visualsMatched == 0) { - return (Visual *)0; - } + vid_printf("visuals matched: %d\n", visualsMatched); + if(visualsMatched == 0) { + return (Visual *)0; + } - visual_chosen = -1; - needs_cmap = 0; - for(i = 0; i < visualsMatched; i++) { - printf("Visual %d\n", i); - printf(" id: %08x, screen: %d, depth: %d, class: %d\n", - (word32)visualList[i].visualid, - visualList[i].screen, - visualList[i].depth, - visualList[i].class); - printf(" red: %08lx, green: %08lx, blue: %08lx\n", - visualList[i].red_mask, - visualList[i].green_mask, - visualList[i].blue_mask); - printf(" cmap size: %d, bits_per_rgb: %d\n", - visualList[i].colormap_size, - visualList[i].bits_per_rgb); - match8 = (visualList[i].class == PseudoColor); - match24 = (visualList[i].class == TrueColor); - if((depth == 8) && match8) { - visual_chosen = i; - Max_color_size = visualList[i].colormap_size; - needs_cmap = 1; - break; - } - if((depth != 8) && match24) { - visual_chosen = i; - Max_color_size = -1; - needs_cmap = 0; - break; - } - } + visual_chosen = -1; + needs_cmap = 0; + for(i = 0; i < visualsMatched; i++) { + printf("Visual %d\n", i); + printf(" id: %08x, screen: %d, depth: %d, class: %d\n", + (word32)visualList[i].visualid, + visualList[i].screen, + visualList[i].depth, + visualList[i].class); + printf(" red: %08lx, green: %08lx, blue: %08lx\n", + visualList[i].red_mask, + visualList[i].green_mask, + visualList[i].blue_mask); + printf(" cmap size: %d, bits_per_rgb: %d\n", + visualList[i].colormap_size, + visualList[i].bits_per_rgb); + match8 = (visualList[i].class == PseudoColor); + match24 = (visualList[i].class == TrueColor); + if((depth == 8) && match8) { + visual_chosen = i; + Max_color_size = visualList[i].colormap_size; + needs_cmap = 1; + break; + } + if((depth != 8) && match24) { + visual_chosen = i; + Max_color_size = -1; + needs_cmap = 0; + break; + } + } - if(visual_chosen < 0) { - printf("Couldn't find any good visuals at depth %d!\n", - depth); - return (Visual *)0; - } + if(visual_chosen < 0) { + printf("Couldn't find any good visuals at depth %d!\n", + depth); + return (Visual *)0; + } - printf("Chose visual: %d, max_colors: %d\n", visual_chosen, - Max_color_size); + printf("Chose visual: %d, max_colors: %d\n", visual_chosen, + Max_color_size); - v_chosen = &(visualList[visual_chosen]); - x_set_mask_and_shift(v_chosen->red_mask, &g_red_mask, - &g_red_left_shift, &g_red_right_shift); - x_set_mask_and_shift(v_chosen->green_mask, &g_green_mask, - &g_green_left_shift, &g_green_right_shift); - x_set_mask_and_shift(v_chosen->blue_mask, &g_blue_mask, - &g_blue_left_shift, &g_blue_right_shift); + v_chosen = &(visualList[visual_chosen]); + x_set_mask_and_shift(v_chosen->red_mask, &g_red_mask, + &g_red_left_shift, &g_red_right_shift); + x_set_mask_and_shift(v_chosen->green_mask, &g_green_mask, + &g_green_left_shift, &g_green_right_shift); + x_set_mask_and_shift(v_chosen->blue_mask, &g_blue_mask, + &g_blue_left_shift, &g_blue_right_shift); - g_screen_depth = depth; - mdepth = depth; - if(depth > 8) { - mdepth = 16; - } - if(depth > 16) { - mdepth = 32; - } - g_screen_mdepth = mdepth; - g_needs_cmap = needs_cmap; - *visual_list_ptr = visualList; + g_screen_depth = depth; + mdepth = depth; + if(depth > 8) { + mdepth = 16; + } + if(depth > 16) { + mdepth = 32; + } + g_screen_mdepth = mdepth; + g_needs_cmap = needs_cmap; + *visual_list_ptr = visualList; - return v_chosen->visual; + return v_chosen->visual; } -void -x_set_mask_and_shift(word32 x_mask, word32 *mask_ptr, int *shift_left_ptr, - int *shift_right_ptr) -{ - int shift; - int i; +void x_set_mask_and_shift(word32 x_mask, word32 *mask_ptr, int *shift_left_ptr, + int *shift_right_ptr) { + int shift; + int i; - /* Shift until we find first set bit in mask, then remember mask,shift*/ + /* Shift until we find first set bit in mask, then remember mask,shift*/ - shift = 0; - for(i = 0; i < 32; i++) { - if(x_mask & 1) { - /* we're done! */ - break; - } - x_mask = x_mask >> 1; - shift++; - } - *mask_ptr = x_mask; - *shift_left_ptr = shift; - /* Now, calculate shift_right_ptr */ - shift = 0; - x_mask |= 1; // make sure at least one bit is set - while(x_mask < 0x80) { - shift++; - x_mask = x_mask << 1; - } + shift = 0; + for(i = 0; i < 32; i++) { + if(x_mask & 1) { + /* we're done! */ + break; + } + x_mask = x_mask >> 1; + shift++; + } + *mask_ptr = x_mask; + *shift_left_ptr = shift; + /* Now, calculate shift_right_ptr */ + shift = 0; + x_mask |= 1; // make sure at least one bit is set + while(x_mask < 0x80) { + shift++; + x_mask = x_mask << 1; + } - *shift_right_ptr = shift; - return; + *shift_right_ptr = shift; + return; } int g_xshm_error = 0; -int -xhandle_shm_error(Display *display, XErrorEvent *event) -{ - g_xshm_error = 1; - return 0; +int xhandle_shm_error(Display *display, XErrorEvent *event) { + g_xshm_error = 1; + return 0; } -void -x_get_kimage(Kimage *kimage_ptr) { - if(g_use_shmem) { - g_use_shmem = get_shm(kimage_ptr); - } - if(!g_use_shmem) { - get_ximage(kimage_ptr); - } +void x_get_kimage(Kimage *kimage_ptr) { + if(g_use_shmem) { + g_use_shmem = get_shm(kimage_ptr); + } + if(!g_use_shmem) { + get_ximage(kimage_ptr); + } } -int -get_shm(Kimage *kimage_ptr) -{ +int get_shm(Kimage *kimage_ptr) { #ifdef X_SHARED_MEM - XShmSegmentInfo *seginfo; - XImage *xim; - int (*old_x_handler)(Display *, XErrorEvent *); - int width; - int height; - int depth; + XShmSegmentInfo *seginfo; + XImage *xim; + int (*old_x_handler)(Display *, XErrorEvent *); + int width; + int height; + int depth; - width = kimage_ptr->width_req; - height = kimage_ptr->height; - depth = kimage_ptr->depth; + width = kimage_ptr->width_req; + height = kimage_ptr->height; + depth = kimage_ptr->depth; - seginfo = (XShmSegmentInfo *)malloc(sizeof(XShmSegmentInfo)); - xim = XShmCreateImage(g_display, g_vis, depth, ZPixmap, - (char *)0, seginfo, width, height); + seginfo = (XShmSegmentInfo *)malloc(sizeof(XShmSegmentInfo)); + xim = XShmCreateImage(g_display, g_vis, depth, ZPixmap, + (char *)0, seginfo, width, height); - /* check mdepth! */ - if(xim->bits_per_pixel != kimage_ptr->mdepth) { - printf("get_shm bits_per_pix: %d != %d\n", - xim->bits_per_pixel, g_screen_mdepth); - } + /* check mdepth! */ + if(xim->bits_per_pixel != kimage_ptr->mdepth) { + printf("get_shm bits_per_pix: %d != %d\n", + xim->bits_per_pixel, g_screen_mdepth); + } - vid_printf("xim: %p\n", xim); - kimage_ptr->dev_handle = xim; - kimage_ptr->dev_handle2 = seginfo; - if(xim == 0) { - return 0; - } + vid_printf("xim: %p\n", xim); + kimage_ptr->dev_handle = xim; + kimage_ptr->dev_handle2 = seginfo; + if(xim == 0) { + return 0; + } - /* It worked, we got it */ - seginfo->shmid = shmget(IPC_PRIVATE, xim->bytes_per_line * xim->height, - IPC_CREAT | 0777); - vid_printf("seginfo->shmid = %d\n", seginfo->shmid); - if(seginfo->shmid < 0) { - XDestroyImage(xim); - return 0; - } + /* It worked, we got it */ + seginfo->shmid = shmget(IPC_PRIVATE, xim->bytes_per_line * xim->height, + IPC_CREAT | 0777); + vid_printf("seginfo->shmid = %d\n", seginfo->shmid); + if(seginfo->shmid < 0) { + XDestroyImage(xim); + return 0; + } - /* Still working */ - seginfo->shmaddr = (char *)shmat(seginfo->shmid, 0, 0); - vid_printf("seginfo->shmaddr: %p\n", seginfo->shmaddr); - if(seginfo->shmaddr == ((char *) -1)) { - XDestroyImage(xim); - return 0; - } + /* Still working */ + seginfo->shmaddr = (char *)shmat(seginfo->shmid, 0, 0); + vid_printf("seginfo->shmaddr: %p\n", seginfo->shmaddr); + if(seginfo->shmaddr == ((char *) -1)) { + XDestroyImage(xim); + return 0; + } - /* Still working */ - xim->data = seginfo->shmaddr; - seginfo->readOnly = False; + /* Still working */ + xim->data = seginfo->shmaddr; + seginfo->readOnly = False; - /* XShmAttach will trigger X error if server is remote, so catch it */ - g_xshm_error = 0; - old_x_handler = XSetErrorHandler(xhandle_shm_error); + /* XShmAttach will trigger X error if server is remote, so catch it */ + g_xshm_error = 0; + old_x_handler = XSetErrorHandler(xhandle_shm_error); - XShmAttach(g_display, seginfo); - XSync(g_display, False); + XShmAttach(g_display, seginfo); + XSync(g_display, False); - vid_printf("about to RMID the shmid\n"); - shmctl(seginfo->shmid, IPC_RMID, 0); + vid_printf("about to RMID the shmid\n"); + shmctl(seginfo->shmid, IPC_RMID, 0); - XFlush(g_display); - XSetErrorHandler(old_x_handler); + XFlush(g_display); + XSetErrorHandler(old_x_handler); - if(g_xshm_error) { - XDestroyImage(xim); - /* We could release the shared mem segment, but by doing the */ - /* RMID, it will go away when we die now, so just leave it */ - printf("Not using shared memory\n"); - return 0; - } + if(g_xshm_error) { + XDestroyImage(xim); + /* We could release the shared mem segment, but by doing the */ + /* RMID, it will go away when we die now, so just leave it */ + printf("Not using shared memory\n"); + return 0; + } - kimage_ptr->data_ptr = (byte *)xim->data; - vid_printf("Sharing memory. xim: %p, xim->data: %p\n", xim, xim->data); + kimage_ptr->data_ptr = (byte *)xim->data; + vid_printf("Sharing memory. xim: %p, xim->data: %p\n", xim, xim->data); - return 1; + return 1; #else - return 0; /* No shared memory */ -#endif /* X_SHARED_MEM */ + return 0; /* No shared memory */ +#endif /* X_SHARED_MEM */ } -void -get_ximage(Kimage *kimage_ptr) -{ - XImage *xim; - byte *ptr; - int width; - int height; - int depth; - int mdepth; +void get_ximage(Kimage *kimage_ptr) { + XImage *xim; + byte *ptr; + int width; + int height; + int depth; + int mdepth; - width = kimage_ptr->width_req; - height = kimage_ptr->height; - depth = kimage_ptr->depth; - mdepth = kimage_ptr->mdepth; + width = kimage_ptr->width_req; + height = kimage_ptr->height; + depth = kimage_ptr->depth; + mdepth = kimage_ptr->mdepth; - ptr = (byte *)malloc((width * height * mdepth) >> 3); + ptr = (byte *)malloc((width * height * mdepth) >> 3); - vid_printf("ptr: %p\n", ptr); + vid_printf("ptr: %p\n", ptr); - if(ptr == 0) { - printf("malloc for data failed, mdepth: %d\n", mdepth); - exit(2); - } + if(ptr == 0) { + printf("malloc for data failed, mdepth: %d\n", mdepth); + exit(2); + } - kimage_ptr->data_ptr = ptr; + kimage_ptr->data_ptr = ptr; - xim = XCreateImage(g_display, g_vis, depth, ZPixmap, 0, - (char *)ptr, width, height, 8, 0); + xim = XCreateImage(g_display, g_vis, depth, ZPixmap, 0, + (char *)ptr, width, height, 8, 0); #if defined(GSPLUS_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) // OSX needs to calculate endianness mid-compilation, can't be passed on compile command - xim->byte_order = LSBFirst; + xim->byte_order = LSBFirst; #else - xim->byte_order = MSBFirst; + xim->byte_order = MSBFirst; #endif - XInitImage(xim); /* adjust to new byte order */ + XInitImage(xim); /* adjust to new byte order */ - /* check mdepth! */ - if(xim->bits_per_pixel != mdepth) { - printf("shm_ximage bits_per_pix: %d != %d\n", - xim->bits_per_pixel, mdepth); - } + /* check mdepth! */ + if(xim->bits_per_pixel != mdepth) { + printf("shm_ximage bits_per_pix: %d != %d\n", + xim->bits_per_pixel, mdepth); + } - vid_printf("xim: %p\n", xim); + vid_printf("xim: %p\n", xim); - kimage_ptr->dev_handle = xim; + kimage_ptr->dev_handle = xim; - return; + return; } -void -x_toggle_status_lines() -{ - XSizeHints my_winSizeHints; - XClassHint my_winClassHint; - int base_height = X_A2_WINDOW_HEIGHT; - if ((g_win_status_debug = !g_win_status_debug)) - base_height += MAX_STATUS_LINES * 13; - //printf("Resize returns %d\n", XResizeWindow(g_display, g_a2_win, BASE_WINDOW_WIDTH, base_height)); - my_winSizeHints.flags = PSize | PMinSize | PMaxSize; - my_winSizeHints.width = BASE_WINDOW_WIDTH; - my_winSizeHints.height = base_height; - my_winSizeHints.min_width = BASE_WINDOW_WIDTH; - my_winSizeHints.min_height = base_height; - my_winSizeHints.max_width = BASE_WINDOW_WIDTH; - my_winSizeHints.max_height = base_height; - my_winClassHint.res_name = "GSport"; - my_winClassHint.res_class = "GSport"; - XSetWMProperties(g_display, g_a2_win, 0, 0, 0, - 0, &my_winSizeHints, 0, &my_winClassHint); - XMapRaised(g_display, g_a2_win); - XFlush(g_display); - x_redraw_status_lines(); +void x_toggle_status_lines() { + XSizeHints my_winSizeHints; + XClassHint my_winClassHint; + int base_height = X_A2_WINDOW_HEIGHT; + if ((g_win_status_debug = !g_win_status_debug)) + base_height += MAX_STATUS_LINES * 13; + //printf("Resize returns %d\n", XResizeWindow(g_display, g_a2_win, BASE_WINDOW_WIDTH, base_height)); + my_winSizeHints.flags = PSize | PMinSize | PMaxSize; + my_winSizeHints.width = BASE_WINDOW_WIDTH; + my_winSizeHints.height = base_height; + my_winSizeHints.min_width = BASE_WINDOW_WIDTH; + my_winSizeHints.min_height = base_height; + my_winSizeHints.max_width = BASE_WINDOW_WIDTH; + my_winSizeHints.max_height = base_height; + my_winClassHint.res_name = "GSport"; + my_winClassHint.res_class = "GSport"; + XSetWMProperties(g_display, g_a2_win, 0, 0, 0, + 0, &my_winSizeHints, 0, &my_winClassHint); + XMapRaised(g_display, g_a2_win); + XFlush(g_display); + x_redraw_status_lines(); } -void -x_redraw_status_lines() -{ - char *buf; - int line; - int height; - int margin; - word32 white, black; +void x_redraw_status_lines() { + char *buf; + int line; + int height; + int margin; + word32 white, black; - if (g_win_status_debug) - { - height = g_text_FontSt->ascent + g_text_FontSt->descent; - margin = g_text_FontSt->ascent; - - white = (g_a2vid_palette << 4) + 0xf; - black = (g_a2vid_palette << 4) + 0x0; - if(g_screen_depth != 8) { - white = (2 << (g_screen_depth - 1)) - 1; - black = 0; - } - XSetForeground(g_display, g_a2_winGC, white); - XSetBackground(g_display, g_a2_winGC, black); - - for(line = 0; line < MAX_STATUS_LINES; line++) { - buf = g_status_ptrs[line]; - if(buf == 0) { - /* skip it */ - continue; - } - XDrawImageString(g_display, g_a2_win, g_a2_winGC, 0, - X_A2_WINDOW_HEIGHT + height*line + margin, - buf, strlen(buf)); - } - - XFlush(g_display); + if (g_win_status_debug) + { + height = g_text_FontSt->ascent + g_text_FontSt->descent; + margin = g_text_FontSt->ascent; + + white = (g_a2vid_palette << 4) + 0xf; + black = (g_a2vid_palette << 4) + 0x0; + if(g_screen_depth != 8) { + white = (2 << (g_screen_depth - 1)) - 1; + black = 0; } + XSetForeground(g_display, g_a2_winGC, white); + XSetBackground(g_display, g_a2_winGC, black); + + for(line = 0; line < MAX_STATUS_LINES; line++) { + buf = g_status_ptrs[line]; + if(buf == 0) { + /* skip it */ + continue; + } + XDrawImageString(g_display, g_a2_win, g_a2_winGC, 0, + X_A2_WINDOW_HEIGHT + height*line + margin, + buf, strlen(buf)); + } + + XFlush(g_display); + } } -void -x_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, - int width, int height) -{ - XImage *xim; +void x_push_kimage(Kimage *kimage_ptr, int destx, int desty, int srcx, int srcy, + int width, int height) { + XImage *xim; - xim = (XImage *)kimage_ptr->dev_handle; + xim = (XImage *)kimage_ptr->dev_handle; #ifdef X_SHARED_MEM - if(g_use_shmem) { - XShmPutImage(g_display, g_a2_win, g_a2_winGC, xim, - srcx, srcy, destx, desty, width, height, False); - } + if(g_use_shmem) { + XShmPutImage(g_display, g_a2_win, g_a2_winGC, xim, + srcx, srcy, destx, desty, width, height, False); + } #endif - if(!g_use_shmem) { - XPutImage(g_display, g_a2_win, g_a2_winGC, xim, - srcx, srcy, destx, desty, width, height); - } + if(!g_use_shmem) { + XPutImage(g_display, g_a2_win, g_a2_winGC, xim, + srcx, srcy, destx, desty, width, height); + } } -void -x_push_done() -{ - XFlush(g_display); +void x_push_done() { + XFlush(g_display); } -#define KEYBUFLEN 128 +#define KEYBUFLEN 128 int g_num_check_input_calls = 0; int g_check_input_flush_rate = 2; -int -x_update_mouse(int raw_x, int raw_y, int button_states, int buttons_valid) -{ - int x, y; +int x_update_mouse(int raw_x, int raw_y, int button_states, int buttons_valid) { + int x, y; - x = raw_x - BASE_MARGIN_LEFT; - y = raw_y - BASE_MARGIN_TOP; + x = raw_x - BASE_MARGIN_LEFT; + y = raw_y - BASE_MARGIN_TOP; - if(g_warp_pointer && (x == A2_WINDOW_WIDTH/2) && - (y == A2_WINDOW_HEIGHT/2) && (buttons_valid == 0) ) { - /* tell adb routs to recenter but ignore this motion */ - update_mouse(x, y, 0, -1); - return 0; - } - return update_mouse(x, y, button_states, buttons_valid & 7); + if(g_warp_pointer && (x == A2_WINDOW_WIDTH/2) && + (y == A2_WINDOW_HEIGHT/2) && (buttons_valid == 0) ) { + /* tell adb routs to recenter but ignore this motion */ + update_mouse(x, y, 0, -1); + return 0; + } + return update_mouse(x, y, button_states, buttons_valid & 7); } -void -check_input_events() -{ - XEvent ev; - int len; - int motion; - int buttons; - int refresh_needed; +void check_input_events() { + XEvent ev; + int len; + int motion; + int buttons; + int refresh_needed; - g_num_check_input_calls--; - if(g_num_check_input_calls < 0) { - len = XPending(g_display); - g_num_check_input_calls = g_check_input_flush_rate; - } else { - len = QLength(g_display); - } + g_num_check_input_calls--; + if(g_num_check_input_calls < 0) { + len = XPending(g_display); + g_num_check_input_calls = g_check_input_flush_rate; + } else { + len = QLength(g_display); + } - motion = 0; - refresh_needed = 0; - while(len > 0) { - XNextEvent(g_display, &ev); - len--; - if(len == 0) { - /* Xaccel on linux only buffers one X event */ - /* must look for more now */ - len = XPending(g_display); - } - switch(ev.type) { - case FocusIn: - case FocusOut: - if(ev.xfocus.type == FocusOut) { - /* Allow keyrepeat again! */ - vid_printf("Left window, auto repeat on\n"); - x_auto_repeat_on(0); - g_has_focus = 0; - } else if(ev.xfocus.type == FocusIn) { - /* Allow keyrepeat again! */ - vid_printf("Enter window, auto repeat off\n"); - x_auto_repeat_off(0); - g_has_focus = 1; - } - break; - case EnterNotify: - case LeaveNotify: - /* These events are disabled now */ - printf("Enter/Leave event for winow %08x, sub: %08x\n", - (word32)ev.xcrossing.window, - (word32)ev.xcrossing.subwindow); - printf("Enter/L mode: %08x, detail: %08x, type:%02x\n", - ev.xcrossing.mode, ev.xcrossing.detail, - ev.xcrossing.type); - break; - case ButtonPress: - vid_printf("Got button press of button %d!\n", - ev.xbutton.button); - buttons = (1 << ev.xbutton.button) >> 1; - motion |= x_update_mouse(ev.xbutton.x, ev.xbutton.y, - buttons, buttons & 7); + motion = 0; + refresh_needed = 0; + while(len > 0) { + XNextEvent(g_display, &ev); + len--; + if(len == 0) { + /* Xaccel on linux only buffers one X event */ + /* must look for more now */ + len = XPending(g_display); + } + switch(ev.type) { + case FocusIn: + case FocusOut: + if(ev.xfocus.type == FocusOut) { + /* Allow keyrepeat again! */ + vid_printf("Left window, auto repeat on\n"); + x_auto_repeat_on(0); + g_has_focus = 0; + } else if(ev.xfocus.type == FocusIn) { + /* Allow keyrepeat again! */ + vid_printf("Enter window, auto repeat off\n"); + x_auto_repeat_off(0); + g_has_focus = 1; + } + break; + case EnterNotify: + case LeaveNotify: + /* These events are disabled now */ + printf("Enter/Leave event for winow %08x, sub: %08x\n", + (word32)ev.xcrossing.window, + (word32)ev.xcrossing.subwindow); + printf("Enter/L mode: %08x, detail: %08x, type:%02x\n", + ev.xcrossing.mode, ev.xcrossing.detail, + ev.xcrossing.type); + break; + case ButtonPress: + vid_printf("Got button press of button %d!\n", + ev.xbutton.button); + buttons = (1 << ev.xbutton.button) >> 1; + motion |= x_update_mouse(ev.xbutton.x, ev.xbutton.y, + buttons, buttons & 7); - break; - case ButtonRelease: - buttons = (1 << ev.xbutton.button) >> 1; - motion |= x_update_mouse(ev.xbutton.x, ev.xbutton.y, 0, - buttons & 7); - break; - case MotionNotify: - if(ev.xmotion.window != g_a2_win) { - printf("Motion in window %08x unknown!\n", - (word32)ev.xmotion.window); - } - motion |= x_update_mouse(ev.xmotion.x, ev.xmotion.y, 0, - 0); - break; - case Expose: - refresh_needed = -1; - break; - case NoExpose: - /* do nothing */ - break; - case KeyPress: - case KeyRelease: - handle_keysym(&ev); - break; - case KeymapNotify: - break; - case ColormapNotify: - vid_printf("ColormapNotify for %08x\n", - (word32)(ev.xcolormap.window)); - vid_printf("colormap: %08x, new: %d, state: %d\n", - (word32)ev.xcolormap.colormap, - ev.xcolormap.new, ev.xcolormap.state); - break; - case ClientMessage: - if (ev.xclient.data.l[0] == (long)WM_DELETE_WINDOW) - { - iwm_shut(); - my_exit(1); - } - break; - default: - printf("X event 0x%08x is unknown!\n", - ev.type); - break; - } - } + break; + case ButtonRelease: + buttons = (1 << ev.xbutton.button) >> 1; + motion |= x_update_mouse(ev.xbutton.x, ev.xbutton.y, 0, + buttons & 7); + break; + case MotionNotify: + if(ev.xmotion.window != g_a2_win) { + printf("Motion in window %08x unknown!\n", + (word32)ev.xmotion.window); + } + motion |= x_update_mouse(ev.xmotion.x, ev.xmotion.y, 0, + 0); + break; + case Expose: + refresh_needed = -1; + break; + case NoExpose: + /* do nothing */ + break; + case KeyPress: + case KeyRelease: + handle_keysym(&ev); + break; + case KeymapNotify: + break; + case ColormapNotify: + vid_printf("ColormapNotify for %08x\n", + (word32)(ev.xcolormap.window)); + vid_printf("colormap: %08x, new: %d, state: %d\n", + (word32)ev.xcolormap.colormap, + ev.xcolormap.new, ev.xcolormap.state); + break; + case ClientMessage: + if (ev.xclient.data.l[0] == (long)WM_DELETE_WINDOW) + { + iwm_shut(); + my_exit(1); + } + break; + default: + printf("X event 0x%08x is unknown!\n", + ev.type); + break; + } + } - if(motion && g_warp_pointer) { - XWarpPointer(g_display, None, g_a2_win, 0, 0, 0, 0, - BASE_MARGIN_LEFT + (A2_WINDOW_WIDTH/2), - BASE_MARGIN_TOP + (A2_WINDOW_HEIGHT/2)); - } + if(motion && g_warp_pointer) { + XWarpPointer(g_display, None, g_a2_win, 0, 0, 0, 0, + BASE_MARGIN_LEFT + (A2_WINDOW_WIDTH/2), + BASE_MARGIN_TOP + (A2_WINDOW_HEIGHT/2)); + } - if(refresh_needed) { - printf("Full refresh needed\n"); - g_a2_screen_buffer_changed = -1; - g_full_refresh_needed = -1; + if(refresh_needed) { + printf("Full refresh needed\n"); + g_a2_screen_buffer_changed = -1; + g_full_refresh_needed = -1; - g_border_sides_refresh_needed = 1; - g_border_special_refresh_needed = 1; - g_status_refresh_needed = 1; + g_border_sides_refresh_needed = 1; + g_border_special_refresh_needed = 1; + g_status_refresh_needed = 1; - /* x_refresh_ximage(); */ - /* redraw_border(); */ - } + /* x_refresh_ximage(); */ + /* redraw_border(); */ + } } -void -x_hide_pointer(int do_hide) -{ - if(do_hide) { - XDefineCursor(g_display, g_a2_win, g_cursor); - } else { - XDefineCursor(g_display, g_a2_win, None); - } +void x_hide_pointer(int do_hide) { + if(do_hide) { + XDefineCursor(g_display, g_a2_win, g_cursor); + } else { + XDefineCursor(g_display, g_a2_win, None); + } } -void -handle_keysym(XEvent *xev_in) -{ - KeySym keysym; - word32 state; - int keycode; - int a2code; - int type; - int is_up; +void handle_keysym(XEvent *xev_in) { + KeySym keysym; + word32 state; + int keycode; + int a2code; + int type; + int is_up; - keycode = xev_in->xkey.keycode; - type = xev_in->xkey.type; + keycode = xev_in->xkey.keycode; + type = xev_in->xkey.type; - keysym = XLookupKeysym(&(xev_in->xkey), 0); + keysym = XLookupKeysym(&(xev_in->xkey), 0); - state = xev_in->xkey.state; + state = xev_in->xkey.state; - vid_printf("keycode: %d, type: %d, state:%d, sym: %08x\n", - keycode, type, state, (word32)keysym); + vid_printf("keycode: %d, type: %d, state:%d, sym: %08x\n", + keycode, type, state, (word32)keysym); - x_update_modifier_state(state); + x_update_modifier_state(state); - is_up = 0; - if(type == KeyRelease) { - is_up = 1; - } + is_up = 0; + if(type == KeyRelease) { + is_up = 1; + } #if 0 - if(keysym == XK_Alt_L || keysym == XK_Meta_L) { - g_alt_left_up = is_up; - } + if(keysym == XK_Alt_L || keysym == XK_Meta_L) { + g_alt_left_up = is_up; + } - if(keysym == XK_Alt_R || keysym == XK_Meta_R) { - g_alt_right_up = is_up; - } + if(keysym == XK_Alt_R || keysym == XK_Meta_R) { + g_alt_right_up = is_up; + } - if(g_alt_left_up == 0 && g_alt_right_up == 0) { - printf("Sending sound to file\n"); - g_send_sound_to_file = 1; - } else { - if(g_send_sound_to_file) { - printf("Stopping sending sound to file\n"); - close_sound_file(); - } - g_send_sound_to_file = 0; - } + if(g_alt_left_up == 0 && g_alt_right_up == 0) { + printf("Sending sound to file\n"); + g_send_sound_to_file = 1; + } else { + if(g_send_sound_to_file) { + printf("Stopping sending sound to file\n"); + close_sound_file(); + } + g_send_sound_to_file = 0; + } #endif - /* first, do conversions */ - switch(keysym) { - case XK_Alt_R: - case XK_Meta_R: - case XK_Super_R: - case XK_Mode_switch: - case XK_Cancel: - keysym = XK_Print; /* option */ - break; - case XK_Alt_L: - case XK_Meta_L: - case XK_Super_L: - case XK_Menu: - keysym = XK_Scroll_Lock; /* cmd */ - break; - case 0x1000003: - if(keycode == 0x3c) { - /* enter key on Mac OS X laptop--make it option */ - keysym = XK_Print; - } - break; - case NoSymbol: - switch(keycode) { - /* 94-95 are for my PC101 kbd + windows keys on HPUX */ - case 0x0095: - /* left windows key = option */ - keysym = XK_Print; - break; - case 0x0096: - case 0x0094: - /* right windows key = cmd */ - keysym = XK_Scroll_Lock; - break; - /* 0072 is for cra@WPI.EDU who says it's Break under XFree86 */ - case 0x0072: - /* 006e is break according to mic@research.nj.nec.com */ - case 0x006e: - keysym = XK_Break; - break; + /* first, do conversions */ + switch(keysym) { + case XK_Alt_R: + case XK_Meta_R: + case XK_Super_R: + case XK_Mode_switch: + case XK_Cancel: + keysym = XK_Print; /* option */ + break; + case XK_Alt_L: + case XK_Meta_L: + case XK_Super_L: + case XK_Menu: + keysym = XK_Scroll_Lock; /* cmd */ + break; + case 0x1000003: + if(keycode == 0x3c) { + /* enter key on Mac OS X laptop--make it option */ + keysym = XK_Print; + } + break; + case NoSymbol: + switch(keycode) { + /* 94-95 are for my PC101 kbd + windows keys on HPUX */ + case 0x0095: + /* left windows key = option */ + keysym = XK_Print; + break; + case 0x0096: + case 0x0094: + /* right windows key = cmd */ + keysym = XK_Scroll_Lock; + break; + /* 0072 is for cra@WPI.EDU who says it's Break under XFree86 */ + case 0x0072: + /* 006e is break according to mic@research.nj.nec.com */ + case 0x006e: + keysym = XK_Break; + break; - /* 0x0042, 0x0046, and 0x0048 are the windows keys according */ - /* to Geoff Weiss on Solaris x86 */ - case 0x0042: - case 0x0046: - /* flying windows == open apple */ - keysym = XK_Scroll_Lock; - break; - case 0x0048: - case 0x0076: /* Windows menu key on Mac OS X */ - /* menu windows == option */ - keysym = XK_Print; - break; - } - } + /* 0x0042, 0x0046, and 0x0048 are the windows keys according */ + /* to Geoff Weiss on Solaris x86 */ + case 0x0042: + case 0x0046: + /* flying windows == open apple */ + keysym = XK_Scroll_Lock; + break; + case 0x0048: + case 0x0076: /* Windows menu key on Mac OS X */ + /* menu windows == option */ + keysym = XK_Print; + break; + } + } - a2code = x_keysym_to_a2code(keysym, is_up); - if(a2code >= 0) { - adb_physical_key_update(a2code, is_up); - } else if(a2code != -2) { - printf("Keysym: %04x of keycode: %02x unknown\n", - (word32)keysym, keycode); - } + a2code = x_keysym_to_a2code(keysym, is_up); + if(a2code >= 0) { + adb_physical_key_update(a2code, is_up); + } else if(a2code != -2) { + printf("Keysym: %04x of keycode: %02x unknown\n", + (word32)keysym, keycode); + } } -int -x_keysym_to_a2code(int keysym, int is_up) -{ - int i; +int x_keysym_to_a2code(int keysym, int is_up) { + int i; - if(keysym == 0) { - return -1; - } + if(keysym == 0) { + return -1; + } - if((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) { - if(is_up) { - g_x_shift_control_state &= ~ShiftMask; - } else { - g_x_shift_control_state |= ShiftMask; - } - } - if(keysym == XK_Caps_Lock) { - if(is_up) { - g_x_shift_control_state &= ~LockMask; - } else { - g_x_shift_control_state |= LockMask; - } - } - if((keysym == XK_Control_L) || (keysym == XK_Control_R)) { - if(is_up) { - g_x_shift_control_state &= ~ControlMask; - } else { - g_x_shift_control_state |= ControlMask; - } - } + if((keysym == XK_Shift_L) || (keysym == XK_Shift_R)) { + if(is_up) { + g_x_shift_control_state &= ~ShiftMask; + } else { + g_x_shift_control_state |= ShiftMask; + } + } + if(keysym == XK_Caps_Lock) { + if(is_up) { + g_x_shift_control_state &= ~LockMask; + } else { + g_x_shift_control_state |= LockMask; + } + } + if((keysym == XK_Control_L) || (keysym == XK_Control_R)) { + if(is_up) { + g_x_shift_control_state &= ~ControlMask; + } else { + g_x_shift_control_state |= ControlMask; + } + } - /* Look up Apple 2 keycode */ - for(i = g_num_a2_keycodes - 1; i >= 0; i--) { - if((keysym == a2_key_to_xsym[i][1]) || - (keysym == a2_key_to_xsym[i][2])) { + /* Look up Apple 2 keycode */ + for(i = g_num_a2_keycodes - 1; i >= 0; i--) { + if((keysym == a2_key_to_xsym[i][1]) || + (keysym == a2_key_to_xsym[i][2])) { - vid_printf("Found keysym:%04x = a[%d] = %04x or %04x\n", - (int)keysym, i, a2_key_to_xsym[i][1], - a2_key_to_xsym[i][2]); + vid_printf("Found keysym:%04x = a[%d] = %04x or %04x\n", + (int)keysym, i, a2_key_to_xsym[i][1], + a2_key_to_xsym[i][2]); - return a2_key_to_xsym[i][0]; - } - } + return a2_key_to_xsym[i][0]; + } + } - return -1; + return -1; } -void -x_update_modifier_state(int state) -{ - int state_xor; - int is_up; +void x_update_modifier_state(int state) { + int state_xor; + int is_up; - state = state & (ControlMask | LockMask | ShiftMask); - state_xor = g_x_shift_control_state ^ state; - is_up = 0; - if(state_xor & ControlMask) { - is_up = ((state & ControlMask) == 0); - adb_physical_key_update(0x36, is_up); - } - if(state_xor & LockMask) { - is_up = ((state & LockMask) == 0); - adb_physical_key_update(0x39, is_up); - } - if(state_xor & ShiftMask) { - is_up = ((state & ShiftMask) == 0); - adb_physical_key_update(0x38, is_up); - } + state = state & (ControlMask | LockMask | ShiftMask); + state_xor = g_x_shift_control_state ^ state; + is_up = 0; + if(state_xor & ControlMask) { + is_up = ((state & ControlMask) == 0); + adb_physical_key_update(0x36, is_up); + } + if(state_xor & LockMask) { + is_up = ((state & LockMask) == 0); + adb_physical_key_update(0x39, is_up); + } + if(state_xor & ShiftMask) { + is_up = ((state & ShiftMask) == 0); + adb_physical_key_update(0x38, is_up); + } - g_x_shift_control_state = state; + g_x_shift_control_state = state; } -void -x_auto_repeat_on(int must) -{ - if((g_auto_repeat_on <= 0) || must) { - g_auto_repeat_on = 1; - XAutoRepeatOn(g_display); - XFlush(g_display); - adb_kbd_repeat_off(); - } +void x_auto_repeat_on(int must) { + if((g_auto_repeat_on <= 0) || must) { + g_auto_repeat_on = 1; + XAutoRepeatOn(g_display); + XFlush(g_display); + adb_kbd_repeat_off(); + } } -void -x_auto_repeat_off(int must) -{ - if((g_auto_repeat_on != 0) || must) { - XAutoRepeatOff(g_display); - XFlush(g_display); - g_auto_repeat_on = 0; - adb_kbd_repeat_off(); - } +void x_auto_repeat_off(int must) { + if((g_auto_repeat_on != 0) || must) { + XAutoRepeatOff(g_display); + XFlush(g_display); + g_auto_repeat_on = 0; + adb_kbd_repeat_off(); + } } -void -x_full_screen(int do_full) -{ - return; +void x_full_screen(int do_full) { + return; } // OG Adding release -void x_release_kimage(Kimage* kimage_ptr) -{ - if (kimage_ptr->dev_handle == (void*)-1) - { - free(kimage_ptr->data_ptr); - kimage_ptr->data_ptr = NULL; - } +void x_release_kimage(Kimage* kimage_ptr) { + if (kimage_ptr->dev_handle == (void*)-1) + { + free(kimage_ptr->data_ptr); + kimage_ptr->data_ptr = NULL; + } } // OG Addding ratio -int x_calc_ratio(float x,float y) -{ - return 1; +int x_calc_ratio(float x,float y) { + return 1; } -void -clipboard_paste(void) -{ - // TODO: Add clipboard support +void clipboard_paste(void) { + // TODO: Add clipboard support } -int -clipboard_get_char(void) -{ - // TODO: Add clipboard support - return 0; +int clipboard_get_char(void) { + // TODO: Add clipboard support + return 0; }