From 26341faff2325820aab3c522c413ad20693f05b9 Mon Sep 17 00:00:00 2001 From: tudnai Date: Wed, 20 May 2020 22:22:44 -0700 Subject: [PATCH] Mouse 2 JoyStick --- .../xcdebugger/Expressions.xcexplist | 3 + A2Mac/ViewController.swift | 91 +++++++++++-------- src/cpu/6502.h | 4 +- src/dev/game/paddle.h | 13 +-- 4 files changed, 66 insertions(+), 45 deletions(-) diff --git a/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist b/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist index 8114c2a..bbef11a 100644 --- a/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist +++ b/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist @@ -494,6 +494,9 @@ + + diff --git a/A2Mac/ViewController.swift b/A2Mac/ViewController.swift index b64f311..9423071 100644 --- a/A2Mac/ViewController.swift +++ b/A2Mac/ViewController.swift @@ -364,42 +364,51 @@ class ViewController: NSViewController { kbdUp() } -// override func flagsChanged(with event: NSEvent) { -// switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { -// case [.shift]: -// print("shift key is pressed") -// case [.control]: -// print("control key is pressed") -// case [.option] : -// print("option key is pressed") -// case [.command]: -// print("Command key is pressed") -// case [.control, .shift]: -// print("control-shift keys are pressed") -// case [.option, .shift]: -// print("option-shift keys are pressed") -// case [.command, .shift]: -// print("command-shift keys are pressed") -// case [.control, .option]: -// print("control-option keys are pressed") -// case [.control, .command]: -// print("control-command keys are pressed") -// case [.option, .command]: -// print("option-command keys are pressed") -// case [.shift, .control, .option]: -// print("shift-control-option keys are pressed") -// case [.shift, .control, .command]: -// print("shift-control-command keys are pressed") -// case [.control, .option, .command]: -// print("control-option-command keys are pressed") -// case [.shift, .command, .option]: -// print("shift-command-option keys are pressed") -// case [.shift, .control, .option, .command]: -// print("shift-control-option-command keys are pressed") -// default: -// print("no modifier keys are pressed") -// } -// } + + override func flagsChanged(with event: NSEvent) { + switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { + case [.shift]: + print("shift key is pressed") + case [.control]: + print("control key is pressed") + case [.option] : + setIO(0xC061, 1 << 7); + print("option key is pressed") + case [.command]: + print("Command key is pressed") + setIO(0xC062, 1 << 7); + case [.control, .shift]: + print("control-shift keys are pressed") + case [.option, .shift]: + print("option-shift keys are pressed") + case [.command, .shift]: + print("command-shift keys are pressed") + case [.control, .option]: + print("control-option keys are pressed") + case [.control, .command]: + print("control-command keys are pressed") + case [.option, .command]: + print("option-command keys are pressed") + case [.shift, .control, .option]: + print("shift-control-option keys are pressed") + case [.shift, .control, .command]: + print("shift-control-command keys are pressed") + case [.control, .option, .command]: + print("control-option-command keys are pressed") + case [.shift, .command, .option]: + print("shift-command-option keys are pressed") + case [.shift, .control, .option, .command]: + print("shift-control-option-command keys are pressed") + case [.function]: + print("function key is pressed") + case [.capsLock]: + print("capsLock key is pressed") + default: + setIO(0xC061, 0); + setIO(0xC062, 0); + print("no modifier keys are pressed") + } + } @@ -414,9 +423,13 @@ class ViewController: NSViewController { var halted = true; + var mouseLocation = NSPoint.zero + func Update() { // clk_6502_per_frm_max = 0 + + clkCounter += Double(clkfrm) // we start a new frame from here, so CPU is running even while rendering clkfrm = 0 @@ -569,8 +582,14 @@ class ViewController: NSViewController { // stream speaker from a separate thread from the simulation // TODO: Do we need to do this from here? // spkr_update() + + // Mouse 2 JoyStick (Game Controller / Paddle) + self.mouseLocation = self.view.window!.mouseLocationOutsideOfEventStream + pdl_valarr[0] = Double(self.mouseLocation.x / (self.displayField.frame.width) ) + pdl_valarr[1] = 1 - Double(self.mouseLocation.y / (self.displayField.frame.height) ) } + #if SPEEDTEST #else if ( !halted ) { diff --git a/src/cpu/6502.h b/src/cpu/6502.h index 1ebe077..804835b 100644 --- a/src/cpu/6502.h +++ b/src/cpu/6502.h @@ -148,6 +148,8 @@ extern uint8_t * const RAM; extern uint8_t * const MEM; // Pointer to the Shadow Memory Map so we can use this from Swift//extern uint8_t * AUX_VID_RAM; extern uint32_t * videoMemPtr; +extern double * pdl_valarr; + //extern void hires_Update(void); extern double mips; @@ -161,7 +163,7 @@ extern void tst6502(void); extern void m6502_ColdReset( const char * bundlePath, const char * romFilePath ); extern void m6502_Run(void); extern void kbdInput ( uint8_t code ); -extern void kbdUp (); +extern void kbdUp (void); extern void setIO ( uint16_t ioaddr, uint8_t val ); extern void interrupt_IRQ(void); diff --git a/src/dev/game/paddle.h b/src/dev/game/paddle.h index 71d4fb5..49a1b2a 100644 --- a/src/dev/game/paddle.h +++ b/src/dev/game/paddle.h @@ -11,14 +11,16 @@ #include "6502.h" -#define PDL_MAX_TIME 3300.0 +// TODO: We may need a joystick calibration +#define PDL_MAX_TIME 3072.0 // 3300.0 #define PDL_TIME_DECAY ( 255.0 / PDL_MAX_TIME ) uint64_t pdl_reset_time = 0; uint64_t pdl_current_time = 0; uint64_t pdl_elapsed_time = 0; -float pdl_value[4] = { 0.5, 0.0, 1.0, 0.1 }; +double pdl_value[4] = { 0.5, 0.5, 0.5, 0.5 }; +double * pdl_valarr = pdl_value; INLINE uint8_t pdl_reset() { pdl_reset_time = m6502.clktime + clkfrm; @@ -29,13 +31,8 @@ INLINE uint8_t pdl_reset() { INLINE uint8_t pdl_read( uint8_t pdl ) { pdl_current_time = m6502.clktime + clkfrm; pdl_elapsed_time = pdl_current_time - pdl_reset_time; - double normalized_time = pdl_elapsed_time / PDL_MAX_TIME; // 0: started, >= 1 ended - -// printf("PDL%d: %d\n", pdl, (uint8_t) ( pdl_value[pdl] * 512 * ( 1 - (pdl_elapsed_time / PDL_MAX_TIME) )) ); - -// return (uint8_t) ( pdl_value[pdl] * 512 * ( 1 - (pdl_elapsed_time / PDL_MAX_TIME) ) ); - return normalized_time >= pdl_value[pdl] ? 0 : 0x80; + return normalized_time >= pdl_value[pdl] ? 0 : 1 << 7; } #endif /* paddle_h */