diff --git a/README.md b/README.md index b3653c6..11fc8e7 100644 --- a/README.md +++ b/README.md @@ -2763,6 +2763,12 @@ as fast as the card's rate drains them to the machine, so a 300-baud program sees 300 baud. Like the Ethernet cards it talks to the system directly and is Linux only; `src/modem.zig` is the state machine and the socket both. +The same modem serves both machines. On a IIe it sits on the Super Serial +Card's line, so `--modem` there wants `--serial-rom`; on a IIgs it sits on the +SCC's modem port, which is built in, so `--modem` needs nothing else. Either +way the machine drives the serial chip it always had, and the modem is the +device the wire runs to. + ### The MIDI card A Passport Designs MIDI interface in slot 2, which is what most Apple II MIDI diff --git a/src/gs.zig b/src/gs.zig index 9330928..ff5b0db 100644 --- a/src/gs.zig +++ b/src/gs.zig @@ -1388,6 +1388,10 @@ pub const Params = struct { /// for whichever Ethernet card is fitted. ethernet_interface: ?[]const u8 = null, mac: ?[]const u8 = null, + /// A Hayes modem on the SCC's modem port, dialing over TCP, and the port it + /// answers incoming calls on if it is to listen. + modem: bool = false, + modem_answer: ?u16 = null, /// How much RAM the machine has altogether, in bytes, counting the Mega /// II's 128K. Nothing means a machine with nothing in its expansion slot. memory: ?usize = null, @@ -1597,6 +1601,10 @@ pub fn run(init: std.process.Init, params: Params) !void { break :blk null; }; } + if (params.modem) { + app.session.modem = .{}; + if (params.modem_answer) |answer_port| app.session.modem_line.listen(answer_port); + } // What the Control Panel was set to, kept for next time. defer if (params.battery_ram) |path| { std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = &cpu.memory.rtc.ram }) catch |err| { diff --git a/src/main.zig b/src/main.zig index 10c52a0..3d2b9ab 100644 --- a/src/main.zig +++ b/src/main.zig @@ -591,10 +591,12 @@ const usage = \\ --serial-out PATH write what the machine sends to PATH. On a IIgs \\ these two are the modem port, which is channel B of \\ the SCC, rather than a card in a slot. - \\ --modem put a Hayes modem on the Super Serial Card's line - \\ instead of the files: AT commands, and ATDT host:port - \\ dials out over TCP, so a terminal program can reach a - \\ telnet BBS. Needs --serial-rom; Linux only. + \\ --modem put a Hayes modem on the serial line instead of the + \\ files: AT commands, and ATDT host:port dials out over + \\ TCP, so a terminal program can reach a telnet BBS. On a + \\ IIe it is the Super Serial Card, so it needs + \\ --serial-rom; on a IIgs it is the SCC modem port, built + \\ in. Linux only. \\ --modem-answer PORT the same, and also listen on PORT so something can \\ telnet in and be answered as an incoming call. \\ --printer-out PATH write what a IIgs sends out of its printer port to @@ -4079,6 +4081,8 @@ pub fn main(init: std.process.Init) !void { .uthernet1 = options.uthernet1, .ethernet_interface = options.ethernet_interface, .mac = options.mac, + .modem = options.modem, + .modem_answer = options.modem_answer, .halted = options.halted, }); diff --git a/src/session16.zig b/src/session16.zig index 267c49b..31f489c 100644 --- a/src/session16.zig +++ b/src/session16.zig @@ -180,6 +180,12 @@ pub const Session = struct { serial_in: ?[]u8 = null, serial_in_at: usize = 0, serial_in_due: u64 = 0, + /// A Hayes modem on the SCC's modem port (channel B), dialing out over TCP, + /// when one is attached in place of the serial files — the same modem the + /// eight-bit machines put on the Super Serial Card. + modem: ?zig65.modem.Modem = null, + modem_line: zig65.modem.Line = .{}, + modem_in_due: u64 = 0, /// And the printer port's outward end, which is channel A. Only outward: /// the channel is the same as the other one and would carry bytes back @@ -693,6 +699,7 @@ pub const Session = struct { } const port = self.cpu.memory.scc.channelOf(.b); + if (self.modem != null) return self.pumpModem(port); if (self.serial_out) |file| { const sent = port.sent(); if (sent.len > 0) { @@ -713,6 +720,38 @@ pub const Session = struct { } } + /// The modem on the SCC's modem port, driven the way `session8.zig`'s is on + /// the Super Serial Card: the machine's bytes go to the modem, the modem's + /// come back paced by the port's rate, and what it dials crosses the host's + /// TCP. `port` is the SCC's channel B. + fn pumpModem(self: *Self, port: anytype) void { + const mdm = &(self.modem.?); + const cps = self.cpu.memory.scc.cycles_per_second; + const cycle = self.cpu.clock.cycles; + mdm.cycles_per_tick = cps / 50; + self.modem_line.connect_rate = port.rate(); + + const sent = port.sent(); + for (sent) |byte| mdm.fromMachine(byte, cycle); + if (sent.len > 0) port.consume(sent.len); + mdm.tick(cycle); + + switch (mdm.takeWants()) { + .nothing => {}, + .dial => |d| self.modem_line.dial(d.host, d.port), + .hangup => self.modem_line.hangup(), + .answer => {}, + } + self.modem_line.pump(mdm); + + while (mdm.to_machine_len > 0 and cycle >= self.modem_in_due) { + if (!port.receiving()) break; + _ = port.receive(mdm.machineBytes()[0]); + mdm.takeMachine(1); + self.modem_in_due = cycle + port.cyclesPerByte(cps); + } + } + /// Moves frames between the chip and the network, both ways. What the /// machine sent goes to the group, minus the handshake the bridge answers /// itself; what the group sent goes onto the chip's wire, minus the @@ -935,6 +974,8 @@ pub const Session = struct { // And leaves the Uthernet's multicast group. if (self.ethernet) |*bridge| bridge.close(); self.ethernet = null; + // Hangs up the modem, if one was dialing. + self.modem_line.deinit(); } };