summaryrefslogtreecommitdiff
path: root/ROMs/typewriter.c
blob: 08865320ce38448ca4c46c37370ce0f1cedd13c9 (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
#load "font.bin"

int drawChar(int ch, int x, int y, int color)
{
    // adjust for jmp instruction
    char* fontStart = 0x1FFF + 2;
    int charStart = ch * 8;

    for (int yOff = 0; yOff < 8; yOff++)
    {
        int row = fontStart[charStart + yOff];

        for (int xOff = 0; xOff < 8; xOff++)
        {
            int bit = (row >> (7 - xOff)) & 1;
            int pix = bit ? 8 : color;
            setPixel(x + xOff, y + yOff, pix);
        }
    }
}

int setPixel(int x, int y, int c)
{
    char* screenStart = 0x402F;
    int i = (y * 128) + x;
    screenStart[i] = c;
}

int clear(int c)
{
    char* screenStart = 0x402F;
    char screenSize = 64 * 128;
    for (int i = 0; i < screenSize; i++)
    {
        screenStart[i] = c;
    }
}

int getKeyState(int scanCode)
{
    char* hid = 0x6033;
    int index = scanCode / 8;
    int bit   = scanCode % 8;

    int val = hid[index - 1];
    return (val & (1 << bit)) != 0;
}

int print(int* text, int len, int x, int y, int color)
{
    for (int i = 0; i < len; i++)
    {
        int ch = text[i];
        drawChar(ch, x + (i * 8), y, color);
    }
}

int main() 
{
    int x = 0;
    int y = 0;
    while (1)
    {
        for (int sk = 0; sk < 256; sk++)
        {
            if (getKeyState(sk))
            {
                while (getKeyState(sk)) { }
                // Newline pressed
                if (sk == 13)
                {
                    y += 8;
                    x = 0;
                    continue;
                }
                // Backspace pressed
                if (sk == 8)
                {
                    x -= 8;
                    if (x < 0)
                        x = 0;
                    int* text = " ";
                    print(text, 1, x, y, 1);
                    continue;
                }
                
                int* text = new int[] { sk };
                print(text, 1, x, y, 1);
                x += 8;
            }
        }

        if (x >= 128)
        {
            x = 0;
            y += 8;
        }

        if (y >= 64)
        {
            x = 0;
            y = 0;
            clear(8);
        }
        
    }
}