((section 2 "Outdated egg!" (p "This is an egg for CHICKEN 4, the unsupported old release.  You're almost certainly looking for " (int-link "/eggref/5/sixtyfive-oh-two" "the CHICKEN 5 version of this egg") ", if it exists.") (p "If it does not exist, there may be equivalent functionality provided by another egg; have a look at the " (link "https://wiki.call-cc.org/chicken-projects/egg-index-5.html" "egg index") ". Otherwise, please consider porting this egg to the current version of CHICKEN.") (tags "eggs") (toc)) (section 2 "sixtyfive-oh-two" (section 3 "Introduction" (p "An emulator for the " (link "https://en.wikipedia.org/wiki/6502" "6502") " CPU.") (p "Only the pure CPU is emulated, no hardware-interaction model is provided.") (p "The opcode-interpreter is implemented in " (int-link "crunch") ", and so a C++ compiler must be installed on your system. The interpreter-code is pre-compiled and included in the egg as compiling it takes quite a while. Compiling the C++ code needs " (tt "crunch.h") ", so the " (int-link "crunch") " extension must be installed.")) (section 3 "Procedures" (def (sig (procedure "(make-processor #!key memory registers)" (id make-processor))) (p "Returns a record structure representing a single 6502 processor. " (tt "memory") " should be an u8vector that is used as RAM, usually of length 65546. " (tt "registers") " should be an 7 element u8vector, where the elements hold the values of the registers, according to the following table:") (table (tr (th "Index") (th "Register")) "\n" (tr (td "0") (td (tt "A"))) "\n" (tr (td "1") (td (tt "X"))) "\n" (tr (td "2") (td (tt "Y"))) "\n" (tr (td "3") (td (tt "P"))) "\n" (tr (td "4") (td (tt "S"))) "\n" (tr (td "5") (td (tt "PC") " (low byte)")) "\n" (tr (td "6") (td (tt "PC") " (high byte)")))) (def (sig (procedure "(processor? X)" (id processor?))) (p "Returns " (tt "#t") " if " (tt "X") " is a " (tt "processor") " record or " (tt "#f") " otherwise.")) (def (sig (procedure "(processor-memory P)" (id processor-memory)) (procedure "(processor-memory-set! P U8VECTOR)" (id processor-memory-set!)) (procedure "(processor-registers P)" (id processor-registers)) (procedure "(processor-registers-set! P U8VECTOR)" (id processor-registers-set!))) (p "Accessors for the " (tt "processor") " record.")) (def (sig (procedure "(reset! P)" (id reset!))) (p "Resets the CPU represented by P by setting " (tt "A") ", " (tt "X") ", " (tt "Y") " to zero, " (tt "P") " to " (tt "#x20") ", " (tt "S") " to " (tt "#xff") " and " (tt "PC") " to the 16-bit address stored at location " (tt "#xfffc") ".")) (def (sig (procedure "(execute! P #!key cycles)" (id execute!))) (p "Executes instructions for processor " (tt "P") " with the state currently active in the processor record. Execution continues endlessly or after at least " (tt "cycles") " CPU cycles have been consumed (instructions always run to completion).")) (def (sig (procedure "(interrupt! P VEC)" (id interrupt!))) (p "Triggers an interrupt for processor " (tt "P") " by pushing the " (tt "PC") " and " (tt "P") " registers, setting the interrupt flag and continuing execution at the address stored at the memory location given in " (tt "VEC") "."))) (section 3 "Examples" (p "Say you need a fast way of multiplying an 8-bit quantity with 10, then you could do:") (highlight scheme "(use sixtyfive-oh-two srfi-4 lolevel utils miscmacros)\n\n;; Fast Multiply by 10\n;; By Leo Nechaev (leo@ogpi.orsk.ru), 28 October 2000.\n\n; * = $1000\n; LDA #18\n; JSR MULT10\n; L1:\n; JMP L1\n; MULT10:\n; ASL A         ;multiply by 2\n; STA TEMP    ;temp store in TEMP\n; ASL A        ;again multiply by 2 (*4)\n; ASL A        ;again multiply by 2 (*8)\n; CLC\n; ADC TEMP    ;as result, A = x*8 + x*2\n; RTS\n\n(define mult10\n  '#${a9 12\n      20 08 10\n      4c 05 10\n      0a\n      8d 00 20\n      0a\n      0a\n      18\n      6d 00 20\n      60})\n\n(define p (make-processor))\n(move-memory! mult10 (processor-memory p) (blob-size mult10) 0 #x1000)\n(define regs (processor-registers p))\n(u8vector-set! regs 5 #x00)\t\t\t; PC = #x1000\n(u8vector-set! regs 6 #x10)\n(execute! p cycles: 100)\n(pp regs)\n(assert (= 180 (u8vector-ref regs 0)))") (p "The " (tt "sixtyfive-oh-two") " egg also contains a copy of the " (link "http://www.forth.org/" "FIG") " Forth interpreter for the 6502. Download the code by entering") (pre " chicken-install -retrieve -test sixtyfive-oh-two") (p "and check out the \"test\" subdirectory. More examples, the sources for the interpreter and hardware emulation code can be found in the egg SVN repository: " (link "http://code.call-cc.org/svn/chicken-eggs/release/4/sixtyfive-oh-two/stuff/") " (user: " (tt "anonymous") ", password: empty).")) (section 3 "Authors" (p "Felix Winkelmann")) (section 3 "License" (pre "(c)2012 Felix Winkelmann") (pre "Available under a 3-clause BSD license.")) (section 3 "Version History" (dl (dt "0.1") (dd "initial release")))))