Skip to content

Instantly share code, notes, and snippets.

@elvircrn
Last active November 1, 2017 20:45
Show Gist options
  • Save elvircrn/eb17be4e0deafa6781e90212bf498a9b to your computer and use it in GitHub Desktop.
Save elvircrn/eb17be4e0deafa6781e90212bf498a9b to your computer and use it in GitHub Desktop.
#include <vector>
#include <limits>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <utility>
#include <iostream>
#include <fstream>
inline dec(int x) {
return x > 0 ? x - 1 : x;
}
inline int cycles(int b, int c, int d) {
return b * (2 + c * (2 + dec(d) * 3 + 2) + dec(c) * 3 + 2)
+ dec(b) * 3 + 2;
}
std::string getHex(int num) {
std::stringstream ss;
ss << std::hex << " 0x" << std::setw(2) << std::setfill('0') << num;
return ss.str();
}
void dispCoeffs(const std::vector<int> &sol) {
for (int i = 0; i < sol.size(); i++)
std::cout << (char)('a' + i) << ": " << getHex(sol[i]) << '\n';
}
void dump(const std::vector<int> &sol, const std::vector<std::pair<int, int>> &seq) {
std::ofstream outFile("main.asm");
outFile << R"(;Brojac sa LED
list p=16f1939
#include <p16f1939.inc>
__CONFIG _CONFIG1,_FOSC_HS&_WDTE_OFF&_PWRTE_OFF&_MCLRE_ON&_CP_OFF&_CPD_OFF&_BOREN_OFF&_CLKOUTEN_OFF&_IESO_OFF&_FCMEN_OFF
__CONFIG _CONFIG2,_WRT_OFF&_VCAPEN_OFF&_PLLEN_OFF&_STVREN_OFF&_LVP_OFF
;*****Deklaracija promjenjivih *****
; Napomena: promjenljive su u BANK0
brojaca equ 0x71
brojacb equ 0x72
brojacc equ 0x73
;***** Struktura programske memorije *****
ORG 0x00 ; Reset vector
goto Glavni
ORG 0x04 ; Interapt vector
goto Glavni ; nema interapt rutine
;**** Pocetak programa ****
Glavni
;***** inicijalizacija portova *****
bsf BSR, BSR0 ;izbor BANK1
bcf BSR, BSR1 ;izbor BANK1
movlw B'00000000' ; inicijalizacija PORTD kao izlazni
movwf TRISD
bcf BSR, BSR0 ;izbor BANK2
bsf BSR, BSR1 ;izbor BANK2
bcf LATD, 0
L0)" << "\n";
for (const auto &p : seq) {
outFile << (p.second ? " bsf " : " bcf ") << "LATD, 0\n";
for (int i = 0; i < p.first; i++)
outFile << " call msec\n";
}
outFile << R"(goto L0
msec
movlw)" << getHex(sol[0]) << R"(
movwf brojaca ;
L1
movlw)" << getHex(sol[1]) << R"(
movwf brojacb ;
L2
movlw)" << getHex(sol[2]) << R"(
movwf brojacc ;
L3
decfsz brojacc, 1
goto L3
decfsz brojacb, 1
goto L2
decfsz brojaca, 1
goto L1
return
end
)";
}
int main() {
constexpr double t = 0.0007;
constexpr int CYCLE_CNT = t * (2000000);
const std::vector<std::pair<int, int>> pattern = {{2, 1}, {2, 0}, {1, 1}, {1, 0}, {1, 1}, {1, 0}};
int min = std::numeric_limits<int>::max();
std::vector<int> sol;
for (int a = 0; a < 256; a++) {
for (int b = 0; b < 256; b++) {
for (int c = 0; c < 256; c++) {
int diff = std::abs(CYCLE_CNT - cycles(a, b, c));
if (diff < 1)
{
sol = { a, b, c };
std::cout << "Diff: " << min << '\n';
dispCoeffs(sol);
dump(sol, pattern);
return 0;
}
if (diff < min)
{
min = diff;
sol = { a, b, c };
}
}
}
}
dispCoeffs(sol);
std::cout << "Diff: " << min << '\n';
dump(sol, pattern);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment