Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/GamecubeConsole/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "GamecubeConsole.hpp"
#include "gamecube_definitions.h"

#include <hardware/clocks.h>
#include <hardware/pio.h>
#include <pico/stdlib.h>

Expand Down
1 change: 1 addition & 0 deletions examples/GamecubeController/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "GamecubeController.hpp"
#include "gamecube_definitions.h"

#include <hardware/clocks.h>
#include <hardware/pio.h>
#include <pico/stdlib.h>
#include <stdio.h>
Expand Down
1 change: 1 addition & 0 deletions examples/N64Console/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "N64Console.hpp"
#include "n64_definitions.h"

#include <hardware/clocks.h>
#include <hardware/pio.h>
#include <pico/stdlib.h>

Expand Down
1 change: 1 addition & 0 deletions examples/N64Controller/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "N64Controller.hpp"
#include "gamecube_definitions.h"

#include <hardware/clocks.h>
#include <hardware/pio.h>
#include <pico/stdlib.h>
#include <stdio.h>
Expand Down
61 changes: 35 additions & 26 deletions include/joybus.pio.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,56 @@
// ------ //

#define joybus_wrap_target 0
#define joybus_wrap 22
#define joybus_wrap 25
#define joybus_pio_version 1

#define joybus_T1 10
#define joybus_T2 20
#define joybus_T3 10

#define joybus_offset_read 0u
#define joybus_offset_write 5u
#define joybus_offset_write 8u

static const uint16_t joybus_program_instructions[] = {
// .wrap_target
0xe080, // 0: set pindirs, 0
0x3320, // 1: wait 0 pin, 0 [19]
0x4001, // 2: in pins, 1
0x20a0, // 3: wait 1 pin, 0
0x0001, // 4: jmp 1
0xe081, // 5: set pindirs, 1
0xe001, // 6: set pins, 1
0x80e0, // 7: pull ifempty block
0x6021, // 8: out x, 1
0x00ee, // 9: jmp !osre, 14
0x00b3, // 10: jmp x != y, 19
0x80e0, // 11: pull ifempty block
0x6021, // 12: out x, 1
0x000f, // 13: jmp 15
0xa342, // 14: nop [3]
0xa142, // 15: nop [1]
0xe900, // 16: set pins, 0 [9]
0xb201, // 17: mov pins, x [18]
0x0006, // 18: jmp 6
0xa442, // 19: nop [4]
0xe900, // 20: set pins, 0 [9]
0xf201, // 21: set pins, 1 [18]
0x0000, // 22: jmp 0
0xe080, // 0: set pindirs, 0
0x20a0, // 1: wait 1 pin, 0
0x3320, // 2: wait 0 pin, 0 [19]
0x4f01, // 3: in pins, 1 [15]
0x00c2, // 4: jmp pin, 2
0xa0c3, // 5: mov isr, null
0x20a0, // 6: wait 1 pin, 0
0x0002, // 7: jmp 2
0xe081, // 8: set pindirs, 1
0xe001, // 9: set pins, 1
0x80e0, // 10: pull ifempty block
0x6021, // 11: out x, 1
0x00f1, // 12: jmp !osre, 17
0x00b6, // 13: jmp x != y, 22
0x80e0, // 14: pull ifempty block
0x6021, // 15: out x, 1
0x0012, // 16: jmp 18
0xa342, // 17: nop [3]
0xa142, // 18: nop [1]
0xe900, // 19: set pins, 0 [9]
0xb201, // 20: mov pins, x [18]
0x0009, // 21: jmp 9
0xa442, // 22: nop [4]
0xe900, // 23: set pins, 0 [9]
0xf201, // 24: set pins, 1 [18]
0x0000, // 25: jmp 0
// .wrap
};

#if !PICO_NO_HARDWARE
static const struct pio_program joybus_program = {
.instructions = joybus_program_instructions,
.length = 23,
.length = 26,
.origin = -1,
.pio_version = joybus_pio_version,
#if PICO_PIO_VERSION > 0
.used_gpio_ranges = 0x0
#endif
};

static inline pio_sm_config joybus_program_get_default_config(uint offset) {
Expand All @@ -70,6 +78,7 @@ static inline pio_sm_config joybus_program_get_config(PIO pio, uint sm, uint off
sm_config_set_out_pins(&c, pin, 1);
sm_config_set_set_pins(&c, pin, 1);
sm_config_set_in_pins(&c, pin);
sm_config_set_jmp_pin(&c, pin);
// Shift to left, no autopull, 9 bit
sm_config_set_out_shift(&c, false, false, 9);
// Shift to left, autopush, 8 bit
Expand Down
8 changes: 6 additions & 2 deletions src/joybus.pio
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
; Autopush with 8 bit ISR threshold
public read:
set pindirs 0 ; Set pin to input
wait 1 pin 0 ; Make sure the line is high before entering the loop
read_loop:
wait 0 pin 0 [T1 + T2 / 2 - 1] ; Wait for falling edge, then wait until halfway through the 2uS which represents the bit value
in pins, 1 ; Read bit value
wait 1 pin 0 ; Done reading, so make sure we wait for the line to go high again before restarting the loop
in pins, 1 [T2 / 2 + T3 / 2] ; Read bit value, then wait until halfway through the expected rising edge
jmp pin read_loop ; Continue the loop if there was a rising edge, if not then restart the loop
mov isr, null ; Line was pulled low, thus we should restart the loop by emptying the input shift register
wait 1 pin 0 ; Make sure we wait for the line to go high again before restarting the loop
jmp read_loop


Expand Down Expand Up @@ -51,6 +54,7 @@ static inline pio_sm_config joybus_program_get_config(PIO pio, uint sm, uint off
sm_config_set_out_pins(&c, pin, 1);
sm_config_set_set_pins(&c, pin, 1);
sm_config_set_in_pins(&c, pin);
sm_config_set_jmp_pin(&c, pin);
// Shift to left, no autopull, 9 bit
sm_config_set_out_shift(&c, false, false, 9);
// Shift to left, autopush, 8 bit
Expand Down