2016-05-08 09:20:05 +02:00
|
|
|
require 'spec_helper'
|
|
|
|
describe Waterfoul::CPU do
|
|
|
|
before { $mmu = Waterfoul::MMU.new }
|
|
|
|
subject { Waterfoul::CPU.new }
|
|
|
|
|
2016-07-23 12:58:36 +02:00
|
|
|
describe '#serve_interrupt' do
|
2016-09-25 11:00:55 +02:00
|
|
|
before { subject.ei } # master interrupt enable
|
|
|
|
before { $mmu.write_byte 0xFFFF, 0xFF } # enable all interrupts
|
|
|
|
before { subject.set_register :sp, 0xFFFE }
|
|
|
|
before { subject.set_register :pc, 0x101 }
|
2016-07-23 12:58:36 +02:00
|
|
|
|
2016-09-25 11:00:55 +02:00
|
|
|
context 'when no interrupt to serve' do
|
|
|
|
it 'does alter the execution path' do
|
|
|
|
subject.step
|
|
|
|
expect(subject.pc).to eq 0x102
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not push anything onto the stack' do
|
|
|
|
subject.step
|
|
|
|
expect(subject.sp).to eq 0xFFFE
|
|
|
|
end
|
2016-07-23 12:58:36 +02:00
|
|
|
end
|
|
|
|
|
2016-09-25 11:00:55 +02:00
|
|
|
context 'when timer interrupt is served' do
|
|
|
|
before { $mmu.write_byte 0xFF0F, 0x4 } # request timer interrupt
|
|
|
|
|
|
|
|
it 'sets the program counter to 0x50' do
|
2016-07-23 12:58:36 +02:00
|
|
|
subject.step
|
2016-09-25 11:00:55 +02:00
|
|
|
expect(subject.pc).to eq 0x50
|
2016-07-23 12:58:36 +02:00
|
|
|
end
|
|
|
|
|
2016-09-25 11:00:55 +02:00
|
|
|
it 'saves the current program counter onto the stack' do
|
|
|
|
subject.step
|
|
|
|
expect($mmu.read_word(0xFFFC)).to eq 0x101
|
2016-07-23 12:58:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-08 09:20:05 +02:00
|
|
|
describe '#step' do
|
2016-09-25 11:06:55 +02:00
|
|
|
before { subject.set_register :pc, 0x101 }
|
|
|
|
|
|
|
|
it 'increments the program counter' do
|
|
|
|
subject.step
|
|
|
|
expect(subject.pc).to eq 0x102
|
2016-05-08 09:20:05 +02:00
|
|
|
end
|
|
|
|
|
2016-09-25 11:06:55 +02:00
|
|
|
context 'when the CPU is halted' do
|
|
|
|
before { subject.halt }
|
2016-05-08 09:20:05 +02:00
|
|
|
|
2016-09-25 11:06:55 +02:00
|
|
|
it 'does not increment the PC' do
|
2016-05-08 09:20:05 +02:00
|
|
|
subject.step
|
2016-09-25 11:06:55 +02:00
|
|
|
expect(subject.pc).to eq 0x101
|
2016-05-08 09:20:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-25 11:06:55 +02:00
|
|
|
it 'sets the instruction cycle time' do
|
|
|
|
subject.step
|
|
|
|
expect(subject.m).to eq 4
|
2016-05-08 09:20:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|