Subversion Repositories Shiroi

Rev

Rev 1 | Rev 3 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 nishi 1
/* $Id: shiroi.c 2 2024-08-28 08:12:18Z nishi $ */
2
 
3
unsigned char inp(unsigned char);
4
void outp(unsigned char, unsigned char);
5
void write_vram(unsigned short);
6
void putchar(char);
7
void putstr(char*);
8
void scroll_y(void);
9
void beep(void);
10
void _beep(unsigned long howlong);
11
void init_cards(void);
12
 
13
unsigned short posx;
14
unsigned short posy;
15
 
16
short vdp_addr;
17
short vdp_data;
18
short psg_addr;
19
short psg_data;
20
short fpu_addr;
21
short fpu_data;
22
 
2 nishi 23
void main(void){
1 nishi 24
	int i;
25
 
26
	posx = 0;
27
	posy = 0;
28
 
29
	vdp_addr = -1;
30
	psg_addr = -1;
31
	fpu_addr = -1;
32
 
33
	init_cards();
34
 
2 nishi 35
	if(vdp_addr == -1){
1 nishi 36
		int i;
2 nishi 37
		for(i = 0; i < 3; i++){
1 nishi 38
			_beep(3L * 1024);
39
			unsigned long j;
2 nishi 40
			for(j = 0; j < 3L * 1024; j++);
1 nishi 41
		}
2 nishi 42
		while(1);
1 nishi 43
	}
2 nishi 44
 
1 nishi 45
	outp(vdp_addr, 0x00);
46
	outp(vdp_addr, 0x80);
47
 
48
	outp(vdp_addr, 0xd0);
49
	outp(vdp_addr, 0x81);
50
 
51
	outp(vdp_addr, 0x02);
52
	outp(vdp_addr, 0x82);
53
 
54
	outp(vdp_addr, 0x00);
55
	outp(vdp_addr, 0x84);
56
 
57
	outp(vdp_addr, 0xf4);
58
	outp(vdp_addr, 0x87);
59
 
60
	write_vram(0);
61
	for(i = 0; i < 0x800; i++) outp(vdp_data, *((unsigned char*)(0x2000 + i)));
62
 
63
	beep();
64
 
65
	putstr("Shiroi Microcomputer BASIC\r\n");
2 nishi 66
	if(fpu_addr == -1){
1 nishi 67
		putstr("Math Card Mark I not present\r\n");
2 nishi 68
	}else{
1 nishi 69
		putstr("Math Card Mark I present\r\n");
70
	}
2 nishi 71
	if(psg_addr == -1){
1 nishi 72
		putstr("Sound Card Mark I not present\r\n");
2 nishi 73
	}else{
1 nishi 74
		putstr("Sound Card Mark I present\r\n");
75
	}
76
 
77
	write_vram(0x800 + 40 * 12);
78
	for(i = 0; i < 0x100; i++) outp(vdp_data, i);
79
 
2 nishi 80
	while(1);
1 nishi 81
}
82
 
2 nishi 83
void init_cards(void){
1 nishi 84
	int i;
85
	int port = 2;
2 nishi 86
	for(i = 0; i < 256 / 3; i++){
1 nishi 87
		int t = inp(port);
2 nishi 88
		if(t != 0){
89
			if(t == 0x01){
1 nishi 90
				vdp_addr = port - 2;
91
				vdp_data = port - 1;
2 nishi 92
			}else if(t == 0x11){
1 nishi 93
				psg_addr = port - 2;
94
				psg_data = port - 1;
2 nishi 95
			}else if(t == 0x21){
1 nishi 96
				fpu_addr = port - 2;
97
				fpu_data = port - 1;
98
			}
99
		}
100
		port += 3;
101
	}
102
}
103
 
2 nishi 104
void beep(void){
105
	_beep(3L * 1024);
106
}
1 nishi 107
 
2 nishi 108
void _beep(unsigned long howlong){
1 nishi 109
	if(psg_addr == -1) return;
110
	unsigned long i;
111
 
112
	outp(psg_addr, 8);
113
	outp(psg_data, 0x0f);
114
 
115
	outp(psg_addr, 0);
116
	outp(psg_data, 0xd6);
117
 
118
	outp(psg_addr, 1);
119
	outp(psg_data, 0x0);
120
 
121
	outp(psg_addr, 7);
122
	outp(psg_data, 0x3e);
123
 
2 nishi 124
	for(i = 0; i < howlong; i++);
1 nishi 125
 
126
	outp(psg_addr, 7);
127
	outp(psg_data, 0x3f);
128
}
129
 
2 nishi 130
void write_vram(unsigned short addr){
1 nishi 131
	addr |= 0x4000;
132
	outp(vdp_addr, addr & 0x00ff);
133
	outp(vdp_addr, ((addr & 0xff00) >> 8));
134
}
135
 
2 nishi 136
void read_vram(unsigned short addr){
1 nishi 137
	outp(vdp_addr, addr & 0x00ff);
138
	outp(vdp_addr, ((addr & 0xff00) >> 8));
139
}
140
 
2 nishi 141
void scroll_y(void){
1 nishi 142
	int i;
2 nishi 143
	for(i = 0; i < 40 * 23; i++){
1 nishi 144
		read_vram(0x800 + i + 40);
145
		unsigned char ch = inp(vdp_data);
146
		write_vram(0x800 + i);
147
		outp(vdp_data, ch);
148
	}
2 nishi 149
	for(i = 0; i < 40; i++){
1 nishi 150
		outp(vdp_data, 0);
151
	}
152
}
153
 
2 nishi 154
void putchar(char c){
155
	if(c == '\r'){
1 nishi 156
		posx = 0;
2 nishi 157
	}else if(c == '\n'){
1 nishi 158
		posy++;
2 nishi 159
	}else{
1 nishi 160
		write_vram(0x800 + posy * 40 + posx);
161
		outp(vdp_data, c);
162
		posx++;
2 nishi 163
		if(posx == 40){
1 nishi 164
			posx = 0;
165
			posy++;
166
		}
167
	}
2 nishi 168
	if(posy == 24){
1 nishi 169
		scroll_y();
170
		posy = 23;
171
		posx = 0;
172
	}
173
}
174
 
2 nishi 175
void putstr(char* str){
1 nishi 176
	int i;
177
	for(i = 0; str[i] != 0; i++) putchar(str[i]);
178
}
179
 
2 nishi 180
unsigned char inp(unsigned char port) __naked {
181
__asm
182
	ld      hl,#2
183
	add     hl,sp
184
	ld      c,(hl)
185
	in      a,(c)
186
	ld      l,a
187
	ld      h,#0
188
	ret
189
__endasm;
190
}
1 nishi 191
 
2 nishi 192
void outp(unsigned char port, unsigned char data) __naked {
193
__asm
194
	push bc
195
	ld      hl,#4
196
	add     hl,sp
197
	ld      c,(hl)
198
	inc     hl
199
	ld      b,(hl)
200
	out     (c),b
201
	pop     bc
202
	ret
203
__endasm;
204
}