<commit_before>--Advanced RAM API

--[[

--RAM PLAN--

- MetaData (1 KB)
- DiskData (63 KB)
---
- Persistant Data (2 KB)
- GPIO (128 Bytes)
- Reserved (768 Bytes)
- Draw State (64 Bytes)
- Reserved (64 Bytes)
- Free Space (1 KB)
- Reserved (4 KB)
- Label Image (12 KBytes)
- VRAM (12 KBytes)

--Meta Data--

- Header -> LK12;OSData;DiskOS;GameDisk;Bin; -> 32 byte
- DiskVersion -> 1 Byte
- APIVersion -> 1 Byte
- AuthorName -> 16 Bytes.
- GameName -> 16 Bytes.
- DISK META -> 1 Packed Byte.

--Editors Data--
-- [EDITORID] \0 [DataSize] 2 bytes, [METASIZE] (Tokenized) \0 [THE METADATA]

--Disk META--
[1] Licensed Under CC0.
[2] Write Protection.
[3] Edit Protection.
[4] Auto Event Loop
[5] + [6] + [7] -> Language (Lua, Lisp, Moonscript, ASM, MetaLua)
[6] ^
[7] ^
[8] Mobile Friendly

]]

--The RAM table is created by boot.lua, It's the peripheral table.
if not RAM then return end --Incase if the RAM peripheral is disabled.
--Make the non-system functions as globals
for k,v in pairs(RAM) do
  if k:sub(1,1) ~= "_" then
    _G[k] = v
  end
end

--Localize the bit api for better perfromance.
local band, bor, bxor, lshift, rshift = bit.band, bit.bor, bit.bxor, bit.lshift, bit.rshift

local sw,sh = screenSize()

local MapObj = require("Libraries/map")
local UnallocatedRam = 1024*63+256

RAM.Resources = {} --A table of the available resources.
RAM.IResources = {} --A table of initialized resources.
RAM.DiskDataID = 0
RAM.DiskDataSize = 1024*64 - 736

--A function to convert from kilobytes into bytes, a SyntaxSugar.
local function KB(v) return v*1024 end

--Initialize the RAM
function RAM:initialize()
  --Remove any existing sections--
  local sections = self._getSections()
  for i=1,#sections do self._removeSection() end
  
  local Layout = {
    {736},    --[1] 0x0000 Meta Data (736 Bytes)
    {KB(64)-736}, --[2]    DiskData
    {KB(20)}, --[3] 0xB000 Compressed Lua Code (20 KB)
    {KB(02)}, --[4] 0x10000 Persistant Data (2 KB)
    {128},    --[5] 0x10800 GPIO (128 Bytes)
    {768},    --[6] 0x10880 Reserved (768 Bytes)
    {64},     --[7] 0x10B80 Draw State (64 Bytes)
    {64},     --[8] 0x10BC0 Reserved (64 Bytes)
    {KB(01)}, --[9] 0x10C00 Free Space (1 KB)
    {KB(04)}, --[10] 0x11000 Reserved (4 KB)
    {KB(12)}, --[11] 0x12000 Label Image (12 KBytes)
    {KB(12),"VRAM"}  --[12] 0x15000 VRAM (12 KBytes)
  }
  
  --Some reference variables for use later when creating Resources.
  self.DiskDataID = 2
  self.DiskDataSize = KB(64)-736
  
  --LabelImage Handler
  self.LabelImage = imageData(sw,sh)
  self.LabelHandler = self.newImageHandler(self.LabelImage)
  Layout[11] = self.LabelHandler
  
  --Create the new sections--
  for id, data in ipairs(Layout) do
    self._newSection(data[1], data[2])
  end
end

