-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.c
150 lines (140 loc) · 3 KB
/
console.c
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
#include <fxlib.h>
#include <stdarg.h>
#include "MonochromeLib.h"
#include "console.h"
#define BUFFER_SIZE 1024
char inputBuffer[BUFFER_SIZE], *inputPtr;
int inputSize=0;
int cur;
static int SysCallCode[] = {0xD201422B,0x60F20000,0x80010070};
static int (*SysCall)( int R4, int R5, int R6, int R7, int FNo ) = (void*)&SysCallCode;
void Cursor_SetFlashMode(long flashmode)
{
(*SysCall)(flashmode, 0, 0, 0, 0x13A);
}
void clear()
{
inputSize = 0;
cur = 0;
ML_clear_vram();
}
void putchar(char c)
{
if(c == '\n') {
cur = 21 * (cur/21 + 1);
} else {
locate((cur%21)+1, (cur/21)+1);
PrintC(&c);
cur++;
}
if(cur >= 8*21) {
ML_vertical_scroll(-8);
cur -= 21;
locate(1, 8);
Print(" ");
locate(1, 8);
}
ML_display_vram();
}
void print(char* str, ...)
{
va_list ap;
va_start(ap, str);
for( ; *str ; str++)
if(*str == '%') {
switch(*(++str)) {
case '%': putchar('%'); break;
case 'c': putchar(va_arg(ap, char)); break;
case 'i': print_int(va_arg(ap, int)); break;
}
} else putchar(*str);
va_end(ap);
}
void print_int(int n)
{
int i;
if(n < 0) putchar('-'), n = -n;
for(i=1 ; n/i ; i*=10);
for(i/=10 ; i ; i/=10) putchar((n/i)%10+'0');
}
char getchar()
{
if(inputSize) {
inputSize--;
inputPtr++;
} else {
unsigned int key = 0;
inputPtr = inputBuffer;
locate((cur%21)+1, (cur/21)+1);
Cursor_SetFlashMode(1);
while(IsKeyDown(KEY_CTRL_EXE));
while(key != KEY_CTRL_EXE) {
GetKey(&key);
if(key == KEY_CTRL_DEL && inputSize) {
cur--;
putchar(' ');
cur--;
locate((cur%21)+1, (cur/21)+1);
inputSize--;
inputPtr--;
}
if (key==KEY_CHAR_MULT && inputSize<BUFFER_SIZE-1) {
putchar('*');
*inputPtr = '*';
inputPtr++;
inputSize++;
} else if (key==KEY_CHAR_DIV && inputSize<BUFFER_SIZE-1) {
putchar('/');
*inputPtr = '/';
inputPtr++;
inputSize++;
} else if (key==KEY_CHAR_PLUS && inputSize<BUFFER_SIZE-1) {
putchar('+');
*inputPtr = '+';
inputPtr++;
inputSize++;
} else if (key==KEY_CHAR_MINUS && inputSize<BUFFER_SIZE-1) {
putchar('-');
*inputPtr = '-';
inputPtr++;
inputSize++;
} else if (key==KEY_CTRL_XTT && inputSize<BUFFER_SIZE-1) {
putchar('x');
*inputPtr = 'x';
inputPtr++;
inputSize++;
} else if (key==KEY_CHAR_POW && inputSize<BUFFER_SIZE-1) {
putchar('^');
*inputPtr = '^';
inputPtr++;
inputSize++;
} else if (key==KEY_CHAR_SQUARE && inputSize<BUFFER_SIZE-1) {
putchar('^');
*inputPtr = '^';
inputPtr++;
inputSize++;
putchar('2');
*inputPtr = '2';
inputPtr++;
inputSize++;
} else if (key==KEY_CTRL_EXIT && inputSize<BUFFER_SIZE-1) {
putchar('q');
*inputPtr = 'q';
inputPtr++;
inputSize++;
break;
} else if(key < 256 && inputSize<BUFFER_SIZE-1) {
putchar(key);
*inputPtr = key;
inputPtr++;
inputSize++;
}
}
putchar('\n');
*inputPtr = '\n';
inputPtr = inputBuffer;
Cursor_SetFlashMode(0);
while(IsKeyDown(KEY_CTRL_EXE));
}
return *inputPtr;
}