---@diagnostic disable: undefined-global -- []===========================[] -- превращает "YYYY-MM-DD" в "DD", "MM", "YYYY" function Get_date_RU(str_date) return string.char(string.byte(str_date,10), string.byte(str_date,11)), string.char(string.byte(str_date,7), string.byte(str_date,8)), string.char(string.byte(str_date,2), string.byte(str_date,3), string.byte(str_date,4), string.byte(str_date,5)) end -- []===========================[] -- []===========================[] function Get_file_data(fname) -- убираем из дефайна_пути_файла всякие табы и пробелы лишние. Путь должен быть обрамлён одинарными кавычками fname = string.gsub(string.match(fname, '\'.*\''), "'", "") local file = assert(io.open(fname, "rb")) local data = file:read("*all") file:close() return data end -- []===========================[] -- []===========================[] -- возвращает число из строки или файла заданной размерности -- get_number (file, offset, bytes) function Get_number (str,offset, bytes) local factor = 1 local number = 0 if bytes > 8 then sj.error("error in function 'get_number' with variable 'bytes'", bytes) sj.exit(1) end for i = offset, offset+bytes-1, 1 do number = number + str:byte(i) * factor factor = factor * 256 end return number end -- []===========================[] -- []===========================[] -- считает контрольную сумму для файла -- get_checksum(FileName, FileLength) function Get_checksum(fname, fStart, fLength) local reg_b = 0 local reg_c = 0 local reg_d = 0 local reg_e = 0 local data = Get_file_data(fname) fLength = tonumber(fLength) if (fLength == 0) or (fLength == nil) then fLength = data:len() end if (fStart == 0) or (fStart == nil) then fStart = 1 end -- Расчёт контрольной суммы (регистры BC - младший и DE - старший): --print(fname, "fStart", fStart, "fLength", fLength) for i = fStart, fLength, 1 do reg_c = reg_c + data:byte(i) if reg_c > 255 then reg_c = reg_c - 256 reg_b = reg_b + data:byte(i) if reg_b > 255 then reg_b = reg_b - 256 reg_e = reg_e + data:byte(i) if reg_e > 255 then reg_e = reg_e - 256 reg_d = reg_d + data:byte(i) if reg_d > 255 then reg_d = reg_d - 256 end end end end --print (string.format("%x",i-1), string.format("%x", reg_d*16777216+reg_e*65536+reg_b*256+reg_c)) end return reg_d*16777216+reg_e*65536+reg_b*256+reg_c end -- []===========================[] -- []===========================[] -- достаёт из BMP-8bit разрешение, размер растра, смещение растра, кол-во цветов function Get_bmp8bit_values(fname) local TBitMapFileHeader = { bfType = 0+1, bfSize = 2+1, bfOffBits = 10+1 } local TBitmapInfoHeader = { biSize = 0+14+1, biWidth = 4+14+1, biHeight = 8+14+1, biplanes = 12+14+1, biBitCount = 14+14+1, biCompression = 16+14+1, biSizeImage = 20+14+1, biXPelsPerMeter = 24+14+1, biYPelsPerMeter = 28+14+1, biClrUsed = 32+14+1, biClrImportant = 36+14+1 } local bmp_height, bmp_width, bmp_image_size, bmp_colors, bmp_image_offset, temp_variable local data = Get_file_data(fname) temp_variable = Get_number (data,TBitMapFileHeader.bfType, 2) if temp_variable ~= 19778 then sj.error("error, not BMP file") sj.exit(1) end temp_variable = Get_number(data, TBitmapInfoHeader.biSize, 4) if temp_variable ~= 40 then sj.error("error, unsupported DIB header", temp_variable) sj.exit(1) end temp_variable = Get_number (data,TBitmapInfoHeader.biBitCount, 2) if temp_variable > 8 then sj.error("error in BMP file, not 8-bit pallete, but", temp_variable) sj.exit(1) end bmp_height = Get_number (data, TBitmapInfoHeader.biHeight, 4) --[[ if bmp_height > 256 then sj.error("error in BMP file, max height is 256, but", bmp_height) sj.exit(1) end ]]-- bmp_width = Get_number(data, TBitmapInfoHeader.biWidth, 4) --[[ if bmp_width > 320 then sj.error("error in BMP file, max width is 320, but", bmp_width) sj.exit(1) end ]]-- bmp_image_offset = Get_number(data, TBitMapFileHeader.bfOffBits, 4) bmp_image_size = bmp_height * bmp_width bmp_colors = (Get_number (data, TBitMapFileHeader.bfOffBits, 4) - Get_number (data, TBitmapInfoHeader.biSize, 4) - (TBitmapInfoHeader.biSize - 1))/4 return bmp_width, bmp_height, bmp_image_size, bmp_image_offset, bmp_colors end -- []===========================[] -- []===========================[] function Detect_os() local BinaryFormat = package.cpath:match("%p[\\|/][?]%p(%a+)") if BinaryFormat == "dll" then return "Windows" elseif BinaryFormat == "dylib" then return "MacOS" elseif BinaryFormat == "so" then if (os.execute("test -e /sbin/dynamic_pager")) then return "MacOS" else return "Linux" end else print("--[ ERROR! OS is not detected!!! ]--") os.exit(1) end end -- []===========================[] -- []===========================[] function File_save(fname, new_fname, offset, size, fstep, fskip) if (fstep and fskip) == nil then fstep = 0 fskip = 0 end local data = Get_file_data(fname) local file = assert(io.open(new_fname, "w+b")) if (io.type(file) == "file" ) then if fstep + fskip ~= 0 then local i = offset + 1 repeat file:write(string.sub(data, i, i+fstep-1)) i = i+fstep+fskip until i >= (offset + 1 + size - 1) file:close() return true else file:write(string.sub(data, offset+1, offset+1+size-1)) file:close() return true end else file:close() return false end end -- []===========================[] -- []===========================[] function Get_Full_Filename(pname, fname, OStype) -- убираем из дефайна_пути_файла всякие табы и пробелы лишние. Путь должен быть обрамлён одинарными кавычками local psep = "/" fname = string.gsub(string.match(fname, '\'.*\''), "'", "") pname = string.gsub(string.match(pname, '\'.*\''), "'", "") return (pname .. psep .. fname) --print (fname, pname) end -- []===========================[] -- []===========================[] function INCLUDING_INFO_START() -- {ii_parent_num} = ii_count -- {ii_parent_num, ii_nested_level, ii_file_name, ii_start_addr, ii_end_addr, ii_its_size} -- {ii_parent_num, ii_nested_level, ii_file_name, ii_start_addr, ii_end_addr, ii_its_size} -- {ii_parent_num} = ii_count -- {ii_parent_num, ii_nested_level, ii_file_name, ii_start_addr, ii_end_addr, ii_its_size} -- {ii_parent_num, ii_nested_level, ii_file_name, ii_start_addr, ii_end_addr, ii_its_size} --ii_parent_num = 1 ii_nested_level = 1 ii_file_name = 2 ii_start_addr = 3 ii_end_addr = 4 ii_its_size = 5 ii_array = {} -- nest level, data ii_count = 0 ii_nest = {} ii_nest_count = 0 ii_tmp_nested_level = -1 end -- []---------------------------[] function INCLUDING_INFO_ADDstart() local temp_cnt = tonumber(sj.get_define("__INCLUDE_LEVEL__")) if temp_cnt < ii_tmp_nested_level then print("Error in script INCLUDING_INFO_ADDstart: new nesting < old nesting\r\n","File: " .. sj.get_define("__FILE__"), "Line: " .. sj.get_define("__LINE__")) sj.exit(1) elseif temp_cnt > 20 then print("Error in script INCLUDING_INFO_ADDstart: current nesting > 20\r\n","File: " .. sj.get_define("__FILE__"), "Line: " .. sj.get_define("__LINE__")) sj.exit(1) end ii_array[ii_count] = {--ii_count, -- ii_parent_num temp_cnt, -- ii_nested_level sj.get_define("__FILE__"), sj.current_address, 0,0 } ii_nest[ii_nest_count] = ii_count ii_nest_count = ii_nest_count+1 ii_count = ii_count + 1 ii_tmp_nested_level = temp_cnt end -- []---------------------------[] function INCLUDING_INFO_ADDend() local temp_cnt = tonumber(sj.get_define("__INCLUDE_LEVEL__")) if temp_cnt > ii_tmp_nested_level then print("Error in script INCLUDING_INFO_ADDend: current nesting > old nesting\r\n","File: " .. sj.get_define("__FILE__"), "Line: " .. sj.get_define("__LINE__")) sj.exit(1) end ii_nest_count = ii_nest_count-1 temp_cnt = ii_nest[ii_nest_count] ii_array[temp_cnt][ii_end_addr] = sj.current_address ii_array[temp_cnt][ii_its_size] = sj.current_address - ii_array[temp_cnt][ii_start_addr] ii_tmp_nested_level = ii_array[temp_cnt][ii_nested_level] end -- []---------------------------[] function INCLUDING_INFO_END() local nest_sting_start = "└├│" local nest_sting_line = "────────────────────" local previous_nest = 0 print(" File\t\t\t\t\t","Start \tEnd \tSize ") for i = 0, ii_count-1, 1 do --print(tostring(ii_array[i][ii_nested_level]) .. " " .. ii_array[i][ii_file_name]:sub(ii_array[i][ii_file_name]:find("/")+1,-1),"\r\t\t\t\t\t", tostring(ii_array[i][ii_start_addr]), tostring(ii_array[i][ii_end_addr]), tostring(ii_array[i][ii_its_size])) print(tostring(ii_array[i][ii_nested_level]) .. " " .. ii_array[i][ii_file_name]:sub(ii_array[i][ii_file_name]:find("/")+1,-1),"\r\t\t\t\t\t", string.format("%X",ii_array[i][ii_start_addr]), string.format("%X",ii_array[i][ii_end_addr]), tostring(ii_array[i][ii_its_size])) end end -- []===========================[] -- []===========================[] function Hrust_File(cmdLine, firstPath, secondPath, firstName, secondName) cmdLine = string.gsub(string.match(cmdLine, '\'.*\''), "'", "") firstPath = string.gsub(string.match(firstPath, '\'.*\''), "'", "") secondPath = string.gsub(string.match(secondPath, '\'.*\''), "'", "") firstName = string.gsub(string.match(firstName, '\'.*\''), "'", "") secondName = string.gsub(string.match(secondName, '\'.*\''), "'", "") local fullCmdLine = cmdLine .. firstPath .. firstName .. " " .. secondPath .. secondName print(fullCmdLine) end -- []===========================[] -- []===========================[] -- Конвертит число в строку его двоичного представления. bits - разрядность, если число больше, -- чем разрядность для представления этого числа, то будет баг function Number2Binary(n, bits) bits = bits-1 local t = {} for i = bits, 0, -1 do t[#t+1] = math.floor(n/2^i) n = n%2^i end return table.concat(t) end -- []===========================[] -- []===========================[] function DCP_Page2Table (DCP_PAGE) local portsTable = {} local bitAddress = 0 local bitTMPor = 0 local bitMask = 0 local CNFadr = 0 local CNFadrEnd = 0 for Altera_Port = 1, 0xFF, 1 do for CNF = 0, 3, 1 do CNFadr = 1 + CNF * 0x1000 CNFadrEnd = CNFadr + 0x0FFF bitAddress = 0 bitTMPor = 0 for address = CNFadr, CNFadrEnd, 1 do if (DCP_PAGE:byte(address) == Altera_Port) then if bitAddress == 0 then bitAddress = (address-1) bitTMPor = (address-1) else bitAddress = bitAddress & (address-1) bitTMPor = bitTMPor | (address-1) end end end if (bitAddress ~= 0) then bitMask = ~bitAddress ~ bitTMPor bitMask = bitMask & 0x3FFF portsTable[#portsTable+1] = bitAddress portsTable[#portsTable+1] = bitMask portsTable[#portsTable+1] = Altera_Port end end end return portsTable end -- []===========================[] -- []===========================[] function Generate_DCPASM_fromPAGE (DCP_Page_file, DCP_new_ASM) -- получаем порты из файла print(DCP_Page_file) print(DCP_new_ASM) local data = Get_file_data(DCP_Page_file) DCP_TABLE = DCP_Page2Table(data) -- открываем файл на запись local DCP_TABLE_CODE -- убираем из дефайна_пути_файла всякие табы и пробелы лишние. Путь должен быть обрамлён одинарными кавычками DCP_new_ASM = string.gsub(string.match(DCP_new_ASM, '\'.*\''), "'", "") DCP_TABLE_CODE = assert(io.open(DCP_new_ASM, "w+")) -- шапка assert(DCP_TABLE_CODE:write(';', '\r\n')) assert(DCP_TABLE_CODE:write(';-----------------------------------------------------------------------;', '\r\n', '\r\n')) -- таблица for i = 1, #DCP_TABLE, 3 do assert(DCP_TABLE_CODE:write(';', '\r\n')) assert(DCP_TABLE_CODE:write(' DW %', Number2Binary(DCP_TABLE[i], 14), '\r\n')) assert(DCP_TABLE_CODE:write(' DW %', Number2Binary(DCP_TABLE[i+1], 14), '\r\n')) assert(DCP_TABLE_CODE:write(' DB #', string.format("%X",DCP_TABLE[i+2]), '\r\n')) end -- подвал assert(DCP_TABLE_CODE:write('; DCP END MARKER', '\r\n')) assert(DCP_TABLE_CODE:write(' DW 0,0,0', '\r\n')) assert(DCP_TABLE_CODE:write(';-----------------------------------------------------------------------;', '\r\n')) assert(DCP_TABLE_CODE:write(';')) -- закрытие файла assert(DCP_TABLE_CODE:flush()) assert(DCP_TABLE_CODE:close()) end -- []===========================[] -- []===========================[] -- Function reads number from file , increases it, creates define "BUILD" with the number and saves the number to . -- With this function you can control count of compilations. function increase_build(fname) local fp local build fp = assert(io.open(fname, "rb")) build = tonumber(fp:read("*all")) assert(fp:close()) if type(build) == "nil" then build = 0 end build = build + 1; --sj.insert_define("BUILD", build) fp = assert(io.open(fname, "wb")) assert(fp:write(build)) assert(fp:flush()) assert(fp:close()) return build end -- []===========================[]