{"id":21215436,"url":"https://github.com/choueric/reggen","last_synced_at":"2025-07-10T11:31:29.900Z","repository":{"id":75764096,"uuid":"128747107","full_name":"choueric/regGen","owner":"choueric","description":"Generate boilerplate code of C macro definition for all registers and their fields of a chip.","archived":false,"fork":false,"pushed_at":"2018-05-31T08:21:50.000Z","size":120,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T13:01:53.326Z","etag":null,"topics":["boilerplate","embedded","golang","registers"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/choueric.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-04-09T09:25:56.000Z","updated_at":"2022-08-17T06:08:32.000Z","dependencies_parsed_at":"2023-03-08T06:15:30.238Z","dependency_job_id":null,"html_url":"https://github.com/choueric/regGen","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/choueric/regGen","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choueric%2FregGen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choueric%2FregGen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choueric%2FregGen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choueric%2FregGen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/choueric","download_url":"https://codeload.github.com/choueric/regGen/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choueric%2FregGen/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264572952,"owners_count":23630376,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["boilerplate","embedded","golang","registers"],"created_at":"2024-11-20T21:38:32.131Z","updated_at":"2025-07-10T11:31:29.895Z","avatar_url":"https://github.com/choueric.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# regGen\n\n[![Build Status](https://travis-ci.org/choueric/regGen.svg?branch=master)](https://travis-ci.org/choueric/kbdashboard)\n\nIt is a tool written in Golang to automatically generate code of registers and\nfields in registers of a chip by reading a register-description file.\n\nNow it firstly supports the auto-generation of C format because it's the most\napplication scence.\n\n# usage\n\nTo see the usage information:\n\n```sh\n$ reggen -h\nUsage of reggen:\n  -d    enable debug\n  -f string\n        output format type. [c] (default \"c\")\n  -i string\n        input file. (default \"input.regs\")\n```\n\nSo to output what you want, just:\n\n```sh\n$ reggen -i input.regs \u003e regs.h\n```\n\nBy redirection, you will get `regs.h` containing the macros about registers and\nfields.\n\n# example\n\nThe content of `input.regs` is:\n\n```\n# This is a simple regs file\n\u003cchip\u003e:simpleChip\n\n\u003cREG\u003e[Control]: 0\n        BYPASS: 1\n        FREE_RUN: 7 - 6 \n\n\u003cREG\u003e: 0x2\ntype: 4-5 (0b00: client, 0x01: server, 2: route, 3: peer)\n```\n\nAnd the output of C format will be:\n\n```c\n#pragma once\n\n#ifndef BIT\n#define BIT(x) (1 \u003c\u003c (x))\n#endif\n\n// ONLY for _8bit-width_ register\n#define MASK(a, b) (((uint8_t)-1 \u003e\u003e (7-(b))) \u0026 ~((1U\u003c\u003c(a))-1))\n\n// Registers of simpleChip\n\n#define REG_CONTROL 0x0 // 0\n\t#define REG_BYPASS_BIT BIT(1)\n\t#define REG_BYPASS_POS 1\n\t#define REG_BYPASS_VAL(rv) (((rv) \u0026 BIT(1)) \u003e\u003e 1)\n\t\t#define REG_BYPASS_ENABLE\t1\t// 0b1\t0x1\n\t\t#define REG_BYPASS_DISABLE\t0\t// 0b0\t0x0\n\t#define REG_FREE_RUN_STR 6\n\t#define REG_FREE_RUN_END 7\n\t#define REG_FREE_RUN_MSK MASK(6, 7)\n\t#define REG_FREE_RUN_VAL(rv) (((rv) \u0026 REG_FREE_RUN_MSK) \u003e\u003e 6)\n\t#define REG_FREE_RUN_SFT(v) (((v) \u0026 MASK(0, 1)) \u003c\u003c 6)\n\n#define REG_2 0x2 // 2\n\t#define REG_TYPE_STR 4\n\t#define REG_TYPE_END 5\n\t#define REG_TYPE_MSK MASK(4, 5)\n\t#define REG_TYPE_VAL(rv) (((rv) \u0026 REG_TYPE_MSK) \u003e\u003e 4)\n\t#define REG_TYPE_SFT(v) (((v) \u0026 MASK(0, 1)) \u003c\u003c 4)\n\t\t#define REG_TYPE_CLIENT\t0\t// 0b0\t0x0\n\t\t#define REG_TYPE_SERVER\t1\t// 0b1\t0x1\n\t\t#define REG_TYPE_ROUTE\t2\t// 0b10\t0x2\n\t\t#define REG_TYPE_PEER\t3\t// 0b11\t0x3\n```\n\n# input format\n\nThe above example shows the input file's basic format which contains only a few\nkeywords and follows simple and loose rules. All keywords are case-insensitive.\n\n- `COMMENT`: A line is treated as comment line if starting with `#`.\n  Example: `# si5324 is a clock generator`.\n\n- `CHIP`: Specify the chip's name. It is optional and name is empty.\n  Example: `\u003cchip\u003e:si5324`, where `si5324` is the name.\n  \n- `REG`: : It defines the name (optional) and offset of a register and generally\n  followed with serveral `FIELD` lines which defines this register's fields.\n  \n  Example: `\u003cREG\u003e[Control]: 0`, where the register's name is `Control` and offset is 0.\n  \n- `FIELD`: As says above, `FIELD` lines contains information of a register's\n  field, including name, offset and enumrations (optional) in the format of\n  `name: offset (enumVal: enumName,)` that contains two parts. The `name-offset`\n  part is mandatory contrasting to the `enums` part. Below explains more details\n  about `FIELD` line.\n  \n  Example: `ck_prior1 : 0-1`, where the field's name is `ck_prior1`, offset\n  range is `0-1` and there is no `enums` part.\n  \n## `name-offset` part\n\nThis part of `FIELD` line contains contains field's name and offset. The offset\nare either bit or range which use `-` to connect. The start bit and end bit can\nin either sides of `-`.\n  \n## `enums` part\n\nThis part of `FIELD` line is optional and gives the enumeration range this\nfield can have, which is usually used when the field's offset is not only one\nbit.\n\nEmbraced by `(` and `)`, the enums are paired with `:`, where the enum value \nis at left and value name right. Enum pairs are separated with `,`.\n\nFor example, the chip spec have one field like:\n\n```\n4:3 | VALTIME[2]\n00: 2ms\n01: 100ms\n10: 200ms\n11: 13 seconds\n```\n\nThen the `FIELD` line should be one of lines below:\n```\nVALTIME: 4-3 (0b00: 2ms, 0b01: 100ms, 0b10: 200ms, 0b11: 13s)\nVALTIME: 4-3 (0: 2ms, 1: 100ms, 2: 200ms, 3: 13s)\n```\n\nAs the example shows, the enum number can be decimal and binary. Actually, it\nsupport four formats:\n\n- dec, like 10 = 10\n- bin, like 0b11 = 3\n- oct, like 012 = 10\n- hex, like 0x1f = 31\n\n## spec\n\nA formal-like sepcification is:\n\n```\n\u003cchip\u003e: chipName\n\n\u003creg\u003e[regName]: offset\nfield_1: bit\nfield_2: startBit - endBit\nfield_3: endBit - startBit (val1: val1Name, val2: val2Name)\n```\n\n# output format\n\nRight now, the only one output format is the C format which uses `#define`\nmacros to represent all registers and fields and capitalize all their names.\n\nIt handle two types of field: `bit` and `range`. There are some common macros and\nsome are not. The common macros are:\n\n- `REG_XXX_VAL(rv)`: For `bit` type, it gets the value of this field, 1 or 0 of\n  course. For `range`, it can get this field's value from register value `rv`.\n  For example, `REG_XXX_VAL(0x10) = 2` for `3-4`.\n\n- `REG_XXX_VALNAME VALNUM`: It is often used for `range` type. As the above\n  example `type: 4-5 (0b00: client, 0x01: server, 2: route, 3: peer)`, the ouput\n  is:\n  ```\n\t\t#define REG_TYPE_CLIENT\t0\t// 0b0\t0x0\n\t\t#define REG_TYPE_SERVER\t1\t// 0b1\t0x1\n\t\t#define REG_TYPE_ROUTE\t2\t// 0b10\t0x2\n\t\t#define REG_TYPE_PEER\t3\t// 0b11\t0x3\n  ```\n\n- The macros only for `bit` type are:\n\t- `REG_XXX_BIT`: i.e. BIT(n), n is the offset.\n\t- `REG_XXX_POS`: it is the offset of this field.\n\n- The macros only for `range` type are:\n\t- `REG_XXX_MSK`: the mask for this field. For example, `REG_XXX_MSK = 0x18`\n\t  for `3-4`.\n\t- `REG_XXX_SFT(v)`: it shifts the field's value `v` to the correct offset.\n\t  For example, `REG_XXX_SFT(1) = 0x08` for `3-4`.\n\t  \n# short  pattern\nsimple chip:\n\n```txt\nREG: REG_\u003creg\u003e\nFLD:\tREG_\u003cfield\u003e\nENU:\t\tREG_\u003cfiled\u003e_\u003cenum\u003e\n```\n\ncomplex chip with module:\n\n```txt\n//////////////////////////////////////////////\n// \u003cmodule\u003e\nMOD: REG_\u003cmodule\u003e_BASE_ADDR 0x0000000\nREG: REG_\u003cmodule\u003e_\u003creg\u003e\nFLD: \tREG_\u003cmodule\u003e_\u003cfield\u003e\nENM: \t\tREG_\u003cmodule\u003e_\u003cfiled\u003e_\u003cenum\u003e\n```\n\n# full pattern:\nsimple chip:\n```txt\nREG: REG_\u003creg\u003e\nFLD:\tREG_\u003creg\u003e_\u003cfield\u003e\nENU:\t\tREG_\u003creg\u003e_\u003cfiled\u003e_\u003cenum\u003e\n```\n\ncomplex chip with module:\n\n```txt\n//////////////////////////////////////////////\n// \u003cmodule\u003e\nMOD: REG_\u003cmodule\u003e_BASE_ADDR 0x0000000\nREG: REG_\u003cmodule\u003e_\u003creg\u003e\nFLD:\tREG_\u003cmodule\u003e_\u003creg\u003e_\u003cfield\u003e\nENM:\t\tREG_\u003cmodule\u003e_\u003creg\u003e_\u003cfiled\u003e_\u003cenum\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoueric%2Freggen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchoueric%2Freggen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoueric%2Freggen/lists"}