Skip to content

Commit 8cdc948

Browse files
committed
Load non-overlayed programs
1 parent a96de3c commit 8cdc948

File tree

3 files changed

+106
-82
lines changed

3 files changed

+106
-82
lines changed

src/displayapp/screens/Pawn.cpp

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -327,32 +327,64 @@ static int AMXAPI prun_Overlay(AMX* amx, int index) {
327327
return AMX_ERR_NONE;
328328
}
329329

330-
static int load_program(AMX* amx, const uint8_t* data) {
330+
int Pawn::LoadProgram() {
331+
(void) program_len;
332+
333+
int result;
331334
AMX_HEADER hdr;
332-
memcpy(&hdr, data, sizeof(hdr));
335+
memcpy(&hdr, program, sizeof(hdr));
333336

334337
if (hdr.magic != AMX_MAGIC)
335338
return AMX_ERR_FORMAT;
336339

337-
void* header = malloc(hdr.cod);
338-
memcpy(header, data, hdr.cod);
340+
memset(&amx, 0, sizeof(amx));
339341

340-
void* datablock = malloc(hdr.stp - hdr.dat); // This block contains data, heap and stack
341-
memcpy(datablock, data + hdr.dat, hdr.hea - hdr.dat);
342+
header = malloc(hdr.cod);
343+
if (header == NULL)
344+
return AMX_ERR_MEMORY;
342345

343-
constexpr int poolsize = 512;
344-
void* overlaypool = malloc(poolsize + 8);
346+
memcpy(header, program, hdr.cod);
345347

346-
amx_poolinit(overlaypool, poolsize + 8);
348+
if (hdr.flags & AMX_FLAG_OVERLAY) {
349+
datablock = malloc(hdr.stp - hdr.dat); // This block contains data, heap and stack
350+
if (datablock == NULL)
351+
return AMX_ERR_MEMORY;
347352

348-
memset(amx, 0, sizeof(*amx));
349-
amx->data = (unsigned char*) datablock;
350-
amx->overlay = prun_Overlay;
353+
memcpy(datablock, program + hdr.dat, hdr.hea - hdr.dat);
351354

352-
int result = amx_Init(amx, header);
353-
if (result != AMX_ERR_NONE) {
354-
free(datablock);
355-
amx->base = NULL;
355+
constexpr int poolsize = 512;
356+
overlaypool = malloc(poolsize + 8);
357+
if (overlaypool == NULL)
358+
return AMX_ERR_MEMORY;
359+
360+
amx_poolinit(overlaypool, poolsize + 8);
361+
362+
amx.data = (unsigned char*) datablock;
363+
amx.overlay = prun_Overlay;
364+
365+
result = amx_Init(&amx, header);
366+
if (result != AMX_ERR_NONE) {
367+
free(header);
368+
header = NULL;
369+
free(datablock);
370+
datablock = NULL;
371+
free(overlaypool);
372+
overlaypool = NULL;
373+
}
374+
} else {
375+
datablock = malloc(hdr.stp); // This block contains code, data, heap and stack
376+
if (datablock == NULL)
377+
return AMX_ERR_MEMORY;
378+
379+
memcpy(datablock, program, hdr.size);
380+
381+
result = amx_Init(&amx, datablock);
382+
if (result != AMX_ERR_NONE) {
383+
free(header);
384+
header = NULL;
385+
free(datablock);
386+
datablock = NULL;
387+
}
356388
}
357389

358390
return result;
@@ -378,8 +410,7 @@ extern "C" const AMX_NATIVE pawn_natives[] = {
378410
};
379411

380412
Pawn::Pawn(Controllers::DateTime& dateTimeController) : dateTimeController(dateTimeController) {
381-
load_program(&amx, program);
382-
(void) program_len;
413+
LoadProgram();
383414

384415
amx.userdata[0] = this;
385416

@@ -404,7 +435,13 @@ Pawn::~Pawn() {
404435
lv_obj_clean(lv_scr_act());
405436

406437
amx_Cleanup(&amx);
407-
free(amx.base);
438+
439+
if (header)
440+
free(header);
441+
if (datablock)
442+
free(datablock);
443+
if (overlaypool)
444+
free(overlaypool);
408445
}
409446

410447
void Pawn::Refresh() {

src/displayapp/screens/Pawn.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ namespace Pinetime {
2020

2121
void Refresh() override;
2222

23-
2423
Utility::DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::minutes>> currentDateTime {};
2524
Controllers::DateTime& dateTimeController;
2625

2726
private:
2827
AMX amx;
2928
int refresh_index;
3029
lv_task_t* taskRefresh = 0;
30+
31+
void *header = nullptr, *datablock = nullptr, *overlaypool = nullptr;
32+
33+
int LoadProgram();
3134
};
3235
}
3336

src/displayapp/screens/program.h

Lines changed: 46 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,54 @@
11
const unsigned char program[] = {
2-
0x94, 0x03, 0x00, 0x00, 0xe0, 0xf1, 0x0b, 0x0b, 0x05, 0x00, 0x08, 0x00,
3-
0x7c, 0x00, 0x00, 0x00, 0x24, 0x03, 0x00, 0x00, 0x94, 0x03, 0x00, 0x00,
4-
0x5c, 0x04, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
2+
0xd4, 0x02, 0x00, 0x00, 0xe0, 0xf1, 0x0b, 0x0b, 0x05, 0x00, 0x08, 0x00,
3+
0x7c, 0x00, 0x00, 0x00, 0x64, 0x02, 0x00, 0x00, 0xd4, 0x02, 0x00, 0x00,
4+
0x9c, 0x03, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00,
55
0x44, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00,
66
0x4c, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x4c, 0x00, 0x00, 0x00,
77
0x01, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8-
0x6f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
9-
0x64, 0x01, 0x00, 0x00, 0x44, 0x01, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
10-
0x5c, 0x01, 0x00, 0x00, 0x1f, 0x00, 0x40, 0x72, 0x65, 0x66, 0x72, 0x65,
8+
0x6f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
9+
0x08, 0x01, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
10+
0x04, 0x01, 0x00, 0x00, 0x1f, 0x00, 0x40, 0x72, 0x65, 0x66, 0x72, 0x65,
1111
0x73, 0x68, 0x00, 0x66, 0x6f, 0x6e, 0x74, 0x5f, 0x6a, 0x6d, 0x65, 0x63,
12-
0x00, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13-
0x1e, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
14-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
15-
0xfd, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
16-
0x08, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
17-
0x56, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
18-
0xf6, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00,
19-
0x03, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20-
0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
21-
0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00,
22-
0x08, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff,
23-
0x14, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
24-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x99, 0x99, 0x99, 0x00,
25-
0x89, 0x80, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
26-
0x70, 0x00, 0x00, 0x00, 0xf4, 0xff, 0xff, 0xff, 0x14, 0x00, 0x00, 0x00,
27-
0x71, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
28-
0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0xfd, 0xff, 0xff, 0xff,
29-
0x08, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
30-
0x71, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31-
0x00, 0x00, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32-
0x55, 0x00, 0x00, 0x00, 0x8e, 0x80, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00,
33-
0x04, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0xf2, 0xff, 0xff, 0xff,
34-
0x14, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
35-
0x56, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
36-
0xf6, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00,
37-
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38-
0x08, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
39-
0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00,
40-
0x04, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff,
41-
0x14, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x4e, 0x00, 0x00, 0x00,
42-
0x1e, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
12+
0x00, 0x00, 0x00, 0x00, 0xad, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00,
13+
0x8e, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
14+
0xfd, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x87, 0x00, 0x08, 0x00,
15+
0x92, 0x00, 0x0c, 0x00, 0x8f, 0x00, 0x08, 0x00, 0x70, 0x00, 0x00, 0x00,
16+
0xf6, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x3c, 0x00,
17+
0x8e, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
18+
0xfe, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00,
19+
0x8f, 0x00, 0x08, 0x00, 0x70, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff,
20+
0x14, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00,
21+
0x95, 0x00, 0x02, 0x00, 0x99, 0x99, 0x99, 0x00, 0x89, 0x80, 0x00, 0x00,
22+
0x8f, 0x00, 0x08, 0x00, 0x70, 0x00, 0x00, 0x00, 0xf4, 0xff, 0xff, 0xff,
23+
0x14, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00,
24+
0x70, 0x00, 0x00, 0x00, 0xfd, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00,
25+
0x87, 0x00, 0x04, 0x00, 0x8e, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00,
26+
0x8f, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, 0x8e, 0x80, 0x00, 0x00,
27+
0x8f, 0x00, 0x04, 0x00, 0x70, 0x00, 0x00, 0x00, 0xf2, 0xff, 0xff, 0xff,
28+
0x14, 0x00, 0x00, 0x00, 0x92, 0x00, 0x10, 0x00, 0x8f, 0x00, 0x04, 0x00,
29+
0x70, 0x00, 0x00, 0x00, 0xf6, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00,
30+
0x8e, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x08, 0x00,
31+
0x70, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
32+
0x16, 0x00, 0x00, 0x00, 0x8f, 0x00, 0x04, 0x00, 0x70, 0x00, 0x00, 0x00,
33+
0xf8, 0xff, 0xff, 0xff, 0x14, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00,
34+
0x4e, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x92, 0x00, 0x18, 0x00,
4335
0x70, 0x00, 0x00, 0x00, 0xf1, 0xff, 0xff, 0xff, 0x04, 0x00, 0x00, 0x00,
44-
0x09, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
45-
0x23, 0x00, 0x00, 0x00, 0x18, 0x01, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
46-
0x18, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
47-
0x18, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
48-
0x64, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,
49-
0x59, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00,
50-
0x08, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
51-
0x70, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x14, 0x00, 0x00, 0x00,
52-
0x59, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00,
53-
0x04, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0xf6, 0xff, 0xff, 0xff,
54-
0x08, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
55-
0x60, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
56-
0xf0, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00,
57-
0x18, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
58-
0x18, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00,
59-
0x09, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00,
60-
0x10, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00, 0x00,
61-
0x02, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00,
62-
0x55, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00,
63-
0x30, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
64-
0x1c, 0x00, 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
65-
0x56, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00,
66-
0xf6, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x56, 0x00, 0x00, 0x00,
67-
0x08, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0xf7, 0xff, 0xff, 0xff,
36+
0x83, 0x00, 0x18, 0x00, 0x07, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00,
37+
0xbc, 0x00, 0x00, 0x00, 0x83, 0x00, 0x18, 0x00, 0xa0, 0x00, 0x08, 0x00,
38+
0x18, 0x00, 0x00, 0x00, 0x83, 0x00, 0x18, 0x00, 0xa0, 0x00, 0x0c, 0x00,
39+
0x18, 0x00, 0x00, 0x00, 0x92, 0x00, 0x50, 0x00, 0x8e, 0x00, 0x08, 0x00,
40+
0x92, 0x00, 0x30, 0x00, 0x70, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
41+
0x14, 0x00, 0x00, 0x00, 0x92, 0x00, 0x30, 0x00, 0x8f, 0x00, 0x04, 0x00,
42+
0x70, 0x00, 0x00, 0x00, 0xf6, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00,
43+
0x92, 0x00, 0x60, 0x00, 0x92, 0x00, 0x5c, 0x00, 0x70, 0x00, 0x00, 0x00,
44+
0xf0, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00, 0x83, 0x00, 0x18, 0x00,
45+
0xa0, 0x00, 0x14, 0x00, 0x18, 0x00, 0x00, 0x00, 0x92, 0x00, 0x60, 0x00,
46+
0x83, 0x00, 0x18, 0x00, 0xa0, 0x00, 0x10, 0x00, 0x18, 0x00, 0x00, 0x00,
47+
0x92, 0x00, 0x5c, 0x00, 0x92, 0x00, 0x64, 0x00, 0x8e, 0x00, 0x08, 0x00,
48+
0x92, 0x00, 0x30, 0x00, 0x70, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
49+
0x1c, 0x00, 0x00, 0x00, 0x92, 0x00, 0x30, 0x00, 0x8f, 0x00, 0x08, 0x00,
50+
0x70, 0x00, 0x00, 0x00, 0xf6, 0xff, 0xff, 0xff, 0x08, 0x00, 0x00, 0x00,
51+
0x8f, 0x00, 0x08, 0x00, 0x70, 0x00, 0x00, 0x00, 0xf7, 0xff, 0xff, 0xff,
6852
0x04, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x4e, 0x00, 0x00, 0x00,
6953
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7054
0x00, 0x00, 0x3f, 0x3f, 0x30, 0x3a, 0x30, 0x30, 0x00, 0x00, 0x00, 0x30,
@@ -77,4 +61,4 @@ const unsigned char program[] = {
7761
0x00, 0x00, 0x00, 0x00, 0x25, 0x20, 0x73, 0x25, 0x73, 0x25, 0x20, 0x64,
7862
0x00, 0x64, 0x25, 0x20
7963
};
80-
unsigned int program_len = 916;
64+
unsigned int program_len = 724;

0 commit comments

Comments
 (0)