Files
Sprinter-SDCC/tests/mem_test/mem_test.c
T
snark13 737c974400 Add mdview markdown viewer, reorganize tests/examples and libc layout
- Split tests/ (libc feature tests) and examples/ (real apps); shared
  app.mk in repo root, was examples/example.mk
- libc/io/* split into libc/{conio,env,errno,file,mouse,string,sys,
  time,video}/ — clearer module boundaries
- New examples/mdview/: markdown viewer (Phases 1-5 + light nested
  lists). Headers (H1-H4), HR, ulist/olist/quote with nesting via
  leading spaces, fenced code blocks, inline emphasis (bold/italic/
  underscore/code), wrap/unwrap mode with soft wrap (F2), horizontal
  pan (← →) with '>' truncation indicator
- libc additions: scroll() in conio (ESTEX SCROLL), strlwr/strupr,
  gets() test
- Makefile updates across tests/ for the new shared app.mk path

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-04 22:23:36 +03:00

88 lines
2.5 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sprinter.h>
#include <sprinter_mem.h>
/*
* Exercises the ESTEX EMM page allocator + bank_read/bank_write helpers.
*
* Plan:
* 1. Show free-page count via mem_info.
* 2. Allocate 3 pages → block id.
* 3. For each page in the block, write a recognizable pattern via
* bank_write, then read it back via bank_read into a near buffer.
* 4. Cross-check the readback to prove pages are independent.
* 5. Free the block, show the free-page count again.
*/
static void show_mem(const char *label)
{
uint16_t total, free_pages;
mem_info(&total, &free_pages);
printf(" %s total=%u free=%u (each page = 16 KB)\n",
label, total, free_pages);
}
int main(void)
{
puts("Sprinter page allocator demo");
puts("");
show_mem("before:");
uint8_t blk = mem_alloc_pages(3);
if (blk == 0) {
puts(" mem_alloc_pages(3) failed");
(void)getchar();
return 1;
}
printf(" allocated block id = %u (3 pages)\n", blk);
show_mem("after alloc:");
/* Show the physical page numbers we got. */
for (uint8_t i = 0; i < 3; i++) {
uint8_t phys = mem_get_page(blk, i);
printf(" page %u of block = phys 0x%02X\n", i, phys);
}
/* Write a 32-byte pattern at offset 0 of each page; pattern depends
* on page index so we can distinguish them. */
char buf[33];
for (uint8_t i = 0; i < 3; i++) {
uint8_t phys = mem_get_page(blk, i);
for (int j = 0; j < 32; j++) {
buf[j] = 'A' + (char)i; /* page 0 -> 'A', page 1 -> 'B', page 2 -> 'C' */
}
buf[32] = 0;
bank_write(phys, 0, buf, 32);
printf(" wrote 32 x '%c' to page %u (phys 0x%02X)\n",
'A' + i, i, phys);
}
/* Read back and verify each page has its own pattern. */
puts("");
puts("readback:");
for (uint8_t i = 0; i < 3; i++) {
uint8_t phys = mem_get_page(blk, i);
memset(buf, 0, sizeof(buf));
bank_read(phys, 0, buf, 32);
buf[32] = 0;
printf(" page %u (phys 0x%02X) first 32 bytes: \"%s\"\n",
i, phys, buf);
}
/* Single-byte API spot check. */
bank_store_byte(mem_get_page(blk, 1), 100, 0x42);
uint8_t got = bank_load_byte(mem_get_page(blk, 1), 100);
printf("\n store/load single byte at page1[100]: 0x%02X (expect 0x42)\n", got);
mem_free_block(blk);
show_mem("after free:");
puts("");
puts("Press any key to exit.");
(void)getchar();
return 0;
}