-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnote_corewar.txt
288 lines (230 loc) · 6.93 KB
/
note_corewar.txt
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
**************************************************************
* *
* COREWAR *
* *
**************************************************************
********************************
* SUMMARY *
********************************
GENERAL:
- register-based process VM
- 1 joueur = 1 champion = 1 ou + processus
- 1 cycle = tous les processus font une action
EACH PROCESS:
- 16 registres de 4 octets
- 1 PC (contient l'adresse de la next op du process)
- 1 carry - vaut 1 si derniere op reussie
PLAYER NUMBER:
- soit specifié par la VM en fonction de l'ordre des argv
- soit specifié en option
CHAMPION INIT:
- r1 = num player (sert pour le live)
- autres rx = 0
- sauf le PC
CHAMPION LOADING:
- espacer equitablement leur point d'entrée
- size du champion doit etre <= MAX_CHAMP_SIZE (p7)
ARENA MEMORY:
- malloc MEM_SIZE
- memoire est circulaire
- l'adressage commence a 0
LIVE MANAGEMENT:
- tous les CYCLE_TO_DIE (1536) check si le champion a emis
au moins 1 LIVE depuis le dernier check
- else, kill le process
- compteur de LIVE a chaque check (live_count)
- if (live_count >= NBR_LIVE)
CYCLE_TO_DIE = CYCLE_TO_DIE - CYCLE_DELTA
- a chaque LIVE emis, print "un processus dit que... "
- si on a pas decrementé CYCLE_TO_DIE depuis MAX_CHECKS (nb_checks)
if (nb_checks == MAX_CHECKS)
CYCLE_TO_DIE = CYCLE_TO_DIE - CYCLE_DELTA
nb_checks = 0
- LIVE doit etre fait avec le num du player
possibilite de faire des LIVE d'autres players
END OF GAME:
- quand il n'y a plus de process en vie
- main loop du programme ?
while (live_count)
while (i < CYCLE_TO_DIE)
WINNER OF GAME:
- dernier champion a avoir emis LIVE
- variable last_live = num player ?
- print win statement "le joueur x(champion_name) a gagne"
ERROR HANDLING:
- afficher les erreurs sur stderr (fd = 2)
USAGE:
- ./corewar [-dump nb_cycles] [[-n number] champion1.cor] ...
MEMORY DUMP:
- dump memoire en hexa - 32 octets par ligne
CARRY: instr. qui le modifient en cas d'echec:
BIG ENDIANNESS:
- voir : https://chortle.ccsu.edu/AssemblyTutorial/Chapter-15/ass15_3.html
- https://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/
- to check computer Endianness
#include <stdio.h>
int main(void) {
unsigned int x = 1;
(((char *)&x)[0]) == 1 ? printf("Big Endian\n")
: printf("Little Endian\n");
return (0);
}
VM:
- Save opcode during the first cycle
- Execute the operation at the end of the cast time
- using the saved opcode (even if the memory is different now)
- use the current parameters / args
- Error : If the parameters are now invalid
- do nothing, and move the opcode one cell further
Doubts:
=======
- .name .comment chargé dans l'arene ? ## No
- carry modifier en cas d'echec d'op - qu'est qu'un echec ?
- num player commence a 0 ou 1 ? ## 1
- que signifie kill le process (p6) ?
- NBR_LIVE = MAX LIVES emis par un seul champion ou le total des LIVES emis par tous ?
- voir le format exact du dump sur la VM 42
- CHAMP_MAX_SIZE inclut-il .name .comment ? (probablement pas) ## No
To try:
=======
[ ] checker les options de l'ASM, Corewar
[ ] nm les fonctions de corewar
[ ] checker les memory dump de corewar
********************************
* Structure *
********************************
VM Setup
- getopt - parsing des options
- get players
- input error handling
- generate arena
- read & init process
- load process
Main loop
- cycle - while (process alive)
- update VM parameters - every x cycles
- update process - every cycle
- launch each process op - when ready (not casting)
- update arena/VM
- kill and fork process - when necessary
Select Winner
- print last Live Player/champion
Clean VM
- free process
- free vm
- free players
********************************
* To do *
********************************
Player input
[x] ERROR - Invalid name extension .cor
[x] ERROR - Too big program
[x] ERROR - Wrong chmod permission denied
[x] ERROR - Can't open the file - read return
[x] ERROR - Wrong exec_magic - file type
[x] ERROR - Too many players - MAX_PLAYERS
[ ] Automatic player numbering
[ ] Getopt support
********************************
* misc. *
********************************
Translator : Assembler language application (p22)
IR : Intermediate Representation
VM : Stack-based Bytecode interpreter (p26)
Assembler IR : AST -> Abstract Syntax Tree
LL Recursive Descent Parser
Syntax Directed Translator (p26/p36) -> Allow a fast and easy translation
Bytecode Assembler (p265)
**************************************************************
* *
* ASM *
* *
**************************************************************
********************************
* header *
********************************
if Symbol == .
if Label == name
if String
encode String
else
ERROR
else
ERROR
else
ERROR
if Symbol == .
if Label == comment
if String
encode String
else
ERROR
else
ERROR
else
ERROR
********************************
* opcode *
********************************
if Label
if Symbol == :
continue
else
ERROR
else if Keyword
foreach args in op_tab separated by Symbol == ,
if Arguments get != Argument require
ERROR
else
encode
else
ERROR
********************************
* args *
********************************
if Symbol == %
if Symbol == :
if Label exist
encode Direct Label
else
ERROR
else if Symbol == = || Symbol == +
if Number
encore Direct Number
else
ERROR
else if Number
encode Direct Number
else
ERROR
else if Symbol == - || Symbol == +
if Number
encode Indirect Number
else
ERROR
else if Number
encode Indirect Number
else if Register
encode Register
********************************
* deasm *
********************************
Parse Header -> Easy
name start at 4
comment start at name.max + 8
Program start at name.max + comment.max + 8
while opcode = read(4)
if opcode.octal
args_type = read(1)
res = args_type >> 6 & 0b11
if res = T_IND
read(2)
else if res = T_DIR
read(opcode.label ? 2 : 4)
else
read(1)
res = args_type >> 4 & 0b11
SAME
res = args_type >> 2 & 0b11
SAME
res = args_type >> 0 & 0b11 #-- Always false