mangocity 发表于 2015-6-13 00:30:16

突然要用 STC 写个东西,但是发现没有SDCC头文件,写个工具转...

突然要用 STC 写个东西,但是发现没有SDCC头文件,写个工具将 keil 的头文件转成 sdcc 的.
闲话不说,上代码. 写的比较脏,别介意.

<code>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char* args[])
{
    string filename = "stc15.h";
    ifstream file_in(filename.c_str());

    string s;
    string previous_addr;
    while( getline(file_in,s) )
    {
      if(s == '/' || s == '#')
            cout << s << endl;
      else if(s.length() > 4)
      {
            string::size_type type_end = s.find_first_of(' ');
            string type = s.substr(0, type_end);

            string::size_type name_begin = s.find_first_not_of(' ', type_end);
            string::size_type name_end = s.find_first_of(' ', name_begin);
            string name = s.substr(name_begin, name_end - name_begin);

            string::size_type addr_begin = s.find_first_not_of(" =", name_end);
            string::size_type addr_end = s.find_first_of(" ;", addr_begin);
            string addr = s.substr(addr_begin, addr_end - addr_begin);

            string comment;
            bool haveComment = true;
            if(addr_end == s.length() - 1)
            {
                haveComment = false;
            }

            if(haveComment)
            {
                string::size_type comment_begin = s.find_first_not_of("; ", addr_end);
                comment = s.substr(comment_begin);
            }

            if(type == "sfr")
            {
                cout << "SFR(" << name << ", "<< addr << ");";
                if(haveComment) cout << comment;
                cout << endl;
                previous_addr = addr;
            }
            else if(type == "sbit")
            {
                string bit_offset = addr.substr(addr.length()-1);

                cout << "SBIT(" << name << ", "<< previous_addr << ", " << bit_offset << ");";
                if(haveComment) cout << comment;
                cout << endl;
            }

      }
    }
    return 0;
}
</code>

tim 发表于 2015-6-13 00:34:41

<code>...</code>用方括号

anta09 发表于 2015-6-13 10:01:29

什么玩意

fclose 发表于 2015-6-13 16:34:03

虽然用不上,支持一下, 顺便测试一下2楼的。。。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char* args[])
{
    string filename = "stc15.h";
    ifstream file_in(filename.c_str());

    string s;
    string previous_addr;
    while( getline(file_in,s) )
    {
      if(s == '/' || s == '#')
            cout << s << endl;
      else if(s.length() > 4)
      {
            string::size_type type_end = s.find_first_of(' ');
            string type = s.substr(0, type_end);

            string::size_type name_begin = s.find_first_not_of(' ', type_end);
            string::size_type name_end = s.find_first_of(' ', name_begin);
            string name = s.substr(name_begin, name_end - name_begin);

            string::size_type addr_begin = s.find_first_not_of(" =", name_end);
            string::size_type addr_end = s.find_first_of(" ;", addr_begin);
            string addr = s.substr(addr_begin, addr_end - addr_begin);

            string comment;
            bool haveComment = true;
            if(addr_end == s.length() - 1)
            {
                haveComment = false;
            }

            if(haveComment)
            {
                string::size_type comment_begin = s.find_first_not_of("; ", addr_end);
                comment = s.substr(comment_begin);
            }

            if(type == "sfr")
            {
                cout << "SFR(" << name << ", "<< addr << ");";
                if(haveComment) cout << comment;
                cout << endl;
                previous_addr = addr;
            }
            else if(type == "sbit")
            {
                string bit_offset = addr.substr(addr.length()-1);

                cout << "SBIT(" << name << ", "<< previous_addr << ", " << bit_offset << ");";
                if(haveComment) cout << comment;
                cout << endl;
            }

      }
    }
    return 0;
}

mowin 发表于 2015-6-13 22:33:21

SDCC标准51头文件不够用么?没搞明白

mangocity 发表于 2015-6-14 14:51:43

mowin 发表于 2015-6-13 22:33
SDCC标准51头文件不够用么?没搞明白

有些寄存器没有的。懒得一个一个添加了,干脆一劳永逸。

饭桶 发表于 2015-6-14 17:30:25

就一个支持C的单片机,楼主想多了。

笑笑我笑了 发表于 2015-6-14 20:04:41

就是关键字转换嘛

Error.Dan 发表于 2020-2-12 23:37:22

发现protues 8.9里面内置了一个STC的模型可以仿真的,又不想装keil,放狗一搜搜到这个帖子.
不搞CPP,电脑上连C++环境都没有,花了半个小时现场撸了一套MinGW编译出来试了一下
关键是,不能用啊LZ同学~

