-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifsrv.py
executable file
·170 lines (136 loc) · 4.3 KB
/
fifsrv.py
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
#!/usr/bin/env python3
##
# fifsrv.py
#
# Copyright (C) David McNaughton 2023-present
#
# a utility to remotely mount disk image files (*.dsk) to the IMSAI8080esp
# using the RESTful interface provided for IO and DMA
#
# dependencies:
# python3
# requests (module) - to install, use: pip install requests
# simple_http_server (module) - to install, use: pip install simple_http_server
#
# TODO:
# - add more error detection and return more error codes
#
# known issues:
# - TBD
#
# history:
# 13-JUL-2023 1.0 Initial release
##
import requests
import sys
import os
import socket
from simple_http_server import route, server, Response, ModelDict, logger
logger.set_level("ERROR")
srv = os.path.splitext(os.path.basename(sys.argv[0]))[0]
FIF_PORT = 0xFD
SRV_PORT = 3000
SRV_PATH = srv
hosturl = 'http://imsai8080'
_srvurl = f'http://{socket.gethostname()}:{SRV_PORT}/{SRV_PATH}'
disks = { 'A': 'cpm22b01.dsk', 'B': 'comms.dsk', 'C': 'dazzler.dsk', 'D': 'ZorkI.dsk' }
disk_to_unit = { 'A': 1, 'B': 2, 'C': 4, 'D': 8, 'I': 15 }
sel_units = [ ]
unit_file = { }
def main():
print('DISKS:')
for d in disks:
sel_units.append(disk_to_unit[d])
unit_file[disk_to_unit[d.upper()]] = disks[d]
print(f'\tDSK:{d.upper()}: = {disks[d]}')
sys_get = requests.patch(f'{hosturl}/io?p=-{FIF_PORT:02X}', data=_srvurl)
if sys_get.status_code == 200:
print(f'Listening and registered on Port {FIF_PORT:02X}h to {sys_get.text}')
## DONT RUN THIS IN A VM OR THE HOST CAN'T BE SEEN
server.start(host="", port=SRV_PORT)
@route(f'/{SRV_PATH}', method="PUT")
def io_out(p, m=ModelDict()):
port = int(p, 16)
#BODY is a little hard to get as it ends up as the first KEY in the DICT m
data = int(next(iter(m)), 16)
# print(f'{port:02X} {data:02X}')
if port == FIF_PORT:
t = fif_out(data)
if t == 1:
return Response(status_code=201)
return #normal 200 response
fdstate = 0
descno = 0
fdaddr = [0] * 16
def fif_out(data):
global descno
global fdstate
global fdaddr
res = 0
if fdstate == 0:
op = data & 0xF0
if op == 0x00:
descno = data & 0x0F
res = disk_io(fdaddr[descno])
elif op == 0x10:
descno = data & 0x0F
fdstate += 1
elif fdstate == 1:
fdaddr[descno] = data
fdstate += 1
elif fdstate == 2:
fdaddr[descno] += data << 8
# print(f'Descriptor={descno} addr={fdaddr[descno]:04X}')
fdstate = 0
else:
print(f'Internal error fdstate={fdstate}')
fdstate = 0
return res
cmd_str = [ "", "WRITE", "READ", "FORMAT", "VERIFY" ]
SEC_SZ = 128
SPT8 = 26
def disk_io(addr):
dma_get = requests.get(f'{hosturl}/dma?m={addr:04X}&n=7')
mem = dma_get.content
unit = mem[0] & 0x0F
cmd = mem[0] >> 4
res = mem[1]
fmt = mem[2]
track = mem[3]
sector = mem[4]
dma_addr = (mem[6] << 8) + mem[5]
# print(f'{cmd_str[cmd]} {unit}:{track}:{sector} <-> {dma_addr:04X}')
if unit in sel_units:
if cmd == 1:
fd = open(unit_file[unit], 'r+b')
pos = (track * SPT8 + sector - 1) * SEC_SZ
sec_get = requests.get(f'{hosturl}/dma?m={dma_addr:04X}&n={SEC_SZ:02X}')
blksec = sec_get.content
fd.seek(pos)
fd.write(blksec)
fd.close()
disk_res = bytes.fromhex('01')
elif cmd == 2:
fd = open(unit_file[unit], 'rb')
pos = (track * SPT8 + sector - 1) * SEC_SZ
fd.seek(pos)
block = fd.read(SEC_SZ)
sec_put = requests.put(f'{hosturl}/dma?m={dma_addr:04X}&n={SEC_SZ:02X}', data=block)
fd.close()
disk_res = bytes.fromhex('01')
else:
disk_res = bytes.fromhex('A1')
dma_put = requests.put(f'{hosturl}/dma?m={(addr + 1):04X}', data=disk_res)
# print(dma_put.status_code, dma_put.text)
return 1
return 0
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
# do nothing here
print("KEY INT")
sys_get = requests.delete(f'{hosturl}/io?p={FIF_PORT:02X}')
if sys_get.status_code == 200:
print(f'De-registered on {sys_get.text}')
pass