Yazwh0_BitMagic_Legacy/Mega65DocumentGenerator/Parser/OpCodes.cstxt
using Mega65Parser;
using System.Text;
namespace Mega65Processor;

public static class CpuDocumentationGenerator
{
    public static void Output(Parser parser)
    {
        var instructions = parser.Instructions.GroupBy(i => i.Code).OrderBy(i => i.Key).ToArray();
        var sb = new StringBuilder();
        .# @(parser.ChipName) Instructions
        .
        .## Credits and License
        .
        .All text derived from the [Mega65 User Guide](https://github.com/MEGA65/mega65-user-guide). Copyright 2019-2022 by Paul Gardner-Stephen, the MEGA Museum of Electronic Games & Art e.V., and contributors.
        .
        .This reference guide is made available under the GNU Free Documentation License
        .v1.3, or later, if desired. This means that you are free to modify, reproduce and redistribute this reference guide, subject to certain conditions. The full text of the GNU
        .Free Documentation License v1.3 can be found at [https://www.gnu.org/licenses/fdl-1.3.en.html](https://www.gnu.org/licenses/fdl-1.3.en.html).
        .
        .## All Instructions
        .
        int size = 12;
        .| |  |  |  |  |  |  |  |  |  |  |  |
        .| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | 
        for(var i = 0; i < instructions.Length; i += size)
        {
            var thisLine = instructions.Skip(i).Take(size);
            sb.Clear();
            sb.Append("|");
            foreach(var item in thisLine.Select(i => i.Key))
            {
                sb.Append($" [{item}](#{item}) |");
            }
            .@(sb)
        }
        var cycleNotes = new List<Char>();
        foreach(var group in instructions)
        {
            var explanation = parser.Explanation[group.Key];
            .
            .## @(group.Key)
            .
            .**@(explanation.Description)**\
            foreach(var exp in explanation.Explanations) {
                .@(exp)\
            }
            .Flags: @(explanation.Flags)
            . | Mode | Syntax | Hex | Len | Cycles |
            . | --- | --- | --- | --- | --- |
            cycleNotes.Clear();
            foreach(var ip in parser.Instructions.
                    Where(i => i.Code == group.Key).
                    Select(i => (Instruction: i, Description: parser.ParametersOrder[i.Parameters])).
                    OrderBy(ip => ip.Description.Order))
            {
                string cycles = ip.Instruction.Cycles != 0 ? ip.Instruction.Cycles.ToString() : "";
                . | @(ip.Description.Name) | @(ip.Instruction.Code) @(ip.Instruction.ParametersBase) | @(ip.Instruction.OpCodeDisplay()) | @(ip.Description.ByteCount + ip.Instruction.InstructionLength()) | @(cycles)<sup>@(ip.Instruction.CycleNotesDisplay)</sup> |
                cycleNotes.AddRange(ip.Instruction.CycleNotes);
            }
            if (cycleNotes.Any())
            {
                .
                var notes = cycleNotes.Distinct().OrderBy(c => c).ToArray();
                for(var i = 0; i < notes.Length; i++)
                {
                    if (i == notes.Length - 1)
                    {
                        . <sup>@(notes[i])</sup> @(parser.CycleNotes[notes[i]])
                    } else {
                        . <sup>@(notes[i])</sup> @(parser.CycleNotes[notes[i]])\
                    }
                }
            }
            if (!string.IsNullOrWhiteSpace(explanation.ExplanationText))
            {
                .
                .@(explanation.ExplanationText)
            }
        }        
    }
}
