v1.0.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
bg0rom.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * This file is part of the public interface to the Sifteo SDK.
4  * Copyright <c> 2012 Sifteo, Inc. All rights reserved.
5  */
6 
7 #pragma once
8 #ifdef NOT_USERSPACE
9 # error This is a userspace-only header, not allowed by the current build.
10 #endif
11 
12 #include <sifteo/abi.h>
13 #include <sifteo/macros.h>
14 #include <sifteo/math.h>
15 
16 namespace Sifteo {
17 
35  _SYSAttachedVideoBuffer sys;
36 
40  enum Palette {
41  BLACK_ON_WHITE = 0 << 10,
42  BLUE_ON_WHITE = 1 << 10,
43  ORANGE_ON_WHITE = 2 << 10,
44  YELLOW_ON_BLUE = 3 << 10,
45  RED_ON_WHITE = 4 << 10,
46  GRAY_ON_WHITE = 5 << 10,
47  WHITE_ON_BLACK = 6 << 10,
48  WHITE_ON_BLUE = 7 << 10,
49  WHITE_ON_TEAL = 8 << 10,
50  BLACK_ON_YELLOW = 9 << 10,
51  DKGRAY_ON_LTGRAY = 10 << 10,
52  GREEN_ON_WHITE = 11 << 10,
53  WHITE_ON_GREEN = 12 << 10,
54  PURPLE_ON_WHITE = 13 << 10,
55  LTBLUE_ON_DKBLUE = 14 << 10,
56  GOLD_ON_WHITE = 15 << 10,
57 
58  // Aliases for white background
59  BLACK = BLACK_ON_WHITE,
60  BLUE = BLUE_ON_WHITE,
61  ORANGE = ORANGE_ON_WHITE,
62  RED = RED_ON_WHITE,
63  GRAY = GRAY_ON_WHITE,
64  GREEN = GREEN_ON_WHITE,
65  PURPLE = PURPLE_ON_WHITE,
66  GOLD = GOLD_ON_WHITE,
67  };
68 
72  enum Tiles {
73  FONT_SPACE = 0,
74  SOLID_BG = 0,
75  SOLID_FG = 104,
76  V_BARGRAPH = 224,
77  H_BARGRAPH = 231,
78  };
79 
83  enum ColorMode {
84  TWO_COLOR = 0 << 9,
85  FOUR_COLOR = 1 << 9,
86  };
87 
91  static unsigned tileWidth() {
92  return _SYS_VRAM_BG0_WIDTH;
93  }
94 
98  static unsigned tileHeight() {
99  return _SYS_VRAM_BG0_WIDTH;
100  }
101 
105  static UInt2 tileSize() {
106  return vec(tileWidth(), tileHeight());
107  }
108 
112  static unsigned pixelWidth() {
113  return tileWidth() * 8;
114  }
115 
119  static unsigned pixelHeight() {
120  return tileHeight() * 8;
121  }
122 
126  static UInt2 pixelSize() {
127  return vec(pixelWidth(), pixelHeight());
128  }
129 
133  static unsigned sizeInBytes() {
134  return tileWidth() * tileHeight() * 2;
135  }
136 
140  static unsigned sizeInWords() {
141  return tileWidth() * tileHeight();
142  }
143 
149  uint16_t tileAddr(UInt2 pos) {
150  return pos.x + pos.y * tileWidth();
151  }
152 
157  void erase(uint16_t index = 0) {
158  _SYS_vbuf_fill(&sys.vbuf, 0, _SYS_TILE77(index), sizeInWords());
159  setPanning(vec(0,0));
160  }
161 
170  void setPanning(Int2 pixels) {
171  _SYS_vbuf_poke(&sys.vbuf, offsetof(_SYSVideoRAM, bg0_x) / 2,
172  umod(pixels.x, pixelWidth()) |
173  (umod(pixels.y, pixelHeight()) << 8));
174  }
175 
180  Int2 getPanning() const {
181  unsigned word = _SYS_vbuf_peek(&sys.vbuf, offsetof(_SYSVideoRAM, bg0_x) / 2);
182  return vec<int>(word & 0xFF, word >> 8);
183  }
184 
188  static uint16_t charTile(char c, enum Palette palette = BLACK) {
189  return palette ^ (c - ' ' + FONT_SPACE);
190  }
191 
197  void plot(UInt2 pos, uint16_t tileIndex) {
198  ASSERT(pos.x < tileWidth() && pos.y < tileHeight());
199  _SYS_vbuf_poke(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex));
200  }
201 
208  void span(UInt2 pos, unsigned width, unsigned tileIndex)
209  {
210  ASSERT(pos.x <= tileWidth() && width <= tileWidth() &&
211  (pos.x + width) <= tileWidth() && pos.y < tileHeight());
212  _SYS_vbuf_fill(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex), width);
213  }
214 
221  void fill(UInt2 topLeft, UInt2 size, unsigned tileIndex)
222  {
223  while (size.y) {
224  span(topLeft, size.x, tileIndex);
225  size.y--;
226  topLeft.y++;
227  }
228  }
229 
238  void hBargraph(Int2 topLeft, unsigned pixelWidth,
239  enum Palette palette = BLACK, unsigned tileHeight = 1)
240  {
241  unsigned addr = tileAddr(topLeft);
242  int wTiles = pixelWidth / 8;
243  int wRemainder = pixelWidth % 8;
244 
245  while (tileHeight--) {
246  _SYS_vbuf_fill(&sys.vbuf, addr,
247  _SYS_TILE77(palette ^ SOLID_FG), wTiles);
248  if (wRemainder)
249  _SYS_vbuf_poke(&sys.vbuf, addr + wTiles,
250  _SYS_TILE77(palette ^ (H_BARGRAPH + wRemainder - 1)));
251  addr += tileWidth();
252  }
253  }
254 
262  void text(Int2 topLeft, const char *str, enum Palette palette = BLACK)
263  {
264  unsigned addr = tileAddr(topLeft);
265  unsigned lineAddr = addr;
266  char c;
267 
268  while ((c = *str)) {
269  if (c == '\n')
270  addr = (lineAddr += tileWidth());
271  else
272  _SYS_vbuf_poke(&sys.vbuf, addr++, _SYS_TILE77(charTile(c, palette)));
273  str++;
274  }
275  }
276 
280  _SYSVideoBuffer &videoBuffer() {
281  return sys.vbuf;
282  }
283 
287  CubeID cube() const {
288  return sys.cube;
289  }
290 };
291 
296 }; // namespace Sifteo
297