--[[ Create a new RAM resource type.
name (string): The name of the resource.
type (string or nil): The type of the resource, defaults to 'unknown'.
subtype (sting or nil): The subtype of the resource, defaults to ''.
steps (number or nil): The number of space steps, nil for infinity.
calcSize (function or nil): Calculate the required space provided a step number.
unit (function or nil): Return the name of the mesuring unit, defaults to 'bytes'.
enable (function): Clame the wanted resources and register some handlers.
disable (function): Revert changes made by the enable function.]]
function RAM:newResource(name,rtype,subtype,steps,calcSize,unit,enable,disable)
  local rtype, subtype = rtype or "unknown", subtype or ""
  local calcSize = calcSize or function(s)
    if s >= 1024 then
      return (s-1023)*1024
    else
      return s
    end
  end
  local unit = unit or function(st,sz)
    if s >= 1024 then
      return "KB", (s-1023)
    else
      return "Byte", s
    end
  end
  if type(name) ~= "string" then return error("Resource Name must be a string, provided: "..type(name)) end
  if type(rtype) ~= "string" then return error("Resource Type must be a string or a nil, provided: "..type(rtype)) end
  if type(subtype) ~= "string" then return error("Resource SubType must be a string or a nil, provided: "..type(subtype)) end
  if steps and type(steps) ~= "number" then return error("Resource size steps must be a number or a nil, provided: "..type(steps)) end
  if type(calcSize) ~= "function" then return error("Resource CalcSize must be a function or a nil, provided: "..type(calcSize)) end
  if type(unit) ~= "function" then return error("Resource unit must be a function or a nil, provided: "..type(unit)) end
  if type(enable) ~= "function" then return error("Resource enable must be a function, provided: "..type(enable)) end
  if type(disable) ~= "function" then return error("Resource disable must be a function, provided: "..type(disable)) end
  
  if self.Resources[name] then return error("Resource '"..name.."' already exists !") end
  self.Resources[name] = {
    name = name,
    type = rtype,
    subtype = subtype,
    steps = steps or false,
    calcSize = calcSize,
    unit = unit,
    enable = enable,
    disable = disable
  }
  return self, self.Resources[name]
end

--Create/Inilialize/Allocate a resource.
function RAM.createResource(name,steps)
  if type(name) ~= "string" then return error("Resource Name must be a string, provided: "..type(name)) end
  if type(steps) ~= "number" then retuen error("Resource Size Steps must be a number, provided: "..type(steps)) end
end

--Create a new RAM handler for an imagedata.
function RAM:newImageHandler(img)
  local imgline = img:width()/2 --The size of each image line in bytes.
  local imgsize = imgline * img:height() --The size of the image in bytes.
  local imgWidth, imgHeight = img:size()
  
  local imgline4, imgsize4 = imgline * 2, imgsize * 2 --For peek4 and poke4
  
  local changed = false --Is there any changes applied on the image ?
  
  local function hand(mode,startAddress,address,...)
    --cprint("IMAGE HAND",mode,startAddress,address,...)<commit_msg>Typo fix<commit_after>--Advanced RAM API

--[[

--RAM PLAN--

- MetaData (1 KB)
- DiskData (63 KB)
---
- Persistant Data (2 KB)
- GPIO (128 Bytes)
- Reserved (768 Bytes)
- Draw State (64 Bytes)
- Reserved (64 Bytes)
- Free Space (1 KB)
- Reserved (4 KB)
- Label Image (12 KBytes)
- VRAM (12 KBytes)

--Meta Data--

- Header -> LK12;OSData;DiskOS;GameDisk;Bin; -> 32 byte
- DiskVersion -> 1 Byte
- APIVersion -> 1 Byte
- AuthorName -> 16 Bytes.
- GameName -> 16 Bytes.
- DISK META -> 1 Packed Byte.

--Editors Data--
-- [EDITORID] \0 [DataSize] 2 bytes, [METASIZE] (Tokenized) \0 [THE METADATA]

--Disk META--
[1] Licensed Under CC0.
[2] Write Protection.
[3] Edit Protection.
[4] Auto Event Loop
[5] + [6] + [7] -> Language (Lua, Lisp, Moonscript, ASM, MetaLua)
[6] ^
[7] ^
[8] Mobile Friendly

]]

--The RAM table is created by boot.lua, It's the peripheral table.
if not RAM then return end --Incase if the RAM peripheral is disabled.
--Make the non-system functions as globals
for k,v in pairs(RAM) do
  if k:sub(1,1) ~= "_" then
    _G[k] = v
  end
end

--Localize the bit api for better perfromance.
local band, bor, bxor, lshift, rshift = bit.band, bit.bor, bit.bxor, bit.lshift, bit.rshift

local sw,sh = screenSize()

local MapObj = require("Libraries/map")
local UnallocatedRam = 1024*63+256

RAM.Resources = {} --A table of the available resources.
RAM.IResources = {} --A table of initialized resources.
RAM.DiskDataID = 0
RAM.DiskDataSize = 1024*64 - 736

--A function to convert from kilobytes into bytes, a SyntaxSugar.
local function KB(v) return v*1024 end

