我正在Ada做一个Z80模拟器.我正在实现JR(Jump relative)系列,但我对我的代码不满意:
with Ada.Text_IO;
procedure main is
type UInt16 is mod 2 ** 16;
type UInt8 is mod 2 ** 8;
type Int8 is range -128 .. 127;
package UInt16_IO is new Ada.Text_IO.Modular_IO (UInt16);
function Two_Complement(N : UInt8) return Int8 is
begin
if N <= 127 then
return Int8 (N);
end if;
return Int8 (Integer (N) - 256);
end Two_Complement;
-- Relative jump
function Jr (Address : UInt16; D: UInt8) return UInt16 is
begin
return UInt16 (Integer (Address) + Integer (Two_Complement (D) + 2));
end Jr;
Address : UInt16;
begin
Address := 16#683#;
UInt16_IO.Put (Item => Jr (Address, 16#F1#), Base => 16); -- Get 16#676# which is good !
end main;
Run Code Online (Sandbox Code Playgroud)
它似乎有效,但我发现有太多类型的转换.你有什么建议吗?
谢谢,
奥利维尔.
你可以看看
function Jr (Address : UInt16; D: UInt8) return UInt16 is
Offset : constant Uint16
:= Uint16 (D) + (if D >= 16#80# then 16#ff00# else 0);
begin
return Address + Offset + 2;
end Jr;
Run Code Online (Sandbox Code Playgroud)
但它取决于你需要发生什么 - 例如 - 地址是0而D是,比方说,16#80(上面的代码返回16#ff82#).