llvm-mos-sdk
neslib.h
Go to the documentation of this file.
1 #ifndef _NESLIB_H_
2 #define _NESLIB_H_
3 
4 #include <famitone2.h>
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 // NES hardware-dependent functions by Shiru (shiru@mail.ru)
11 // Feel free to do anything you want with this code, consider it Public Domain
12 
13 // for nesdoug version 1.2, 1/1/2022
14 // changes, removed sprid from oam functions, oam_spr 11% faster, meta 5% faster
15 
16 // Versions history:
17 // 050517 - pad polling code optimized, button bits order reversed
18 // 280215 - fixed palette glitch caused by the active DMC DMA glitch
19 // 030914 - minor fixes in the VRAM update system
20 // 310814 - added vram_flush_update
21 // 120414 - removed adr argument from vram_write and vram_read,
22 // unrle_vram renamed to vram_unrle, with adr argument removed
23 // 060414 - many fixes and improvements, including sequential VRAM updates
24 // previous versions were created since mid-2011, there were many updates
25 
26 // set bg and spr palettes, data is 32 bytes array
27 __attribute__((leaf)) void pal_all(const void *data);
28 
29 // set bg palette only, data is 16 bytes array
30 __attribute__((leaf)) void pal_bg(const void *data);
31 
32 // set spr palette only, data is 16 bytes array
33 __attribute__((leaf)) void pal_spr(const void *data);
34 
35 // set a palette entry, index is 0..31
36 void pal_col(char index, char color);
37 
38 // reset palette to $0f
39 __attribute__((leaf)) void pal_clear(void);
40 
41 // set virtual bright both for sprites and background, 0 is black, 4 is normal,
42 // 8 is white
43 void pal_bright(char bright);
44 
45 // set virtual bright for sprites only
46 void pal_spr_bright(char bright);
47 
48 // set virtual bright for sprites background only
49 void pal_bg_bright(char bright);
50 
51 // wait actual TV frame, 50hz for PAL, 60hz for NTSC
52 __attribute__((leaf)) void ppu_wait_nmi(void);
53 
54 // wait virtual frame, it is always 50hz, frame-to-frame in PAL, frameskip in
55 // NTSC don't use this one
56 __attribute__((leaf)) void ppu_wait_frame(void);
57 
58 // turn off rendering, nmi still enabled when rendering is disabled
59 void ppu_off(void);
60 
61 // turn on bg, spr
62 void ppu_on_all(void);
63 
64 // turn on bg only
65 void ppu_on_bg(void);
66 
67 // turn on spr only
68 void ppu_on_spr(void);
69 
70 // set PPU_MASK directly
71 void ppu_mask(char mask);
72 
73 // get current video system, 0 for PAL, not 0 for NTSC
74 char ppu_system(void);
75 
76 // clear OAM buffer, all the sprites are hidden
77 // Note: changed. Now also changes sprid (index to buffer) to zero
78 __attribute__((leaf)) void oam_clear(void);
79 
80 // set sprite display mode, 0 for 8x8 sprites, 1 for 8x16 sprites
81 __attribute__((leaf)) void oam_size(char size);
82 
83 // set sprite in OAM buffer, chrnum is tile, attr is attribute
84 // Note: sprid removed for speed
85 __attribute__((leaf)) void oam_spr(char x, char y, char chrnum, char attr);
86 
87 // set metasprite in OAM buffer
88 // meta sprite is a const char array, it contains four bytes per sprite
89 // in order x offset, y offset, tile, attribute
90 // x=128 is end of a meta sprite
91 // Note: sprid removed for speed
92 __attribute__((leaf)) void oam_meta_spr(char x, char y, const void *data);
93 
94 // hide all remaining sprites from given offset
95 // Note: sprid removed for speed
96 // Now also changes sprid (index to buffer) to zero
97 __attribute__((leaf)) void oam_hide_rest(void);
98 
99 // to manually change the sprid (index to sprite buffer)
100 // perhaps as part of a sprite shuffling algorithm
101 // Note: this should be a multiple of 4 (0,4,8,12,etc.)
102 void oam_set(char index);
103 
104 // returns the sprid (index to the sprite buffer)
105 char oam_get(void);
106 
107 // poll controller and return flags like PAD_LEFT etc, input is pad number (0 or
108 // 1)
109 __attribute__((leaf)) char pad_poll(char pad);
110 
111 // poll controller in trigger mode, a flag is set only on button down, not hold
112 // if you need to poll the pad in both normal and trigger mode, poll it in the
113 // trigger mode for first, then use pad_state
114 char pad_trigger(char pad);
115 
116 // get previous pad state without polling ports
117 char pad_state(char pad);
118 
119 // set scroll, including the top bits
120 // it is always applied at beginning of a TV frame, not at the function call
121 __attribute__((leaf)) void scroll(unsigned x, unsigned y);
122 
123 // set scroll after screen split invoked by the sprite 0 hit
124 // warning: all CPU time between the function call and the actual split point
125 // will be wasted! warning: the program loop has to fit into the frame time,
126 // ppu_wait_frame should not be used
127 // otherwise empty frames without split will be inserted, resulting in
128 // jumpy screen
129 // warning: only X scroll could be changed in this version
130 __attribute__((leaf)) void split(unsigned x); // removed y, not used %%
131 
132 // select current chr bank for sprites, 0..1
133 __attribute__((leaf)) void bank_spr(char n);
134 
135 // select current chr bank for background, 0..1
136 __attribute__((leaf)) void bank_bg(char n);
137 
138 // get random number 0..255
139 __attribute__((leaf)) char rand8(void);
140 
141 // get random number 0..65535
142 __attribute__((leaf)) unsigned rand16(void);
143 
144 // set random seed
145 __attribute__((leaf)) void set_rand(unsigned seed);
146 
147 // when display is enabled, vram access could only be done with this vram update
148 // system the function sets a pointer to the update buffer that contains data
149 // and addresses in a special format. It allows to write non-sequential bytes,
150 // as well as horizontal or vertical nametable sequences. buffer pointer could
151 // be changed during rendering, but it only takes effect on a new frame number
152 // of transferred bytes is limited by vblank time to disable updates, call this
153 // function with NULL pointer
154 //
155 // the update data format:
156 // MSB, LSB, byte for a non-sequential write
157 // MSB|NT_UPD_HORZ, LSB, LEN, [bytes] for a horizontal sequence
158 // MSB|NT_UPD_VERT, LSB, LEN, [bytes] for a vertical sequence
159 // NT_UPD_EOF to mark end of the buffer
160 //
161 // length of this data should be under 256 bytes
162 void set_vram_update(const void *buf);
163 
164 // all following vram functions only work when display is disabled
165 
166 // do a series of VRAM writes, the same format as for set_vram_update, but
167 // writes done right away
168 __attribute__((leaf)) void flush_vram_update(const void *buf);
169 
170 // set vram pointer to write operations if you need to write some data to vram
171 void vram_adr(unsigned adr);
172 
173 // put a byte at current vram address, works only when rendering is turned off
174 void vram_put(char n);
175 
176 // fill a block with a byte at current vram address, works only when rendering
177 // is turned off
178 __attribute__((leaf)) void vram_fill(char n, unsigned len);
179 
180 // set vram autoincrement, 0 for +1 and not 0 for +32
181 __attribute__((leaf)) void vram_inc(char n);
182 
183 // read a block from current address of vram, works only when rendering is
184 // turned off
185 __attribute__((leaf)) void vram_read(void *dst, unsigned size);
186 
187 // write a block to current address of vram, works only when rendering is turned
188 // off
189 __attribute__((leaf)) void vram_write(const void *src, unsigned size);
190 
191 // unpack RLE data to current address of vram, mostly used for nametables
192 __attribute__((leaf)) void vram_unrle(const void *data);
193 
194 // delay for N frames
195 void delay(char frames);
196 
197 #define PAD_A 0x80
198 #define PAD_B 0x40
199 #define PAD_SELECT 0x20
200 #define PAD_START 0x10
201 #define PAD_UP 0x08
202 #define PAD_DOWN 0x04
203 #define PAD_LEFT 0x02
204 #define PAD_RIGHT 0x01
205 
206 #define OAM_FLIP_V 0x80
207 #define OAM_FLIP_H 0x40
208 #define OAM_BEHIND 0x20
209 
210 #define MAX(x1, x2) ((x1) < (x2) ? (x2) : (x1))
211 #define MIN(x1, x2) ((x1) < (x2) ? (x1) : (x2))
212 
213 #define MASK_SPR 0x10
214 #define MASK_BG 0x08
215 #define MASK_EDGE_SPR 0x04
216 #define MASK_EDGE_BG 0x02
217 
218 #define NAMETABLE_A 0x2000
219 #define NAMETABLE_B 0x2400
220 #define NAMETABLE_C 0x2800
221 #define NAMETABLE_D 0x2c00
222 
223 #define NULL 0
224 #define TRUE 1
225 #define FALSE 0
226 
227 #define NT_UPD_HORZ 0x40
228 #define NT_UPD_VERT 0x80
229 #define NT_UPD_EOF 0xff
230 
231 // macro to calculate nametable address from X,Y in compile time
232 #define NTADR_A(x, y) (NAMETABLE_A | (((y) << 5) | (x)))
233 
234 // macro to calculate nametable address from X,Y in compile time
235 #define NTADR_B(x, y) (NAMETABLE_B | (((y) << 5) | (x)))
236 
237 // macro to calculate nametable address from X,Y in compile time
238 #define NTADR_C(x, y) (NAMETABLE_C | (((y) << 5) | (x)))
239 
240 // macro to calculate nametable address from X,Y in compile time
241 #define NTADR_D(x, y) (NAMETABLE_D | (((y) << 5) | (x)))
242 
243 // macro to get MSB
244 #define MSB(x) (((x) >> 8))
245 
246 // macro to get LSB
247 #define LSB(x) (((x)&0xff))
248 
249 #ifdef __cplusplus
250 }
251 #endif
252 
253 #endif // _NESLIB_H_
set_vram_update
void set_vram_update(const void *buf)
len
unsigned len
Definition: neslib.h:178
famitone2.h
data
char const void * data
Definition: neslib.h:92
ppu_mask
void ppu_mask(char mask)
ppu_on_spr
void ppu_on_spr(void)
vram_adr
void vram_adr(unsigned adr)
pal_spr_bright
void pal_spr_bright(char bright)
y
char y
Definition: neslib.h:85
ppu_off
void ppu_off(void)
pal_col
void pal_col(char index, char color)
pal_bright
void pal_bright(char bright)
attr
char char char attr
Definition: neslib.h:85
index
char index
Definition: nesdoug.h:113
ppu_system
char ppu_system(void)
src
const void * src
Definition: memory.h:57
vram_put
void vram_put(char n)
scroll
unsigned scroll
Definition: nesdoug.h:70
x
byte x
Definition: api.h:26
oam_set
void oam_set(char index)
ppu_on_all
void ppu_on_all(void)
size
unsigned size
Definition: neslib.h:185
pad_state
char pad_state(char pad)
delay
void delay(char frames)
ppu_on_bg
void ppu_on_bg(void)
oam_get
char oam_get(void)
pal_bg_bright
void pal_bg_bright(char bright)
pad_trigger
char pad_trigger(char pad)
chrnum
char char chrnum
Definition: neslib.h:85