<commit_before>"use strict";

/*
 * Serial ports
 * http://wiki.osdev.org/UART
 * https://github.com/s-macke/jor1k/blob/master/js/worker/dev/uart.js
 * https://www.freebsd.org/doc/en/articles/serial-uart/
 */

/** @const */
var DLAB = 0x80;

/** @const */ var UART_IER_THRI = 0x02; /* Enable Transmitter holding register int. */
/** @const */ var UART_IER_RDI = 0x01; /* Enable receiver data interrupt */

/** @const */var UART_IIR_MSI = 0x00; /* Modem status interrupt (Low priority) */
/** @const */var UART_IIR_NO_INT = 0x01;
/** @const */var UART_IIR_THRI = 0x02; /* Transmitter holding register empty */
/** @const */var UART_IIR_RDI = 0x04; /* Receiver data interrupt */
/** @const */var UART_IIR_RLSI = 0x06; /* Receiver line status interrupt (High p.) */
/** @const */var UART_IIR_CTI = 0x0c; /* Character timeout */


/** 
 * @constructor 
 */
function UART(cpu, port, bus)
{
    this.bus = bus;

    this.pic = cpu.devices.pic;

    this.ints = 0;

    this.baud_rate = 0;

    this.line_control = 0;
    this.line_status = 0;

    this.fifo_control = 0;

    // interrupts enable
    this.ier = 0;

    // interrupt identification register
    this.iir = 1;

    this.modem_control = 0;
    this.modem_status = 0;

    this.scratch_register = 0;

    this.irq = 0;

    this.input = new ByteQueue(4096);

    this.current_line = "";

    if(port === 0x3E8 || port === 0x3F8)
    {
        this.irq = 4;
    }
    else if(port === 0x3E8 || port === 0x3E8)
    {
        this.irq = 3;
    }
    else
    {
        dbg_log("Invalid port: " + h(port), LOG_SERIAL);
        return;
    }

    this.bus.register("serial0-input", function(data)
    {
        this.data_received(data);
    }, this);

    var io = cpu.io;

    io.register_write(port, this, function(out_byte) 
    {
        if(this.line_control & DLAB)
        {
            this.baud_rate = this.baud_rate & ~0xFF | out_byte;
            return;
        }

        dbg_log("data: " + h(out_byte), LOG_SERIAL);

        this.ThrowTHRI();

        if(out_byte === 0xFF)
        {
            return;
        }

        var char = String.fromCharCode(out_byte);

        this.bus.send("serial0-output-char", char);

        //if(this.bus.should_send("serial0-output-line"))
        //{
        //    this.current_line += char;
        //    if(char === "\n")
        //    {
        //        this.bus.send("serial0-output-line", this.current_line);
        //        this.current_line = "";
        //    }
        //}
    });

    io.register_write(port | 1, this, function(out_byte)
    {
        if(this.line_control & DLAB)
        {
            this.baud_rate = this.baud_rate & 0xFF | out_byte << 8;
            dbg_log("baud rate: " + h(this.baud_rate), LOG_SERIAL);
        }
        else
        {
            this.ier = out_byte;
            dbg_log("interrupt enable: " + h(out_byte), LOG_SERIAL);
            this.NextInterrupt();
        }
    });

    io.register_read(port, this, function()
    {
        if(this.line_control & DLAB)
        {
            return this.baud_rate & 0xFF;
        }
        else
        {
            var data = this.input.shift();

            if(data === -1)<commit_msg>whoops<commit_after>"use strict";

/*
 * Serial ports
 * http://wiki.osdev.org/UART
 * https://github.com/s-macke/jor1k/blob/master/js/worker/dev/uart.js
 * https://www.freebsd.org/doc/en/articles/serial-uart/
 */

/** @const */
var DLAB = 0x80;

/** @const */ var UART_IER_THRI = 0x02; /* Enable Transmitter holding register int. */
/** @const */ var UART_IER_RDI = 0x01; /* Enable receiver data interrupt */

/** @const */var UART_IIR_MSI = 0x00; /* Modem status interrupt (Low priority) */
/** @const */var UART_IIR_NO_INT = 0x01;
/** @const */var UART_IIR_THRI = 0x02; /* Transmitter holding register empty */
/** @const */var UART_IIR_RDI = 0x04; /* Receiver data interrupt */
/** @const */var UART_IIR_RLSI = 0x06; /* Receiver line status interrupt (High p.) */
/** @const */var UART_IIR_CTI = 0x0c; /* Character timeout */


/** 
 * @constructor 
 */
function UART(cpu, port, bus)
{
    this.bus = bus;

    this.pic = cpu.devices.pic;

    this.ints = 0;

    this.baud_rate = 0;

    this.line_control = 0;
    this.line_status = 0;

    this.fifo_control = 0;

    // interrupts enable
    this.ier = 0;

    // interrupt identification register
    this.iir = 1;

    this.modem_control = 0;
    this.modem_status = 0;

    this.scratch_register = 0;

    this.irq = 0;

    this.input = new ByteQueue(4096);

    this.current_line = "";

    if(port === 0x3E8 || port === 0x3F8)
    {
        this.irq = 4;
    }
    else if(port === 0x3E8 || port === 0x3E8)
    {
        this.irq = 3;
    }
    else
    {
        dbg_log("Invalid port: " + h(port), LOG_SERIAL);
        return;
    }

    this.bus.register("serial0-input", function(data)
    {
        this.data_received(data);
    }, this);

    var io = cpu.io;

    io.register_write(port, this, function(out_byte) 
    {
        if(this.line_control & DLAB)
        {
            this.baud_rate = this.baud_rate & ~0xFF | out_byte;
            return;
        }

        dbg_log("data: " + h(out_byte), LOG_SERIAL);

        this.ThrowTHRI();

        if(out_byte === 0xFF)
        {
            return;
        }

        var char = String.fromCharCode(out_byte);

        this.bus.send("serial0-output-char", char);

        if(this.bus.should_send("serial0-output-line"))
        {
            this.current_line += char;
            if(char === "\n")
            {
                this.bus.send("serial0-output-line", this.current_line);
                this.current_line = "";
            }
        }
    });

    io.register_write(port | 1, this, function(out_byte)
    {
        if(this.line_control & DLAB)
        {
            this.baud_rate = this.baud_rate & 0xFF | out_byte << 8;
            dbg_log("baud rate: " + h(this.baud_rate), LOG_SERIAL);
        }
        else
        {
            this.ier = out_byte;
            dbg_log("interrupt enable: " + h(out_byte), LOG_SERIAL);
            this.NextInterrupt();
        }
    });

    io.register_read(port, this, function()
    {
        if(this.line_control & DLAB)
        {
            return this.baud_rate & 0xFF;
        }
        else
        {
            var data = this.input.shift();

            if(data === -1)
{'fraction_non_alphanumeric': 0.1506223412635891, 'fraction_numerical': 0.02111233653694659, 'mean_word_length': 1.8633288227334235, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1912297', 'n_tokens_mistral': 2361, 'n_tokens_neox': 2191, 'n_words': 409}