-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmemory.cpp
93 lines (77 loc) · 3.33 KB
/
memory.cpp
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
//==============================================================================
// _____ __ __ __ ____ ____ ___ _ __
// | ____| \/ | / /_| ___| / ___( _ )/ |/ /_
// | _| | |\/| |_____| '_ \___ \| | / _ \| | '_ \
// | |___| | | |_____| (_) |__) | |__| (_) | | (_) |
// |_____|_|__|_|___ __\___/____/ \____\___/|_|\___/
// | ____/ ___|| _ \___ /___ \
// | _| \___ \| |_) ||_ \ __) |
// | |___ ___) | __/___) / __/
// |_____|____/|_| |____/_____|
//
//------------------------------------------------------------------------------
// Copyright (C),2019 Andrew John Jacobs
// All rights reserved.
//
// This work is made available under the terms of the Creative Commons
// Attribution-NonCommercial-ShareAlike 4.0 International license. Open the
// following URL to see the details.
//
// http://creativecommons.org/licenses/by-nc-sa/4.0/
//------------------------------------------------------------------------------
// Notes:
//
// The ESP32's heap appears to be highly fragmented at startup and only a few
// large blocks can be allocated before malloc fails. Using a smaller block
// size allows more RAM to be utilised.
//==============================================================================
#include <Arduino.h>
#pragma GCC optimize ("-O3")
#include "memory.h"
//==============================================================================
const uint8_t *Memory::pRd [RAM_BLOCKS + ROM_BLOCKS];
uint8_t *Memory::pWr [RAM_BLOCKS + ROM_BLOCKS];
//==============================================================================
// Construct and initialise a Memory instance
Memory::Memory ()
{ }
// Build a RAM region from dynamically allocated blocks
void Memory::add (uint32_t address, int32_t size)
{
Serial.printf ("%.6x-%.6x: RAM (Allocated)\n", address, address + size - 1);
for (; size > 0; address += BLOCK_SIZE, size -= BLOCK_SIZE) {
register uint32_t block = blockOf (address);
register uint8_t *pRAM = (uint8_t *) malloc (BLOCK_SIZE);
if (pRAM)
pRd [block] = pWr [block] = pRAM;
else
Serial.printf ("!! Attempt to add NULL RAM block at %.6x", address);
}
}
// Build a RAM region from an existing contiguous blocks
void Memory::add (uint32_t address, uint8_t *pRAM, int32_t size)
{
Serial.printf ("%.6x-%.6x: RAM (Contiguous)\n", address, address + size - 1);
if (pRAM) {
for (; size > 0; address += BLOCK_SIZE, pRAM += BLOCK_SIZE, size -= BLOCK_SIZE) {
register uint32_t block = blockOf (address);
pRd [block] = pWr [block] = pRAM;
}
}
else
Serial.printf ("!! Attempt to add NULL RAM block at %.6x", address);
}
// Build a ROM region from an existing contiguous blocks
void Memory::add (uint32_t address, const uint8_t *pROM, int32_t size)
{
Serial.printf ("%.6x-%.6x: ROM (Contiguous)\n", address, address + size - 1);
if (pROM) {
for (; size > 0; address += BLOCK_SIZE, pROM += BLOCK_SIZE, size -= BLOCK_SIZE) {
register uint32_t block = blockOf (address);
pRd [block] = pROM;
pWr [block] = NULL;
}
}
else
Serial.printf ("!! Attempt to add NULL ROM block at %.6x", address);
}