2013-11-06 01:12:55 +01:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Programmable Interrupt Controller
|
|
|
|
* http://stanislavs.org/helppc/8259.html
|
|
|
|
*
|
|
|
|
* @constructor
|
2015-02-25 18:21:54 +01:00
|
|
|
* @param {CPU} cpu
|
2013-11-06 01:12:55 +01:00
|
|
|
* @param {PIC=} master
|
|
|
|
*/
|
2014-06-15 22:25:17 +02:00
|
|
|
function PIC(cpu, master)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
/**
|
|
|
|
* all irqs off
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.irq_mask = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {number}
|
|
|
|
*
|
|
|
|
* Bogus default value (both master and slave mapped to 0).
|
|
|
|
* Will be initialized by the BIOS
|
|
|
|
*/
|
|
|
|
this.irq_map = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* in-service register
|
|
|
|
* Holds interrupts that are currently being serviced
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.isr = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* interrupt request register
|
|
|
|
* Holds interrupts that have been requested
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.irr = 0;
|
|
|
|
|
|
|
|
this.is_master = master === undefined;
|
|
|
|
this.slave = undefined;
|
|
|
|
|
|
|
|
this.expect_icw4 = false;
|
|
|
|
this.state = 0;
|
|
|
|
this.read_irr = 1;
|
|
|
|
this.auto_eoi = 1;
|
|
|
|
|
|
|
|
if(this.is_master)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
this.slave = new PIC(cpu, this);
|
2013-11-06 01:12:55 +01:00
|
|
|
|
2014-06-15 22:25:17 +02:00
|
|
|
this.check_irqs = function()
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
var enabled_irr = this.irr & this.irq_mask;
|
2013-11-06 01:12:55 +01:00
|
|
|
|
|
|
|
if(!enabled_irr)
|
|
|
|
{
|
2015-04-22 04:15:32 +02:00
|
|
|
dbg_log("master> no unmasked irrs. irr=" + h(this.irr, 2) + " mask=" + h(this.irq_mask & 0xff, 2), LOG_PIC);
|
2014-09-27 04:51:30 +02:00
|
|
|
return this.slave.check_irqs();
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var irq = enabled_irr & -enabled_irr;
|
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
if(this.isr && (this.isr & -this.isr) <= irq)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
|
|
|
// wait for eoi of higher or same priority interrupt
|
2015-04-22 04:15:32 +02:00
|
|
|
dbg_log("master> higher prio: isr=" + h(this.isr, 2) + " irq=" + h(irq, 2), LOG_PIC);
|
2013-11-06 01:12:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-01 13:56:06 +01:00
|
|
|
dbg_assert(irq !== 0);
|
2015-01-12 18:50:15 +01:00
|
|
|
var irq_number = v86util.int_log2_byte(irq);
|
2013-11-25 11:58:36 +01:00
|
|
|
irq = 1 << irq_number;
|
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
this.irr &= ~irq;
|
2013-11-06 01:12:55 +01:00
|
|
|
|
|
|
|
if(irq === 4)
|
|
|
|
{
|
|
|
|
// this should always return true
|
2014-09-27 04:51:30 +02:00
|
|
|
return this.slave.check_irqs();
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
if(!this.auto_eoi)
|
2013-11-25 11:58:36 +01:00
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
this.isr |= irq;
|
2013-11-25 11:58:36 +01:00
|
|
|
}
|
|
|
|
|
2015-04-22 04:15:32 +02:00
|
|
|
dbg_log("master handling irq " + irq_number, LOG_PIC);
|
2013-12-28 23:21:43 +01:00
|
|
|
//dbg_trace(LOG_PIC);
|
2014-07-26 23:06:52 +02:00
|
|
|
|
|
|
|
// call_interrupt_vector can cause an exception in the CPU, so we
|
|
|
|
// have to set previous_ip correctly here
|
|
|
|
cpu.previous_ip = cpu.instruction_pointer;
|
2014-09-27 04:51:30 +02:00
|
|
|
cpu.call_interrupt_vector(this.irq_map | irq_number, false, false);
|
2013-11-06 01:12:55 +01:00
|
|
|
|
|
|
|
return true;
|
2014-10-21 21:51:42 +02:00
|
|
|
};
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// is slave
|
2014-06-15 22:25:17 +02:00
|
|
|
this.check_irqs = function()
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
var enabled_irr = this.irr & this.irq_mask;
|
2013-11-06 01:12:55 +01:00
|
|
|
|
|
|
|
if(!enabled_irr)
|
|
|
|
{
|
2015-04-22 04:15:32 +02:00
|
|
|
dbg_log("slave > no unmasked irrs. irr=" + h(this.irr, 2) + " mask=" + h(this.irq_mask & 0xff, 2), LOG_PIC);
|
2013-11-06 01:12:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
var irq = enabled_irr & -enabled_irr;
|
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
if(this.isr && (this.isr & -this.isr) <= irq)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
|
|
|
// wait for eoi of higher or same priority interrupt
|
2015-04-22 04:15:32 +02:00
|
|
|
dbg_log("slave > higher prio: isr=" + h(this.isr, 2) + " irq=" + h(irq, 2), LOG_PIC);
|
2013-11-06 01:12:55 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-01 13:56:06 +01:00
|
|
|
dbg_assert(irq !== 0);
|
2015-01-12 18:50:15 +01:00
|
|
|
var irq_number = v86util.int_log2_byte(irq);
|
2013-11-25 11:58:36 +01:00
|
|
|
irq = 1 << irq_number;
|
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
this.irr &= ~irq;
|
|
|
|
this.isr |= irq;
|
2013-11-06 01:12:55 +01:00
|
|
|
|
2015-04-22 04:15:32 +02:00
|
|
|
dbg_log("slave > handling irq " + irq_number, LOG_PIC);
|
2014-07-26 23:06:52 +02:00
|
|
|
cpu.previous_ip = cpu.instruction_pointer;
|
2014-09-27 04:51:30 +02:00
|
|
|
cpu.call_interrupt_vector(this.irq_map | irq_number, false, false);
|
2013-11-06 01:12:55 +01:00
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
if(this.irr)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
|
|
|
// tell the master we have one more
|
2015-05-17 01:56:50 +02:00
|
|
|
master.raise_irq(2);
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
if(!this.auto_eoi)
|
2013-11-25 11:58:36 +01:00
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
this.isr &= ~irq;
|
2013-11-25 11:58:36 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 01:12:55 +01:00
|
|
|
return true;
|
2014-10-21 21:51:42 +02:00
|
|
|
};
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.dump = function()
|
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
dbg_log("mask: " + h(this.irq_mask & 0xFF), LOG_PIC);
|
|
|
|
dbg_log("base: " + h(this.irq_map), LOG_PIC);
|
|
|
|
dbg_log("requested: " + h(this.irr), LOG_PIC);
|
|
|
|
dbg_log("serviced: " + h(this.isr), LOG_PIC);
|
2013-11-06 01:12:55 +01:00
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
if(this.is_master)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
this.slave.dump();
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
var io_base;
|
|
|
|
if(this.is_master)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
|
|
|
io_base = 0x20;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
io_base = 0xA0;
|
|
|
|
}
|
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
cpu.io.register_write(io_base, this, port20_write);
|
|
|
|
cpu.io.register_read(io_base, this, port20_read);
|
2013-11-06 01:12:55 +01:00
|
|
|
|
2014-10-21 21:51:42 +02:00
|
|
|
cpu.io.register_write(io_base | 1, this, port21_write);
|
|
|
|
cpu.io.register_read(io_base | 1, this, port21_read);
|
2013-11-06 01:12:55 +01:00
|
|
|
|
|
|
|
function port20_write(data_byte)
|
|
|
|
{
|
|
|
|
//dbg_log("20 write: " + h(data_byte), LOG_PIC);
|
|
|
|
if(data_byte & 0x10) // xxxx1xxx
|
|
|
|
{
|
|
|
|
// icw1
|
|
|
|
dbg_log("icw1 = " + h(data_byte), LOG_PIC);
|
2014-09-27 04:51:30 +02:00
|
|
|
this.expect_icw4 = data_byte & 1;
|
|
|
|
this.state = 1;
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
else if(data_byte & 8) // xxx01xxx
|
|
|
|
{
|
|
|
|
// ocw3
|
|
|
|
dbg_log("ocw3: " + h(data_byte), LOG_PIC);
|
2014-09-27 04:51:30 +02:00
|
|
|
this.read_irr = data_byte & 1;
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
else // xxx00xxx
|
|
|
|
{
|
|
|
|
// ocw2
|
|
|
|
// end of interrupt
|
2013-11-25 11:58:36 +01:00
|
|
|
//dbg_log("eoi: " + h(data_byte), LOG_PIC);
|
2013-11-06 01:12:55 +01:00
|
|
|
|
|
|
|
var eoi_type = data_byte >> 5;
|
|
|
|
|
|
|
|
if(eoi_type === 1)
|
|
|
|
{
|
|
|
|
// non-specific eoi
|
2014-09-27 04:51:30 +02:00
|
|
|
this.isr &= this.isr - 1;
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
else if(eoi_type === 3)
|
|
|
|
{
|
|
|
|
// specific eoi
|
2014-09-27 04:51:30 +02:00
|
|
|
this.isr &= ~(1 << (data_byte & 7));
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dbg_log("Unknown eoi: " + h(data_byte), LOG_PIC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function port20_read()
|
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
if(this.read_irr)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
return this.irr;
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
return this.isr;
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function port21_write(data_byte)
|
|
|
|
{
|
|
|
|
//dbg_log("21 write: " + h(data_byte), LOG_PIC);
|
2014-09-27 04:51:30 +02:00
|
|
|
if(this.state === 0)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
if(this.expect_icw4)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
|
|
|
// icw4
|
2014-09-27 04:51:30 +02:00
|
|
|
this.expect_icw4 = false;
|
|
|
|
this.auto_eoi = data_byte & 2;
|
2013-11-06 01:12:55 +01:00
|
|
|
dbg_log("icw4: " + h(data_byte), LOG_PIC);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// ocw1
|
2014-09-27 04:51:30 +02:00
|
|
|
this.irq_mask = ~data_byte;
|
2014-07-13 00:42:39 +02:00
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
//dbg_log("interrupt mask: " + (this.irq_mask & 0xFF).toString(2) +
|
|
|
|
// " (" + (this.is_master ? "master" : "slave") + ")", LOG_PIC);
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
|
|
|
}
|
2014-09-27 04:51:30 +02:00
|
|
|
else if(this.state === 1)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
|
|
|
// icw2
|
2014-09-27 04:51:30 +02:00
|
|
|
this.irq_map = data_byte;
|
|
|
|
dbg_log("interrupts are mapped to " + h(this.irq_map) +
|
|
|
|
" (" + (this.is_master ? "master" : "slave") + ")", LOG_PIC);
|
|
|
|
this.state++;
|
2013-11-06 01:12:55 +01:00
|
|
|
}
|
2014-09-27 04:51:30 +02:00
|
|
|
else if(this.state === 2)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
|
|
|
// icw3
|
2014-09-27 04:51:30 +02:00
|
|
|
this.state = 0;
|
2013-11-06 01:12:55 +01:00
|
|
|
dbg_log("icw3: " + h(data_byte), LOG_PIC);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function port21_read()
|
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
//dbg_log("21h read " + h(~this.irq_mask & 0xff), LOG_PIC);
|
|
|
|
return ~this.irq_mask & 0xFF;
|
2013-11-06 01:12:55 +01:00
|
|
|
};
|
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
if(this.is_master)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
2015-05-17 01:56:50 +02:00
|
|
|
this.raise_irq = function(irq_number)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
|
|
|
dbg_assert(irq_number >= 0 && irq_number < 16);
|
|
|
|
|
|
|
|
if(irq_number >= 8)
|
|
|
|
{
|
2015-05-17 01:56:50 +02:00
|
|
|
this.slave.raise_irq(irq_number - 8);
|
2013-11-06 01:12:55 +01:00
|
|
|
irq_number = 2;
|
|
|
|
}
|
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
this.irr |= 1 << irq_number;
|
2013-11-06 01:12:55 +01:00
|
|
|
|
2014-06-15 22:25:17 +02:00
|
|
|
cpu.handle_irqs();
|
2013-11-06 01:12:55 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-17 01:56:50 +02:00
|
|
|
this.raise_irq = function(irq_number)
|
2013-11-06 01:12:55 +01:00
|
|
|
{
|
|
|
|
dbg_assert(irq_number >= 0 && irq_number < 8);
|
|
|
|
|
2014-09-27 04:51:30 +02:00
|
|
|
this.irr |= 1 << irq_number;
|
2013-11-06 01:12:55 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
this.get_isr = function()
|
|
|
|
{
|
2014-09-27 04:51:30 +02:00
|
|
|
return this.isr;
|
2013-11-06 01:12:55 +01:00
|
|
|
};
|
|
|
|
}
|