For this year’s RetroChallenge, I am hoping to interface an NES / Famicon controller to my RC2014 computer.
The NES controller has 8 buttons: Up, Down, Left, Right, Select, Start, A, and B.
The cable only has 7 pins, so how can it send up to 8 button presses using 7 wires?
Well, the NES controller only actually uses 5 of those wires. Two of those wires are GND and +5V. The other three are Latch, Clock, and Data.
Inside the controller, there is a 4021 shift register. When the Latch is pulsed from high to low, the 4021 captures the state of the buttons in 8 bits. The first capture bit will then be placed on the Data line. When Clock is pulsed from high to low, the 4021 will then shift the internal data one place forward, and the next bit will be placed on the Data line. When the next Clock pulse arrives, the next bit is sent, and so on. This needs to happen 8 times until all 8 bits have been sent. A low bit on the Data line means the button has been pressed; otherwise, the bit will remain high.
The bits are sent in the following order.
Bit
Button
0
A
1
B
2
Select
3
Start
4
Up
5
Down
6
Left
7
Right
Using software bit-banging, I should be able to send the Latch and Clock signals, and reassemble the received 8 bits back into a single byte.
This year, my plan is to interface a classic Nintento Entertainment System joypad to my RC2014.
There is already a module for reading Atari-style joysticks, but I don’t think there is anything for a NES controller.
There are 8 buttons on the NES controller, but the connector has only 7 pins. This means that there is something interesting going on, and it’s not just each button shorting one pin.
One thing I really wanted to do as part of this year’s Retro Challenge was to build my first custom PCB for the RC2014 computer.
I have chosen to use EasyEDA to design my PCB. I was heavily influenced by watching James Sharman’s excellent YouTube videos in which he designs his own CPU using EasyEDA. The alternative was KiCad, which seems more complex for a beginner.
Spencer has kindly put the specifications for a RC2014 module template up on the RC2014 website. I will try to follow this when it comes to laying out my PCB.
The Circuit
The RC2014 Peripheral Addressing didn’t originally target exact addresses, instead it used 74LS138 chips to target ranges of addresses.
The SC219 takes a different approach, and uses a 74HCT688 8-Bit Magnitude Comparator to target a specific address. I think this is the best approach for my circuit. Using switches for the comparison will mean I can easily change the matched address if there are conflicts with any other cards on my RC2014. I will use the IORQ line from the RC2014 as the OE (Output Enable) line. I can take the output from the 74HCT688 and use a 74HCT32 OR Gate with the RD line to know when the peripheral address I want to respond to is being used. I will use a 74HCT245 Octal Bus Transceiver to present data on the data pins when active.
In my initial post I mentioned I had to use a 74HCT14 Schmitt Trigger to invert the input from the rotary encoder and help produce a cleaner digital signal. This will be connected to the 74HCT245’s D0 to D5 pins. This will allow me to support two rotary encoders, and leave D6 and D7 spare. I will provide two blocks of input pins, one block for each rotary encoder. Each of these will support the CLK, DT, and SW pins, as well as GND and +5V.
I will add some debugging support to the circuit. I like the input LEDs on the SC219 so I will also add these to my circuit so I can see the incoming data from the rotary encoders. I think a breakout of the incoming data lines before the 74HCT245 will be useful to see what is going on, and also allow the board to be used as an input device when the rotary encoders aren’t being used. Finally, I saw a discussion on one of the Sinclair Spectrum groups about adding a hook to GND to attach an oscilliscope to when debugging. This could be very useful if the circuit doesn’t work, so I will add in provision for this.
This is my initial design.
The PCB
As I mentioned earlier, there is a specification for the shape and size of PCBs for the RC2014.
Laying out the board I have tried to use EasyEDA’s measuring tool to ensure this is correct.
Adding the curved corner I found tricky, but eventually I was able to do this by targetting the BoardOutLine layer in EasyEDA.
I read on various forums that adding a ground plane to a PCB was a good idea, so I have done this on both the top and bottom layer of my PCB.
As my circuit is reasonably simple, I used EasyEDA’s auto routing functionality. This worked quickly and I didn’t need to do any manual routing.
For manufacturing I decided to use JLCPCB as EasyEDA has built in support for this manufacturer.
The board itself cost $3.20 for 5. I paid for express delivery because if I have made a mistake and need to create a new revision, it could take several weeks to arrive. The total for the 5 PCBs, delivery, and tax was £20.21. These are due for delivery on October 8th 2024.
JLCPCB adds a manufacturing code to each PCB. You can specify where this appears by adding the text “JLCJLCJLCJLC” to one of the silk layers. I put mine on the BottomSilkLayer so it’s not forward facing.
This is the final layout for my board. I can’t wait for it to arrive so I can solder it up and see if it works.
I now want to try reading it on the RC2014 and using it to control some output.
I’m using the SC219 Digital I/O Board on the RC2014 to accept inputs from a rotary encoder. I have mine mapped to I/O address port 0. Using Microsoft BASIC I can read this using the INP(0) statement. This will return a byte representing the values of D0 to D7 on the input port.
I am using D0 for the CLK, D1 for DT, and D2 for SW. D3 to D7 are not used at the moment. I am only interested in D0 and D1 for the rotary motion. I can use AND statements to mask bits when I’m checking the input value. So to check if D0 was 1 or 0, I could use INP(0) AND 1. This would return either 0 or 1. To check D1, I could use INP(0) AND 2. This would return either 0 or 2.
The following UML activity diagram shows at a high level how I need my BASIC program to operate.
The Microsoft BASIC on the RC2014 is old and limited compared to modern languages, but it is functional.
To save space, I’ll use variable A for CLK, and B for DT. I’ll need to keep track of the last state of A so I know when it has changed. To do this I’ll create a variable LAST_A. Finally, to keep track of the INP(0) result, I’ll create variable INPUT.
When I detect a change in A, I need to look to see if B matches A. If it does, then I know the encoder is being turned to the right. If it doesn’t, then it is being turned to the left.
Here is the BASIC code I came up with to do this. When it detects a change, it prints either “Left” or “Right” to the console.
10 LET LAST_A = 0
20 LET IN = INP(0)
30 LET A = IN AND 1
40 IF A = LASTA THEN GOTO 200
50 LET B = IN AND 2
60 IF (A=1 AND B=2) OR (B=0 AND A=0) THEN GOTO 100
80 PRINT "Right"
90 GOTO 200
100 PRINT "Left"
200 LET LAST_A = A
210 GOTO 20
The SC219 has digital outputs as well as inputs. Using the built-in output LEDs, I can move a dot left or right depending on the input from the rotary encoder.
The 8 output LEDs represent each bit of a byte. So we have the values 1, 2, 4, 8, 16, 32, 64, and 128 represented on bits D0 to D7. This is 2 to the power X, where X is between 0 and 7. We can use the ^ (power) operator in BASIC to help set these bits.
So we need to have a variable, let’s call it COUNTER, to keep track of which power we are currently at. We can say turning the rotary encoder left will increment COUNTER by 1. Turning the rotary encoderrigtht will decrement COUNTER by 1. We need some guards, so if COUNTER goes below 0, we set it to 7. If it goes above 7, we set it to 0. We set one of the LEDs by using OUT 0,2^COUNTER. This means when we turn the rotary encoder, we can move the LED.
Here is the BASIC code I wrote to do this.
10 LET COUNTER = 0
20 LET LAST_A = 0
30 LET IN = INP(0)
40 OUT 0,2^COUNTER
50 LET A = IN AND 1
60 IF A = LASTA THEN GOTO 200
70 LET B = IN AND 2
80 IF (A=1 AND B=2) OR (B=0 AND A=0) THEN GOTO 150
90 IF COUNTER <= 0 THEN GOTO 120
100 LET COUNTER = COUNTER - 1
110 GOTO 130
120 LET COUNTER = 7
130 PRINT "Right"
140 GOTO 200
150 IF COUNTER >= 7 THEN GOTO 180
160 LET COUNTER = COUNTER + 1
170 GOTO 190
180 LET COUNTER = 0
190 PRINT "Left"
200 LET LAST_A = A
210 GOTO 30
Here is a short video of it in action.
Cookie Consent
We use cookies to improve your experience on our site. By using our site, you consent to cookies.
Websites store cookies to enhance functionality and personalise your experience. You can manage your preferences, but blocking some cookies may impact site performance and services.
Essential cookies enable basic functions and are necessary for the proper function of the website.
Name
Description
Duration
Cookie Preferences
This cookie is used to store the user's cookie consent preferences.
30 days
Statistics cookies collect information anonymously. This information helps us understand how visitors use our website.
Google Analytics is a powerful tool that tracks and analyzes website traffic for informed marketing decisions.
Contains information related to marketing campaigns of the user. These are shared with Google AdWords / Google Ads when the Google Ads and Google Analytics accounts are linked together.
90 days
__utma
ID used to identify users and sessions
2 years after last activity
__utmt
Used to monitor number of Google Analytics server requests
10 minutes
__utmb
Used to distinguish new sessions and visits. This cookie is set when the GA.js javascript library is loaded and there is no existing __utmb cookie. The cookie is updated every time data is sent to the Google Analytics server.
30 minutes after last activity
__utmc
Used only with old Urchin versions of Google Analytics and not with GA.js. Was used to distinguish between new sessions and visits at the end of a session.
End of session (browser)
__utmz
Contains information about the traffic source or campaign that directed user to the website. The cookie is set when the GA.js javascript is loaded and updated when data is sent to the Google Anaytics server
6 months after last activity
__utmv
Contains custom information set by the web developer via the _setCustomVar method in Google Analytics. This cookie is updated every time new data is sent to the Google Analytics server.
2 years after last activity
__utmx
Used to determine whether a user is included in an A / B or Multivariate test.
18 months
_ga
ID used to identify users
2 years
_gali
Used by Google Analytics to determine which links on a page are being clicked
30 seconds
_ga_
ID used to identify users
2 years
_gid
ID used to identify users for 24 hours after last activity
24 hours
_gat
Used to monitor number of Google Analytics server requests when using Google Tag Manager
1 minute
Marketing cookies are used to follow visitors to websites. The intention is to show ads that are relevant and engaging to the individual user.
A video-sharing platform for users to upload, view, and share videos across various genres and topics.
Registers a unique ID on mobile devices to enable tracking based on geographical GPS location.
1 day
VISITOR_INFO1_LIVE
Tries to estimate the users' bandwidth on pages with integrated YouTube videos. Also used for marketing
179 days
PREF
This cookie stores your preferences and other information, in particular preferred language, how many search results you wish to be shown on your page, and whether or not you wish to have Google’s SafeSearch filter turned on.
10 years from set/ update
YSC
Registers a unique ID to keep statistics of what videos from YouTube the user has seen.
Session
DEVICE_INFO
Used to detect if the visitor has accepted the marketing category in the cookie banner. This cookie is necessary for GDPR-compliance of the website.
179 days
LOGIN_INFO
This cookie is used to play YouTube videos embedded on the website.
2 years
VISITOR_PRIVACY_METADATA
Youtube visitor privacy metadata cookie
180 days
You can find more information in our Cookie Policy and .