;;;; terminal.scm (define-constant TCONTROL_REGISTER #xc000) (define-constant TINPUT_CHAR #xc001) (define-constant TOUTPUT_CHAR #xc002) (define-constant TINPUT_CHAR_BIT #x80) (define-constant TOUTPUT_CHAR_BIT #x40) (define stdin (current-input-port)) (define stdout (current-output-port)) (define (poll-terminal-device p) (let* ((mem (processor-memory p)) (cr (u8vector-ref mem TCONTROL_REGISTER))) (cond ((zero? (fxand cr TINPUT_CHAR_BIT)) (let ((c (read-char stdin))) (when (eof-object? c) (error "EOF from terminal")) (if (char=? c #\newline) (set! c #\return) (set! c (char-upcase c))) (u8vector-set! mem TINPUT_CHAR (fxand #x7f (char->integer c))))) ((zero? (fxand cr TOUTPUT_CHAR_BIT)) (write-char (integer->char (fxand #x7f (u8vector-ref mem TOUTPUT_CHAR))) stdout) (flush-output stdout))) (u8vector-set! mem TCONTROL_REGISTER (fxior cr (fxior TINPUT_CHAR_BIT TOUTPUT_CHAR_BIT)))))