94 lines
2.1 KiB
Racket
94 lines
2.1 KiB
Racket
|
#lang racket
|
||
|
|
||
|
(require (for-syntax syntax/parse))
|
||
|
|
||
|
;-----------------------
|
||
|
; Memory Data Structure
|
||
|
;-----------------------
|
||
|
; Methods for accessing a 16 bit addressing memory
|
||
|
; including support for banking (planned) and
|
||
|
; access controls for ROM (planned)
|
||
|
;-----------------------
|
||
|
|
||
|
; Needs:
|
||
|
; Banking
|
||
|
; Routes based on memory location
|
||
|
; Banking based on memory
|
||
|
; Registers:
|
||
|
; Fast access
|
||
|
; Hash?
|
||
|
;
|
||
|
|
||
|
(struct memory
|
||
|
(registers state banks)
|
||
|
#:transparent)
|
||
|
|
||
|
(define (make-memory)
|
||
|
(memory (make-regvec) (make-statevec) (make-bank)))
|
||
|
|
||
|
(define (make-regvec)
|
||
|
(make-vector 8))
|
||
|
|
||
|
(define (make-statevec)
|
||
|
(make-vector 2))
|
||
|
|
||
|
(define (make-bank [init 0])
|
||
|
(make-vector (expt 2 16)))
|
||
|
|
||
|
(define (deref-state sym)
|
||
|
(case sym
|
||
|
[(state-PC) 0]
|
||
|
[(state-SP) 1]
|
||
|
[else (error "invalid state:" sym)]))
|
||
|
|
||
|
(define (deref-reg sym)
|
||
|
(case sym
|
||
|
[(reg-A) 0]
|
||
|
[(reg-F) 1]
|
||
|
[(reg-B) 2]
|
||
|
[(reg-C) 3]
|
||
|
[(reg-D) 4]
|
||
|
[(reg-E) 5]
|
||
|
[(reg-H) 6]
|
||
|
[(reg-L) 7]
|
||
|
[else (error "invalid register:" sym)]))
|
||
|
|
||
|
(define (get-reg sym memory)
|
||
|
(vector-ref (memory-registers memory) (deref-reg sym)))
|
||
|
|
||
|
(define (get-state sym memory)
|
||
|
(vector-ref (memory-state memory) (deref-state sym)))
|
||
|
|
||
|
(define (get-byte addr bank memory)
|
||
|
(if (and (>= addr 0)
|
||
|
(< addr 65536))
|
||
|
(vector-ref (memory-banks memory) addr)
|
||
|
(error (format "ERROR: address ~a out of bounds" addr))))
|
||
|
|
||
|
(define (set-reg! sym memory val)
|
||
|
(vector-set! (memory-registers memory) (deref-reg sym) (mod-8bit val)))
|
||
|
|
||
|
(define (set-state! sym memory val)
|
||
|
(vector-set! (memory-state memory) (deref-state sym) (mod-16bit val)))
|
||
|
|
||
|
(define (set-byte! addr bank memory val)
|
||
|
(if (and (>= addr 0)
|
||
|
(< addr 65536))
|
||
|
(vector-set! (memory-banks memory) addr (mod-8bit val))
|
||
|
(error (format "ERROR: address ~a out of bounds" addr))))
|
||
|
|
||
|
;----------------------------
|
||
|
; define-8bit & define-16bit
|
||
|
;----------------------------
|
||
|
; composes operations with a modulo in order
|
||
|
; to keep numbers within 8 bits
|
||
|
;----------------------------
|
||
|
|
||
|
(define (mod-8bit x)
|
||
|
(modulo x 256))
|
||
|
|
||
|
(define (mod-16bit x)
|
||
|
(modulo x 65536))
|
||
|
|
||
|
(provide (all-defined-out))
|