mirror of
https://github.com/JorjBauer/lua-6502.git
synced 2025-01-13 22:34:00 +00:00
more portable signed byte handling
This commit is contained in:
parent
b584954d57
commit
ee9898006c
12
6502.lua
12
6502.lua
@ -1,8 +1,7 @@
|
|||||||
local string = require "string"
|
|
||||||
local _ENV = require 'std.normalize' {
|
local _ENV = require 'std.normalize' {
|
||||||
'std.strict',
|
'std.strict',
|
||||||
'const',
|
'const',
|
||||||
-- 'string', -- this neuters string.pack and string.unpack, not sure why.
|
'string',
|
||||||
'io',
|
'io',
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -505,7 +504,7 @@ _M.getParam = {
|
|||||||
|
|
||||||
-- Have to convert this from an unsigned byte to a signed byte.
|
-- Have to convert this from an unsigned byte to a signed byte.
|
||||||
-- Values are from -128 to +127.
|
-- Values are from -128 to +127.
|
||||||
local p = string.unpack("b", string.pack("B", self:readmem(self.pc)))
|
local p = self:unsignedToSigned(self:readmem(self.pc))
|
||||||
self.pc = (self.pc + 1) & 0xFFFF
|
self.pc = (self.pc + 1) & 0xFFFF
|
||||||
p = (p + self.pc) & 0xFFFF
|
p = (p + self.pc) & 0xFFFF
|
||||||
return p
|
return p
|
||||||
@ -516,7 +515,7 @@ _M.getParam = {
|
|||||||
self.pc = (self.pc + 1) & 0xFFFF
|
self.pc = (self.pc + 1) & 0xFFFF
|
||||||
|
|
||||||
-- Again, have to convert from unsigned byte to signed byte
|
-- Again, have to convert from unsigned byte to signed byte
|
||||||
local zprelParam2 = string.unpack("b", string.pack("B", self:readmem(self.pc)))
|
local zprelParam2 = self:unsignedToSigned(self:readmem(self.pc))
|
||||||
|
|
||||||
self.pc = (self.pc + 1) & 0xFFFF
|
self.pc = (self.pc + 1) & 0xFFFF
|
||||||
zprelParam2 = (zprelParam2 + self.pc) & 0xFFFF
|
zprelParam2 = (zprelParam2 + self.pc) & 0xFFFF
|
||||||
@ -1403,4 +1402,9 @@ function _M:writemem(a,v)
|
|||||||
self.ram[a] = v
|
self.ram[a] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function _M:unsignedToSigned(v)
|
||||||
|
if ((v & 0x80) == 0x00) then return v end
|
||||||
|
return -((v ~ 0xFF) + 1)
|
||||||
|
end
|
||||||
|
|
||||||
return _M
|
return _M
|
||||||
|
Loading…
x
Reference in New Issue
Block a user