Skip to content

Commit 8820455

Browse files
committed
Add opcode unit tests
Keep on revamping stuffs
1 parent 4032bdd commit 8820455

File tree

12 files changed

+391
-275
lines changed

12 files changed

+391
-275
lines changed

CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ find_package(Jansson)
2828
set(CMAKE_C_STANDARDS 11)
2929

3030
add_subdirectory(externals)
31-
#[[
31+
3232
set(etripator_SRC
3333
message.c
3434
message/file.c
@@ -45,7 +45,7 @@ set(etripator_SRC
4545
label/save.c
4646
irq.c
4747
memory.c
48-
memorymap.c
48+
memory_map.c
4949
rom.c
5050
cd.c
5151
ipl.c
@@ -64,7 +64,7 @@ set(etripator_HDR
6464
label.h
6565
irq.h
6666
memory.h
67-
memorymap.h
67+
memory_map.h
6868
rom.h
6969
cd.h
7070
ipl.h
@@ -75,7 +75,7 @@ set_target_properties(etripator PROPERTIES C_STANDARD 11)
7575
target_include_directories(etripator PUBLIC ${JANSSON_INCLUDE_DIRS} ${EXTRA_INCLUDE} externals)
7676
target_compile_definitions(etripator PRIVATE _POSIX_C_SOURCE)
7777
target_link_libraries(etripator PUBLIC ${JANSSON_LIBRARIES} cwalk)
78-
78+
#[[
7979
add_executable(etripator_cli cli/etripator.c cli/options.c)
8080
set_target_properties(etripator_cli
8181
PROPERTIES

decode.c

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static void print_statement_address(FILE *out, int n, uint16_t logical, uint8_t
8888
fprintf(out, "%*c; bank: $%03x logical: $%04x", last_column_spacing(n), ' ', page, logical);
8989
}
9090

91-
static void print_label(FILE *out, label_t *label) {
91+
static void print_label(FILE *out, Label *label) {
9292
int n = last_column_spacing((int)strlen(label->name) + 1);
9393
/* Print description */
9494
print_comment(out, label->description);
@@ -97,7 +97,7 @@ static void print_label(FILE *out, label_t *label) {
9797
}
9898

9999
/* Finds any jump address from the current section. */
100-
int label_extract(section_t *section, memmap_t *map, label_repository_t *repository) {
100+
bool label_extract(Section *section, MemoryMap *map, LabelRepository *repository) {
101101
int i;
102102
uint8_t inst;
103103
uint8_t data[6];
@@ -106,9 +106,9 @@ int label_extract(section_t *section, memmap_t *map, label_repository_t *reposit
106106
uint16_t logical;
107107
uint8_t page;
108108

109-
const opcode_t *opcode;
109+
const Opcode *opcode;
110110

111-
if (section->type != Code) {
111+
if (section->type != SECTION_TYPE_CODE) {
112112
return 1;
113113
}
114114

@@ -160,7 +160,7 @@ int label_extract(section_t *section, memmap_t *map, label_repository_t *reposit
160160
return 1;
161161
}
162162

163-
static int data_extract_binary(FILE *out, section_t *section, memmap_t *map, label_repository_t *repository) {
163+
static int data_extract_binary(FILE *out, Section *section, MemoryMap *map, LabelRepository *repository) {
164164
uint16_t logical;
165165
int32_t i;
166166
for (i = 0, logical = section->logical; i < section->size; i++, logical++) {
@@ -170,16 +170,16 @@ static int data_extract_binary(FILE *out, section_t *section, memmap_t *map, lab
170170
return 1;
171171
}
172172

173-
static int data_extract_hex(FILE *out, section_t *section, memmap_t *map, label_repository_t *repository,
174-
comment_repository_t *comments, int extra_infos) {
173+
static int data_extract_hex(FILE *out, Section *section, MemoryMap *map, LabelRepository *repository,
174+
CommentRepository *comments, int extra_infos) {
175175
const int32_t element_size = section->data.element_size;
176176
const int32_t elements_per_line = section->data.elements_per_line;
177177

178178
int32_t i, j;
179179
uint16_t logical;
180180

181-
label_t label;
182-
comment_t comment;
181+
Label label;
182+
Comment comment;
183183

184184
uint8_t buffer[2] = {0};
185185
int32_t top = 0;
@@ -211,7 +211,7 @@ static int data_extract_hex(FILE *out, section_t *section, memmap_t *map, label_
211211
j = 0;
212212
}
213213

214-
comment_t dummy;
214+
Comment dummy;
215215
if (comment_repository_find(comments, logical, page, &dummy)) {
216216
if (has_comment) {
217217
if (top && (top < element_size)) {
@@ -223,7 +223,7 @@ static int data_extract_hex(FILE *out, section_t *section, memmap_t *map, label_
223223
}
224224
print_inline_comment(out, (int)(ftell(out) - line_offset), comment.text);
225225
}
226-
memcpy(&comment, &dummy, sizeof(comment_t));
226+
memcpy(&comment, &dummy, sizeof(Comment));
227227
has_comment = 1;
228228
j = 0;
229229
}
@@ -288,8 +288,8 @@ static int data_extract_hex(FILE *out, section_t *section, memmap_t *map, label_
288288
return 1;
289289
}
290290

291-
static int data_extract_string(FILE *out, section_t *section, memmap_t *map, label_repository_t *repository,
292-
comment_repository_t *comments, int extra_infos) {
291+
static int data_extract_string(FILE *out, Section *section, MemoryMap *map, LabelRepository *repository,
292+
CommentRepository *comments, int extra_infos) {
293293
const int32_t elements_per_line = section->data.elements_per_line;
294294

295295
int32_t i, j;
@@ -303,13 +303,13 @@ static int data_extract_string(FILE *out, section_t *section, memmap_t *map, lab
303303
uint16_t line_logical = 0;
304304
uint8_t line_page;
305305

306-
comment_t comment = {0};
306+
Comment comment = {0};
307307

308308
for (i = 0, j = 0, logical = section->logical; i < section->size; i++, logical++) {
309309
uint8_t data = memmap_read(map, logical);
310310
uint8_t page = memmap_page(map, logical);
311311

312-
label_t label = {0};
312+
Label label = {0};
313313

314314
has_label = label_repository_find(repository, logical, page, &label);
315315

@@ -325,7 +325,7 @@ static int data_extract_string(FILE *out, section_t *section, memmap_t *map, lab
325325
print_label(out, &label);
326326
}
327327

328-
comment_t dummy;
328+
Comment dummy;
329329
if (comment_repository_find(comments, logical, page, &dummy)) {
330330
if (j) {
331331
if (c) { // close string if neededs
@@ -337,7 +337,7 @@ static int data_extract_string(FILE *out, section_t *section, memmap_t *map, lab
337337
if (has_comment) {
338338
print_inline_comment(out, (int)(ftell(out) - line_offset), comment.text);
339339
}
340-
memcpy(&comment, &dummy, sizeof(comment_t));
340+
memcpy(&comment, &dummy, sizeof(Comment));
341341
has_comment = 1;
342342
}
343343

@@ -409,16 +409,16 @@ static int data_extract_string(FILE *out, section_t *section, memmap_t *map, lab
409409
return 1;
410410
}
411411

412-
static int data_extract_jump_table(FILE *out, section_t *section, memmap_t *map, label_repository_t *repository,
413-
comment_repository_t *comments, int extra_infos) {
412+
static int data_extract_jump_table(FILE *out, Section *section, MemoryMap *map, LabelRepository *repository,
413+
CommentRepository *comments, int extra_infos) {
414414
const int32_t elements_per_line = section->data.elements_per_line;
415415

416416
int32_t i, j;
417417
uint8_t page;
418418
uint16_t logical;
419419

420-
label_t label;
421-
comment_t comment;
420+
Label label;
421+
Comment comment;
422422

423423
size_t line_offset = ftell(out);
424424
uint8_t line_page = section->page;
@@ -444,12 +444,12 @@ static int data_extract_jump_table(FILE *out, section_t *section, memmap_t *map,
444444
j = 0;
445445
}
446446

447-
comment_t dummy;
447+
Comment dummy;
448448
if (comment_repository_find(comments, logical, page, &dummy)) {
449449
if (has_comment) {
450450
print_inline_comment(out, (int)(ftell(out) - line_offset), comment.text);
451451
}
452-
memcpy(&comment, &dummy, sizeof(comment_t));
452+
memcpy(&comment, &dummy, sizeof(Comment));
453453
has_comment = 1;
454454
j = 0;
455455
}
@@ -498,25 +498,25 @@ static int data_extract_jump_table(FILE *out, section_t *section, memmap_t *map,
498498
}
499499

500500
/* Process data section. The result will be output has a binary file or an asm file containing hex values or strings. */
501-
int data_extract(FILE *out, section_t *section, memmap_t *map, label_repository_t *repository,
502-
comment_repository_t *comments, int extra_infos) {
501+
bool data_extract(FILE *out, Section *section, MemoryMap *map, LabelRepository *repository,
502+
CommentRepository *comments, int extra_infos) {
503503
switch (section->data.type) {
504-
case Binary:
504+
case DATA_TYPE_BINARY:
505505
return data_extract_binary(out, section, map, repository);
506-
case Hex:
506+
case DATA_TYPE_HEX:
507507
return data_extract_hex(out, section, map, repository, comments, extra_infos);
508-
case String:
508+
case DATA_TYPE_STRING:
509509
return data_extract_string(out, section, map, repository, comments, extra_infos);
510-
case JumpTable:
510+
case DATA_TYPE_JUMP_TABLE:
511511
return data_extract_jump_table(out, section, map, repository, comments, extra_infos);
512512
default:
513-
return 0;
513+
return false;
514514
}
515515
}
516516

517517
/* Process code section. */
518-
int decode(FILE *out, uint16_t *logical, section_t *section, memmap_t *map, label_repository_t *repository,
519-
comment_repository_t *comments, int extra_infos) {
518+
int decode(FILE *out, uint16_t *logical, Section *section, MemoryMap *map, LabelRepository *repository,
519+
CommentRepository *comments, int extra_infos) {
520520
int i, delta;
521521
uint8_t inst, data[6], is_jump;
522522
char eor;
@@ -525,9 +525,9 @@ int decode(FILE *out, uint16_t *logical, section_t *section, memmap_t *map, labe
525525
uint8_t current_page;
526526
uint16_t current_logical;
527527
uint16_t next_logical;
528-
label_t label;
528+
Label label;
529529

530-
const opcode_t *opcode;
530+
const Opcode *opcode;
531531

532532
eor = 0;
533533

@@ -794,7 +794,7 @@ int decode(FILE *out, uint16_t *logical, section_t *section, memmap_t *map, labe
794794
}
795795

796796
/* display inline comments if any */
797-
comment_t comment = {0};
797+
Comment comment = {0};
798798
int n = (int)(ftell(out) - start);
799799
if(comment_repository_find(comments, current_logical, current_page, &comment)) {
800800
print_inline_comment(out, n, comment.text);
@@ -806,10 +806,10 @@ int decode(FILE *out, uint16_t *logical, section_t *section, memmap_t *map, labe
806806
}
807807

808808
/* Computes section size. */
809-
int32_t compute_size(section_t *sections, int index, int count, memmap_t *map) {
809+
int32_t compute_size(Section *sections, int index, int count, MemoryMap *map) {
810810
uint8_t i;
811811
uint8_t data[7];
812-
section_t *current = &sections[index];
812+
Section *current = &sections[index];
813813
uint32_t start = current->logical;
814814
uint32_t logical = start;
815815

@@ -833,7 +833,7 @@ int32_t compute_size(section_t *sections, int index, int count, memmap_t *map) {
833833
}
834834
uint8_t page = memmap_page(map, logical);
835835
data[0] = memmap_read(map, logical);
836-
const opcode_t *opcode = opcode_get(data[0]);
836+
const Opcode *opcode = opcode_get(data[0]);
837837
for (i = 1; i < opcode->size; i++) {
838838
data[i] = memmap_read(map, logical + i);
839839
}
@@ -875,10 +875,10 @@ int32_t compute_size(section_t *sections, int index, int count, memmap_t *map) {
875875
}
876876

877877
/* Output hardware IO port and RAM labels. */
878-
void label_dump(FILE *out, memmap_t *map, label_repository_t *repository) {
878+
void label_dump(FILE *out, MemoryMap *map, LabelRepository *repository) {
879879
int count = label_repository_size(repository);
880880
for (int i = 0; i < count; i++) {
881-
label_t label;
881+
Label label;
882882
if (label_repository_get(repository, i, &label)) {
883883
// IO port and RAM
884884
if ((label.page == 0xff) || (label.page == 0xf8)) {

decode.h

Lines changed: 41 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,59 +39,52 @@
3939
#include "config.h"
4040
#include "label.h"
4141
#include "section.h"
42-
#include "memorymap.h"
42+
#include "memory_map.h"
4343
#include "comment.h"
4444

45-
/**
46-
* Finds any jump address from the current section.
47-
* @param [in] section Current section.
48-
* @param [in] map Memory map.
49-
* @param [in out] repository Label repository.
50-
* @return 1 upon success, 0 otherwise.
51-
*/
52-
int label_extract(section_t *section, memmap_t *map, label_repository_t *repository);
45+
/// Finds any jump address from the current section.
46+
/// \param [in] section Current section.
47+
/// \param [in] map Memory map.
48+
/// \param [in out] repository Label repository.
49+
/// \return true upon success.
50+
/// \return false if an error occured.
51+
bool label_extract(Section *section, MemoryMap *map, LabelRepository *repository);
5352

54-
/**
55-
* Process data section. The result will be output has a binary file or an asm file containing hex values or strings.
56-
* @param [out] out File output.
57-
* @param [in] section Current section.
58-
* @param [in] map Memory map.
59-
* @param [in] repository Label repository.
60-
* @param [in] comments Comments repository.
61-
* @param [in] extra_infos Display extra informations as comments (if none set).
62-
* @return 1 upon success, 0 otherwise.
63-
*/
64-
int data_extract(FILE *out, section_t *section, memmap_t *map, label_repository_t *repository, comment_repository_t *comments, int extra_infos);
53+
/// Process data section. The result will be output has a binary file or an asm file containing hex values or strings.
54+
/// \param [out] out File output.
55+
/// \param [in] section Current section.
56+
/// \param [in] map Memory map.
57+
/// \param [in] repository Label repository.
58+
/// \param [in] comments Comments repository.
59+
/// \param [in] extra_infos Display extra informations as comments (if none set).
60+
/// \return true upon success.
61+
/// \return false if an error occured.
62+
bool data_extract(FILE *out, Section *section, MemoryMap *map, LabelRepository *repository, CommentRepository *comments, int extra_infos);
6563

66-
/**
67-
* Process code section.
68-
* @param [out] out File output.
69-
* @param [in out] logical Current logical address.
70-
* @param [in] section Current section.
71-
* @param [in] map Memory map.
72-
* @param [in] repository Label repository.
73-
* @param [in] comments Comments repository.
74-
* @param [in] extra_infos Display extra informations as comments (if none set).
75-
* @return 1 if rts, rti or brk instruction was decoded, 0 otherwise.
76-
*/
77-
int decode(FILE *out, uint16_t *logical, section_t *section, memmap_t *map, label_repository_t *repository, comment_repository_t *comments, int extra_infos);
64+
/// Process code section.
65+
/// \param [out] out File output.
66+
/// \param [in out] logical Current logical address.
67+
/// \param [in] section Current section.
68+
/// \param [in] map Memory map.
69+
/// \param [in] repository Label repository.
70+
/// \param [in] comments Comments repository.
71+
/// \param [in] extra_infos Display extra informations as comments (if none set).
72+
/// \return true if rts, rti or brk instruction was decoded.
73+
/// \return false otherwise.
74+
int decode(FILE *out, uint16_t *logical, Section *section, MemoryMap *map, LabelRepository *repository, CommentRepository *comments, int extra_infos);
7875

79-
/**
80-
* Computes section size.
81-
* @param [in] sections Section array.
82-
* @param [in] index Index of the current section.
83-
* @param [in] count Number of sections.
84-
* @param [in] map Memory map.
85-
* @return Section size.
86-
*/
87-
int32_t compute_size(section_t *sections, int index, int count, memmap_t *map);
76+
/// Computes section size.
77+
/// \param [in] sections Section array.
78+
/// \param [in] index Index of the current section.
79+
/// \param [in] count Number of sections.
80+
/// \param [in] map Memory map.
81+
/// \return Section size.
82+
int32_t compute_size(Section *sections, int index, int count, MemoryMap *map);
8883

89-
/**
90-
* Output hardware IO port and RAM labels.
91-
* @param [out] out File output.
92-
* @param [in] map Memory map.
93-
* @param [in] repository Label repository.
94-
*/
95-
void label_dump(FILE *out, memmap_t *map, label_repository_t *repository);
84+
/// Output hardware IO port and RAM labels.
85+
/// \param [out] out File output.
86+
/// \param [in] map Memory map.
87+
/// \param [in] repository Label repository.
88+
void label_dump(FILE *out, MemoryMap *map, LabelRepository *repository);
9689

9790
#endif // ETRIPATOR_DECODE_H

0 commit comments

Comments
 (0)