-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer_interface.py
More file actions
188 lines (143 loc) · 5.2 KB
/
computer_interface.py
File metadata and controls
188 lines (143 loc) · 5.2 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
This script serves as a very basic magic BIOS and "operating system" for the FVC (Fun Virtual Computer).
In the future, I will write the operating system on the computer itself rather than in Python
"""
import os
from components import bus, keyboard
from random import randint
# The virtual OS uses pygame
import pygame
pygame.init()
# Get useful values
resolution = bus.vid.true_resolution
colour_bound = bus.vid.colour_bound
text_bound = bus.vid.text_bound
palette_bound = bus.vid.palette_bound
mode_bound = bus.vid.mode_bound
ram_bound = bus.mapping["vram"][0]
def os_msg(status_code, *args):
status_messages = [
"Booting...",
"File not found",
"Bad header"
]
if status_code not in range(0, len(status_messages)):
msg = "Unknown status code"
else:
msg = status_messages[status_code]
print("Message:", msg, *args)
def power_on():
# Show boot message
os_msg(0)
# Load default palette
default_palette = open("files/default_palette.txt", 'r').read().split('\n')
default_palette = [int(bs, 2) for bs in default_palette]
default_palette_bytes = bytearray(len(default_palette))
for i in range(len(default_palette)):
default_palette_bytes[i] = default_palette[i]
bus.io(1, ram_bound + text_bound, default_palette_bytes)
# Display boot image on screen
draw_coords = open("files/boot_img.txt", 'r').read().split('\n')
test_img_bitstring = ""
for i in range(resolution[0] * resolution[1]):
if str(i) in draw_coords:
test_img_bitstring += '001'
else:
test_img_bitstring += '000'
test_img_bitstring = [test_img_bitstring[i:i + 8] for i in range(0, len(test_img_bitstring), 8)]
test_img_ints = [int(v, 2) for v in test_img_bitstring]
test_img = bytearray(len(test_img_ints))
for i in range(len(test_img_ints)):
test_img[i] = test_img_ints[i]
bus.io(1, ram_bound, test_img)
refresh_display()
# Play test sound
pass
# Load font
font = open("files/font2.bgt", 'rb').read()
bus.io(1, 532, font)
# Enter text mode
newmode = 1
bus.io(1, ram_bound + palette_bound, newmode.to_bytes(1, "little"))
# Start the operating system
await_input()
# Very basic operating system for the virtual computer written in Python, of course
# Eventually, I'd like to write the operating system on the machine itself
def await_input():
while True:
x = None
x = input("? ")
if x == "randimg":
rand_img = bytearray(colour_bound)
for i in range(len(rand_img)):
rand_img[i] = randint(0, 255)
bus.io(1, ram_bound, rand_img)
elif x == "randpal":
rand_p = bytearray(8)
for i in range(len(rand_p)):
rand_p[i] = randint(0, 255)
bus.io(1, ram_bound + text_bound, rand_p)
elif x == "testimg":
test_image = bytearray(colour_bound)
for i in range(len(test_image)):
test_image[i] = int(i * 255 / 24000)
bus.io(1, ram_bound, test_image)
elif x == "loadprog":
path = input(" path: ")
if not os.path.isfile(path):
os_msg(1)
quit()
prog = open(path, 'rb').read()
header = prog[:4]
if header[:3].decode("ASCII", "ignore") != "9I6":
os_msg(2, *header)
quit()
prog = prog[4:] # Discard the 4-byte header
bus.processor.process_instructions(prog)
elif x == "showgvram":
memcpy = bus.io(2, ram_bound, colour_bound-ram_bound)
print(*memcpy)
elif x == "showtvram":
memcpy = bus.io(2, colour_bound+ram_bound, text_bound-colour_bound)
print(*memcpy)
elif x == "showram":
memcpy = bus.io(2, 0, ram_bound)
print(*memcpy)
elif x == "showins":
memcpy = bus.io(2, 23, 2)
print(*memcpy)
elif x == "showpal":
memcpy = bus.io(2, palette_bound-8+ram_bound, 8)
print(*memcpy)
elif x == "textmode":
# Set mode byte
newmode = 1
bus.io(1, ram_bound + palette_bound, newmode.to_bytes(1, "little"))
elif x == "graphicsmode":
newmode = 0
bus.io(1, ram_bound + palette_bound, newmode.to_bytes(1, "little"))
elif x == "loadfont":
# Load font into RAM
font = open("files/font2.bgt", 'rb').read()
bus.io(1, 532, font)
elif x == "clearram":
# Clear program memory, but not the first 32 bytes of ram, or VRAM
bus.io(1, 32, bytes(999-32))
elif x == "quit":
quit()
else:
pass
#print("unknown command")
refresh_keyboard()
refresh_display()
def refresh_display():
#gvram = bus.io(2, ram_bound, mode_bound)
#bus.io(1, ram_bound, gvram)
bus.vid.refresh()
def refresh_keyboard():
# Handle inputs using the virtual keyboard driver
pygame_events = pygame.event.get()
for e in pygame_events:
if e.type == pygame.KEYDOWN:
keyboard.parse_keys(e)
power_on()