--Initialize the RAM
function RAM:initialize()
  --Remove any existing sections--
  local sections = self._getSections()
  for i=1,#sections do self._removeSection() end
  
  local Layout = {
    {736},    --[1] 0x0000 Meta Data (736 Bytes)
    {KB(64)-736}, --[2]    DiskData
    {KB(20)}, --[3] 0xB000 Compressed Lua Code (20 KB)
    {KB(02)}, --[4] 0x10000 Persistant Data (2 KB)
    {128},    --[5] 0x10800 GPIO (128 Bytes)
    {768},    --[6] 0x10880 Reserved (768 Bytes)
    {64},     --[7] 0x10B80 Draw State (64 Bytes)
    {64},     --[8] 0x10BC0 Reserved (64 Bytes)
    {KB(01)}, --[9] 0x10C00 Free Space (1 KB)
    {KB(04)}, --[10] 0x11000 Reserved (4 KB)
    {KB(12)}, --[11] 0x12000 Label Image (12 KBytes)
    {KB(12),"VRAM"}  --[12] 0x15000 VRAM (12 KBytes)
  }
  
  --Some reference variables for use later when creating Resources.
  self.DiskDataID = 2
  self.DiskDataSize = KB(64)-736
  
  --LabelImage Handler
  self.LabelImage = imageData(sw,sh)
  self.LabelHandler = self.newImageHandler(self.LabelImage)
  Layout[11] = self.LabelHandler
  
  --Create the new sections--
  for id, data in ipairs(Layout) do
    self._newSection(data[1], data[2])
  end
end

--[[ Create a new RAM resource type.
name (string): The name of the resource.
type (string or nil): The type of the resource, defaults to 'unknown'.
subtype (sting or nil): The subtype of the resource, defaults to ''.
steps (number or nil): The number of space steps, nil for infinity.
calcSize (function or nil): Calculate the required space provided a step number.
unit (function or nil): Return the name of the mesuring unit, defaults to 'bytes'.
enable (function): Clame the wanted resources and register some handlers.
disable (function): Revert changes made by the enable function.]]
function RAM:newResource(name,rtype,subtype,steps,calcSize,unit,enable,disable)
  local rtype, subtype = rtype or "unknown", subtype or ""
  local calcSize = calcSize or function(s)
    if s >= 1024 then
      return (s-1023)*1024
    else
      return s
    end
  end
  local unit = unit or function(st,sz)
    if s >= 1024 then
      return "KB", (s-1023)
    else
      return "Byte", s
    end
  end
  if type(name) ~= "string" then return error("Resource Name must be a string, provided: "..type(name)) end
  if type(rtype) ~= "string" then return error("Resource Type must be a string or a nil, provided: "..type(rtype)) end
  if type(subtype) ~= "string" then return error("Resource SubType must be a string or a nil, provided: "..type(subtype)) end
  if steps and type(steps) ~= "number" then return error("Resource size steps must be a number or a nil, provided: "..type(steps)) end
  if type(calcSize) ~= "function" then return error("Resource CalcSize must be a function or a nil, provided: "..type(calcSize)) end
  if type(unit) ~= "function" then return error("Resource unit must be a function or a nil, provided: "..type(unit)) end
  if type(enable) ~= "function" then return error("Resource enable must be a function, provided: "..type(enable)) end
  if type(disable) ~= "function" then return error("Resource disable must be a function, provided: "..type(disable)) end
  
  if self.Resources[name] then return error("Resource '"..name.."' already exists !") end
  self.Resources[name] = {
    name = name,
    type = rtype,
    subtype = subtype,
    steps = steps or false,
    calcSize = calcSize,
    unit = unit,
    enable = enable,
    disable = disable
  }
  return self, self.Resources[name]
end

--Create/Inilialize/Allocate a resource.
function RAM.createResource(name,steps)
  if type(name) ~= "string" then return error("Resource Name must be a string, provided: "..type(name)) end
  if type(steps) ~= "number" then return error("Resource Size Steps must be a number, provided: "..type(steps)) end
end

--Create a new RAM handler for an imagedata.
function RAM:newImageHandler(img)
  local imgline = img:width()/2 --The size of each image line in bytes.
  local imgsize = imgline * img:height() --The size of the image in bytes.
  local imgWidth, imgHeight = img:size()
  
  local imgline4, imgsize4 = imgline * 2, imgsize * 2 --For peek4 and poke4
  
  local changed = false --Is there any changes applied on the image ?
  
  local function hand(mode,startAddress,address,...)
    --cprint("IMAGE HAND",mode,startAddress,address,...)
{'fraction_non_alphanumeric': 0.13038692744052155, 'fraction_numerical': 0.03962407924815849, 'mean_word_length': 3.4439428141459745, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4788894', 'n_tokens_mistral': 4228, 'n_tokens_neox': 3877, 'n_words': 1474}