Skip to content

Commit 3bd5091

Browse files
committed
more contextual help work
1 parent 3b1231a commit 3bd5091

File tree

12 files changed

+326
-70
lines changed

12 files changed

+326
-70
lines changed

data/test.asm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ innerLoop:
1313
rts
1414

1515
.generate.b 0, 16, sin(I)*50
16-
16+

source/LogWindow.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,12 @@ void LogWindow::SnapScrollBarToMouseY(int y)
252252
ClampTargetScroll();
253253
}
254254

255-
void LogWindow::LogTextArray(LogFilter filter, const char** textArray)
255+
void LogWindow::LogTextArray(LogFilter filter, const char** textArray, int col)
256256
{
257257
const char** t = textArray;
258-
int colIdx = 0;
259258
while (*t)
260259
{
261-
LogText(filter, string(*t), -1, colIdx);
262-
colIdx = 1;
260+
LogText(filter, string(*t), -1, col);
263261
t++;
264262
}
265263
}

source/application.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const int SCREEN_HEIGHT = 1080;
1111
Application *gApp;
1212

1313
#define APP_TITLE "MAD64"
14-
#define VERSION "2022.04"
14+
#define VERSION "2022.05"
1515

1616
Application::Application()
1717
{
@@ -57,19 +57,23 @@ Application::Application()
5757

5858
void Application::ReloadFont()
5959
{
60-
TTF_Font *newFont = TTF_OpenFont(m_settings->fontPath.c_str(), m_settings->fontSize);
61-
if (newFont)
60+
if (m_fontLoaded != m_settings->fontPath)
6261
{
63-
TTF_CloseFont(m_font);
64-
m_font = newFont;
65-
TTF_GlyphMetrics(m_font, ' ', nullptr, nullptr, nullptr, nullptr, &m_whiteSpaceWidth);
66-
}
62+
TTF_Font* newFont = TTF_OpenFont(m_settings->fontPath.c_str(), m_settings->fontSize);
63+
if (newFont)
64+
{
65+
TTF_CloseFont(m_font);
66+
m_font = newFont;
67+
TTF_GlyphMetrics(m_font, ' ', nullptr, nullptr, nullptr, nullptr, &m_whiteSpaceWidth);
68+
m_fontLoaded = m_settings->fontPath;
69+
}
6770

68-
for (auto f : m_sourceFiles)
69-
{
70-
f->Visualize();
71-
if (f->GetCompileInfo())
72-
f->GetCompileInfo()->ClearVisuals();
71+
for (auto f : m_sourceFiles)
72+
{
73+
f->Visualize();
74+
if (f->GetCompileInfo())
75+
f->GetCompileInfo()->ClearVisuals();
76+
}
7377
}
7478
}
7579

source/application.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Application
1515
~Application();
1616

1717
int MainLoop();
18+
bool IsShuttingDown() { return m_quit; }
1819

1920
TTF_Font* GetFont() { return m_font; }
2021
SDL_Renderer* GetRenderer() { return m_renderer; }
@@ -72,6 +73,7 @@ class Application
7273
bool m_repaint;
7374
bool m_fullscreen;
7475
AppSettings* m_settings;
76+
string m_fontLoaded;
7577

7678
enum InputCapture
7779
{

source/common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ inline float sign(float x) { return (x == 0) ? 0 : (x < 0 ? -1.0f : 1.0f); }
3131
inline float lerp(float v1, float v2, float blend) { return (v1 + (v2 - v1) * blend); }
3232

3333
inline bool StrEqual(const char* a, const char* b) { return SDL_strcasecmp(a, b) == 0; }
34-
inline bool StrEqual(string& s1, const char* s2) { return s1.empty() ? (s2[0] == 0) : (SDL_strcasecmp(s1.c_str(), s2) == 0); }
35-
inline bool StrEqual(string& s1, string& s2) { return s1.empty() ? s2.empty() : (SDL_strcasecmp(s1.c_str(), s2.c_str()) == 0); }
34+
inline bool StrEqual(const string& s1, const char* s2) { return s1.empty() ? (s2[0] == 0) : (SDL_strcasecmp(s1.c_str(), s2) == 0); }
35+
inline bool StrEqual(const string& s1, const string& s2) { return s1.empty() ? s2.empty() : (SDL_strcasecmp(s1.c_str(), s2.c_str()) == 0); }
3636
inline bool HasExtension(const char *s, const char* ext) { const char *strExt = SDL_strrchr(s, '.'); return (strExt) ? StrEqual(strExt, ext) : false; }
3737
extern void CopyToClipboard(vector<string>& buffer);
3838
extern void CopyFromClipboard(vector<string>& buffer);

source/compiler.cpp

Lines changed: 208 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ CompilerLabel s_systemLabels[] =
3131
CompilerLabel("vic.sprite2X", 0xd004), CompilerLabel("vic.sprite2Y", 0xd005), CompilerLabel("vic.sprite3X", 0xd006), CompilerLabel("vic.sprite3Y", 0xd007),
3232
CompilerLabel("vic.sprite4X", 0xd008), CompilerLabel("vic.sprite4Y", 0xd009), CompilerLabel("vic.sprite5X", 0xd00a), CompilerLabel("vic.sprite5Y", 0xd00b),
3333
CompilerLabel("vic.sprite6X", 0xd00c), CompilerLabel("vic.sprite6Y", 0xd00d), CompilerLabel("vic.sprite7X", 0xd00e), CompilerLabel("vic.sprite7Y", 0xd00f),
34-
CompilerLabel("vic.spriteXMSB", 0xd010), CompilerLabel("vic.control1", 0xd011), CompilerLabel("vic.rasterCounter", 0xd012), CompilerLabel("vic.lightPenX", 0xd013),
35-
CompilerLabel("vic.lightPenY", 0xd014), CompilerLabel("vic.spriteEnable", 0xd015), CompilerLabel("vic.control2", 0xd016), CompilerLabel("vic.spriteYSize", 0xd017),
36-
CompilerLabel("vic.memoryPointer", 0xd018), CompilerLabel("vic.interruptRegister", 0xd019), CompilerLabel("vic.interruptEnable", 0xd01a), CompilerLabel("vic.spritePriority", 0xd01b),
34+
CompilerLabel("vic.spriteXMSB", 0xd010), CompilerLabel("vic.control1", gHELP_VIC_CONTROL1, 0xd011), CompilerLabel("vic.rasterCounter", 0xd012), CompilerLabel("vic.lightPenX", 0xd013),
35+
CompilerLabel("vic.lightPenY", 0xd014), CompilerLabel("vic.spriteEnable", 0xd015), CompilerLabel("vic.control2", gHELP_VIC_CONTROL2, 0xd016), CompilerLabel("vic.spriteYSize", 0xd017),
36+
CompilerLabel("vic.memoryPointer", gHELP_VIC_SCRMEM, 0xd018), CompilerLabel("vic.intRegister", gHELP_VIC_IRQREQ, 0xd019), CompilerLabel("vic.intEnable", gHELP_VIC_INTENA, 0xd01a), CompilerLabel("vic.spritePriority", 0xd01b),
3737
CompilerLabel("vic.spriteMulticolor", 0xd01c), CompilerLabel("vic.spriteXSize", 0xd01d), CompilerLabel("vic.spriteToSpriteCollision", 0xd01e), CompilerLabel("vic.spriteToDataCollision", 0xd01f),
3838
CompilerLabel("vic.borderColor", 0xd020), CompilerLabel("vic.backgroundColor0", 0xd021), CompilerLabel("vic.backgroundColor1", 0xd022), CompilerLabel("vic.backgroundColor2", 0xd023),
3939
CompilerLabel("vic.backgroundColor3", 0xd024), CompilerLabel("vic.spriteMulticolor0", 0xd025), CompilerLabel("vic.spriteMulticolor1", 0xd026), CompilerLabel("vic.sprite0Color", 0xd027),
@@ -1555,6 +1555,19 @@ bool CompilerSourceInfo::DoesLabelExist(const char* label)
15551555
return false;
15561556
}
15571557

1558+
int CompilerSourceInfo::FindLineByAddress(u32 address)
1559+
{
1560+
for (auto l : m_lines)
1561+
{
1562+
if ((address >= l->memAddr) && (address < l->memAddr + l->data.size()))
1563+
{
1564+
return l->lineNmbr;
1565+
}
1566+
}
1567+
return -1;
1568+
}
1569+
1570+
15581571
void CompilerSourceInfo::ClearVisuals()
15591572
{
15601573
for (auto l : m_lines)
@@ -1851,6 +1864,68 @@ void Compiler::AddLabelsContaining(CompilerSourceInfo* si, vector<CompilerLabel*
18511864
}
18521865
}
18531866

1867+
void Compiler::AddLabelsMatchingValue(CompilerSourceInfo* si, vector<CompilerLabel*>& labels, double value)
1868+
{
1869+
for (int i = 0; i < sizeof(s_systemLabels) / sizeof(CompilerLabel); i++)
1870+
{
1871+
auto l = &s_systemLabels[i];
1872+
if (l->m_value == value)
1873+
{
1874+
if (std::find(labels.begin(), labels.end(), l) == labels.end())
1875+
{
1876+
labels.push_back(l);
1877+
}
1878+
}
1879+
}
1880+
1881+
if (si)
1882+
{
1883+
for (auto l : si->m_labels)
1884+
{
1885+
if (l->m_value == value)
1886+
{
1887+
if (std::find(labels.begin(), labels.end(), l) == labels.end())
1888+
{
1889+
labels.push_back(l);
1890+
}
1891+
}
1892+
}
1893+
}
1894+
}
1895+
1896+
CompilerLabel* Compiler::FindMatchingLabel(CompilerSourceInfo *si, const string& token)
1897+
{
1898+
for (int i = 0; i < sizeof(s_systemLabels) / sizeof(CompilerLabel); i++)
1899+
{
1900+
auto l = &s_systemLabels[i];
1901+
if (StrEqual(l->m_name, token))
1902+
{
1903+
return l;
1904+
}
1905+
}
1906+
1907+
for (auto l : si->m_labels)
1908+
{
1909+
if (StrEqual(l->m_name, token))
1910+
{
1911+
return l;
1912+
}
1913+
}
1914+
return nullptr;
1915+
}
1916+
1917+
CommandHelp* Compiler::FindMatchingCommand(const string& token)
1918+
{
1919+
CommandHelp* cmd = gHELP_CMD;
1920+
while (cmd->name)
1921+
{
1922+
if (StrEqual(token, cmd->name))
1923+
return cmd;
1924+
cmd++;
1925+
}
1926+
return nullptr;
1927+
}
1928+
18541929
void Compiler::AddCommandsContaining(vector<CommandHelp*>& commands, const string& token)
18551930
{
18561931
CommandHelp* cmd = gHELP_CMD;
@@ -1872,60 +1947,148 @@ void Compiler::LogContextualHelp(SourceFile* sf, int line)
18721947
{
18731948
auto lw = gApp->GetLogWindow();
18741949
auto si = sf->GetLines()[line];
1875-
auto& tokens = si->GetTokens();
1950+
auto ci = sf->GetCompileInfo();
1951+
CompilerLineInfo* li = 0;
1952+
if (ci && ci->m_lines.size() > line)
1953+
li = ci->m_lines[line];
18761954

1877-
int tokenIdx = 0;
1878-
int colorToggle = 0;
1955+
if (li && !li->error)
1956+
{
1957+
lw->ClearLog(LogWindow::LF_InstructionHelp);
1958+
switch (li->type)
1959+
{
1960+
case LT_BasicStartup:
1961+
case LT_GenerateBytes:
1962+
case LT_GenerateWords:
1963+
case LT_DataBytes:
1964+
case LT_DataWords:
1965+
case LT_Include:
1966+
{
1967+
auto cmd = FindMatchingCommand(si->GetTokens()[0]);
1968+
if (cmd)
1969+
{
1970+
lw->LogText(LogWindow::LF_InstructionHelp, cmd->help, -1, 1);
1971+
if (cmd->detail1)
1972+
lw->LogText(LogWindow::LF_InstructionHelp, cmd->detail1, -1, 1);
1973+
if (cmd->detail2)
1974+
lw->LogText(LogWindow::LF_InstructionHelp, cmd->detail2, -1, 1);
1975+
}
1976+
}
1977+
break;
1978+
1979+
case LT_Variable:
1980+
case LT_Label:
1981+
{
1982+
auto l = FindMatchingLabel(ci, li->label);
1983+
if (l)
1984+
{
1985+
if (l->m_value >= 0 && abs(fmod(l->m_value, 1.0)) < 0.0000001 && abs(l->m_value) < 1000000000.0f)
1986+
lw->LogText(LogWindow::LF_InstructionHelp, FormatString("%s : $%x %d", l->m_name.c_str(), (u32)l->m_value, (int)(l->m_value)), l->m_lineNmbr, 1);
1987+
else
1988+
lw->LogText(LogWindow::LF_InstructionHelp, FormatString("%s : %1.2f", l->m_name.c_str(), l->m_value), l->m_lineNmbr, 1);
1989+
if (l->m_help)
1990+
lw->LogTextArray(LogWindow::LF_InstructionHelp, l->m_help, 1);
1991+
}
1992+
}
1993+
break;
18791994

1880-
vector<CompilerLabel*> labels;
1881-
vector<CommandHelp*> commands;
1995+
case LT_Instruction:
1996+
{
1997+
auto cmd = FindMatchingCommand(s_opcodes[li->opcode].name);
1998+
if (cmd)
1999+
{
2000+
lw->LogText(LogWindow::LF_InstructionHelp, cmd->help, -1, 1);
2001+
if (cmd->detail1)
2002+
lw->LogText(LogWindow::LF_InstructionHelp, cmd->detail1, -1, 1);
2003+
if (cmd->detail2)
2004+
lw->LogText(LogWindow::LF_InstructionHelp, cmd->detail2, -1, 1);
2005+
}
18822006

1883-
auto cs = sf->GetCompileInfo();
1884-
while (tokenIdx < tokens.size())
2007+
// find possible labels
2008+
if (gAddressingModeSize[s_opcodes[li->opcode].addressMode] > 1)
2009+
{
2010+
vector<CompilerLabel*> labels;
2011+
double val = (double)li->operand;
2012+
if (s_opcodes[li->opcode].addressMode == AM_Relative)
2013+
val = (double)(li->memAddr + li->operand + 2);
2014+
2015+
AddLabelsMatchingValue(ci, labels, val);
2016+
int colorToggle = 0;
2017+
for (auto l : labels)
2018+
{
2019+
if (l->m_value >= 0 && abs(fmod(l->m_value, 1.0)) < 0.0000001 && abs(l->m_value) < 1000000000.0f)
2020+
lw->LogText(LogWindow::LF_InstructionHelp, FormatString("%s : $%x %d", l->m_name.c_str(), (u32)l->m_value, (int)(l->m_value)), l->m_lineNmbr, 1 + colorToggle);
2021+
else
2022+
lw->LogText(LogWindow::LF_InstructionHelp, FormatString("%s : %1.2f", l->m_name.c_str(), l->m_value), l->m_lineNmbr, 1 + colorToggle);
2023+
if (labels.size() == 1 && l->m_help)
2024+
lw->LogTextArray(LogWindow::LF_InstructionHelp, l->m_help, 1 + colorToggle);
2025+
2026+
colorToggle = 1 - colorToggle;
2027+
}
2028+
}
2029+
}
2030+
break;
2031+
}
2032+
}
2033+
else
18852034
{
1886-
auto& token = tokens[tokenIdx++];
1887-
if (token.empty() || token[0] == ' ' || token[0] == '\t')
1888-
continue;
2035+
auto& tokens = si->GetTokens();
2036+
2037+
int tokenIdx = 0;
2038+
int colorToggle = 0;
18892039

1890-
if (token == "$" || token == "%")
2040+
vector<CompilerLabel*> labels;
2041+
vector<CommandHelp*> commands;
2042+
2043+
auto cs = sf->GetCompileInfo();
2044+
while (tokenIdx < tokens.size())
18912045
{
1892-
tokenIdx++;
1893-
continue;
1894-
}
2046+
auto& token = tokens[tokenIdx++];
2047+
if (token.empty() || token[0] == ' ' || token[0] == '\t')
2048+
continue;
18952049

1896-
if (token[0] >= '0' && token[0] <= '9')
1897-
continue;
2050+
if (token == "$" || token == "%")
2051+
{
2052+
tokenIdx++;
2053+
continue;
2054+
}
18982055

1899-
AddLabelsContaining(cs, labels, token);
1900-
AddCommandsContaining(commands, token);
1901-
}
1902-
1903-
lw->ClearLog(LogWindow::LF_InstructionHelp);
1904-
if (!labels.empty())
1905-
{
1906-
lw->LogText(LogWindow::LF_InstructionHelp, "Suggested Labels:");
1907-
for (auto l : labels)
2056+
if (token[0] >= '0' && token[0] <= '9')
2057+
continue;
2058+
2059+
AddLabelsContaining(cs, labels, token);
2060+
AddCommandsContaining(commands, token);
2061+
}
2062+
2063+
lw->ClearLog(LogWindow::LF_InstructionHelp);
2064+
if (!labels.empty())
19082065
{
1909-
if (l->m_value >= 0 && abs(fmod(l->m_value, 1.0)) < 0.0000001 && abs(l->m_value) < 1000000000.0f)
1910-
lw->LogText(LogWindow::LF_InstructionHelp, FormatString("%s : $%x %d", l->m_name.c_str(), (u32)l->m_value, (int)(l->m_value)), l->m_lineNmbr, 1+ colorToggle);
1911-
else
1912-
lw->LogText(LogWindow::LF_InstructionHelp, FormatString("%s : %1.2f", l->m_name.c_str(), l->m_value), l->m_lineNmbr, 1+ colorToggle);
2066+
lw->LogText(LogWindow::LF_InstructionHelp, "Suggested Labels:");
2067+
for (auto l : labels)
2068+
{
2069+
if (l->m_value >= 0 && abs(fmod(l->m_value, 1.0)) < 0.0000001 && abs(l->m_value) < 1000000000.0f)
2070+
lw->LogText(LogWindow::LF_InstructionHelp, FormatString("%s : $%x %d", l->m_name.c_str(), (u32)l->m_value, (int)(l->m_value)), l->m_lineNmbr, 1 + colorToggle);
2071+
else
2072+
lw->LogText(LogWindow::LF_InstructionHelp, FormatString("%s : %1.2f", l->m_name.c_str(), l->m_value), l->m_lineNmbr, 1 + colorToggle);
2073+
if (labels.size() == 1 && l->m_help)
2074+
lw->LogTextArray(LogWindow::LF_InstructionHelp, l->m_help, 1 + colorToggle);
19132075

1914-
colorToggle = 1 - colorToggle;
2076+
colorToggle = 1 - colorToggle;
2077+
}
19152078
}
1916-
}
1917-
if (!commands.empty())
1918-
{
1919-
lw->LogText(LogWindow::LF_InstructionHelp, "Suggested Commands:");
1920-
colorToggle = 0;
1921-
for (auto c : commands)
2079+
if (!commands.empty())
19222080
{
1923-
lw->LogText(LogWindow::LF_InstructionHelp, c->help, -1, 1 + colorToggle);
1924-
if (c->detail1)
1925-
lw->LogText(LogWindow::LF_InstructionHelp, c->detail1, -1, 1 + colorToggle);
1926-
if (c->detail2)
1927-
lw->LogText(LogWindow::LF_InstructionHelp, c->detail2, -1, 1 + colorToggle);
1928-
colorToggle = 1 - colorToggle;
2081+
lw->LogText(LogWindow::LF_InstructionHelp, "Suggested Commands:");
2082+
colorToggle = 0;
2083+
for (auto c : commands)
2084+
{
2085+
lw->LogText(LogWindow::LF_InstructionHelp, c->help, -1, 1 + colorToggle);
2086+
if (c->detail1)
2087+
lw->LogText(LogWindow::LF_InstructionHelp, c->detail1, -1, 1 + colorToggle);
2088+
if (c->detail2)
2089+
lw->LogText(LogWindow::LF_InstructionHelp, c->detail2, -1, 1 + colorToggle);
2090+
colorToggle = 1 - colorToggle;
2091+
}
19292092
}
19302093
}
19312094
}

0 commit comments

Comments
 (0)