Skip to content

Commit

Permalink
Improve memory read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
qwe661234 committed Apr 6, 2023
1 parent 66300d0 commit 8d6908f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
64 changes: 40 additions & 24 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,37 +85,21 @@ uint32_t memory_ifetch(memory_t *m, uint32_t addr)
uint32_t memory_read_w(memory_t *m, uint32_t addr)
{
const uint32_t addr_lo = addr & mask_lo;
if (addr_lo <= 0xfffc) { /* test if this is within one chunk */
chunk_t *c;
if ((c = m->chunks[addr >> 16]))
return *(const uint32_t *) (c->data + addr_lo);
return 0U;
}
uint32_t dst = 0;
memory_read(m, (uint8_t *) &dst, addr, 4);
return dst;
chunk_t *c = m->chunks[addr >> 16];
return *(const uint32_t *) (c->data + addr_lo);
}

uint16_t memory_read_s(memory_t *m, uint32_t addr)
{
const uint32_t addr_lo = addr & mask_lo;
if (addr_lo <= 0xfffe) { /* test if this is within one chunk */
chunk_t *c;
if ((c = m->chunks[addr >> 16]))
return *(const uint16_t *) (c->data + addr_lo);
return 0U;
}
uint16_t dst = 0;
memory_read(m, (uint8_t *) &dst, addr, 2);
return dst;
chunk_t *c = m->chunks[addr >> 16];
return *(const uint16_t *) (c->data + addr_lo);
}

uint8_t memory_read_b(memory_t *m, uint32_t addr)
{
chunk_t *c;
if ((c = m->chunks[addr >> 16]))
return *(c->data + (addr & 0xffff));
return 0U;
chunk_t *c = m->chunks[addr >> 16];
return c->data[addr & mask_lo];
}

void memory_write(memory_t *m, uint32_t addr, const uint8_t *src, uint32_t size)
Expand All @@ -125,14 +109,46 @@ void memory_write(memory_t *m, uint32_t addr, const uint8_t *src, uint32_t size)
uint32_t x = p >> 16;
chunk_t *c = m->chunks[x];
if (!c) {
c = malloc(sizeof(chunk_t));
memset(c->data, 0, sizeof(c->data));
c = calloc(1, sizeof(chunk_t));
m->chunks[x] = c;
}
c->data[p & 0xffff] = src[i];
}
}

void memory_write_w(memory_t *m, uint32_t addr, const uint8_t *src)
{
const uint32_t addr_lo = addr & mask_lo, addr_hi = addr >> 16;
chunk_t *c = m->chunks[addr_hi];
if (unlikely(!c)) {
c = calloc(1, sizeof(chunk_t));
m->chunks[addr_hi] = c;
}
*(uint32_t *) (c->data + addr_lo) = *(const uint32_t *) src;
}

void memory_write_s(memory_t *m, uint32_t addr, const uint8_t *src)
{
const uint32_t addr_lo = addr & mask_lo, addr_hi = addr >> 16;
chunk_t *c = m->chunks[addr_hi];
if (unlikely(!c)) {
c = calloc(1, sizeof(chunk_t));
m->chunks[addr_hi] = c;
}
*(uint16_t *) (c->data + addr_lo) = *(const uint16_t *) src;
}

void memory_write_b(memory_t *m, uint32_t addr, const uint8_t *src)
{
const uint32_t addr_lo = addr & mask_lo, addr_hi = addr >> 16;
chunk_t *c = m->chunks[addr_hi];
if (unlikely(!c)) {
c = calloc(1, sizeof(chunk_t));
m->chunks[addr_hi] = c;
}
c->data[addr_lo] = src[0];
}

void memory_fill(memory_t *m, uint32_t addr, uint32_t size, uint8_t val)
{
for (uint32_t i = 0; i < size; ++i) {
Expand Down
7 changes: 7 additions & 0 deletions src/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ void memory_write(memory_t *m,
uint32_t addr,
const uint8_t *src,
uint32_t size);

void memory_write_w(memory_t *m, uint32_t addr, const uint8_t *src);

void memory_write_s(memory_t *m, uint32_t addr, const uint8_t *src);

void memory_write_b(memory_t *m, uint32_t addr, const uint8_t *src);

void memory_fill(memory_t *m, uint32_t addr, uint32_t size, uint8_t val);
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static const char *opt_prog_name = "a.out";
{ \
state_t *s = rv_userdata(rv); \
IIF(RW) \
(memory_write(s->mem, addr, (uint8_t *) &data, sizeof(data)), \
(memory_##op(s->mem, addr, (uint8_t *) &data), \
return memory_##op(s->mem, addr)); \
}

Expand Down

0 comments on commit 8d6908f

Please sign in to comment.