mirror of
https://github.com/pskupinski/65816.js.git
synced 2024-06-25 03:29:31 +00:00
Make sure branching can't cause the program counter to go below zero
or above 0xffff
This commit is contained in:
parent
833048fefa
commit
10cd88d08e
30
cpu.js
30
cpu.js
|
@ -4133,8 +4133,11 @@ var BMI = {
|
||||||
// Handle single byte two's complement numbers as the branch argument.
|
// Handle single byte two's complement numbers as the branch argument.
|
||||||
if(bytes[0]<=127) {
|
if(bytes[0]<=127) {
|
||||||
cpu.r.pc+=bytes[0];
|
cpu.r.pc+=bytes[0];
|
||||||
|
cpu.r.pc&=0xffff;
|
||||||
} else {
|
} else {
|
||||||
cpu.r.pc-=256-bytes[0];
|
cpu.r.pc-=256-bytes[0];
|
||||||
|
if(cpu.r.pc<0)
|
||||||
|
cpu.r.pc+=0xffff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4149,8 +4152,11 @@ var BPL = {
|
||||||
// Handle single byte two's complement numbers as the branch argument.
|
// Handle single byte two's complement numbers as the branch argument.
|
||||||
if(bytes[0]<=127) {
|
if(bytes[0]<=127) {
|
||||||
cpu.r.pc+=bytes[0];
|
cpu.r.pc+=bytes[0];
|
||||||
|
cpu.r.pc&=0xffff;
|
||||||
} else {
|
} else {
|
||||||
cpu.r.pc-=256-bytes[0];
|
cpu.r.pc-=256-bytes[0];
|
||||||
|
if(cpu.r.pc<0)
|
||||||
|
cpu.r.pc+=0xffff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4165,8 +4171,11 @@ var BVC = {
|
||||||
// Handle single byte two's complement numbers as the branch argument.
|
// Handle single byte two's complement numbers as the branch argument.
|
||||||
if(bytes[0]<=127) {
|
if(bytes[0]<=127) {
|
||||||
cpu.r.pc+=bytes[0];
|
cpu.r.pc+=bytes[0];
|
||||||
|
cpu.r.pc&=0xffff;
|
||||||
} else {
|
} else {
|
||||||
cpu.r.pc-=256-bytes[0];
|
cpu.r.pc-=256-bytes[0];
|
||||||
|
if(cpu.r.pc<0)
|
||||||
|
cpu.r.pc+=0xffff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4182,8 +4191,11 @@ var BVS = {
|
||||||
// Handle single byte two's complement numbers as the branch argument.
|
// Handle single byte two's complement numbers as the branch argument.
|
||||||
if(bytes[0]<=127) {
|
if(bytes[0]<=127) {
|
||||||
cpu.r.pc+=bytes[0];
|
cpu.r.pc+=bytes[0];
|
||||||
|
cpu.r.pc&=0xffff;
|
||||||
} else {
|
} else {
|
||||||
cpu.r.pc-=256-bytes[0];
|
cpu.r.pc-=256-bytes[0];
|
||||||
|
if(cpu.r.pc<0)
|
||||||
|
cpu.r.pc+=0xffff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4198,8 +4210,11 @@ var BCC = {
|
||||||
// Handle single byte two's complement numbers as the branch argument.
|
// Handle single byte two's complement numbers as the branch argument.
|
||||||
if(bytes[0]<=127) {
|
if(bytes[0]<=127) {
|
||||||
cpu.r.pc+=bytes[0];
|
cpu.r.pc+=bytes[0];
|
||||||
|
cpu.r.pc&=0xffff;
|
||||||
} else {
|
} else {
|
||||||
cpu.r.pc-=256-bytes[0];
|
cpu.r.pc-=256-bytes[0];
|
||||||
|
if(cpu.r.pc<0)
|
||||||
|
cpu.r.pc+=0xffff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4214,8 +4229,11 @@ var BCS = {
|
||||||
// Handle single byte two's complement numbers as the branch argument.
|
// Handle single byte two's complement numbers as the branch argument.
|
||||||
if(bytes[0]<=127) {
|
if(bytes[0]<=127) {
|
||||||
cpu.r.pc+=bytes[0];
|
cpu.r.pc+=bytes[0];
|
||||||
|
cpu.r.pc&=0xffff;
|
||||||
} else {
|
} else {
|
||||||
cpu.r.pc-=256-bytes[0];
|
cpu.r.pc-=256-bytes[0];
|
||||||
|
if(cpu.r.pc<0)
|
||||||
|
cpu.r.pc+=0xffff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4230,8 +4248,11 @@ var BEQ = {
|
||||||
// Handle single byte two's complement numbers as the branch argument.
|
// Handle single byte two's complement numbers as the branch argument.
|
||||||
if(bytes[0]<=127) {
|
if(bytes[0]<=127) {
|
||||||
cpu.r.pc+=bytes[0];
|
cpu.r.pc+=bytes[0];
|
||||||
|
cpu.r.pc&=0xffff;
|
||||||
} else {
|
} else {
|
||||||
cpu.r.pc-=256-bytes[0];
|
cpu.r.pc-=256-bytes[0];
|
||||||
|
if(cpu.r.pc<0)
|
||||||
|
cpu.r.pc+=0xffff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4246,8 +4267,11 @@ var BNE = {
|
||||||
// Handle single byte two's complement numbers as the branch argument.
|
// Handle single byte two's complement numbers as the branch argument.
|
||||||
if(bytes[0]<=127) {
|
if(bytes[0]<=127) {
|
||||||
cpu.r.pc+=bytes[0];
|
cpu.r.pc+=bytes[0];
|
||||||
|
cpu.r.pc&=0xffff;
|
||||||
} else {
|
} else {
|
||||||
cpu.r.pc-=256-bytes[0];
|
cpu.r.pc-=256-bytes[0];
|
||||||
|
if(cpu.r.pc<0)
|
||||||
|
cpu.r.pc+=0xffff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4262,8 +4286,11 @@ var BRA = {
|
||||||
// Handle single byte two's complement numbers as the branch argument.
|
// Handle single byte two's complement numbers as the branch argument.
|
||||||
if(bytes[0]<=127) {
|
if(bytes[0]<=127) {
|
||||||
cpu.r.pc+=bytes[0];
|
cpu.r.pc+=bytes[0];
|
||||||
|
cpu.r.pc&=0xffff;
|
||||||
} else {
|
} else {
|
||||||
cpu.r.pc-=256-bytes[0];
|
cpu.r.pc-=256-bytes[0];
|
||||||
|
if(cpu.r.pc<0)
|
||||||
|
cpu.r.pc+=0xffff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -4277,8 +4304,11 @@ var BRL = {
|
||||||
var num = (bytes[1]<<8)|bytes[0];
|
var num = (bytes[1]<<8)|bytes[0];
|
||||||
if(num<=32767) {
|
if(num<=32767) {
|
||||||
cpu.r.pc+=num;
|
cpu.r.pc+=num;
|
||||||
|
cpu.r.pc&=0xffff;
|
||||||
} else {
|
} else {
|
||||||
cpu.r.pc-=65536-num;
|
cpu.r.pc-=65536-num;
|
||||||
|
if(cpu.r.pc<0)
|
||||||
|
cpu.r.pc+=0xffff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user