自己用c#撸了一个,替LZ把这个坑填了~
同时提供一个预编译过的,把头文件拖上去就行了,需要.net 4.5以上的运行库支撑.
不保证好使,只用了一个STC软件里面导出的头文件做了测试~

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Keil2SDCC
{
    class Program
    {
      static void Main(string[] args)
      {
            if(args.Length < 1)
            {
                Console.WriteLine("Input Is Empty");
                Console.ReadKey();
                return ;
            }

            List<string> fileNames = new List<string>(args);
            Dictionary<string, string> nameAddr = new Dictionary<string, string>();
            foreach (var item in fileNames)
            {
                var encodeType = GetFileEncodeType(item);

                if(item.EndsWith(".h") || item.EndsWith(".H"))
                {
                  string realName = item.Substring(0, item.Length - 2);
                  var lines = File.ReadAllLines(item,encodeType);
                  var newLines = newList<string>();

                  foreach (var line in lines)
                  {
                        if(line.StartsWith("sfr"))
                        {
                            //sfr ACC = 0xE0;   //0000,0000 累加器Accumulator
                            //0    123      4
                            //__sfr __at (0x80) P0         ;/* PORT 0                                                */
                            //0   1   2   3               4   


                            var arr = line.Split(' ');
                            arr = "";
                            int index = 0;
                            string addr="", name="",comment="";
                            for (int i = 0; i < arr.Length; i++)
                            {
                              if (arr != "")
                              {
                                    index++;
                                    if (index == 1) name = arr;
                                    if (index == 3) addr = arr.Substring(0,arr.Length-1);
                                    if (index > 3) comment += arr;
                              }
                            }
                            nameAddr.Add(name, addr);
                            string newLineTmp = $"__sfr __at ({addr}) {name};{comment}";
                            newLines.Add(newLineTmp);
                        }
                        else if(line.StartsWith("sbit"))
                        {
                            //sbit P00      =   P0^0;
                            //0   1         2   3       4   
                            //__sbit __at (0x90) P1_0;
                            //0      1      2       3      
                            var arr = line.Split(' ');
                            arr = "";
                            int index = 0;
                            string addr = "", name = "", comment = "";
                            for (int i = 0; i < arr.Length; i++)
                            {
                              if (arr != "")
                              {
                                    index++;
                                    if (index == 1) name = arr;
                                    if (index == 3) addr = sbitGetAddr(arr,nameAddr);
                                    if (index > 3) comment += arr;
                              }
                            }
                            string newLineTmp = $"__sbit __at ({addr}) {name} ; {comment}";
                            newLines.Add(newLineTmp);
                        }
                        else
                        {
                            newLines.Add(line);
                        }
                  }
                  File.WriteAllLines(realName + "4SDCC.h", newLines.ToArray());
                }
            }
      }

      static string sbitGetAddr(string name,Dictionary<string,string> dic)
      {
            var valueArr = name.Split('^');
            string addrBase = valueArr;
            string addrOffset = valueArr.Substring(0, valueArr.Length - 1);

            string tBase = dic;
            char lastChar = tBase.Last();
            int offset = (int)lastChar - 0x30;
            if (offset < 8)
            {
                string tRes = tBase.Substring(0, tBase.Length - 1) + addrOffset;
                return tRes;
            }
            else
            {
                var tt = 8 + Convert.ToByte(addrOffset);
                string ttt = tt.ToString("x");
                string tRes = tBase.Substring(0, tBase.Length - 1) + ttt;
                return tRes;
            }

      }

      static System.Text.Encoding GetFileEncodeType(string filename)
      {
            using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
                byte[] buffer = br.ReadBytes(2);

                if (buffer >= 0xEF)
                {
                  if (buffer == 0xEF && buffer == 0xBB)
                  {
                        return System.Text.Encoding.UTF8;
                  }
                  else if (buffer == 0xFE && buffer == 0xFF)
                  {
                        return System.Text.Encoding.BigEndianUnicode;
                  }
                  else if (buffer == 0xFF && buffer == 0xFE)
                  {
                        return System.Text.Encoding.Unicode;
                  }
                  else
                  {
                        return System.Text.Encoding.Default;
                  }
                }
                else
                {
                  return System.Text.Encoding.Default;
                }
            }
      }

    }
}



页: [1]
查看完整版本: 突然要用 STC 写个东西,但是发现没有SDCC头文件,写个工具转...