YaK:: NitrOS-9 memory mapping experiment (May 12 2022) [Changes]   [Calendar]   [Search]   [Index]   [PhotoTags]   
[mega_changes]
[photos]

NitrOS-9 memory mapping experiment (May 12 2022)


strick@nand:~/coco-shelf/frobio$ cat /home/strick/glap/strickyak/pythonine.with.experimental/v0.1/tool/kmem.c
#include <cmoc.h>
#include <assert.h>

#include "mmap.h"

#define NUM_BLOCKS 40

byte BlockNum[NUM_BLOCKS];
byte Buffer[1024];

void PrintBlockMap() {
#if 0
  word bpb = 0;
  word num_blks = 0;
  int err = GetBlockMap(Buffer, &bpb, &num_blks);
  if (err) { printf("ERROR %d\n", err); return; }

  printf("bpb: %u num_blks: %u\n", bpb, num_blks);
  for (int i=0; i < (num_blks+15); i+=16) {
    printf("%04x:", i);
    for (int j=0; j<16; j++) {
      printf(" %02x", Buffer[i+j]);
    }
    printf("\n");
  }
#endif
}

int main() {
  printf("main=$%04x\n", (unsigned)(void*)main);
  PrintBlockMap();

  for (int i=0; i < NUM_BLOCKS; i++) {
    word start_blk, addr;
    printf("allocate ram[%d] ... ", i);
    byte err = AllocateBlock(1, &start_blk);
    if (err) { printf("ERROR %d\n", err); return err; }
    BlockNum[i] = (byte)start_blk;
    printf("=> blk %d; ", start_blk);
    err = MapBlock(start_blk, 1, &addr);
    printf(" MAP addr => %x; error %d\n", addr, err);
    PrintBlockMap();

    if (!err && (i%3)>0) {
        err = UnMapBlock(addr, 1);
        printf(" UNMAP addr: %x ; error %d\n", addr, err);
        PrintBlockMap();
    }
  }

  printf("Part Two\n");
  for (word addr = 0x2000; addr < 0xE000; addr += 0x2000) {
        byte err = UnMapBlock(addr, 1);
        printf(" UNMAP addr: %x ; error %d\n", addr, err);
  }

  printf("Part Three\n");
  for (word k = 0; k < NUM_BLOCKS; k++) {
        byte b = BlockNum[k];
        word addr;
        byte err = MapBlock(b, 1, &addr);
        printf("k=%d MAP start: %d addr: %x ; error %d\n", k, b, addr, err);
        *(byte*)addr = 0xDE ^ b;
        err = UnMapBlock(addr, 1);
        printf("...  UNMAP addr: %x ; error %d\n", addr, err);
  }

  printf("Part Four\n");
  for (word k = 0; k < NUM_BLOCKS; k++) {
        byte b = BlockNum[k];
        word addr;
        byte err = MapBlock(b, 1, &addr);
        printf("k=%d MAP start: %d addr: %x ; error %d\n", k, b, addr, err);
        assert( *(byte*)addr == 0xDE ^ b);
        err = UnMapBlock(addr, 1);
        printf("...  UNMAP addr: %x ; error %d\n", addr, err);
  }

  printf("Part Five\n");
  for (word k = 0; k < NUM_BLOCKS; k++) {
        byte b = BlockNum[k];
        printf("POKE: k=%d MAP start: %d \n", k, b );
        Disable();
        POKE(0xFFA0 + 8 + 6, b);
        assert( *(byte*)0xC000 == 0xDE ^ b);
        Enable();
  }
  return 0;
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


strick@nand:~/glap/strickyak/pythonine.with.experimental/v0.1/tool$ cat mmap.h
#ifndef _MMAP_H_
#define _MMAP_H_

#include "types.h"

asm void Disable();  // interrupts.
asm void Enable();  // interrupts.
byte AllocateBlock(byte num_blks, word* start_blk_out);
byte MapBlock(word start_blk, byte num_blks, word* addr_out);
byte UnMapBlock(word addr, byte num_blks);
int GetBlockMap(byte* buf, word* bpb_out, word* num_blks_out);

#define PEEK(ADDR) (*(byte*)(ADDR))
#define POKE(ADDR,VAL) (*(byte*)(ADDR) = (VAL))

#endif

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


strick@nand:~/glap/strickyak/pythonine.with.experimental/v0.1/tool$ cat mmap.c
#include "mmap.h"

#define F_AllRAM 0x39
#define F_GBlkMp 0x19
#define F_ClrBlk 0x50
#define F_MapBlk 0x4F

asm void Disable() {
  asm {
    orcc    #$50
  }
}

asm void Enable() {
  asm {
    andcc    #$AF
  }
}

byte AllocateBlock(byte num_blks, word* start_blk_out) {
  byte err = 0;
  asm {
    PSHS Y,U
    LDB num_blks
    os9 F_AllRAM
    PULS Y,U
    bcc AR__OKAY

AR__BAD
    stb err
    bra AR__RET

AR__OKAY
    std [start_blk_out]

AR__RET
    nop
  }
  return err;
}

byte MapBlock(word start_blk, byte num_blks, word* addr_out) {
  byte err = 0;
  asm {
    PSHS Y,U
    LDB num_blks
    LDX start_blk
    os9 F_MapBlk
    TFR U,X
    PULS Y,U
    bcc MB__OKAY

MB__BAD
    stb err
    bra MB__RET

MB__OKAY
    stx [addr_out]

MB__RET
    nop
  }
  return err;
}
byte UnMapBlock(word addr, byte num_blks) {
  byte err = 0;
  asm {
    PSHS Y,U
    LDB num_blks
    LDU addr
    os9 F_ClrBlk
    PULS Y,U
    bcc UMB__OKAY

UMB__BAD
    stb err
    bra UMB__RET

UMB__OKAY
UMB__RET
    nop
  }
  return err;
}

int GetBlockMap(byte* buf, word* bpb_out, word* num_blks_out) {
  int err = 0;
  asm {
    PSHS Y
    LDX buf
    PSHS U
    os9 F_GBlkMp
    PULS U
    bcc GVM__OKAY

GVM__BAD
    PULS Y
    clra
    std err
    bra GVM__RET

GVM__OKAY
    TFR Y,X
    PULS Y
    stD [bpb_out]
    stX [num_blks_out]

GVM__RET
    nop
  }
  return err;
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

strick@nand:~/glap/strickyak/pythonine.with.experimental/v0.1/tool$ cat types.h
#ifndef _TYPES_H_
#define _TYPES_H_

typedef unsigned char byte;
typedef unsigned int word;
typedef byte bool;
typedef byte error;
#define true 1
#define false 0

#endif
(unless otherwise marked) Copyright 2002-2014 YakPeople. All rights reserved.
(last modified 2024-12-19)       [Login]
(No back references.)