added support for json schemas

This commit is contained in:
Louis Heredero 2023-11-24 17:37:26 +01:00
parent ab168b5f0a
commit 84b935c898
2 changed files with 124 additions and 1 deletions

105
example3.json Normal file
View File

@ -0,0 +1,105 @@
{
"structures": {
"main": {
"bits": 32,
"ranges": {
"31-28": {
"name": "cond"
},
"27": {
"name": "0"
},
"26": {
"name": "1"
},
"25": {
"name": "I"
},
"24": {
"name": "P",
"description": "pre / post indexing bit",
"values": {
"0": "post, add offset after transfer",
"1": "pre, add offset before transfer"
}
},
"23": {
"name": "U",
"description": "up / down bit",
"values": {
"0": "down, subtract offset from base",
"1": "up, addition offset to base"
}
},
"22": {
"name": "B",
"description": "byte / word bit",
"values": {
"0": "transfer word quantity",
"1": "transfer byte quantity"
}
},
"21": {
"name": "W",
"description": "write-back bit",
"values": {
"0": "no write-back",
"1": "write address into base"
}
},
"20": {
"name": "L",
"description": "load / store bit",
"values": {
"0": "store to memory",
"1": "load from memory"
}
},
"19-16": {
"name": "Rn",
"description": "base register"
},
"15-12": {
"name": "Rd",
"description": "source / destination register"
},
"11-0": {
"name": "offset",
"depends-on": "25",
"values": {
"0": {
"description": "offset is an immediate value",
"structure": "immediateOffset"
},
"1": {
"description": "offset is a register",
"structure": "registerOffset"
}
}
}
}
},
"immediateOffset": {
"bits": 12,
"ranges": {
"11-0": {
"name": "12-bit immediate offset",
"description": "unsigned number"
}
}
},
"registerOffset": {
"bits": 12,
"ranges": {
"11-4": {
"name": "shift",
"description": "shift applied to Rm"
},
"3-0": {
"name": "Rm",
"description": "offset register"
}
}
}
}
}

View File

@ -1,18 +1,36 @@
import json
import os
import yaml
from config import Config
from renderer import Renderer
from structure import Structure
class UnsupportedFormatException(Exception):
...
class InstructionSetSchema:
VALID_EXTENSIONS = ("yaml", "json")
def __init__(self, path: str, configPath: str = "config.json") -> None:
self.config = Config(configPath)
self.path = path
self.load()
def load(self) -> None:
_, ext = os.path.splitext(self.path)
ext = ext[1:].lower()
if ext not in InstructionSetSchema.VALID_EXTENSIONS:
fmts = tuple(map(lambda fmt: f".{fmt}", InstructionSetSchema.VALID_EXTENSIONS))
raise UnsupportedFormatException(f"'.{ext}' files are not supported. Valid formats: {fmts}")
with open(self.path, "r") as f:
schema = yaml.safe_load(f)
if ext == "yaml":
schema = yaml.safe_load(f)
elif ext == "json":
schema = json.load(f)
self.structures = {}