public void ParseHeaderFile(string file) { Regex rgFLASHRAM = new Regex(@"#define[ \t]+(RAMSIZE|RAMSTART|RAMEND|FLASHEND|INTERNAL_SRAM_START|INTERNAL_SRAM_SIZE|PROGMEM_SIZE)[ \t]+\(?([0-9a-fA-Fx]+)\)?[ \t]*($|/)"); Regex rgFLASHRAM2 = new Regex(@"#define[ \t]+(FLASHEND)[ \t]+\(?([0-9a-fA-Fx]+) - 1\)?[ \t]*($|/)"); Regex rgIO = new Regex(@"#define[ \t]+([^ ]+)[ \t]+_SFR_IO([0-9]+)\(([0-9a-fA-Fx]+)\)"); Regex rgPin = new Regex(@"#define[ \t]+([^ ]+)[ \t]+([0-9]+)"); var lines = File.ReadAllLines(file); ParsedRegister lastReg = null; foreach (var line in ExpandIncludes(file)) { var iom = rgIO.Match(line); if (iom.Success) Registers.Add(lastReg = new ParsedRegister { Name = iom.Groups[1].Value, Addr = ParseInt(iom.Groups[3].Value), Size = int.Parse(iom.Groups[2].Value) }); else if (lastReg != null) { var pm = rgPin.Match(line); if (pm.Success) { int pin = int.Parse(pm.Groups[2].Value); if (pin >= 0 && pin < lastReg.Size) { if (lastReg.PinNames == null) lastReg.PinNames = new string[lastReg.Size]; lastReg.PinNames[pin] = pm.Groups[1].Value; } else lastReg = null; } else lastReg = null; } var m = rgFLASHRAM.Match(line); if (m.Success) { switch(m.Groups[1].Value) { case "RAMSTART": case "INTERNAL_SRAM_START": RAMSTART = ParseInt(m.Groups[2].Value); break; case "RAMSIZE": case "INTERNAL_SRAM_SIZE": RAMSIZE = ParseInt(m.Groups[2].Value); break; case "RAMEND": RAMSIZE = ParseInt(m.Groups[2].Value) + 1; break; case "FLASHEND": FLASHSIZE = ParseInt(m.Groups[2].Value) + 1; break; case "PROGMEM_SIZE": FLASHSIZE = ParseInt(m.Groups[2].Value); break; } } else if ((m = rgFLASHRAM2.Match(line)).Success) { switch (m.Groups[1].Value) { case "FLASHEND": FLASHSIZE = ParseInt(m.Groups[2].Value); break; } } } if (RAMSTART == 0 || RAMSIZE < 4 || FLASHSIZE < 4) throw new Exception("Failed to detect FLASH or RAM parameters for " + file); }
public void ParseHeaderFile(string file) { Regex rgFLASHRAM = new Regex(@"#define[ \t]+(RAMSIZE|RAMSTART|RAMEND|FLASHEND|INTERNAL_SRAM_START|INTERNAL_SRAM_SIZE|PROGMEM_SIZE)[ \t]+\(?([0-9a-fA-Fx]+)\)?[ \t]*($|/)"); Regex rgFLASHRAM2 = new Regex(@"#define[ \t]+(FLASHEND)[ \t]+\(?([0-9a-fA-Fx]+) - 1\)?[ \t]*($|/)"); Regex rgIO = new Regex(@"#define[ \t]+([^ ]+)[ \t]+_SFR_IO([0-9]+)\(([0-9a-fA-Fx]+)\)"); Regex rgPin = new Regex(@"#define[ \t]+([^ ]+)[ \t]+([0-9]+)"); var lines = File.ReadAllLines(file); ParsedRegister lastReg = null; foreach (var line in ExpandIncludes(file)) { var iom = rgIO.Match(line); if (iom.Success) { Registers.Add(lastReg = new ParsedRegister { Name = iom.Groups[1].Value, Addr = ParseInt(iom.Groups[3].Value), Size = int.Parse(iom.Groups[2].Value) }); } else if (lastReg != null) { var pm = rgPin.Match(line); if (pm.Success) { int pin = int.Parse(pm.Groups[2].Value); if (pin >= 0 && pin < lastReg.Size) { if (lastReg.PinNames == null) { lastReg.PinNames = new string[lastReg.Size]; } lastReg.PinNames[pin] = pm.Groups[1].Value; } else { lastReg = null; } } else { lastReg = null; } } var m = rgFLASHRAM.Match(line); if (m.Success) { switch (m.Groups[1].Value) { case "RAMSTART": case "INTERNAL_SRAM_START": RAMSTART = ParseInt(m.Groups[2].Value); break; case "RAMSIZE": case "INTERNAL_SRAM_SIZE": RAMSIZE = ParseInt(m.Groups[2].Value); break; case "RAMEND": RAMSIZE = ParseInt(m.Groups[2].Value) + 1; break; case "FLASHEND": FLASHSIZE = ParseInt(m.Groups[2].Value) + 1; break; case "PROGMEM_SIZE": FLASHSIZE = ParseInt(m.Groups[2].Value); break; } } else if ((m = rgFLASHRAM2.Match(line)).Success) { switch (m.Groups[1].Value) { case "FLASHEND": FLASHSIZE = ParseInt(m.Groups[2].Value); break; } } } if (RAMSTART == 0 || RAMSIZE < 4 || FLASHSIZE < 4) { throw new Exception("Failed to detect FLASH or RAM parameters for " + file); } }