summaryrefslogtreecommitdiff
path: root/packages/libndsfpc/src/nds/arm9/background.inc
blob: 4d50611514596baea21b07a3f15f5afa2b73b1ae (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
(*
  $Id: background.inc 25 2007-12-10 21:06:46Z p4p3r0 $
  ------------------------------------------------------------------------------
  	Copyright (C) 2005
  		Jason Rogers (dovoto)
  		Dave Murphy (WinterMute)
  	
  	This software is provided 'as-is', without any express or implied
  	warranty.  In no event will the authors be held liable for any
  	damages arising from the use of this software.
  	
  	Permission is granted to anyone to use this software for any
  	purpose, including commercial applications, and to alter it and
  	redistribute it freely, subject to the following restrictions:
  	
  	1.	The origin of this software must not be misrepresented; you
  		must not claim that you wrote the original software. If you use
  		this software in a product, an acknowledgment in the product
  		documentation would be appreciated but is not required.
  	
  	2.	Altered source versions must be plainly marked as such, and
  		must not be misrepresented as being the original software.
  	
  	3.	This notice may not be removed or altered from any source
  		distribution.
  ------------------------------------------------------------------------------
    
    
  Conversion by Legolas (http://itaprogaming.free.fr) for freepascal compiler
  (http://www.freepascal.org)
   
  Copyright (C) 2006  Francesco Lombardi
  Check http://sourceforge.net/projects/libndsfpc for updates
   
  ------------------------------------------------------------------------------

  $Log$

*)

{$ifdef NDS_INTERFACE}
type
  bg_scroll = record
    x: cuint16;
    y: cuint16;
  end;
  Tbg_scroll = bg_scroll;
  Pbg_scroll = ^bg_scroll;

  bg_transform = record
    xdx: cint16;
    xdy: cint16;
    ydx: cint16;
    ydy: cint16;
    dx: cint32;
    dy: cint32;
  end;
  Tbg_transform = bg_transform;
  Pbg_transform = ^bg_transform;

  bg_attribute = record
    control: array [0..3] of cuint16;
    scroll: array [0..3] of bg_scroll;
    bg2_rotation: bg_transform;
    bg3_rotation: bg_transform;
  end;
  Tbg_attribute = bg_attribute;
  Pbg_attribute = ^bg_attribute;


const
  MAP_BASE_SHIFT = 8;
  TILE_BASE_SHIFT = 2;

function BG_TILE_BASE(base: cint): cint; inline; 
function BG_MAP_BASE(base: cint): cint; inline;  
function BG_BMP_BASE(base: cint): cint; inline;  
function BG_PRIORITY(n: cint): cint; inline;
function TILE_PALETTE(n: cint): cint; inline;

const
  TILE_FLIP_H = (1 shl 10);
  TILE_FLIP_V = (1 shl 11);

type
  BackgroundControl = cint;
const
  BG_32x32: BackgroundControl = (0 shl 14);
  BG_64x32: BackgroundControl = (1 shl 14);
  BG_32x64: BackgroundControl = (2 shl 14);
  BG_64x64: BackgroundControl = (3 shl 14);
  
  BG_RS_16x16: BackgroundControl =  (0 shl 14);
  BG_RS_32x32: BackgroundControl =  (1 shl 14);
  BG_RS_64x64: BackgroundControl =  (2 shl 14);
  BG_RS_128x128: BackgroundControl =  (3 shl 14);
  
  BG_BMP8_128x128: BackgroundControl = ((0 shl 14) or (1 shl 7));
  BG_BMP8_256x256: BackgroundControl = ((1 shl 14) or (1 shl 7));
  BG_BMP8_512x256: BackgroundControl = ((2 shl 14) or (1 shl 7));
  BG_BMP8_512x512: BackgroundControl = ((3 shl 14) or (1 shl 7));
  BG_BMP8_1024x512: BackgroundControl = (1 shl 14);
  BG_BMP8_512x1024: BackgroundControl = 0;
  
  BG_BMP16_128x128: BackgroundControl = ((0 shl 14) or (1 shl 7) or (1 shl 2));
  BG_BMP16_256x256: BackgroundControl = ((1 shl 14) or (1 shl 7) or (1 shl 2));
  BG_BMP16_512x256: BackgroundControl = ((2 shl 14) or (1 shl 7) or (1 shl 2));
  BG_BMP16_512x512: BackgroundControl = ((3 shl 14) or (1 shl 7) or (1 shl 2));
  
  BG_MOSAIC_ON: BackgroundControl = ((1 shl 6));
  BG_MOSAIC_OFF: BackgroundControl = (0);
  
  BG_PRIORITY_0: BackgroundControl = (0);
  BG_PRIORITY_1: BackgroundControl = (1);
  BG_PRIORITY_2: BackgroundControl = (2);
  BG_PRIORITY_3: BackgroundControl = (3);
  
  BG_WRAP_OFF: BackgroundControl = (0);
  BG_WRAP_ON: BackgroundControl = (1 shl 13);
  
  BG_PALETTE_SLOT0: BackgroundControl = 0;
  BG_PALETTE_SLOT1: BackgroundControl = 0;
  BG_PALETTE_SLOT2: BackgroundControl = (1 shl 13);
  BG_PALETTE_SLOT3: BackgroundControl = (1 shl 13);
  
  BG_COLOR_256: BackgroundControl = $80;
  BG_COLOR_16: BackgroundControl = $00;

	BACKGROUND: Pbg_attribute = pointer($04000008);
  BG_OFFSET: Pbg_scroll = pointer($04000010);

function BG_MAP_RAM(base: cint): pcuint16; inline;  
function BG_TILE_RAM(base: cint): pcuint16; inline; 
function BG_BMP_RAM(base: cint): pcuint16; inline;  
function CHAR_BASE_BLOCK(n: cint): pcuint16; inline;
function SCREEN_BASE_BLOCK(n: cint): pcuint16; inline;

const
  BGCTRL: pcuint16 = pointer($04000008);
  REG_BG0CNT		: pcuint16 = pointer($4000008);
  REG_BG1CNT		: pcuint16 = pointer($400000A);
  REG_BG2CNT		: pcuint16 = pointer($400000C);
  REG_BG3CNT		: pcuint16 = pointer($400000E);
  
  REG_BGOFFSETS	: pcuint16 = pointer($4000010);
  REG_BG0HOFS		: pcuint16 = pointer($4000010);
  REG_BG0VOFS		: pcuint16 = pointer($4000012);
  REG_BG1HOFS		: pcuint16 = pointer($4000014);
  REG_BG1VOFS		: pcuint16 = pointer($4000016);
  REG_BG2HOFS		: pcuint16 = pointer($4000018);
  REG_BG2VOFS		: pcuint16 = pointer($400001A);
  REG_BG3HOFS		: pcuint16 = pointer($400001C);
  REG_BG3VOFS		: pcuint16 = pointer($400001E);
  
  REG_BG2PA		: pcint16 = pointer($4000020);
  REG_BG2PB		: pcint16 = pointer($4000022);
  REG_BG2PC		: pcint16 = pointer($4000024);
  REG_BG2PD		: pcint16 = pointer($4000026);
  
  REG_BG2X		: pcint32 = pointer($4000028);
  REG_BG2Y		: pcint32 = pointer($400002C);
  
  REG_BG3PA		: pcint16 = pointer($4000030);
  REG_BG3PB		: pcint16 = pointer($4000032);
  REG_BG3PC		: pcint16 = pointer($4000034);
  REG_BG3PD		: pcint16 = pointer($4000036);
  
  REG_BG3X		: pcint32 = pointer($4000038);
  REG_BG3Y		: pcint32 = pointer($400003C);

	BACKGROUND_SUB : Pbg_attribute = pointer($04001008);
  BG_OFFSET_SUB: Pbg_scroll = pointer($04001010);

function BG_MAP_RAM_SUB(base: cint): pcuint16; inline; 
function BG_TILE_RAM_SUB(base: cint): pcuint16; inline; 
function BG_BMP_RAM_SUB(base: cint): pcuint16; inline; 
function SCREEN_BASE_BLOCK_SUB(n: cint): pcuint16; inline;
function CHAR_BASE_BLOCK_SUB(n: cint): pcuint16; inline;


const
  BGCTRL_SUB		: pcuint16 = pointer($4001008);
  REG_BG0CNT_SUB		: pcuint16 = pointer($4001008);
  REG_BG1CNT_SUB		: pcuint16 = pointer($400100A);
  REG_BG2CNT_SUB		: pcuint16 = pointer($400100C);
  REG_BG3CNT_SUB		: pcuint16 = pointer($400100E);
  
  REG_BGOFFSETS_SUB	: pcuint16 = pointer($4001010);
  REG_BG0HOFS_SUB		: pcuint16 = pointer($4001010);
  REG_BG0VOFS_SUB		: pcuint16 = pointer($4001012);
  REG_BG1HOFS_SUB		: pcuint16 = pointer($4001014);
  REG_BG1VOFS_SUB		: pcuint16 = pointer($4001016);
  REG_BG2HOFS_SUB		: pcuint16 = pointer($4001018);
  REG_BG2VOFS_SUB		: pcuint16 = pointer($400101A);
  REG_BG3HOFS_SUB		: pcuint16 = pointer($400101C);
  REG_BG3VOFS_SUB		: pcuint16 = pointer($400101E);
  
  REG_BG2PA_SUB		: pcint16 = pointer($4001020);
  REG_BG2PB_SUB		: pcint16 = pointer($4001022);
  REG_BG2PC_SUB		: pcint16 = pointer($4001024);
  REG_BG2PD_SUB		: pcint16 = pointer($4001026);
  
  REG_BG2X_SUB		: pcint32 = pointer($4001028);
  REG_BG2Y_SUB		: pcint32 = pointer($400102C);
  
  REG_BG3PA_SUB		: pcint16 = pointer($4001030);
  REG_BG3PB_SUB		: pcint16 = pointer($4001032);
  REG_BG3PC_SUB		: pcint16 = pointer($4001034);
  REG_BG3PD_SUB		: pcint16 = pointer($4001036);
  
  REG_BG3X_SUB		: pcint32 = pointer($4001038);
  REG_BG3Y_SUB		: pcint32 = pointer($400103C);
  
type
  //background state
  TBgState = record
    angle: cint;
    centerX, centerY: cint32;
    scaleX, scaleY: cint32;
    scrollX, scrollY: cint32;
    size: cint;
    _type: cint;
    dirty: cbool;
  end;
  PBgState = ^TBgState;
  
var
  bgControl: array [0..7] of pcuint16; cvar; external;
  bgScrollTable: array [0..7] of Pbg_scroll; cvar; external;
  bgTransform: array [0..7] of Pbg_transform; cvar; external;
  bgState: array [0..7] of TBgState; cvar; external;


type
  BgType = integer;
const
  BgType_Text8bpp   = 0;  
  BgType_Text4bpp   = 1; 
  BgType_Rotation   = 2;
  BgType_ExRotation = 3;
  BgType_Bmp8       = 4;
  BgType_Bmp16      = 5;

type
  BgSize = integer;
const
  BgSize_R_128x128    : BgSize = (0 shl 14);
  BgSize_R_256x256    : BgSize = (1 shl 14);
  BgSize_R_512x512    : BgSize = (2 shl 14);
  BgSize_R_1024x1024  : BgSize = (3 shl 14);	 	         
  BgSize_T_256x256    : BgSize = (0 shl 14) or (1 shl 16);	         
  BgSize_T_512x256    : BgSize = (1 shl 14) or (1 shl 16);	        
  BgSize_T_256x512    : BgSize = (2 shl 14) or (1 shl 16);	         
  BgSize_T_512x512    : BgSize = (3 shl 14) or (1 shl 16);	 	         
  BgSize_ER_128x128   : BgSize = (0 shl 14) or (2 shl 16);	         
  BgSize_ER_256x256   : BgSize = (1 shl 14) or (2 shl 16);	         
  BgSize_ER_512x512   : BgSize = (2 shl 14) or (2 shl 16);	         
  BgSize_ER_1024x1024 : BgSize = (3 shl 14) or (2 shl 16);	 	         
  BgSize_B8_128x128   : BgSize = (0 shl 14) or (1 shl 7) or (3 shl 16);	         
  BgSize_B8_256x256   : BgSize = (1 shl 14) or (1 shl 7) or (3 shl 16);	         
  BgSize_B8_512x256   : BgSize = (2 shl 14) or (1 shl 7) or (3 shl 16);	         
  BgSize_B8_512x512   : BgSize = (3 shl 14) or (1 shl 7) or (3 shl 16);	         
  BgSize_B8_1024x512  : BgSize = (1 shl 14) or (3 shl 16);	         
  BgSize_B8_512x1024  : BgSize = (0 or (3 shl 16));	 	         
  BgSize_B16_128x128  : BgSize = (0 shl 14) or (1 shl 7) or (1 shl 2) or (4 shl 16);	         
  BgSize_B16_256x256  : BgSize = (1 shl 14) or (1 shl 7) or (1 shl 2) or (4 shl 16);	         
  BgSize_B16_512x256  : BgSize = (2 shl 14) or (1 shl 7) or (1 shl 2) or (4 shl 16);	         
  BgSize_B16_512x512  : BgSize = (3 shl 14) or (1 shl 7) or (1 shl 2) or (4 shl 16);

function bgIsText(id: cint): cbool;	cdecl; external; 
function bgInit_call(layer: cint; atype: BgType; size: BgSize; mapBase, tileBase: cint): cint;	cdecl; external; 
function bgInitSub_call(layer: cint; atype: BgType; size: BgSize; mapBase, tileBase: cint): cint;	cdecl; external;

procedure bgUpdate(); cdecl; external;

procedure bgSetRotate(id, angle: cint); inline;
procedure bgRotate(id, angle: cint); inline;
procedure bgSet(id, angle: cint; sx, sy, scrollX, scrollY, rotCenterX, rotCenterY: cint32); inline;
procedure bgSetRotateScale(id, angle: cint; sx, sy: cint32); inline;
procedure bgSetScale(id: cint; sx, sy: cint32); inline;
function bgInit(layer: cint; atype: BgType; size: BgSize; mapBase, tileBase: cint): cint; inline;
function bgInitSub(layer: cint; atype: BgType; size: BgSize; mapBase, tileBase: cint): cint; inline;
function bgSetControlBits(id: cint; bits: cuint16): pcuint16; inline;
procedure bgClearControlBits(id: cint; bits: cuint16); inline;
procedure bgWrapOn(id: cint); inline;
procedure bgWrapOff(id: cint); inline;
procedure bgSetPriority(id: cint; priority: cuint); inline;
procedure bgSetMapBase(id: cint; base: cuint); inline;
procedure bgSetTileBase(id: cint; base: cuint); inline;	
procedure bgSetScrollf(id: cint; x, y: cint32); inline;
procedure bgSetScroll(id, x, y: cint); inline;
procedure bgMosaicEnable(id: cint); inline;
procedure bgMosaicDisable(id: cint); inline;
procedure bgSetMosaic(dx, dy: cuint); inline;
procedure bgSetMosaicSub(dx, dy: cuint); inline;
function bgGetPriority(id: cint): cint; inline;
function bgGetMapBase(id: cint): cint; inline;
function bgGetTileBase(id: cint): cint; inline;
function bgGetMapPtr(id: cint): pcuint16; inline;
function bgGetGfxPtr(id: cint): pcuint16; inline;
procedure bgScrollf(id: cint; dx, dy: cint32); inline;
procedure bgScroll(id, dx, dy: cint); inline;
procedure bgShow(id: cint); inline;
procedure bgHide(id: cint); inline;
procedure bgSetCenterf(id: cint; x, y: cint32); inline;
procedure bgSetCenter(id, x, y: cint); inline;

{$endif NDS_INTERFACE}

{$ifdef NDS_IMPLEMENTATION}
function BG_TILE_BASE(base: cint): cint; inline; 
begin 
  BG_TILE_BASE := ((base) shl 2);
end;

function BG_MAP_BASE(base: cint): cint; inline;  
begin
  BG_MAP_BASE := ((base) shl 8);
end;

function BG_BMP_BASE(base: cint): cint; inline;  
begin
  BG_BMP_BASE := ((base) shl 8);
end;

function BG_PRIORITY(n: cint): cint; inline;
begin
  BG_PRIORITY := (n);
end;

function TILE_PALETTE(n: cint): cint; inline;
begin
  TILE_PALETTE := ((n) shl 12);
end;

function BG_MAP_RAM(base: cint): pcuint16; inline;  
begin
  BG_MAP_RAM := pcuint16(((base) * $800) + $06000000);
end;

function BG_MAP_RAM_SUB(base: cint): pcuint16; inline; 
begin
  BG_MAP_RAM_SUB := pcuint16(((base) * $800) + $06200000);
end;


function BG_TILE_RAM(base: cint): pcuint16; inline; 
begin
  BG_TILE_RAM := pcuint16(((base) * $4000) + $06000000);
end;

function BG_TILE_RAM_SUB(base: cint): pcuint16; inline; 
begin
  BG_TILE_RAM_SUB := pcuint16(((base) * $4000) + $06200000);
end;


function BG_BMP_RAM(base: cint): pcuint16; inline;  
begin
  BG_BMP_RAM := pcuint16(((base) * $4000) + $06000000);
end;

function BG_BMP_RAM_SUB(base: cint): pcuint16; inline; 
begin
  BG_BMP_RAM_SUB := pcuint16(((base) * $4000) + $06200000);
end;

function CHAR_BASE_BLOCK(n: cint): pcuint16; inline;
begin
  CHAR_BASE_BLOCK := pcuint16((n * $4000)+ $06000000);
end;

function CHAR_BASE_BLOCK_SUB(n: cint): pcuint16; inline;
begin		
  CHAR_BASE_BLOCK_SUB := pcuint16((n * $4000)+ $06200000);
end;

function SCREEN_BASE_BLOCK(n: cint): pcuint16; inline;
begin		
  SCREEN_BASE_BLOCK := pcuint16((n * $800) + $06000000);
end;

function SCREEN_BASE_BLOCK_SUB(n: cint): pcuint16; inline;
begin	
  SCREEN_BASE_BLOCK_SUB := pcuint16((n * $800) + $06200000);
end;

procedure bgSetRotate(id, angle: cint); inline;
begin
  bgState[id].angle := angle;
  bgState[id].dirty := true;
end;

procedure bgRotate(id, angle: cint); inline;
begin
  sassert(not bgIsText(id), 'Cannot Rotate a Text Background');
  bgSetRotate(id, angle + bgState[id].angle);
end;

procedure bgSet(id, angle: cint; sx, sy, scrollX, scrollY, rotCenterX, rotCenterY: cint32); inline;
begin
  bgState[id].scaleX := sx;
  bgState[id].scaleY := sy;
  bgState[id].scrollX := scrollX;
  bgState[id].scrollY := scrollY;
  bgState[id].centerX := rotCenterX;
  bgState[id].centerY := rotCenterY;
  bgState[id].angle := angle;
  bgState[id].dirty := true;	 
end;

procedure bgSetRotateScale(id, angle: cint; sx, sy: cint32); inline;
begin
  bgState[id].scaleX := sx;
  bgState[id].scaleY := sy;
  bgState[id].angle := angle;
  bgState[id].dirty := true;
end;

procedure bgSetScale(id: cint; sx, sy: cint32); inline;
begin 
  sassert( not bgIsText(id), 'Cannot Scale a Text Background');
  bgState[id].scaleX := sx;
  bgState[id].scaleY := sy;
  bgState[id].dirty := true;
end;

function bgInit(layer: cint; atype: BgType; size: BgSize; mapBase, tileBase: cint): cint; inline;
begin
  sassert( (layer >= 0) and (layer <= 3), 'Only layers 0 - 3 are supported');
  sassert( (tileBase >= 0) and (tileBase <= 15), 'Background tile base is out of range');
  sassert( (mapBase >=0) and (mapBase <= 31), 'Background Map Base is out of range');
  sassert( (layer <> 0) or not video3DEnabled(), 'Background 0 is currently being used for 3D display');
  sassert( (layer > 1) or (atype = BgType_Text8bpp) or (atype = BgType_Text4bpp), 'Incorrect background type for mode');
  //sassert((size != BgSize_B8_512x1024 && size != BgSize_B8_1024x512) || videoGetMode() == 6, "Incorrect background type for mode");
  sassert( (tileBase = 0) or (atype < BgType_Bmp8), 'Tile base is unused for bitmaps.  Can be offset using mapBase * 16KB');
  sassert( ((mapBase = 0) or (atype <> BgType_Bmp8)) or ((size <> BgSize_B8_512x1024) and (size <> BgSize_B8_1024x512)), 'Large Bitmaps cannot be offset');
  result := bgInit_call(layer, atype, size, mapBase, tileBase);	 
end;

function bgInitSub(layer: cint; atype: BgType; size: BgSize; mapBase, tileBase: cint): cint; inline;
begin
  sassert( (layer >= 0) and (layer <= 3), 'Only layers 0 - 3 are supported');
  sassert( (tileBase >= 0) and (tileBase <= 15), 'Background tile base is out of range');
  sassert( (mapBase >=0) and (mapBase <= 31), 'Background Map Base is out of range');
  sassert( (layer > 1) or (atype = BgType_Text8bpp) or (atype = BgType_Text4bpp), 'Incorrect background type for mode');
  sassert( ((size <> BgSize_B8_512x1024) and (size <> BgSize_B8_1024x512)), 'Sub Display has no large Bitmaps');
  result := bgInitSub_call(layer, atype, size, mapBase, tileBase);
end;

function bgSetControlBits(id: cint; bits: cuint16): pcuint16; inline;
begin
  bgControl[id]^ := bgControl[id]^ or bits;
  result := bgControl[id];
end;  
  
procedure bgClearControlBits(id: cint; bits: cuint16); inline;
begin
  bgControl[id]^ := bgControl[id]^ and not bits;	 
end;  

procedure bgWrapOn(id: cint); inline;
begin
  bgSetControlBits(id, BIT(13));
end;

procedure bgWrapOff(id: cint); inline;
begin
  bgClearControlBits(id, BIT(13));
end;

procedure bgSetPriority(id: cint; priority: cuint); inline;
begin
  sassert( (priority < 4), 'Priority must be less than 4');
  bgControl[id]^ := bgControl[id]^ and not 3;
  bgControl[id]^ := bgControl[id]^ or priority;	 	 
end;

procedure bgSetMapBase(id: cint; base: cuint); inline;
begin
  sassert( (base <= 31), 'Map base cannot exceed 31');
  bgControl[id]^ := bgControl[id]^ and not (31 shl MAP_BASE_SHIFT);
  bgControl[id]^ := bgControl[id]^ or (base shl MAP_BASE_SHIFT);
end;
    
procedure bgSetTileBase(id: cint; base: cuint); inline;	
begin
  sassert( (base <= 15), 'Tile base cannot exceed 15');
  bgControl[id]^ := bgControl[id]^ and not (15 shl TILE_BASE_SHIFT);
  bgControl[id]^ := bgControl[id]^ or (base shl TILE_BASE_SHIFT);
end;

procedure bgSetScrollf(id: cint; x, y: cint32); inline;
begin
  bgState[id].scrollX := x;
  bgState[id].scrollY := y;
  bgState[id].dirty := true;
end;

procedure bgSetScroll(id, x, y: cint); inline;
begin
  bgSetScrollf(id, x shl 8, y shl 8);
end;

procedure bgMosaicEnable(id: cint); inline;
begin
  bgControl[id]^ := bgControl[id]^ or BIT(6);
end;

procedure bgMosaicDisable(id: cint); inline;
begin
  bgControl[id]^ := bgControl[id]^ and not BIT(6);
end;

procedure bgSetMosaic(dx, dy: cuint); inline;
begin
  sassert( (dx < 16) and (dy < 16), 'Mosaic range is 0 to 15');
  MOSAIC_CR^ := MOSAIC_CR^ and not $FF;
  MOSAIC_CR^ := MOSAIC_CR^ or (dx or (dy shl 4));
end;

procedure bgSetMosaicSub(dx, dy: cuint); inline;
begin
  sassert( (dx < 16) and (dy < 16), 'Mosaic range is 0 to 15');
  SUB_MOSAIC_CR^ := SUB_MOSAIC_CR^ and not $FF;
  SUB_MOSAIC_CR^ := SUB_MOSAIC_CR^ or (dx or (dy shl 4));	 	 
end;



function bgGetPriority(id: cint): cint; inline;
begin
  result := bgControl[id]^ and 3;
end;

function bgGetMapBase(id: cint): cint; inline;
begin
  result := (bgControl[id]^ shr MAP_BASE_SHIFT) and 31;
end;


function bgGetTileBase(id: cint): cint; inline;
begin
  result := (bgControl[id]^ shr TILE_BASE_SHIFT) and 31;
end;

function bgGetMapPtr(id: cint): pcuint16; inline;
begin
  if (id < 4) then 
    result := pcuint16(BG_MAP_RAM(bgGetMapBase(id))) 
  else
    result := pcuint16(BG_MAP_RAM_SUB(bgGetMapBase(id)));
end;

function bgGetGfxPtr(id: cint): pcuint16; inline;
begin
  if(bgState[id]._type < integer(BgType_Bmp8)) then 
  begin
    if (id < 4) then 
      result := pcuint16(BG_TILE_RAM(bgGetTileBase(id))) 
    else 
      result := pcuint16(BG_TILE_RAM_SUB(bgGetTileBase(id)));	         
  end else	                 
  begin
    if (id < 4) then
      result := pointer(BG_GFX + $2000 * (bgGetMapBase(id))) 
    else 
      result := pointer(BG_GFX_SUB + $2000 * (bgGetMapBase(id)));
  end;
end;

procedure bgScrollf(id: cint; dx, dy: cint32); inline;
begin
  bgSetScrollf(id, bgState[id].scrollX + dx, bgState[id].scrollY + dy);
end;

procedure bgScroll(id, dx, dy: cint); inline;
begin
  bgSetScrollf(id, dx shl 8, dy shl 8);
end;

procedure bgShow(id: cint); inline;
begin
  if (id < 4) then
    videoBgEnable(id)
  else
    videoBgEnableSub(id and 3);
end;

procedure bgHide(id: cint); inline;
begin
  if (id < 4) then
    videoBgDisable(id)
  else
    videoBgDisableSub(id and 3);
end;

procedure bgSetCenterf(id: cint; x, y: cint32); inline;
begin
  sassert(not bgIsText(id), 'Text Backgrounds have no Center of Rotation');
  bgState[id].centerX := x;
  bgState[id].centerY := y;
  bgState[id].dirty := true;
end;

procedure bgSetCenter(id, x, y: cint); inline;
begin
  bgSetCenterf(id, x shl 8, y shl 8);
end;

{$endif NDS_IMPLEMENTATION}