#!/usr/bin/env python

import iec3
import os
import sys

"""
channels:
0 reserved for reading PRG files.
1 reserved for writing PRG files.
2..14 need filetype and rw flags in the filename.
15 for DOS commands.
"""
# TODO: listen and talk addresses can differ.

class D1541(iec3.IECMember):
    def __init__(self, IEC, bus_address):
        iec3.IECMember.__init__(self, IEC, bus_address)
        self.image_file = None
        self.child_stdout = None
        self.child_stdin = None
    def get_reader_fileno(self):
        return self.child_stdout.fileno() if self.child_stdout else -1
    def read(self):
        data = self.child_stdout.read(1)
        if data == "": # EOF
            print "whoops, EOF"
        self.set_lines(ord(data[0]))
    def write(self, lines):
        self.child_stdin.write(chr(lines))
        self.child_stdin.flush()
    def set_image_name(self, name, type):
        if self.child_stdin is not None:
            self.child_stdin.close()
        if self.child_stdout is not None:
            self.child_stdout.close()
        self.child_stdin, self.child_stdout = os.popen2(["./emulators/d1541/d1541", "--", name], "b", 1) # FIXME
