#include "apps.h"
#include "string.h"
-// The large multi-purpose buffer, typically used to hold A/D samples or traces,
-// may be processed in some way. Also used to hold various smaller buffers.
-static uint8_t BigBuf[BIGBUF_SIZE];
+// BigBuf is the large multi-purpose buffer, typically used to hold A/D samples or traces.
+// Also used to hold various smaller buffers and the Mifare Emulator Memory.
+
+// declare it as uint32_t to achieve alignment to 4 Byte boundary
+static uint32_t BigBuf[BIGBUF_SIZE/sizeof(uint32_t)];
// High memory mark
static uint16_t BigBuf_hi = BIGBUF_SIZE;
-// trace related global variables. Change to function calls?
-//uint8_t *trace = BigBuf;
-uint16_t traceLen;
+// pointer to the emulator memory.
+static uint8_t *emulator_memory = NULL;
+
+// trace related global variables
+// (only one left). ToDo: make this static as well?
+uint16_t traceLen = 0;
// get the address of BigBuf
uint8_t *BigBuf_get_addr(void)
{
- return BigBuf;
+ return (uint8_t *)BigBuf;
+}
+
+
+// get the address of the emulator memory. Allocate part of Bigbuf for it, if not yet done
+uint8_t *BigBuf_get_EM_addr(void)
+{
+ if (emulator_memory == NULL) { // not yet allocated
+ emulator_memory = BigBuf_malloc(CARD_MEMORY_SIZE);
+ }
+
+ return emulator_memory;
}
}
-// allocate a chunk of memory from BigBuf. We allocate high memory first. Low memory
-// is always for traces/samples
+// allocate a chunk of memory from BigBuf. We allocate high memory first. The unallocated memory
+// at the beginning of BigBuf is always for traces/samples
uint8_t *BigBuf_malloc(uint16_t chunksize)
{
if (BigBuf_hi - chunksize < 0) {
- return NULL; // no memory left
+ return NULL; // no memory left
} else {
- BigBuf_hi -= chunksize; // aligned to 4 Byte boundary
- return BigBuf + BigBuf_hi;
+ chunksize = (chunksize + 3) & 0xfffc; // round to next multiple of 4
+ BigBuf_hi -= chunksize; // aligned to 4 Byte boundary
+ return (uint8_t *)BigBuf + BigBuf_hi;
}
}
-// free ALL allocated chunks. The whole BigBuf is available for traces again.
+// free ALL allocated chunks. The whole BigBuf is available for traces or samples again.
void BigBuf_free(void)
{
BigBuf_hi = BIGBUF_SIZE;
+ emulator_memory = NULL;
+}
+
+
+// free allocated chunks EXCEPT the emulator memory
+void BigBuf_free_keep_EM(void)
+{
+ if (emulator_memory != NULL) {
+ BigBuf_hi = emulator_memory - (uint8_t *)BigBuf;
+ } else {
+ BigBuf_hi = BIGBUF_SIZE;
+ }
}
// return the maximum trace length (i.e. the unallocated size of BigBuf)
-uint16_t BigBuf_max_trace_len(void)
+uint16_t BigBuf_max_traceLen(void)
{
return BigBuf_hi;
-}
\ No newline at end of file
+}