-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathps1_exe.pro
65 lines (51 loc) · 2.11 KB
/
ps1_exe.pro
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/** <module> PlayStation 1 PSX-EXE file model
See also:
- struct XF_HDR in psyq/include/KERNEL.H
- https://patpend.net/technical/psx/exeheader.txt
*/
:- module(ps1_exe, [
print_exe_info/1
, exe_file__entry_point/2
]).
:- use_module('./transput.pro').
:- use_module('./ps1_bit.pro').
print_exe_info(Path) :-
with_file(Path, read, [type(binary)], Stream, print_exe_info_(Stream)).
print_exe_info_(Stream) :-
stream_pc0(Stream, Pc0),
stream_text(Stream, TAddr, TSize),
stream_data(Stream, DAddr, DSize),
stream_bss(Stream, BAddr, BSize),
stream_stack(Stream, SAddr, SSize),
format('\
entry point
pc0 ~16r
segments
text ~16r ~16r
data ~16r ~16r
bss ~16r ~16r
stack ~16r ~16r
', [Pc0, TAddr, TSize, DAddr, DSize, BAddr, BSize, SAddr, SSize]).
exe_file__entry_point(Path, Pc0) :-
with_file(Path, read, [type(binary)], Stream, stream_pc0(Stream, Pc0)).
stream_pc0(Stream, Pc0) :- stream_begin_count_value_(Stream, 16, 4, [uint32(Pc0)]).
stream_text(Stream, Addr, Size) :- stream_begin_count_value_(Stream, 24, 8, [uint32(Addr), uint32(Size)]).
stream_data(Stream, Addr, Size) :- stream_begin_count_value_(Stream, 32, 8, [uint32(Addr), uint32(Size)]).
stream_bss(Stream, Addr, Size) :- stream_begin_count_value_(Stream, 40, 8, [uint32(Addr), uint32(Size)]).
stream_stack(Stream, Addr, Size) :- stream_begin_count_value_(Stream, 48, 8, [uint32(Addr), uint32(Size)]).
/** stream_begin_count_value_(+Stream, +Begin, ?Count, ?Value)
"In Stream, at Begin, Count bytes forward, is the value Value."
Begin is an integer.
Value is any of these:
- list of =Value=s (recursive)
- uint32(A): little-endian 32-bit unsigned integer
*/
stream_begin_count_value_(_, _, 0, []) :- !.
stream_begin_count_value_(Stream, Position0, Count2, [H | T]) :- !,
stream_begin_count_value_(Stream, Position0, Count0, H),
Position1 is Position0 + Count0,
stream_begin_count_value_(Stream, Position1, Count1, T),
Count2 is Count0 + Count1.
stream_begin_count_value_(Stream, Position, 4, uint32(Value)) :- !,
stream_begin_count_bytes(Stream, Position, 4, A),
bytes_le__uint4(A, Value).