v1.1.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  HEART = 128,
79  FROWN = 136,
80  SMILE = 152,
81  SMILE2 = 141,
82  RIGHT_ARROW = 125,
83  LEFT_ARROW = 126,
84  };
85 
89  enum ColorMode {
90  TWO_COLOR = 0 << 9,
91  FOUR_COLOR = 1 << 9,
92  };
93 
97  static unsigned tileWidth() {
98  return _SYS_VRAM_BG0_WIDTH;
99  }
100 
104  static unsigned tileHeight() {
105  return _SYS_VRAM_BG0_WIDTH;
106  }
107 
111  static UInt2 tileSize() {
112  return vec(tileWidth(), tileHeight());
113  }
114 
118  static unsigned pixelWidth() {
119  return tileWidth() * 8;
120  }
121 
125  static unsigned pixelHeight() {
126  return tileHeight() * 8;
127  }
128 
132  static UInt2 pixelSize() {
133  return vec(pixelWidth(), pixelHeight());
134  }
135 
139  static unsigned sizeInBytes() {
140  return tileWidth() * tileHeight() * 2;
141  }
142 
146  static unsigned sizeInWords() {
147  return tileWidth() * tileHeight();
148  }
149 
155  uint16_t tileAddr(UInt2 pos) {
156  return pos.x + pos.y * tileWidth();
157  }
158 
163  void erase(uint16_t index = 0) {
164  _SYS_vbuf_fill(&sys.vbuf, 0, _SYS_TILE77(index), sizeInWords());
165  setPanning(vec(0,0));
166  }
167 
176  void setPanning(Int2 pixels) {
177  _SYS_vbuf_poke(&sys.vbuf, offsetof(_SYSVideoRAM, bg0_x) / 2,
178  umod(pixels.x, pixelWidth()) |
179  (umod(pixels.y, pixelHeight()) << 8));
180  }
181 
186  Int2 getPanning() const {
187  unsigned word = _SYS_vbuf_peek(&sys.vbuf, offsetof(_SYSVideoRAM, bg0_x) / 2);
188  return vec<int>(word & 0xFF, word >> 8);
189  }
190 
194  static uint16_t charTile(char c, enum Palette palette = BLACK) {
195  return palette ^ (c - ' ' + FONT_SPACE);
196  }
197 
203  void plot(UInt2 pos, uint16_t tileIndex) {
204  ASSERT(pos.x < tileWidth() && pos.y < tileHeight());
205  _SYS_vbuf_poke(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex));
206  }
207 
214  void span(UInt2 pos, unsigned width, unsigned tileIndex)
215  {
216  ASSERT(pos.x <= tileWidth() && width <= tileWidth() &&
217  (pos.x + width) <= tileWidth() && pos.y < tileHeight());
218  _SYS_vbuf_fill(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex), width);
219  }
220 
227  void fill(UInt2 topLeft, UInt2 size, unsigned tileIndex)
228  {
229  while (size.y) {
230  span(topLeft, size.x, tileIndex);
231  size.y--;
232  topLeft.y++;
233  }
234  }
235 
244  void hBargraph(Int2 topLeft, unsigned pixelWidth,
245  enum Palette palette = BLACK, unsigned tileHeight = 1)
246  {
247  unsigned addr = tileAddr(topLeft);
248  int wTiles = pixelWidth / 8;
249  int wRemainder = pixelWidth % 8;
250 
251  while (tileHeight--) {
252  _SYS_vbuf_fill(&sys.vbuf, addr,
253  _SYS_TILE77(palette ^ SOLID_FG), wTiles);
254  if (wRemainder)
255  _SYS_vbuf_poke(&sys.vbuf, addr + wTiles,
256  _SYS_TILE77(palette ^ (H_BARGRAPH + wRemainder - 1)));
257  addr += tileWidth();
258  }
259  }
260 
268  void text(Int2 topLeft, const char *str, enum Palette palette = BLACK)
269  {
270  unsigned addr = tileAddr(topLeft);
271  unsigned lineAddr = addr;
272  char c;
273 
274  while ((c = *str)) {
275  if (c == '\n')
276  addr = (lineAddr += tileWidth());
277  else
278  _SYS_vbuf_poke(&sys.vbuf, addr++, _SYS_TILE77(charTile(c, palette)));
279  str++;
280  }
281  }
282 
286  _SYSVideoBuffer &videoBuffer() {
287  return sys.vbuf;
288  }
289 
293  CubeID cube() const {
294  return sys.cube;
295  }
296 };
297 
302 }; // namespace Sifteo
303