Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

  1. Outdated egg!
  2. sixtyfive-oh-two
    1. Introduction
    2. Procedures
    3. Examples
    4. Authors
    5. License
    6. Version History

sixtyfive-oh-two

Introduction

An emulator for the 6502 CPU.

Only the pure CPU is emulated, no hardware-interaction model is provided.

The opcode-interpreter is implemented in 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 crunch.h, so the crunch extension must be installed.

Procedures

[procedure] (make-processor #!key memory registers)

Returns a record structure representing a single 6502 processor. memory should be an u8vector that is used as RAM, usually of length 65546. registers should be an 7 element u8vector, where the elements hold the values of the registers, according to the following table:

Index Register
0 A
1 X
2 Y
3 P
4 S
5 PC (low byte)
6 PC (high byte)
[procedure] (processor? X)

Returns #t if X is a processor record or #f otherwise.

[procedure] (processor-memory P)
[procedure] (processor-memory-set! P U8VECTOR)
[procedure] (processor-registers P)
[procedure] (processor-registers-set! P U8VECTOR)

Accessors for the processor record.

[procedure] (reset! P)

Resets the CPU represented by P by setting A, X, Y to zero, P to #x20, S to #xff and PC to the 16-bit address stored at location #xfffc.

[procedure] (execute! P #!key cycles)

Executes instructions for processor P with the state currently active in the processor record. Execution continues endlessly or after at least cycles CPU cycles have been consumed (instructions always run to completion).

[procedure] (interrupt! P VEC)

Triggers an interrupt for processor P by pushing the PC and P registers, setting the interrupt flag and continuing execution at the address stored at the memory location given in VEC.

Examples

Say you need a fast way of multiplying an 8-bit quantity with 10, then you could do:

(use sixtyfive-oh-two srfi-4 lolevel utils miscmacros)

;; Fast Multiply by 10
;; By Leo Nechaev (leo@ogpi.orsk.ru), 28 October 2000.

; * = $1000
; LDA #18
; JSR MULT10
; L1:
; JMP L1
; MULT10:
; ASL A         ;multiply by 2
; STA TEMP    ;temp store in TEMP
; ASL A        ;again multiply by 2 (*4)
; ASL A        ;again multiply by 2 (*8)
; CLC
; ADC TEMP    ;as result, A = x*8 + x*2
; RTS

(define mult10
  '#${a9 12
      20 08 10
      4c 05 10
      0a
      8d 00 20
      0a
      0a
      18
      6d 00 20
      60})

(define p (make-processor))
(move-memory! mult10 (processor-memory p) (blob-size mult10) 0 #x1000)
(define regs (processor-registers p))
(u8vector-set! regs 5 #x00)			; PC = #x1000
(u8vector-set! regs 6 #x10)
(execute! p cycles: 100)
(pp regs)
(assert (= 180 (u8vector-ref regs 0)))

The sixtyfive-oh-two egg also contains a copy of the FIG Forth interpreter for the 6502. Download the code by entering

 chicken-install -retrieve -test sixtyfive-oh-two

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: http://code.call-cc.org/svn/chicken-eggs/release/4/sixtyfive-oh-two/stuff/ (user: anonymous, password: empty).

Authors

Felix Winkelmann

License

(c)2012 Felix Winkelmann
Available under a 3-clause BSD license.

Version History

0.1
initial release