-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnes-cartridge.el
110 lines (99 loc) · 3.55 KB
/
nes-cartridge.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
;; -*- lexical-binding: t -*-
(eval-when-compile (require 'cl))
(require 'bindat)
(defconst nes/cartridge-ines-header-spec
'((constant vec 4 u8)
(size-of-prg-rom u8)
(size-of-chr-rom u8)
(flags6 u8)
(flags7 u8)
(size-of-prg-ram u8)
(flags9 u8)
(flags10 u8)
(zero-pad vec 5 u8)))
;;
;; see https://wiki.nesdev.com/w/index.php/INES#iNES_file_format
;;
(defstruct (nes/cartridge
(:conc-name nes/cartridge->))
header
trainer
prg-rom
chr-rom
pc-inst-rom ;; PlayChoice 10
pc-prom-data
pc-prom-counter-out-data)
(defstruct (nes/cartridge-header
(:conc-name nes/cartridge-header->))
constant
size-of-prg-rom
size-of-chr-rom
flags6
flags7
size-of-prg-ram
flags9
flags10
zero-pad)
(defun nes/cartridge--load-header (filepath)
(let* ((data
(with-temp-buffer
;; The first 16 bytes are the iNES header
(insert-file-contents-literally filepath nil 0 16)
(buffer-substring-no-properties (point-min) (point-max))))
(decoded
(bindat-unpack nes/cartridge-ines-header-spec (encode-coding-string data 'raw-text))))
(make-nes/cartridge-header
:constant (bindat-get-field decoded 'constant)
:size-of-prg-rom (bindat-get-field decoded 'size-of-prg-rom)
:size-of-chr-rom (bindat-get-field decoded 'size-of-chr-rom)
:flags6 (bindat-get-field decoded 'flags6)
:flags7 (bindat-get-field decoded 'flags7)
:size-of-prg-ram (bindat-get-field decoded 'size-of-prg-ram)
:flags9 (bindat-get-field decoded 'flags9)
:flags10 (bindat-get-field decoded 'flags10)
:zero-pad (bindat-get-field decoded 'zero-pad))))
(defun nes/cartridge-load (filepath)
(let* ((header
(nes/cartridge--load-header filepath))
(trainer-exists
(eq (logand (lsh (nes/cartridge-header->flags6 header) -2)) 1))
(playchoice-exists
(eq (logand (lsh (nes/cartridge-header->flags7 header) -1)) 1))
(body-spec
`((trainer vec
,(if trainer-exists 512 0)
u8)
(prg-rom vec
,(* 16384 (nes/cartridge-header->size-of-prg-rom header))
u8)
(chr-rom vec
,(* 8192 (nes/cartridge-header->size-of-chr-rom header))
u8)
(pc-inst-rom vec
,(if playchoice-exists 8192 0)
u8)
(pc-prom-data vec
,(if playchoice-exists 16 0)
u8)
(pc-prom-counter-out-data vec
,(if playchoice-exists 16 0)
u8)
))
(body
(with-temp-buffer
(insert-file-contents-literally filepath nil 16)
(buffer-substring-no-properties (point-min) (point-max))))
(body-decoded
(bindat-unpack body-spec (encode-coding-string body 'raw-text))))
(make-nes/cartridge
:header header
:trainer (bindat-get-field body-decoded 'trainer)
:prg-rom (bindat-get-field body-decoded 'prg-rom)
:chr-rom (bindat-get-field body-decoded 'chr-rom)
:pc-inst-rom (bindat-get-field body-decoded 'pc-inst-rom)
:pc-prom-data (bindat-get-field body-decoded 'pc-prom-data)
:pc-prom-counter-out-data (bindat-get-field body-decoded 'pc-prom-counter-out-data))))
(defun nes/cartridge-read-from-prg-rom (cart addr)
(let ((rom (nes/cartridge->prg-rom cart)))
(aref rom (mod addr (length rom)))))
(provide 'nes-cartridge)