summaryrefslogtreecommitdiff
path: root/packages/libndsfpc/src/nds/system.inc
blob: 8056cc02d036362dd295e175aeb18c04e5136cfa (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
{$ifdef NDS_INTERFACE}
const
  REG_DISPSTAT	: pcuint16 = pointer($04000004);
  
type
  DISP_BITS = cint;
const
  DISP_IN_VBLANK    : DISP_BITS = (1 shl 0);
  DISP_IN_HBLANK    : DISP_BITS = (1 shl 1);
  DISP_YTRIGGERED   : DISP_BITS = (1 shl 2);
  DISP_VBLANK_IRQ   : DISP_BITS = (1 shl 3);
  DISP_HBLANK_IRQ   : DISP_BITS = (1 shl 4);
  DISP_YTRIGGER_IRQ : DISP_BITS = (1 shl 5);

  REG_VCOUNT		: pcuint16 = pointer($04000006);
  HALT_CR       : pcuint16 = pointer($04000300);
  REG_POWERCNT  : pcuint16 = pointer($04000304);
  
  REG_SCFG_ROM		: pcuint16 = pointer($04004000);  

{$ifdef ARM7}
  REG_SCFG_A9ROM : pcuint8 = pointer($4004000);
  REG_SCFG_A7ROM : pcuint8 = pointer($4004001);  // ??
{$endif ARM7}

  REG_SCFG_CLK : pcuint16 = pointer($4004004);
  REG_SCFG_RST : pcuint16 = pointer($4004006);
  REG_SCFG_EXT : pcuint32 = pointer($4004008);
  REG_SCFG_MC  : pcuint16 = pointer($4004010);

procedure SetYtrigger(Yvalue: cint); inline;

const
  PM_ARM9_DIRECT = (1 shl 16);

type
  PM_Bits = cint;
const
	PM_SOUND_AMP         : PM_Bits = (1 shl 0);
	PM_SOUND_MUTE        : PM_Bits = (1 shl 1);
	PM_BACKLIGHT_BOTTOM  : PM_Bits = (1 shl 2);
	PM_BACKLIGHT_TOP     : PM_Bits = (1 shl 3);
	PM_SYSTEM_PWR        : PM_Bits = (1 shl 6);

	POWER_LCD      : PM_Bits = PM_ARM9_DIRECT or (1 shl 0);
	POWER_2D_A     : PM_Bits = PM_ARM9_DIRECT or (1 shl 1);
	POWER_MATRIX   : PM_Bits = PM_ARM9_DIRECT or (1 shl 2);
	POWER_3D_CORE  : PM_Bits = PM_ARM9_DIRECT or (1 shl 3);
	POWER_2D_B     : PM_Bits = PM_ARM9_DIRECT or (1 shl 9);
	POWER_SWAP_LCDS: PM_Bits = PM_ARM9_DIRECT or (1 shl 15);
//	POWER_ALL_2D   : PM_Bits = PM_ARM9_DIRECT or POWER_LCD or POWER_2D_A or POWER_2D_B;
	POWER_ALL_2D   : PM_Bits = (1 shl 16) or (1 shl 0) or (1 shl 1) or (1 shl 9);
//	POWER_ALL      : PM_Bits = PM_ARM9_DIRECT or POWER_ALL_2D or POWER_3D_CORE or POWER_MATRIX;
	POWER_ALL      : PM_Bits = (1 shl 16) or (1 shl 0) or (1 shl 1) or (1 shl 9) or (1 shl 3) or (1 shl 2);

//procedure SystemSleep(); cdecl; external;
procedure ledBlink(bm: cint); cdecl; external;

function isDSiMode(): cbool; inline;

{$ifdef ARM9}
procedure SystemSleep(); cdecl; external;
procedure powerOn(bits: cint); cdecl; external;
procedure powerOff(bits: cint); cdecl; external;

procedure systemMsgHandler(bytes: cint; user_data: pointer); cdecl; external;
procedure systemValueHandler(value: cuint32; data: pointer); cdecl; external;

procedure lcdSwap(); inline; 
procedure lcdMainOnTop(); inline; 
procedure lcdMainOnBottom(); inline; 

procedure systemShutDown(); inline;

procedure readFirmware(address: cuint32; buffer: pointer; length: cuint32); cdecl; external;
function writeFirmware(address: cuint32; buffer: pointer; length: cuint32): cint; cdecl; external;


function getBatteryLevel(): cuint32; cdecl; external;

procedure setVectorBase(highVector: cint); cdecl; external;

type 
  sysVectors_t = packed record
    reset: VoidFn;
    undefined: VoidFn;
    swi: VoidFn;
    prefetch_abort: VoidFn;
    data_abort: VoidFn;
    fiq: VoidFn;
  end; 
  sysVectors = sysVectors_t;
  PsysVectors = ^sysVectors;

var
  SystemVectors: sysVectors; cvar; external;

//void setSDcallback(void(*callback)(int));
procedure setSDcallback(callback: pointer); cdecl; external; // ??

function setCpuClock(speed: cbool): cbool; cdecl; external;

// Helper functions for heap size
//! returns current start of heap space
function getHeapStart(): pcuint8; cdecl; external;
//! returns current end of heap space
function getHeapEnd(): pcuint8; cdecl; external;
//! returns current heap limit
function getHeapLimit(): pcuint8; cdecl; external;

{$endif ARM9}


{$ifdef ARM7}
const
  REG_CONSOLEID	: pcuint64 = pointer($04004D00);

type
  ARM7_power = cint;
const
  POWER_SOUND         : ARM7_power = (1 shl 0);
  PM_CONTROL_REG      : ARM7_power = 0;
  PM_BATTERY_REG      : ARM7_power = 1;
  PM_AMPLIFIER_REG    : ARM7_power = 2;
  PM_READ_REGISTER    : ARM7_power = (1 shl 7);
  PM_AMP_OFFSET       : ARM7_power = 2;
  PM_GAIN_OFFSET      : ARM7_power = 3;
  PM_BACKLIGHT_LEVEL  : ARM7_power = 4;
  PM_GAIN_20          : ARM7_power = 0;
  PM_GAIN_40          : ARM7_power = 1;
  PM_GAIN_80          : ARM7_power = 2;
  PM_GAIN_160         : ARM7_power = 3;
  PM_AMP_ON           : ARM7_power = 1;
  PM_AMP_OFF          : ARM7_power = 0;

function PM_LED_CONTROL(m: cint): cint; inline;

procedure installSystemFIFO(); cdecl; external;
procedure systemSleep(); cdecl; external;
function sleepEnabled(): cint; cdecl; external;
function writePowerManagement(reg, command: cint): cint; cdecl; external;

function readPowerManagement(reg: cint): cint; inline; 

procedure powerOn(bits: cint); inline;
procedure powerOff(bits: cint); inline;
procedure readUserSettings(); cdecl; external;
procedure systemShutDown(); cdecl; external;
{$endif ARM7}


type
  BACKLIGHT_LEVELS = cint;
const
	BACKLIGHT_LOW  : BACKLIGHT_LEVELS = 0;
	BACKLIGHT_MED  : BACKLIGHT_LEVELS = 1;
	BACKLIGHT_HIGH : BACKLIGHT_LEVELS = 2;
	BACKLIGHT_MAX  : BACKLIGHT_LEVELS = 3;	

type
  tPERSONAL_DATA = bitpacked record
    RESERVED0: array [0..1] of cuint8;
    
    theme: cuint8;
    birthMonth: cuint8;
    birthDay: cuint8;
    
    RESERVED1: array [0..0] of cuint8;
    
    name: array [0..9] of cint16;
    nameLen: cuint16;
    
    message: array [0..25] of cint16;
    messageLen: cuint16;
    
    alarmHour: cuint8;
    alarmMinute: cuint8;
    
    RESERVED2: array [0..3] of cuint8;
    
    calX1: cuint16;
    calY1: cuint16;
    calX1px: cuint8;
    calY1px: cuint8;
    
    calX2: cuint16;
    calY2: cuint16;
    calX2px: cuint8;
    calY2px: cuint8;
    
  
    _user_data: packed record
      language: 0..3;
      gbaScreen: 0..1;
      defaultBrightness: 0..2;
      autoMode: 0..1;
      RESERVED5: 0..2;
	    settingsLost: 0..1;	
	    RESERVED6: 0..6;
    end;
    RESERVED3: cuint16;
    rtcOffset: cuint32;
    RESERVED4: cuint32;
  end; 
  PERSONAL_DATA = tPERSONAL_DATA;
  PPERSONAL_DATA = ^tPERSONAL_DATA;

const
  PersonalData  : PPERSONAL_DATA = pointer($2FFFC80);

type
  RTCtime = packed record
    year: cuint8;        // add 2000 to get 4 digit year
    month: cuint8;       // 1 to 12
    day: cuint8;         // 1 to (days in month)
    weekday: cuint8;     // day of week
    hours: cuint8;       // 0 to 11 for AM, 52 to 63 for PM
    minutes: cuint8;     // 0 to 59
    seconds: cuint8;     // 0 to 59
//    padding: cuint8;
  end;

const
  ARGV_MAGIC = $5f617267;

type 
  ppcchar = ^pcchar;
  __argv = record 
    argvMagic: cint;		  // argv magic number, set to 0x5f617267 ('_arg') if valid 
    commandLine: pcchar;	// base address of command line, set of null terminated strings
    length: cint;			    // total length of command line
    argc: integer;
    argv: ppcchar;    
    dummy: cint;          // internal use
    host: cuint32;        // internal use, host ip for dslink 
  end;
  Targv = __argv;
  Pargv = ^Targv;

const
  __system_argv: Pargv = pointer($02FFFE70);

const
  BOOTSIG: cuint64 = $62757473746F6F62; // ULL?


type
  __bootstub = packed record      // packed ?
    bootsig: 	cuint64;
    arm9reboot: VoidFn;
    arm7reboot: VoidFn;
    bootsize: cuint32;
  end;


{$ifdef ARM9}
//procedure memCached(address: pointer); cdecl; external;
function memCached(address: pointer): pointer; cdecl; external;
//procedure memUncached(address: pointer); cdecl; external;
function memUncached(address: pointer): pointer; cdecl; external;
procedure resetARM7(address: cuint32); cdecl; external;
{$endif ARM9}

{$ifdef ARM7}
procedure resetARM9(address: cuint32); cdecl; external;
{$endif ARM7}

{$endif NDS_INTERFACE}




{$ifdef NDS_IMPLEMENTATION}

procedure SetYtrigger(Yvalue: cint); inline;
begin
  REG_DISPSTAT^ := (REG_DISPSTAT^ and $007F ) or (Yvalue shl 8) or (( Yvalue and $100 ) shr 1);
end;

{$ifdef ARM9}
procedure lcdSwap(); inline; 
begin
  REG_POWERCNT^ := REG_POWERCNT^ xor POWER_SWAP_LCDS; 
end;

procedure lcdMainOnTop(); inline; 
begin
  REG_POWERCNT^ := REG_POWERCNT^ or cint(POWER_SWAP_LCDS); 
end;

procedure lcdMainOnBottom(); inline; 
begin
  REG_POWERCNT^ := REG_POWERCNT^ and (not cint(POWER_SWAP_LCDS)); 
end;

procedure systemShutDown(); inline;
begin
  powerOn(PM_SYSTEM_PWR);
end;

{$endif ARM9}


{$ifdef ARM7}
function PM_LED_CONTROL(m: cint): cint; inline;
begin
  result := (m) shl 4;
end;

function readPowerManagement(reg: cint): cint; inline; 
begin
	result := writePowerManagement(reg or PM_READ_REGISTER, 0);
end;

procedure powerOn(bits: cint); inline;
begin
	REG_POWERCNT^ := REG_POWERCNT^ or bits;
end;

procedure powerOff(bits: cint); inline;
begin
	REG_POWERCNT^ := REG_POWERCNT^ and not bits;
end;
{$endif ARM7}


var
  __dsimode: cbool; cvar; external;
function isDSiMode(): cbool; inline;
begin
	isDSiMode := __dsimode;
end;

{$endif NDS_IMPLEMENTATION}