├── LICENSE ├── Makefile ├── README.md ├── apple1bin.cfg ├── apple1rom.a ├── doc ├── 6502pinout.jpg ├── Pi6502.jpg ├── Pi6502PinoutStickers.xlsx ├── Pi6502Schematic.jpg ├── PinConnections.jpg └── RpiHeaderPinout.jpg ├── mailbox.c ├── mailbox.h └── pi6502.c /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Builds pi6502 executable and apple1rom.bin from source code. 2 | # requires gcc cc65 libncurses5-dev libncursesw5-dev 3 | 4 | CFLAGS=-O3 -W -Wall -std=c99 -D_XOPEN_SOURCE=500 -g 5 | 6 | all: pi6502 apple1rom.bin 7 | 8 | pi6502: pi6502.o mailbox.o -lncurses 9 | 10 | apple1rom.bin: apple1rom.a 11 | ca65 apple1rom.a -o apple1rom.o 12 | cl65 apple1rom.o -C apple1bin.cfg -o apple1rom.bin 13 | 14 | clean: 15 | rm -f pi6502 apple1rom.bin *.o 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Raspberry Pi 6502 Processor Hat 2 | 3 | A different Apple 1 replica. The picture below shows a Rockwell 6502 processor sitting on top of Raspberry Pi Zero W2. The processor 'hat' is a prototype board and uses wirewrap to connect the 6502 CPU to the Raspberry Pi header. 4 | 5 | The processor is running at about 1.7 MHz (faster than the original Apple 1) and the Raspberry Pi is implementing the memory bus and system clock to create the functions of an Apple 1. The clock speed can be slowed to match the original 1.023 Mhz of the Apple 1 if desired. 6 | 7 | ![6502 Hat in action](doc/Pi6502.jpg) 8 | 9 | 10 | ## Is a Pi fast enough? 11 | I found this cool pi86 project https://github.com/homebrew8088/pi86 which implements an IBM XT system with a 8088 processor on a Pi hat. The challenge I noted with the pi86 project was that the 8088 processor was not clocking higher than about 300 KHz. Its slow, see the video below showing some game play. https://youtu.be/drXkA7xGNrc?t=237 12 | 13 | A part of the objective of this project was to see if the Pi can operate fast enough to run the memory bus and create a 4.7 MHz clock. To do this you have to memory map the GPIOs and a few other tricks to save time. I used a 6502 to prototype this while I wait for my Pi86 board to arrive. I'll post a video of the Apple 1 working soon. 14 | 15 | * Raspberry Pi Zero W2, working Apple 1 max clock **1.7 MHz** 16 | * Raspberry Pi 4 (4GB), working Apple 1 max clock **2.6 MHz** 17 | 18 | These results are pretty good considering the Rockwell R6502P I am using is only rated to 1 MHz. I have ordered some Western Design Centre W65C02S6TPG-14 which are 6502 processors rated up to 8 MHz at 3.3v. There is likely some multithreading and code level optimizations that can get us up to the 4.7 MHz level. 19 | 20 | ## Pi6502 Schematic 21 | 22 | ### 6502 pinout 23 | ![6502pins](doc/6502pinout.jpg) 24 | 25 | ### Pin Connections 26 | ![6502 Hat pin connections](doc/PinConnections.jpg) 27 | 28 | ### Wiring 29 | ![6502 Hat pin connections](doc/Pi6502Schematic.jpg "Link to wiring diagram") 30 | 31 | 32 | ## Pre-requisities 33 | 34 | You need a Raspberry Pi. I used a Pi Zero W 2 with Raspbian installed to build this project. Run the following command to install the necessary packages. 35 | 36 | ``` 37 | sudo apt-get install gcc make cc65 libncurses5-dev libncursesw5-dev 38 | ``` 39 | If you want to install Apple Integer Basic then download basic.rom or apple1basic.bin from the following source and place in the root folder of this project. The code expects a 4096 byte rom image called apple1basic.bin which it will load into addres E000. To run based execute `E000R` at the prompt. 40 | 41 | * https://github.com/jscrane/Apple1/blob/master/images/basic.rom 42 | * http://retro.hansotten.nl/6502-sbc/apple-1/ 43 | 44 | ## Building 45 | The build is setup to compile for a Raspberry Pi 3 or Raspberry Pi Zero W2. If you want to compile for a Raspberry Pi 4 you need to edit `line 42` of `pi6502.c` and set the Raspberry Pi model version. Its possible to write code to detect the Pi model but I wanted to keep it simple. 46 | ``` 47 | # define PI_VERSION 3 48 | ``` 49 | 50 | To build its as simple as running make, the output is a executable called pi6502. 51 | ``` 52 | make 53 | ``` 54 | 55 | ## Running 56 | You need to run the program as root so that it has access to the GPIOs 57 | 58 | ``` 59 | sudo ./pi6502 60 | ``` 61 | 62 | The system should startup and show a `\` prompt. 63 | 64 | There is no physical reset switch but you can reset the system by typing/pressing `Ctrl+A` 65 | 66 | 67 | Try the following 68 | 69 | ``` 70 | \ 71 | E000R 72 | 73 | E000: 4C 74 | >10 PRINT "HELLO WORLD" 75 | >20 GOTO 10 76 | >RUN 77 | ``` 78 | 79 | The program will run in an infinte loop so to stop it press `Ctrl+A` 80 | 81 | 82 | 83 | ## Troubleshooting 84 | 85 | I spent time debugging a particular Raspberry Pi 4 that was not working. Very weird symptons rather than a `\` prompt it showed `L`, nothing was working right. After hours I realised the case *Argon ONE V2* uses GPIO 4 , 22 and 23 for its fan control and unforutunaltey this means this case is not compatible with this project. I fixed the issue by removing the case from the Pi. This project uses all the GPIOs so there are none spare for other things... 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /apple1bin.cfg: -------------------------------------------------------------------------------- 1 | MEMORY { 2 | RAM: start = $0100, size = $7EFF, file = %O; 3 | } 4 | SEGMENTS { 5 | CODE: load = RAM, type = ro; 6 | DATA: load = RAM, type = rw; 7 | } 8 | -------------------------------------------------------------------------------- /apple1rom.a: -------------------------------------------------------------------------------- 1 | ; The WOZ Monitor for the Apple 1 2 | ; Written by Steve Wozniak in 1976 3 | 4 | 5 | ; Page 0 Variables 6 | 7 | XAML = $24 ; Last "opened" location Low 8 | XAMH = $25 ; Last "opened" location High 9 | STL = $26 ; Store address Low 10 | STH = $27 ; Store address High 11 | L = $28 ; Hex value parsing Low 12 | H = $29 ; Hex value parsing High 13 | YSAV = $2A ; Used to see if hex value is given 14 | MODE = $2B ; $00=XAM, $7F=STOR, $AE=BLOCK XAM 15 | 16 | 17 | ; Other Variables 18 | 19 | IN = $0200 ; Input buffer to $027F 20 | KBD = $D010 ; PIA.A keyboard input 21 | KBDCR = $D011 ; PIA.A keyboard control register 22 | DSP = $D012 ; PIA.B display output register 23 | DSPCR = $D013 ; PIA.B display control register 24 | 25 | .org $FF00 26 | .export RESET 27 | 28 | RESET: CLD ; Clear decimal arithmetic mode. 29 | CLI 30 | LDY #$7F ; Mask for DSP data direction register. 31 | STY DSP ; Set it up. 32 | LDA #$A7 ; KBD and DSP control register mask. 33 | STA KBDCR ; Enable interrupts, set CA1, CB1, for 34 | STA DSPCR ; positive edge sense/output mode. 35 | NOTCR: CMP #'_'+$80 ; "_"? 36 | BEQ BACKSPACE ; Yes. 37 | CMP #$9B ; ESC? 38 | BEQ ESCAPE ; Yes. 39 | INY ; Advance text index. 40 | BPL NEXTCHAR ; Auto ESC if > 127. 41 | ESCAPE: LDA #'\'+$80 ; "\\". 42 | JSR ECHO ; Output it. 43 | GETLINE: LDA #$8D ; CR. 44 | JSR ECHO ; Output it. 45 | LDY #$01 ; Initialize text index. 46 | BACKSPACE: DEY ; Back up text index. 47 | BMI GETLINE ; Beyond start of line, reinitialize. 48 | NEXTCHAR: LDA KBDCR ; Key ready? 49 | BPL NEXTCHAR ; Loop until ready. 50 | LDA KBD ; Load character. B7 should be ‘1’. 51 | STA IN,Y ; Add to text buffer. 52 | JSR ECHO ; Display character. 53 | CMP #$8D ; CR? 54 | BNE NOTCR ; No. 55 | LDY #$FF ; Reset text index. 56 | LDA #$00 ; For XAM mode. 57 | TAX ; 0->X. 58 | SETSTOR: ASL ; Leaves $7B if setting STOR mode. 59 | SETMODE: STA MODE ; $00=XAM $7B=STOR $AE=BLOK XAM 60 | BLSKIP: INY ; Advance text index. 61 | NEXTITEM: LDA IN,Y ; Get character. 62 | CMP #$8D ; CR? 63 | BEQ GETLINE ; Yes, done this line. 64 | CMP #'.'+$80 ; "."? 65 | BCC BLSKIP ; Skip delimiter. 66 | BEQ SETMODE ; Yes. Set STOR mode. 67 | CMP #':'+$80 ; ":"? 68 | BEQ SETSTOR ; Yes. Set STOR mode. 69 | CMP #'R'+$80 ; "R"? 70 | BEQ RUN ; Yes. Run user program. 71 | STX L ; $00-> L. 72 | STX H ; and H. 73 | STY YSAV ; Save Y for comparison. 74 | NEXTHEX: LDA IN,Y ; Get character for hex test. 75 | EOR #$B0 ; Map digits to $0-9. 76 | CMP #$0A ; Digit? 77 | BCC DIG ; Yes. 78 | ADC #$88 ; Map letter "A"-"F" to $FA-FF. 79 | CMP #$FA ; Hex letter? 80 | BCC NOTHEX ; No, character not hex. 81 | DIG: ASL 82 | ASL ; Hex digit to MSD of A. 83 | ASL 84 | ASL 85 | LDX #$04 ; Shift count. 86 | HEXSHIFT: ASL ; Hex digit left, MSB to carry. 87 | ROL L ; Rotate into LSD. 88 | ROL H ; Rotate into MSD’s. 89 | DEX ; Done 4 shifts? 90 | BNE HEXSHIFT ; No, loop. 91 | INY ; Advance text index. 92 | BNE NEXTHEX ; Always taken. Check next char for hex. 93 | NOTHEX: CPY YSAV ; Check if L, H empty (no hex digits). 94 | BEQ ESCAPE ; Yes, generate ESC sequence. 95 | BIT MODE ; Test MODE byte. 96 | BVC NOTSTOR ; B6=0 STOR 1 for XAM & BLOCK XAM 97 | LDA L ; LSD’s of hex data. 98 | STA (STL,X) ; Store at current ‘store index’. 99 | INC STL ; Increment store index. 100 | BNE NEXTITEM ; Get next item. (no carry). 101 | INC STH ; Add carry to ‘store index’ high order. 102 | TONEXTITEM: JMP NEXTITEM ; Get next command item. 103 | RUN: JMP (XAML) ; Run at current XAM index. 104 | NOTSTOR: BMI XAMNEXT ; B7=0 for XAM, 1 for BLOCK XAM. 105 | LDX #$02 ; Byte count. 106 | SETADR: LDA L-1,X ; Copy hex data to 107 | STA STL-1,X ; ‘store index’. 108 | STA XAML-1,X ; And to ‘XAM index’. 109 | DEX ; Next of 2 bytes. 110 | BNE SETADR ; Loop unless X=0. 111 | NXTPRNT: BNE PRDATA ; NE means no address to print. 112 | LDA #$8D ; CR. 113 | JSR ECHO ; Output it. 114 | LDA XAMH ; ‘Examine index’ high-order byte. 115 | JSR PRBYTE ; Output it in hex format. 116 | LDA XAML ; Low-order ‘examine index’ byte. 117 | JSR PRBYTE ; Output it in hex format. 118 | LDA #':'+$80 ; ":". 119 | JSR ECHO ; Output it. 120 | PRDATA: LDA #$A0 ; Blank. 121 | JSR ECHO ; Output it. 122 | LDA (XAML,X) ; Get data byte at ‘examine index’. 123 | JSR PRBYTE ; Output it in hex format. 124 | XAMNEXT: STX MODE ; 0->MODE (XAM mode). 125 | LDA XAML 126 | CMP L ; Compare ‘examine index’ to hex data. 127 | LDA XAMH 128 | SBC H 129 | BCS TONEXTITEM ; Not less, so no more data to output. 130 | INC XAML 131 | BNE MOD8CHK ; Increment ‘examine index’. 132 | INC XAMH 133 | MOD8CHK: LDA XAML ; Check low-order ‘examine index’ byte 134 | AND #$07 ; For MOD 8=0 135 | BPL NXTPRNT ; Always taken. 136 | PRBYTE: PHA ; Save A for LSD. 137 | LSR 138 | LSR 139 | LSR ; MSD to LSD position. 140 | LSR 141 | JSR PRHEX ; Output hex digit. 142 | PLA ; Restore A. 143 | PRHEX: AND #$0F ; Mask LSD for hex print. 144 | ORA #'0'+$80 ; Add "0". 145 | CMP #$BA ; Digit? 146 | BCC ECHO ; Yes, output it. 147 | ADC #$06 ; Add offset for letter. 148 | ECHO: BIT DSP ; bit (B7) cleared yet? 149 | BMI ECHO ; No, wait for display. 150 | STA DSP ; Output character. Sets DA. 151 | RTS ; Return. 152 | 153 | BRK ; unused 154 | BRK ; unused 155 | 156 | ; Interrupt Vectors 157 | 158 | .WORD $0F00 ; NMI 159 | .WORD RESET ; RESET 160 | .WORD $0000 ; BRK/IRQ -------------------------------------------------------------------------------- /doc/6502pinout.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robin7g/pi6502/2164e807e17458f0026159f2ea649270380ff299/doc/6502pinout.jpg -------------------------------------------------------------------------------- /doc/Pi6502.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robin7g/pi6502/2164e807e17458f0026159f2ea649270380ff299/doc/Pi6502.jpg -------------------------------------------------------------------------------- /doc/Pi6502PinoutStickers.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robin7g/pi6502/2164e807e17458f0026159f2ea649270380ff299/doc/Pi6502PinoutStickers.xlsx -------------------------------------------------------------------------------- /doc/Pi6502Schematic.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robin7g/pi6502/2164e807e17458f0026159f2ea649270380ff299/doc/Pi6502Schematic.jpg -------------------------------------------------------------------------------- /doc/PinConnections.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robin7g/pi6502/2164e807e17458f0026159f2ea649270380ff299/doc/PinConnections.jpg -------------------------------------------------------------------------------- /doc/RpiHeaderPinout.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/robin7g/pi6502/2164e807e17458f0026159f2ea649270380ff299/doc/RpiHeaderPinout.jpg -------------------------------------------------------------------------------- /mailbox.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2012, Broadcom Europe Ltd. 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright 10 | notice, this list of conditions and the following disclaimer in the 11 | documentation and/or other materials provided with the distribution. 12 | * Neither the name of the copyright holder nor the 13 | names of its contributors may be used to endorse or promote products 14 | derived from this software without specific prior written permission. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | 27 | Original: 28 | https://github.com/raspberrypi/userland/blob/master/host_applications/linux/apps/hello_pi/hello_fft/mailbox.c 29 | */ 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | #include "mailbox.h" 42 | 43 | #define PAGE_SIZE (4*1024) 44 | 45 | void *mapmem(unsigned base, unsigned size) 46 | { 47 | int mem_fd; 48 | unsigned offset = base % PAGE_SIZE; 49 | base = base - offset; 50 | size = size + offset; 51 | /* open /dev/mem */ 52 | if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 53 | printf("can't open /dev/mem\nThis program should be run as root. Try prefixing command with: sudo\n"); 54 | exit (-1); 55 | } 56 | void *mem = mmap( 57 | 0, 58 | size, 59 | PROT_READ|PROT_WRITE, 60 | MAP_SHARED/*|MAP_FIXED*/, 61 | mem_fd, 62 | base); 63 | #ifdef DEBUG 64 | printf("base=0x%x, mem=%p\n", base, mem); 65 | #endif 66 | if (mem == MAP_FAILED) { 67 | printf("mmap error %p\n", mem); 68 | exit (-1); 69 | } 70 | close(mem_fd); 71 | return (char *)mem + offset; 72 | } 73 | 74 | void unmapmem(void *addr, unsigned size) 75 | { 76 | const intptr_t offset = (intptr_t)addr % PAGE_SIZE; 77 | addr = (char *)addr - offset; 78 | size = size + offset; 79 | int s = munmap(addr, size); 80 | if (s != 0) { 81 | printf("munmap error %d\n", s); 82 | exit (-1); 83 | } 84 | } 85 | 86 | /* 87 | * use ioctl to send mbox property message 88 | */ 89 | 90 | static int mbox_property(int file_desc, void *buf) 91 | { 92 | int ret_val = ioctl(file_desc, IOCTL_MBOX_PROPERTY, buf); 93 | 94 | if (ret_val < 0) { 95 | printf("ioctl_set_msg failed:%d\n", ret_val); 96 | } 97 | 98 | #ifdef DEBUG 99 | unsigned *p = buf; int i; unsigned size = *(unsigned *)buf; 100 | for (i=0; i 32 | 33 | #define MAJOR_NUM 100 34 | #define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *) 35 | #define DEVICE_FILE_NAME "/dev/vcio" 36 | 37 | int mbox_open(); 38 | void mbox_close(int file_desc); 39 | 40 | unsigned get_version(int file_desc); 41 | unsigned mem_alloc(int file_desc, unsigned size, unsigned align, unsigned flags); 42 | unsigned mem_free(int file_desc, unsigned handle); 43 | unsigned mem_lock(int file_desc, unsigned handle); 44 | unsigned mem_unlock(int file_desc, unsigned handle); 45 | void *mapmem(unsigned base, unsigned size); 46 | void unmapmem(void *addr, unsigned size); 47 | 48 | unsigned execute_code(int file_desc, unsigned code, unsigned r0, unsigned r1, unsigned r2, unsigned r3, unsigned r4, unsigned r5); 49 | unsigned execute_qpu(int file_desc, unsigned num_qpus, unsigned control, unsigned noflush, unsigned timeout); 50 | unsigned qpu_enable(int file_desc, unsigned enable); 51 | -------------------------------------------------------------------------------- /pi6502.c: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * This code is a part of Raspberry Pi 6502 Processor Hat project 4 | * and implements an Apple 1 replica using a real 6502 processor 5 | * where the Raspberry Pi acts as the memory, I/O bus and 6 | * system clock for the CPU. 7 | * 8 | * This file is Copyright 2022 Robin Grosset, all rights reserved. 9 | * 10 | * Code is provided AS IS in the hope it is useful under the terms of 11 | * Apache License 2.0 12 | * 13 | * Note that files included in this project may be subject to different 14 | * license terms which are marked. 15 | * 16 | * mailbox.h, mailbox.c are copyright Broadcom Europe Ltd and subject to 17 | * terms included in those files. 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | // Physical Memory Allocation, from raspberrypi/userland demo. 34 | #include "mailbox.h" 35 | 36 | 37 | // Don't bother auto-detecting but have it a compile-time option. 38 | // Change if you want to compile for another Raspberry Pi board 39 | // Originally developed on a Raspberry Pi Zero W2 which is 40 | // essentially a PI 3 41 | #ifndef PI_VERSION 42 | # define PI_VERSION 3 43 | #endif 44 | 45 | #define BCM2708_PI1_PERI_BASE 0x20000000 46 | #define BCM2709_PI2_PERI_BASE 0x3F000000 47 | #define BCM2711_PI4_PERI_BASE 0xFE000000 48 | 49 | // General, Pi-specific setup. 50 | #if PI_VERSION == 1 51 | # define PERI_BASE BCM2708_PI1_PERI_BASE 52 | #elif PI_VERSION == 2 || PI_VERSION == 3 53 | # define PERI_BASE BCM2709_PI2_PERI_BASE 54 | #else 55 | # define PERI_BASE BCM2711_PI4_PERI_BASE 56 | #endif 57 | 58 | #define PAGE_SIZE 4096 59 | 60 | // GPIO specific defines 61 | #define GPIO_REGISTER_BASE 0x200000 62 | #define GPIO_SET_OFFSET 0x1C 63 | #define GPIO_CLR_OFFSET 0x28 64 | #define PHYSICAL_GPIO_BUS (0x7E000000 + GPIO_REGISTER_BASE) 65 | 66 | #define GPIO_LEV0_OFFSET 0x34 67 | #define GPIO_LEV1_OFFSET 0x38 68 | 69 | // ---- Memory mappping defines 70 | #define BUS_TO_PHYS(x) ((x)&~0xC0000000) 71 | 72 | // ---- Memory allocating defines 73 | // https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface 74 | #define MEM_FLAG_DIRECT (1 << 2) 75 | #define MEM_FLAG_COHERENT (2 << 2) 76 | #define MEM_FLAG_L1_NONALLOCATING (MEM_FLAG_DIRECT | MEM_FLAG_COHERENT) 77 | 78 | 79 | 80 | 81 | // Return a pointer to a periphery subsystem register. 82 | static void *mmap_bcm_register(off_t register_offset) { 83 | const off_t base = PERI_BASE; 84 | 85 | int mem_fd; 86 | if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) { 87 | perror("can't open /dev/mem: "); 88 | fprintf(stderr, "You need to run this as root!\n"); 89 | return NULL; 90 | } 91 | 92 | uint32_t *result = 93 | (uint32_t*) mmap(NULL, // Any adddress in our space will do 94 | PAGE_SIZE, 95 | PROT_READ|PROT_WRITE, // Enable r/w on GPIO registers. 96 | MAP_SHARED, 97 | mem_fd, // File to map 98 | base + register_offset // Offset to bcm register 99 | ); 100 | close(mem_fd); 101 | 102 | if (result == MAP_FAILED) { 103 | fprintf(stderr, "mmap error %p\n", result); 104 | return NULL; 105 | } 106 | return result; 107 | } 108 | 109 | void set_databus_pins_to_output(volatile uint32_t *gpio_registerset){ 110 | // Objective: Set pins 0 to 7 to output mode as fast as possible. 111 | // 112 | // How: The gpio_registerset address contains 1st 10 GPIO pins configs 113 | // in bits 0 to 29 each IO pin has 3 configuration bits. To set to 114 | // output I need to set the following bit pattern '001'. Note bits 30 115 | // and 31 are not used. To set the first 8 pins to output leaving 116 | // others as input I need to set a binary pattern like the following. 117 | // 118 | // Pin# 10 9 8 7 6 5 4 3 2 1 0 119 | // Binary 000 000 000 001 001 001 001 001 001 001 001 120 | // 121 | // which is 122 | // 123 | // 1+8+64+512+4096+32768+262144+2097152 = 2396745 124 | // 125 | // Magic number time! 126 | // A single assignment sets all pins to the right state. 127 | *(gpio_registerset) = 2396745; 128 | } 129 | 130 | void set_databus_pins_to_input(volatile uint32_t *gpio_registerset){ 131 | // pins 0 to 7 set as fast as possible 132 | // This is easier the bit pattern of input is '000' 133 | // so just set everything to zero 134 | *(gpio_registerset) = 0; 135 | } 136 | 137 | 138 | void initialize_gpio_for_output(volatile uint32_t *gpio_registerset, int bit) { 139 | *(gpio_registerset+(bit/10)) &= ~(7<<((bit%10)*3)); // prepare: set as input 140 | *(gpio_registerset+(bit/10)) |= (1<<((bit%10)*3)); // set as output. 141 | } 142 | 143 | // Data Lines D0-D7 connect to GPIO 0-7 144 | // Address Lines A0-A15 connect to GPIO 8-23 145 | // This wiring makes the coding easier. 146 | // GPIO defines outside of these 147 | #define GPIO_PH0 24 148 | #define GPIO_RESET 25 149 | #define GPIO_PH2 26 150 | #define GPIO_RW 27 151 | #define GPIO_TIMEWASTE 26 152 | 153 | // Apple 1 I/O Address Registers 154 | #define DSP 0xD012 155 | #define DSPCR 0xD013 156 | #define KBD 0xD010 157 | #define KBDCR 0xD011 158 | 159 | // Control Key Macro 160 | #define ctrl(x) ((x) & 0x1f) 161 | 162 | 163 | void perform_memory_read(unsigned char * memory, int address,volatile uint32_t *set_reg,volatile uint32_t *clr_reg ){ 164 | unsigned char readDataByte = memory[address]; 165 | // set output value 166 | *set_reg = readDataByte&0xff; 167 | *clr_reg = ~readDataByte&0xff; 168 | 169 | if (address >=0xD000 && address <=0xDFFF) { 170 | // Reading keyboard reset CR after 171 | if (address == 0xD010) { 172 | memory[0xD010] = 0x00; 173 | memory[0xD011] = 0x00; 174 | } 175 | } else { 176 | //if (busDebug) 177 | // printf("o"); 178 | } 179 | } 180 | void perform_memory_write(unsigned char * memory, int address, char writeDataByte, WINDOW * console ){ 181 | // This code implements memory writes inclyding I/O write operations, 182 | // and checks ROM vs RAM memory writes. 183 | if (address >=0xD000 && address <=0xDFFF) { 184 | // Writing to I/O 185 | memory[address] = writeDataByte; 186 | char outputChar = writeDataByte & 0x7F; 187 | if (address == 0xD012){ 188 | switch (outputChar){ 189 | case 0: 190 | case 0x7f: 191 | // No nuffin 192 | break; 193 | case 0xA: //\n 194 | break; 195 | case 0xD: //\r 196 | waddch(console, '\n'); 197 | wrefresh(console); 198 | break; 199 | default: 200 | waddch(console, outputChar); 201 | wrefresh(console); 202 | break; 203 | } 204 | // set memory to 00 as character has been output 205 | memory[address] = 0x00; 206 | } 207 | } else if (address >= 0xE000 && address <= 0xEFFF) { 208 | // Ignore writes to the 209 | // Apple 1 Basic ROM area. 210 | } else if (address >= 0 && address<0xFF00) { 211 | // Normal write to RAM 212 | memory[address] = writeDataByte; 213 | } else { 214 | printf("Illegal memory write to address 0x%04x",address); 215 | } 216 | } 217 | 218 | void reset_sequence(volatile uint32_t *set_reg, volatile uint32_t *clr_reg, int delay) { 219 | 220 | int startup, loop; 221 | // 6502 RESET Interrupt process. 222 | 223 | *set_reg = (1<>27) & 0x01 ? 'R' : 'W' ; 324 | int address = ((*lev0_reg)>>8) &0xffff; 325 | 326 | *set_reg = (1<