llvm-mos-sdk
osi_screen.h
Go to the documentation of this file.
1 #ifndef _OSI_SCREEN_H
2 #define _OSI_SCREEN_H
3 
4 #include <string.h>
5 
6 template <unsigned int scr_base_uint = 0xD000, // start address of video RAM
7  unsigned int video_ram_size = 0x400, // size of video RAM
8  unsigned int screen_width = 0x1B, // screen width
9  unsigned int screen_height = 0x1B, // screen height
10  unsigned int screen_firstchar =
11  0x85, // offset of cursor position (0, 0) from base of video RAM
12  unsigned int scroll_dist =
13  0x20 // memory distance for scrolling by one line
14  >
15 class __osi_screen {
16 private:
17  static unsigned char cursor_x;
18  static unsigned char cursor_y;
19 
20  static constexpr unsigned int scroll_length(void) {
21  return (screen_height - 1) * scroll_dist;
22  }
23 
24  static constexpr char *scr_base(void) { return (char *)scr_base_uint; }
25 
26  static char *cursor_pos_mem(void) {
27  return scr_base() + screen_firstchar + cursor_y * scroll_dist + cursor_x;
28  }
29 
30  static void newline(void) {
31  cursor_y += 1;
32 
33  if (cursor_y == screen_height) {
34  // Bottom of screen reached, scroll
35  cursor_y -= 1;
36 
37  char *const scr_firstchar_addr = scr_base() + screen_firstchar;
38  char *const scroll_dest = scr_firstchar_addr;
39  char *const scroll_src = scr_firstchar_addr + scroll_dist;
40 
41  memmove(scroll_dest, scroll_src, scroll_length());
42 
43  // Address of first character in last line of screen
44  char *const last_line_start = scr_firstchar_addr + scroll_length();
45 
46  // Fill last line with blanks
47  memset(last_line_start, ' ', screen_width);
48  }
49  }
50 
51 public:
52  static void clrscr(void) {
53  memset((void *)scr_base(), ' ', video_ram_size);
54  cursor_x = cursor_y = 0;
55  }
56 
57  static void cputc(char c) {
58  if (c == '\n') {
59  newline();
60  } else if (c == '\r') {
61  cursor_x = 0;
62  } else {
63  char *const dest = cursor_pos_mem();
64  *dest = c;
65  if (cursor_x >= screen_width - 1) {
66  newline();
67  cursor_x = 0;
68  } else {
69  cursor_x += 1;
70  }
71  }
72  }
73 };
74 
75 extern "C" void __putchar(char c);
76 
77 #endif // _OSI_SCREEN_H
string.h
memmove
void * memmove(void *s1, const void *s2, size_t n)
memset
void * memset(void *ptr, int value, size_t num)
c
byte byte c
Definition: api.h:59