llvm-mos-sdk
nes.h
Go to the documentation of this file.
1 // Copyright 2022 LLVM-MOS Project
2 // Licensed under the Apache License, Version 2.0 with LLVM Exceptions.
3 // See https://github.com/llvm-mos/llvm-mos-sdk/blob/main/LICENSE for license
4 // information.
5 
6 // Originally from cc65. Modified from original version.
7 
8 // clang-format off
9 
10 /*****************************************************************************/
11 /* */
12 /* nes.h */
13 /* */
14 /* NES system specific definitions */
15 /* */
16 /* */
17 /* */
18 /* (C) 2002-2003 Groepaz/Hitmen */
19 /* */
20 /* */
21 /* This software is provided 'as-is', without any expressed or implied */
22 /* warranty. In no event will the authors be held liable for any damages */
23 /* arising from the use of this software. */
24 /* */
25 /* Permission is granted to anyone to use this software for any purpose, */
26 /* including commercial applications, and to alter it and redistribute it */
27 /* freely, subject to the following restrictions: */
28 /* */
29 /* 1. The origin of this software must not be misrepresented; you must not */
30 /* claim that you wrote the original software. If you use this software */
31 /* in a product, an acknowledgment in the product documentation would be */
32 /* appreciated but is not required. */
33 /* 2. Altered source versions must be plainly marked as such, and must not */
34 /* be misrepresented as being the original software. */
35 /* 3. This notice may not be removed or altered from any source */
36 /* distribution. */
37 /* */
38 /*****************************************************************************/
39 
40 
41 
42 #ifndef _NES_H
43 #define _NES_H
44 
45 
46 
47 /* Check for errors */
48 #if !defined(__NES__)
49 # error This module may only be used when compiling for the NES!
50 #endif
51 
52 
53 
54 /* Color defines */
55 #define COLOR_BLACK 0x00
56 #define COLOR_WHITE 0x01
57 #define COLOR_RED 0x02
58 #define COLOR_CYAN 0x03
59 #define COLOR_PURPLE 0x04
60 #define COLOR_GREEN 0x05
61 #define COLOR_BLUE 0x06
62 #define COLOR_YELLOW 0x07
63 #define COLOR_ORANGE 0x08
64 #define COLOR_BROWN 0x09
65 #define COLOR_LIGHTRED 0x0A
66 #define COLOR_GRAY1 0x0B
67 #define COLOR_GRAY2 0x0C
68 #define COLOR_LIGHTGREEN 0x0D
69 #define COLOR_LIGHTBLUE 0x0E
70 #define COLOR_GRAY3 0x0F
71 
72 /* Masks for joy_read */
73 #define JOY_UP_MASK 0x10
74 #define JOY_DOWN_MASK 0x20
75 #define JOY_LEFT_MASK 0x40
76 #define JOY_RIGHT_MASK 0x80
77 #define JOY_BTN_1_MASK 0x01
78 #define JOY_BTN_2_MASK 0x02
79 #define JOY_BTN_3_MASK 0x04
80 #define JOY_BTN_4_MASK 0x08
81 
82 #define JOY_BTN_A_MASK JOY_BTN_1_MASK
83 #define JOY_BTN_B_MASK JOY_BTN_2_MASK
84 #define JOY_SELECT_MASK JOY_BTN_3_MASK
85 #define JOY_START_MASK JOY_BTN_4_MASK
86 
87 #define JOY_BTN_A(v) ((v) & JOY_BTN_A_MASK)
88 #define JOY_BTN_B(v) ((v) & JOY_BTN_B_MASK)
89 #define JOY_SELECT(v) ((v) & JOY_SELECT_MASK)
90 #define JOY_START(v) ((v) & JOY_START_MASK)
91 
92 /* Return codes of get_tv */
93 #define TV_NTSC 0
94 #define TV_PAL 1
95 #define TV_OTHER 2
96 
97 /* Define hardware */
98 
99 /* Picture Processing Unit */
100 struct __ppu {
101  unsigned char control;
102  unsigned char mask; /* color; show sprites, background */
103  char const status;
104  struct {
105  unsigned char address;
106  unsigned char data;
107  } sprite;
108  unsigned char scroll;
109  struct {
110  unsigned char address;
111  unsigned char data;
112  } vram;
113 };
114 #define PPU (*(volatile struct __ppu*)0x2000)
115 #define SPRITE_DMA (APU.sprite.dma)
116 
117 /* Audio Processing Unit */
118 struct __apu {
119  struct {
120  unsigned char control; /* duty, counter halt, volume/envelope */
121  unsigned char ramp;
122  unsigned char period_low; /* timing */
123  unsigned char len_period_high; /* length, timing */
124  } pulse[2];
125  struct {
126  unsigned char counter; /* counter halt, linear counter */
127  unsigned char unused;
128  unsigned char period_low; /* timing */
129  unsigned char len_period_high; /* length, timing */
130  } triangle;
131  struct {
132  unsigned char control; /* counter halt, volume/envelope */
133  unsigned char unused;
134  unsigned char period; /* loop, timing */
135  unsigned char len; /* length */
136  } noise;
137  struct {
138  unsigned char control; /* IRQ, loop, rate */
139  unsigned char output; /* output value */
140  unsigned char address;
141  unsigned char length;
142  } delta_mod; /* delta pulse-code modulation */
143  struct {
144  unsigned char dma;
145  } sprite;
146  char status;
147  unsigned char unused;
148  unsigned char fcontrol;
149 };
150 #define APU (*(volatile struct __apu*)0x4000)
151 
152 #define JOYPAD ((volatile unsigned char *)0x4016)
153 
154 // TODO
155 #if 0
156 void waitvsync (void);
157 /* Wait for start of the next frame */
158 
159 unsigned char get_tv (void);
160 /* Return the video mode the machine is using. */
161 #endif
162 
163 
164 
165 /* End of nes.h */
166 #endif
data
char const void * data
Definition: neslib.h:92
length
uint8_t uint16_t uint16_t length
Definition: bios.h:181
get_tv
unsigned char get_tv(void) __attribute__((leaf))
scroll
unsigned scroll
Definition: nesdoug.h:70
waitvsync
void waitvsync(void)
address
uint8_t uint16_t address
Definition: bios.h:180
len
char len
Definition: nesdoug.h:26