#import "@preview/mannot:0.3.1"
#import "@preview/cetz:0.4.2"
#import "@preview/zap:0.5.0"

#import "../lib/common_rewrite.typ" : *
#import "../lib/truthtable.typ" : *
#import "../lib/fetModel.typ" : *

#show math.integral: it => math.limits(math.integral)
#show math.sum: it => math.limits(math.sum)

#set page(
  paper: "a4", 
  margin: (
    bottom: 10mm,
    top: 5mm,
    left: 5mm,
    right: 5mm
  ),
  flipped:true,
  footer: context [
    #grid(
      align: center,
      columns: (1fr, 1fr, 1fr),
      [#align(left, datetime.today().display("[day].[month].[year]"))], 
      [#align(center, counter(page).display("- 1 -"))], 
      [#align(right, image("../images/cc0.png", height: 5mm,))]
    )
  ],
)

#set text(10pt)

#let pTypeFill = rgb("#dd5959").lighten(10%);
#let nTypeFill = rgb("#5997dd").lighten(10%);

#place(top+center, scope: "parent", float: true, heading(
  [Digitaltechnik]
))

#let SeperatorLine = line(length: 100%, stroke: (paint: black, thickness: 0.2mm))
#let MathAlignLeft(e) = {
  align(left, block(e))
}

#let colorBoolscheLogic = color.hsl(105.13deg, 92.13%, 75.1%)
#let colorOptimierung = color.hsl(202.05deg, 92.13%, 75.1%)
#let colorRealsierung = color.hsl(280deg, 92.13%, 75.1%)
#let colorState = color.hsl(356.92deg, 92.13%, 75.1%)
//#let colorIntegral = color.hsl(34.87deg, 92.13%, 75.1%)

#let LNot(x) = math.op($overline(#x)$)

#columns(4, gutter: 2mm)[
  #bgBlock(fill: colorBoolscheLogic)[
    #subHeading(fill: colorBoolscheLogic)[Allgemein]
    *Moorsches Gesetz:* 2x der Anzahl der Transistoren pro Fläche (in 2 Jahren)

    Flächenskalierung eines Transistors: $1/sqrt(2)$

    *Kombinatorisch:* Kein Gedächtnis

    *(Synchrone) sequenentielle:* Mit Gedächtnis

    *Fan-In:* Anzahl der Inputs eines Gatters

    *Fan-Out:* Anzahl der Output Verbindungen eines Gatters
  ]

  #bgBlock(fill: colorBoolscheLogic)[
    #subHeading(fill: colorBoolscheLogic)[Boolsche Algebra]

    *Dualität*
    $LNot(0) = 1$, $LNot(1) = 0$

    *Äquivalenz* $LNot((LNot(A)))=A$\
    $A dot A = A$, $A + 0 = A$ \

    *Konstanz*
    $A dot 1 = A$ $A + 1 = 1$

    *Komplementärgesetz* \
    $A dot LNot(A) = 0$, $A + LNot(A) = 1$

    #SeperatorLine

    *Kommutativgesetz* \
    $A dot B = B dot A$, $A + B = B + A$

    *Assoziativgesetz*\
    $A dot (B dot C) = (A dot B) dot C$\
    $A + (B + C) = (A + B) + C$

    *Distributivgesetz*\
    $A dot (B + C) = A dot B + A dot C$ \
    $A + (B dot C) = (A + B) dot (A + C)$

    #SeperatorLine

    *De Morgan*\
    $LNot((A + B)) = LNot(A) dot LNot(B)$\
    $LNot((A dot B)) = LNot(A) + LNot(B)$

    *Absorptionsgesetz*\
    $A + (A dot B) = A$\
    $A dot (A + B) = A$

    *Resolutionsgesetz (allgemein)*\
    $X dot A + LNot(X) + B = \ = X dot A + LNot(X) dot B + bold(A dot B)$

    *Resolutionsgesetz (speziell)*\
    $X dot A + LNot(X) dot A = A$\
    $(X + A) dot (LNot(X) + A) = A$
  ]

  #bgBlock(fill: colorBoolscheLogic)[
    #subHeading(fill: colorBoolscheLogic)[Boolsche Funktionen]

    $f: {0,1}^n -> {0,1}$

    Variablenmenge: ${x_0, x_1, ..., x_n}$\

    *Literal-*#text("Menge",orange, weight: "bold"): ${x_0, ..., x_n, LNot(x_0), ... LNot(x_n)}$

    $x_0$: Postives Literal $equiv 1$\
    $LNot(x_0)$: Negatives Literal $equiv 0$\

    #SeperatorLine

    #grid(
      columns: (1fr, auto),
      [
        *Literal-*#text("Länge",blue, weight: "bold"): Anzahl der Gattereingänge

        Inverter NICHT mit zählen
      ],
      image("../images/digitaltechnik/literalMenge.jpg", height: 1.6cm),
    )
    #SeperatorLine


    Einsmenge: $F = {underline(v) in {0,1}^n | f(underline(v)) = 1}$ \
    Nullmenge: $overline(F) = {underline(v) in {0,1}^n | f(underline(v)) = 0}$ \
    Don't-Care-Set: ${underline(v) in {0,1}^n | f(underline(v)) = *}$

    Funktionsbündel: $underline(y)  = underline(f)(underline(x))$ \
    $underline(f): {0,1}^n -> {0,1}^m$
    
    #SeperatorLine

    *Kofaktoren* aka Bit $n$ fixen\ 
    $x_i : f_(x_i) = f_(x_i = 1) = f(x_1, ..., 1, ..., x_n)$\
    $overline(x)_i : f_(overline(x)_i) = f_(x_i = 0) = f(x_1, ..., 0, ..., x_n)$
  
    *Substitutionsregel*

    $x_i dot f = x_i dot f_x_i$

    $overline(x)_i dot f = overline(x)_i dot f_overline(x)_i$

    $x_i + f = x_i + f_overline(x)_i$

    $overline(x)_i + f = overline(x)_i + f_x_i$

    *Boolsche Expansion*\
    $f(underline(x)) = x_i dot f_x_i + overline(x)_i dot f_overline(x)_i$

    $f(underline(x)) = (x_i + f_overline(x)_i) dot (overline(x)_i + f_x_i)$

    $overline(f(underline(x))) = overline(x)_i dot overline(f_overline(x)_i) + x_i dot overline(f_x_i)$

    $overline(f(underline(x))) = (overline(x)_i + overline(f_x_i)) dot (x_i + overline(f_overline(x)_i)) $

    *Eigentschaften:*

    tautologisch: $f(underline(x)) = 1, forall underline(x) in {0,1}^n$\
    kontradiktorisch: $f(underline(x)) = 0, forall underline(x) in {0,1}^n$\
    unabhängig von $x_i <=> f_x_i  = f_overline(x)_i$\
    abhängig von $x_i <=> f_x_i  != f_overline(x)_i$\
  ]

  #bgBlock(fill: colorOptimierung)[
    #subHeading(fill: colorOptimierung)[Kannauth Tabelle]
    - Gray-Coding: Ein Kästechen abstand $equiv$ 1 Bit Unterschied

    #grid(
      columns: (auto, 1fr),
      image("../images/digitaltechnik/kan.jpg", height: 2cm),
      [
        - Primiplikant: Blöcken aus $1,2,4,8,16...$ Kästechen
        - Kernprimplikanten: Blöcke die Kästenchen abdecken die sonst keine anderer Block abdecket
      ]
    )

    - Don't Cares (#raw("*")) können belibig auf 1/0 gesetzt werden
    - Große Blöcke sind gute Blöcke
  ]

  #bgBlock(fill: colorOptimierung)[#text(8pt)[
    #subHeading(fill: colorOptimierung)[Min/Max Terme Und DNF/KNF]

    *Hauptsatz der Schaltalgebra:* jedes $f(x_0, ...)$ kann als DNF oder KNF dargestellt werden
    
    #SeperatorLine
    
    *Minimal*: DNF/KNF aus minmalen Anzahl an Literalern
  
    *Prim-Implika(#text(red, "n"))t (pi/Pi)*: Keine freisetzbaren Variablen mehr \
    *Kern-Prim-Implika(#text(red, "n"))t (Kpi/KPi)*: Ein pi/Pi der eine Minterm überdeckt welcher nur von einem pi/Pi überdeckt wird
    

    DNF $<->$ KNF: Algebrisch (*Distributivgesetz*)

    $A "überdeckt" B : B ==> A$

    #SeperatorLine


    #table(columns: (1fr, 1fr),
      fill: (x, y) => if (calc.rem(x, 2) == 1) { tableFillLow } else { tableFillHigh },
      [
        === DNF

        $LNot(x)_0 dot x_1 + x_0 dot x_1 ...$

        Output: Term $tilde.equiv$ $1$-Zeile \
        Input: $x_0 tilde.equiv 1$, $overline(x_0) tilde.equiv 0$
      ],
      [
        === KNF

        $(LNot(x)_0 + LNot(x)_1) dot (x_0 + x_1) dot...$\

        Output: Term $tilde.equiv$ $0$-Zeile
        Input: $x_0 tilde.equiv 0$, $overline(x_0) tilde.equiv 1$
      ],
      [
        besteht aus \ *Min-Term* / *Implika#text(red, "n")t*

        VerODERungen von mehren VerUNDungen

        Sum of Products (SOP)
      ],
      [
        besteht aus \ *Max-Tern* / *Implikat*

        VerUNDungen von mehren VerODERungen

        Product of Sums (POS)
      ],
    )

    *Kannonisch*: Jeder Input Variable muss in jedem Min/Max-Termn enthalten sein
    
    #table(columns: (1fr, 1fr),
      fill: (x, y) => if (calc.rem(x, 2) == 1) { tableFillLow } else { tableFillHigh },
      [
        *KDNF:* Kanonische DNF

        besteht NUR aus *Minterme*

        *DNF $->$ KDNF:* \
        $a + 0 = a + (overline(b) dot b) = a overline(b) + a b$
      ],
      [
        *KKNF:* Kanonische KNF

        besteht NUR aus *Maxterme*

        *KNF $->$ KKNF:* \
        $a dot 1 = a dot (overline(b) + b) = (a + overline(b)) dot (a +  b)$\
      ],
      [
        *DMF:* Disjunktive #underline("Minimal")-Form: \ 
        $ --> LNot(x_0)x_1 + LNot(x_1)$\
      ],
      [
        *KMF:* Konjunktive #underline("Minimal")-Form: \
        $ --> (LNot(x_0) + x_1) dot LNot(x_1)$
      ]
    )

    #table(columns: (1fr, 1fr),
      fill: (x, y) => if (calc.rem(x, 2) == 1) { tableFillLow } else { tableFillHigh },
      [
        *Minterme $m$:* \ $ = LNot(x)_0 dot x_1 dot ...$

        $tilde.equiv$ $1$-Zeile

        Enthält JEDE Eingangsvariable
      ],
      [
        *Maxterme $M$:* \ $ = LNot(x)_0 + x_1 ...$\

        $tilde.equiv$ $0$-Zeile

        Enthält JEDE Eingangsvariable
      ],
      [
        *Implika#text(red, weight: "bold", "n")t* \
        $ = LNot(x)_0 dot x_1 dot ...$ \
        #text(7pt, $"Mintermen" subset.eq "Implika"#text(red, weight: "bold", "n")"t"$)
      ],
      [
        *Implikat* \
        $ = LNot(x)_0 + x_1 + ...$ \
        #text(7pt, $"Maxtermen" subset.eq "Implikat"$)
      ]
    )
  ]]
  #colbreak()

  // Quine McCluskey
  #bgBlock(fill: colorOptimierung)[
    #subHeading(fill: colorOptimierung)[Quine McCluskey]

    Implika#text("n", weight: "black")t: $a b LNot(c) quad$ (DNF)

    Implikat: $a + b + LNot(c) quad$ KNF
  
    === Quine
    1. KNF/DNF $->$ KKNF/KDNF
    2. Primiplikant Bestimme \
      2.1. Terme nach positive Literal ($x_i$) soltieren\
      2.2. Abosbition zwischen zwei unterliegen Blöcken \
        Eine Literal unterschied, #raw("X") müssen matchen \
      2.3. Abhacken was absorbiert wurde \

    #image("../images/digitaltechnik/qmc6.jpg", height: 2.5cm)
    #SeperatorLine
    
    === McClusky
    1. Überdeckungstabelle aufstellen
    #image("../images/digitaltechnik/qmc1.jpg", height: 2.5cm)
    #SeperatorLine

    2. Kernprimimplikanten  finden \
      (Splaten mit nur einem Eintrag) \
      und vom Kerprimiplaten übdeckte \
      NICHT Kernprimstplaten Streichen
    #image("../images/digitaltechnik/qmc3.jpg", height: 2.5cm)
    #SeperatorLine

    3. Splaten dominazen \
      (Dominierte Spalte streichend)

    #image("../images/digitaltechnik/qmc4.jpg", height: 2.5cm)
    #SeperatorLine

    4. Zeilen dominazen \
      (Domenierete Zeile streiche) \
      Kosten: D1 dominierte D2 \
        D1 $<=$ D2 $->$ NUR dann streichen \
    #image("../images/digitaltechnik/qmc5.jpg", height: 2.5cm)
    #SeperatorLine

    5. Wiederhole 3.-5. solange noch was geht
  ]

  // FlipFlops
  #bgBlock(fill: colorState)[
    #subHeading(fill: colorState)[Latches, Flipflops und Register]

    === D-Latch
    #grid(
      columns: (auto, auto),
      column-gutter: 2mm,
      image("../images/digitaltechnik/dlatch3.jpg", height: 2cm),
      image("../images/digitaltechnik/dlatch4.jpg", height: 2cm)
    )

    === Register

    #grid(
      columns: (auto, auto),
      column-gutter: 2mm,
      image("../images/digitaltechnik/register1.jpg", height: 1.6cm),
      image("../images/digitaltechnik/register2.jpg", height: 1.6cm)
    )

    === Hold/Setup Zeit
    #grid(
      columns: (auto, 1fr),
      column-gutter: 2mm,
      image("../images/digitaltechnik/dlatch2.jpg", height: 3cm),
      text(7.5pt, [
        $t_"c2q"$: Delay vom Latch \
        $t_"hold"\/t_"setup"$: D muss stabile sein 

        *Setup Bedigung* \
        $T_"clk" > T_"c2q" + T_"logic,max"+ T_"setup" $
        
        Maximal Talk = $f = 1/(T_"clk")$

        *Hold Bedigung* \
        $T_"c2q" + T_"logic,min" > T_"hold" $ \
        Problem bei unterschiedlichen Register und straight wire
      ])
    )
  ]
  
  // Pipelining
  #bgBlock(fill: colorState)[
    #subHeading(fill: colorState)[Pipeline/Parallele Verarbeitungseinheiten]

    *Pipelining*
    #image("../images/digitaltechnik/pipeline2.jpg")
    #image("../images/digitaltechnik/pipeline1.jpg")

    - Split am besten bei Balanced $t_"logic"$
    - $t_"c2q"$ des mitleren Latches kommt dazu
    - *Setup Bedigung* \
      $T_"clk" > T_"c2q" + T_"logic,max"+ T_"setup" $
    - *Hold Bedigung* \
        $T_"c2q" + T_"logic,min" > T_"hold" $ \
        Buffer-Gatter einfügen bei Verletzung

    $T = 1/f quad f = 1/T$

    #block(inset: (top: -0.2cm), scale(90%, align(center)[
      #cetz.canvas({
        import cetz.draw: *

        let signal-height = 0.45
        let width = 6
        let switching-width = 0.3
        let padding = 0.3

        let setup = 0.8
        let hold = 1.4
        let c2q = 0.7
        let clk = 2

        line((width / 2 - 1.4, 0.6), (width / 2, 0.6), mark: (end: "straight"), name: "T")
        content("T.centroid", $t$, anchor: "south", padding: 0.1)

        let timing-line(x, depth: 2.7, peak: 0) = {
          line((x, signal-height / 2 + peak * (signal-height / 2 + padding)), (x, - depth * (signal-height + padding) - signal-height / 2), stroke: (dash: "dashed", paint: gray))
        }

        timing-line(-clk, depth: 3.3, peak: 1.1)
        timing-line(clk, depth: 3.3)
        timing-line(-clk + c2q, peak: 0.3)
        timing-line(clk - setup)
        timing-line(-clk + hold, depth: 1.6, peak: 1.1)

        content((-width / 2, 0), [Clock], anchor: "east", padding: 0.1)
        line(stroke: blue,
          (-width / 2, -signal-height / 2),
          (-switching-width/2 - clk, -signal-height / 2),
          (switching-width/2 - clk, signal-height / 2),
          (-switching-width/2, signal-height / 2),
          (switching-width/2, -signal-height / 2),
          (-switching-width/2 + clk, -signal-height / 2),
          (switching-width/2 + clk, signal-height / 2),
          (width/2, signal-height / 2)
        )

        group({
          translate(y: -(signal-height + padding))
          content((-width / 2, 0), [Output], anchor: "east", padding: 0.1)

          line(
            stroke: blue,
            (-width / 2, signal-height / 2),
            (-clk + c2q - switching-width / 2, signal-height / 2),
            (-clk + c2q, 0),
            (-clk + c2q - switching-width / 2, -signal-height / 2),
            (-width / 2, -signal-height / 2),
          )

          line(
            close: true,
            stroke: blue,
            (-clk + c2q, 0),
            (-clk + c2q + switching-width / 2, signal-height / 2),
            (clk + c2q - switching-width / 2, signal-height / 2),
            (clk + c2q, 0),
            (clk + c2q - switching-width / 2, -signal-height / 2),
            (-clk + c2q + switching-width / 2, -signal-height / 2),
          )

          line(
            stroke: blue,
            (width / 2, signal-height / 2),
            (clk + c2q + switching-width / 2, signal-height / 2),
            (clk + c2q, 0),
            (clk + c2q + switching-width / 2, -signal-height / 2),
            (width / 2, -signal-height / 2),
          )
        })

        group({
          translate(y: - 2 *(signal-height + padding))
          content((-width / 2, 0), [Data], anchor: "east", padding: 0.1)

          line(
            stroke: blue,
            (-width / 2, signal-height / 2),
            (-clk - setup - switching-width / 2, signal-height / 2),
            (-clk - setup, 0),
            (-clk - setup - switching-width / 2, -signal-height / 2),
            (-width / 2, -signal-height / 2),
          )

          line(
            stroke: blue,
            close: true,
            (-clk - setup, 0),
            (-clk - setup + switching-width / 2, signal-height / 2),
            (-clk + hold - switching-width / 2, signal-height / 2),
            (-clk + hold, 0),
            (-clk + hold - switching-width / 2, -signal-height / 2),
            (-clk - setup + switching-width / 2, -signal-height / 2),
          )

          line(
            stroke: blue,
            fill: blue.transparentize(70%),
            close: true,
            (-clk + hold, 0),
            (-clk + hold + switching-width / 2, signal-height / 2),
            (clk - setup - switching-width / 2, signal-height / 2),
            (clk - setup, 0),
            (clk - setup - switching-width / 2, -signal-height / 2),
            (-clk + hold + switching-width / 2, -signal-height / 2),
          )

          line(
            stroke: blue,
            (width / 2, -signal-height / 2),
            (clk - setup + switching-width / 2, -signal-height / 2),
            (clk - setup, 0),
            (clk - setup + switching-width / 2, signal-height / 2),
            (width / 2, signal-height / 2),
          )
        })

        let arrow(from, to, height, text) = {
          let annotation-height = - height * (signal-height + padding)

          line((from, annotation-height), (to, annotation-height), mark: (end: "straight", start: "straight", scale: 0.75), name: "A")
          content("A", text, anchor: "south", padding: 0.05)
        }

        arrow(-clk + c2q, -clk + hold, -0.5, $t_"l,min"$)
        arrow(-clk, -clk + hold, -1.1, $t_"hold"$)

        arrow(-clk, -clk + c2q, 3, $t_"c2q"$)
        arrow(clk - setup, clk, 3, $t_"setup"$)
        arrow(-clk, clk, 3.6, $t_"clk"$)
        arrow(-clk + c2q, clk - setup, 3, $t_"logic,max"$)
      })
    ]
  ))


    #SeperatorLine
    
    *Parallel Verarbeitung*
    #image("../images/digitaltechnik/parallel.jpg", height: 3cm)

    - Ver-$n$-fachung des Durcksates
    - Viele höher Kosten

  ]
]
#pagebreak()
#columns(2, gutter: 2mm)[
  // Mosfet
  #bgBlock(fill: colorRealsierung)[
    #subHeading(fill: colorRealsierung)[FET/MOSFET Funktion]

    #grid(columns: (1fr, 2fr), 
      image("../images/digitaltechnik/gateWidth.jpg", height: 3cm),
      
      [
        #grid(columns: (1fr, 1fr),
          row-gutter: 2.5mm,
          [$W$: Kanalweite], [$L$: Kanallänge],
          [Löcherbeweglichkeit: $mu_"n/p"$],
          [Oxdy Dicke: $t_"ox"$],
          [Oxdy Dielektriukum: $epsilon_"ox"$],
          [$mu_"n" = 1,6 mu_"p" "bis" 3,5 mu_"p"$],
        )

        $L$ wir immer so kleine wie möglich gewählt $= L_"min"$

        $beta_"n/p" = (mu_"n/p" epsilon_0 epsilon_"ox")/(t_"ox") W/L = K'_"n/p" W/L quad beta = [A/V^2]$
      ]
    )

    #grid(columns: (auto, auto, auto, auto),
      column-gutter: 6mm,
      $mu_"n" approx 250 dot 10^(-4) m^2/(V s)$,
      $mu_"p" approx 100 dot 10^(-4) m^2/(V s)$,
      $epsilon_0 approx 8,8541878 dot 10^(-12) (A s)/(V m)$,
      $epsilon_"ox" approx 3,9$,
    )


    #table(columns: (1fr, 1fr),
      fill: (x, y) => if (calc.rem(y, 2) == 1) { tableFillLow } else { tableFillHigh },

      [*NMOS*], [*PMOS*],

      align(center+horizon, scale(
        x: 75%, y: 75%,
        zap.circuit({
          import cetz.draw : *
          import zap : *
          rect((1.5,0),(4-1.5, 0.1), fill: rgb("#535353"), stroke: none)
          rect((0,0),(4,-1), fill: pTypeFill, stroke: none)
          rect((0.5,-0),(1.5, -0.5), fill: nTypeFill, stroke: none)
          rect((4 - 1.5,-0),(4-0.5, -0.5), fill: nTypeFill, stroke: none)
          rect((1.5,-0),(2.5, -0.5), fill: none, stroke: (paint: black, dash: "dotted", thickness: 0.06))

          line((3, 0.3), (3, 0))
          line((1, 0.3), (1, 0))
          line((2, 0.3), (2, 0.1))

          cetz.decorations.brace((2.5,-0.6),(1.5,-0.6))
          content((2, -1.3), "Channel")
          content((3, -0.25), $"n"^+$)
          content((1, -0.25), $"n"^+$)
          content((0.5, -0.75), "p")

          content((3, 0.5), "S")
          content((1, 0.5), "D")
          content((2, 0.5), "G")
        })
      )),
      align(center+horizon,scale(
        x: 75%, y: 75%,
        zap.circuit({
          import cetz.draw : *
          import zap : *
          rect((1.5,0),(4-1.5, 0.1), fill: rgb("#535353"), stroke: none)
          rect((0,0),(4,-1), fill: nTypeFill, stroke: none)
          rect((0.5,-0),(1.5, -0.5), fill: pTypeFill, stroke: none)
          rect((4 - 1.5,-0),(4-0.5, -0.5), fill: pTypeFill, stroke: none)
          rect((1.5,-0),(2.5, -0.5), fill: none, stroke: (paint: black, dash: "dotted", thickness: 0.06))

          line((3, 0.3), (3, 0))
          line((1, 0.3), (1, 0))
          line((2, 0.3), (2, 0.1))

          cetz.decorations.brace((2.5,-0.6),(1.5,-0.6))
          content((2, -1.3), "Channel")
          content((3, -0.25), $"p"^+$)
          content((1, -0.25), $"p"^+$)
          content((0.5, -0.75), "n")

          content((3, 0.5), "S")
          content((1, 0.5), "D")
          content((2, 0.5), "G")
        })
      )),

      image("../images/digitaltechnik/nmos3.jpg", height: 2cm),
      image("../images/digitaltechnik/pmos3.jpg", height: 2cm),

      grid(columns: (auto, 1fr), 
        column-gutter: 3mm,
        image("../images/digitaltechnik/nmosD.jpg", height: 3cm),
        [
          - Source am *niedrigen* Potenzial (*GND*)
          - Guter PULL-DOWN
          - Substrat am GND
        ]
      ),
      grid(columns: (auto, 1fr),
        column-gutter: 3mm,
        image("../images/digitaltechnik/pmosD.jpg", height: 3cm),
        [
        - Source am *hohes* Potenzial (*$V_"DD"$*)
        - Guter PULL-UP
        - Substrat am $V_"DD"$
        ]
      ),

      block( inset: (top: 2mm, bottom: 2mm),$ I_"Dn" = cases(
        gap: #0.6em,
        0 & 0 < U_"GS" < U_t,
        beta_n (U_"GS" - U_t - U_"DS" / 2) U_"DS" quad & cases(delim: #none, U_"GS" >= U_t, 0 < U_"DS" < U_"GS" - U_t),
        beta_n/2 (U_"GS" - U_"th")^2 & cases(delim: #none, U_"GS" >= U_t, U_"DS" > U_"GS" - U_t)
      )$),
      block( inset: (top: 2mm, bottom: 2mm), $I_"Dp" = cases(
        gap: #0.6em,
        0 & 0 > U_"GS" > U_t,
        -beta_p (U_"GS" - U_t - U_"DS" / 2) U_"DS" quad & cases(delim: #none, U_"GS" <= U_t, 0 > U_"DS" > U_"GS" - U_t),
        -beta_p/2 (U_"GS" - U_"th")^2 & cases(delim: #none, U_"GS" <= U_t, U_"DS" < U_"GS" - U_t)
      )$),

      grid(
        columns: (auto, auto),
        column-gutter: 2mm,
        image("../images/digitaltechnik/nmos1.jpg", height: 2.5cm),
        image("../images/digitaltechnik/nmos2.jpg", height: 2.5cm),
      ),
      grid(
        columns: (auto, auto),
        column-gutter: 2mm,
        image("../images/digitaltechnik/pmos4.jpg", height: 2.5cm),
        image("../images/digitaltechnik/pmos2.jpg", height: 2.5cm),
      ),
    )
  ]

  #colbreak()

  #columns(2, gutter: 2mm)[
    // CMOS Verlust Inverter
    #bgBlock(fill: colorRealsierung)[
      #subHeading(fill: colorRealsierung)[CMOS Inverter Verlustleistung]
      
      #align(center+horizon, image("../images/digitaltechnik/cmosPower.jpg", height: 5cm))

      #grid(
        columns: (1fr, 1fr),
        $P_"stat" ~ e^(-V_T)$,
        $P_"dyn"~ V_"DD"^2$
      )

      #table(
        columns: (1fr, 1fr),
        fill: (x, y) => if (calc.rem(x, 2) == 1) { tableFillLow } else { tableFillHigh },
        
        [*Dynamisch* \ Nur beim Schalten], [*Statisch*],
        [
          
          *Kurzschluss/Quer* $P_"short"\/ I_"short"$
        
          #text(8pt, $P_"short" = a_01 f beta_n tau (V_"DD" - 2 V_"Tn")^3$)

          $tau$: Kurzschluss/Schaltzeit
        ], 
        [
          *Leckstrom*
        
          Durch die NP-Übergänge am FET


          *Sub Schwellstrom*
        ],
        [
          *Kapazitiv* $P_C \/ I_C$ \
          Lade Strome des $C_L$ $i_c$

          $P_"cap" = alpha_01 f C_L V_"DD"^2$
        ],
        [
          *Gate-Strom*

          Nicht perfekte Isolations des Gates
        ]
      )

      #SeperatorLine

      #grid(columns: (1fr, 1fr),
        row-gutter: 1mm,
        image("../images/digitaltechnik/verlust1.jpg", height: 2.3cm),
        image("../images/digitaltechnik/verlust3.jpg", height: 2.3cm),
        image("../images/digitaltechnik/verlust4.jpg", height: 2.3cm),
        align(horizon, [
          #text(rgb("#08468d"), [Dyn. Verlustleistung])
          #text(rgb("#067154"), [Stat. Verlustleistung])
          #text(rgb("#a50003"), [Verzögerungs-Zeit])
        ])
      )
    ]

    // Schaltungstheorie
    #bgBlock(fill: colorRealsierung)[
      #subHeading(fill: colorRealsierung)[Schaltungstheorie]

      Ohm: $U = R dot I = [V = Omega dot A]$

      *Kapazität* \
      $tau = R C$

      Charging: $V_C (t) = V_0 dot (e^(-t/tau))$

      Discharging: $V_C (t) = V_0 dot (1 - e^(-t/tau))$

      #grid(
        columns: (1fr, 1fr),
        column-gutter: 2mm,
        row-gutter: 3.5mm,
        $q(t) = C dot u(t)$,
        $i(t) = C dot (d u)/(d g t)$,
        $[C] = F = (A s)/V$,
        $E_"C" = q^2/2 C = C/2 u^2$,
        $E = integral_(t_1)^(t_2) i(t) u(t)$,
        $E = integral_(q_1)^(q_2) chi(q) d q$,
      )
    ]

    // CMOS Verzögerung
    #bgBlock(fill: colorRealsierung)[
      #subHeading(fill: colorRealsierung)[CMOS Verzögerung]

      *Inverter*\
      $beta_"n/p" = (mu_"n/p" epsilon_0 epsilon_"ox")/(t_"ox") W/L = K'_"n/p" W/L$

      $C_"G" = epsilon_"ox" epsilon_0 (W L)/t_"ox"$,

      $mu_"n" = 1,6 mu_"p" "bis" 3,5 mu_"p" ==> mu_"n" > mu_"p" \
      ==> beta_"n">beta_"p" => R_"on"_"n" < R_"on"_"p" => t_"nLH" < t_"pLH"
      $


      *Vereinfachung Saturations Bereich* \
      $R_"on" = U_"DS"/I_"D" approx 1/beta(abs(V_"GS")- abs(V_"T"))$

      #table(
        columns: (1fr, 1fr),
        fill: (x, y) => if (calc.rem(x, 2) == 1) { tableFillLow } else { tableFillHigh },
        [*Steigend mit*],
        [*Sinkend mit*],
        [
          - Last $C_L$
          - Oxyddicke $T_"ox"$
          - Kandlalänge $L_"p/n"$
          - Schwellspannung $V_"Tp/n"$ 
        ],
        [
          - Kanalweite
          - Landsträger Veweglichkeit $mu_"p/n"$
        ],

      )
    
      $t_("p/nLH") ~ (C_"L" t_"ox" L_"p/n")/(W_"p/n" mu_"p/n" epsilon(V_"DD" - abs(V_"Tpn"))) =  C_L/(beta(V_"DD" - abs(V_"T"))) $
    ]

    #colbreak()
    // CMOS Verlust Gesamt
    #bgBlock(fill: colorRealsierung)[
      #subHeading(fill: colorRealsierung)[Verlustleistung]
      
      $"#Schaltvorgänge"$ : Ganzer High-Low Cycles eines Signals

      #linebreak()

      $"Energie pro Schaltvorgang:" \ "Lade Verlust" + "Geladene Energie" \ 
      = E_"stored" + E_"heat" = C_L V_"DD"^2$ (unabhängig von $R_"on"$)

      #linebreak()

      $alpha = "#Schaltvorgänge"/"#Takte (#Clk Flanken)"$

      $P_"cap" = alpha dot f_"clk" dot C dot U_"DD"^2$

    ]

    // CMOS Circits
    #bgBlock(fill: colorRealsierung)[
      #subHeading(fill: colorRealsierung)[CMOS]
      $hat(=)$ Complemntary MOS

      #table(
        columns: (1fr, 1fr),
        scale(75%, 
          zap.circuit({
            import zap : *
            import cetz.draw : content
            import "../lib/circuit.typ" : *

            set-style(wire: (stroke: (thickness: 0.025)))

            registerAllCustom();
            fet("N0", (0,0), type: "N", angle: 90deg);
            fet("P0", (0,1), type: "P", angle: 90deg);
            wire("N0.G", (rel: (-0.1, 0)), (horizontal: (), vertical: "P0.G"), "P0.G")

            node("outNode", (0,0.5))
            node("inNode", (-0.6,0.5))
            wire((-1, 0.5), "inNode")
            wire((0.2, 0.5), "outNode")

            node("N2", (0,-0.5))
            node("N2", (0,1.5))

            wire((-1, -0.5), (0.5, -0.5))
            wire((-1, 1.5), (0.5, 1.5))

            content((-1, 0.5), scale($"X"$, 60%), anchor: "east")
            content((0.45, 0.5), scale($overline("X")$, 60%), anchor: "east")
            content((-0.9, 1.5), scale($"U"_"DD"$, 60%), anchor: "east")
            content((-0.9, -0.5), scale($"GND"$, 60%), anchor: "east")
          })
        ),
        [
          *Inverter*
          
          $overline(X)$

        ],
      
        block(height: 3cm, clip: true, inset: (bottom: 1.5cm), scale(75%, zap.circuit({
          import zap : *
          import cetz.draw : content
          import "../lib/circuit.typ" : *

          set-style(wire: (stroke: (thickness: 0.025)))

          registerAllCustom();
          fet("P0", (0.5,0.25), type: "P", angle: 90deg);
          fet("P1", (0.5,1.25), type: "P", angle: 90deg);
          fet("N0", (0,-1), type: "N", angle: 90deg);
          fet("N1", (1,-1), type: "N", angle: 90deg);

          content((-0.7, 1.75), scale($"V"_"DD"$, 60%), anchor: "east")
          content((-0.7, -1.5), scale($"GND"$, 60%), anchor: "east")

          content("N0.G", scale($"B"$, 60%), anchor: "east")
          content("P0.G", scale($"B"$, 60%), anchor: "east")
          content("N1.G", scale($"A"$, 60%), anchor: "east")
          content("P1.G", scale($"A"$, 60%), anchor: "east")

          wire((-0.75, -1.5), (1.5, -1.5))
          wire((-0.75, 1.75), (1.5, 1.75))

          wire("N0.S", "N1.S")
          node("N2", "P0.D")
          wire("N2", (horizontal: (), vertical: "N0.S"))
          node("N3", "N0.D")
          node("N4", "N1.D")
          node("N5", "P1.S")
          node("N6", (horizontal: (), vertical: "N0.S"))

          wire("N2", (horizontal: (rel: (0.5, 0)), vertical: "N2"))
          
          content((horizontal: (rel: (0.65, 0)), vertical: "N2"), scale($"Y"$, 60%))
        }))),

        [
          *NOR*

          $LNot(A + B) = Y \ = LNot(A) dot LNot(B)$

        ],

        block(height: 3cm, clip: true, inset: (bottom: 1.5cm), scale(75%, zap.circuit({
          import zap : *
          import cetz.draw : content
          import "../lib/circuit.typ" : *

          set-style(wire: (stroke: (thickness: 0.025)))

          registerAllCustom();
          content((-0.7,  0.5), scale($"V"_"DD"$, 60%), anchor: "east")
          content((-0.7, -2.75), scale($"GND"$, 60%), anchor: "east")

          fet("P0", (0, 0), type: "P", angle: 90deg);
          fet("P1", (1, 0), type: "P", angle: 90deg);
          fet("N0", (0.5,-1.25), type: "N", angle: 90deg);
          fet("N1", (0.5,-2.25), type: "N", angle: 90deg);

          wire((-0.75, 0.5), (1.5, 0.5))
          wire((-0.75, -2.75), (1.5, -2.75))
          wire("P0.D", "P1.D")

          node("N2", (horizontal: "N1.D", vertical: "P0.D"))
          node("N3", "N0.S")
          wire("N2", "N3")
          wire("N3", (rel: (0.5, 0)))

          content((horizontal: (rel: (0.65, 0)), vertical: "N3"), scale($"Z"$, 60%))
          node("4", "P0.S")
          node("4", "P1.S")
          node("4", "N1.D")

          content("N0.G", scale($"B"$, 60%), anchor: "east")
          content("P0.G", scale($"B"$, 60%), anchor: "east")
          content("N1.G", scale($"A"$, 60%), anchor: "east")
          content("P1.G", scale($"A"$, 60%), anchor: "east")
        }))),

        [
          *NAND*

          $LNot(A dot B) = Z \ = LNot(A) + LNot(B)$
        ],
      )
    ]

    #colbreak()
    // Dotierung
    #bgBlock(fill: colorRealsierung)[
      #subHeading(fill: colorRealsierung)[Dotierung]
      #table(
        columns: (auto, 1fr),
        [N-Type],
        [
          - Dotierung: Phosphor (V)
          - Negative Ladgunsträger ($e^-$)
          - mehr Elektron als Si
        ],
        [P-Type],
        [
          - Dotierung:  Bor (III)
          - Postive Landsträger (Löcher)
          - mehr Löcher als Si
        ]
      )

      #zap.circuit({
        import cetz.draw : *
        import zap : *

        diode("A", (0,1.7), (3,1.7), fill: black, i: (content: $i_d$, anchor: "south"))

        rect((0,0),(1,1), fill: pTypeFill, stroke: none)
        rect((2,0),(3,1), fill: nTypeFill, stroke: none)
        rect((1,0), (1.5,1), fill: color.lighten(pTypeFill, 50%), stroke: none)
        rect((1.5,0), (2,1), fill: color.lighten(nTypeFill, 50%), stroke: none)
        line((2, 0), (2, 1), stroke: (dash: "dotted"))
        line((1, 0), (1, 1), stroke: (dash: "dotted"))
        line((1.5, 0), (1.5, 1), stroke: (dash: "densely-dotted"))

        cetz.decorations.brace((2,-0.1),(1,-0.1))
        content((1.5, -0.6), "RLZ")
        content((2.5, 0.5), "N")
        content((0.5, 0.5), "P")
        content((1.25, 0.5), "-")
        content((1.75, 0.5), "+")
      })

      /*
      #grid(
        columns: (1fr, 1fr),
        column-gutter: 6mm,
        align: center,
        [#align(center)[*NMOS*]], [#align(center)[*PMOS*]],
        grid.cell(inset: 2mm,
          align(center, 
            zap.circuit({
              import "../lib/circuit.typ" : *

              registerAllCustom();
              fet("T", (0,0), type: "N", scale: 150%);
            })
          )
        ),
        grid.cell(inset: 2mm,
          align(center, 
            zap.circuit({
              import "../lib/circuit.typ" : *

              registerAllCustom();
              fet("T", (0,0), type: "P", scale: 150%);
            }),
          )
        ),
        scale(
          x: 75%, y: 75%,
          zap.circuit({
            import cetz.draw : *
            import zap : *
            rect((1.5,0),(4-1.5, 0.1), fill: rgb("#535353"), stroke: none)
            rect((0,0),(4,-1), fill: pTypeFill, stroke: none)
            rect((0.5,-0),(1.5, -0.5), fill: nTypeFill, stroke: none)
            rect((4 - 1.5,-0),(4-0.5, -0.5), fill: nTypeFill, stroke: none)
            rect((1.5,-0),(2.5, -0.5), fill: none, stroke: (paint: black, dash: "dotted", thickness: 0.06))

            line((3, 0.3), (3, 0))
            line((1, 0.3), (1, 0))
            line((2, 0.3), (2, 0.1))

            cetz.decorations.brace((2.5,-0.6),(1.5,-0.6))
            content((2, -1.3), "Channel")
            content((3, -0.25), $"n"^+$)
            content((1, -0.25), $"n"^+$)
            content((0.5, -0.75), "p")

            content((3, 0.5), "S")
            content((1, 0.5), "D")
            content((2, 0.5), "G")
          })
        ),
        scale(
          x: 75%, y: 75%,
          zap.circuit({
            import cetz.draw : *
            import zap : *
            rect((1.5,0),(4-1.5, 0.1), fill: rgb("#535353"), stroke: none)
            rect((0,0),(4,-1), fill: nTypeFill, stroke: none)
            rect((0.5,-0),(1.5, -0.5), fill: pTypeFill, stroke: none)
            rect((4 - 1.5,-0),(4-0.5, -0.5), fill: pTypeFill, stroke: none)
            rect((1.5,-0),(2.5, -0.5), fill: none, stroke: (paint: black, dash: "dotted", thickness: 0.06))

            line((3, 0.3), (3, 0))
            line((1, 0.3), (1, 0))
            line((2, 0.3), (2, 0.1))

            cetz.decorations.brace((2.5,-0.6),(1.5,-0.6))
            content((2, -1.3), "Channel")
            content((3, -0.25), $"p"^+$)
            content((1, -0.25), $"p"^+$)
            content((0.5, -0.75), "n")

            content((3, 0.5), "S")
            content((1, 0.5), "D")
            content((2, 0.5), "G")
          })
        ),
      )
      */
    ]


    #SIPrefixesTable


    #bgBlock(fill: colorBoolscheLogic)[
      #subHeading(fill: colorBoolscheLogic)[MUX/DEMUX]

      #table(columns: (1fr, 1fr),
        fill: (x, y) => if (calc.rem(x, 2) == 1) { tableFillLow } else { tableFillHigh },
        [*MUX*],
        [*DEMUX*], 
        [$ D = LNot(S)A + S B $],
        [$ A = LNot(S)D \ B = S D $],
        image("../images/digitaltechnik/mux1.jpg", height: 1.5cm),
        image("../images/digitaltechnik/demux1.jpg", height: 1.5cm),
        image("../images/digitaltechnik/mux2.jpg", height: 1.5cm),
        image("../images/digitaltechnik/demux2.jpg", height: 1.5cm),
      )

    ]

    #colbreak()

    // Voll adierer
    #bgBlock(fill: colorBoolscheLogic)[
      #subHeading(fill: colorBoolscheLogic)[HA/VA/Ripple Carry Adder]

        === Halbaddierer
        #grid(
        columns: (auto, 1fr),
        column-gutter: 2mm,
        image("../images/digitaltechnik/ha.jpg", height: 1.5cm),
        text(8pt, [
          $C_"out" = A dot B \
          S = A xor B \ 
          = A LNot(B) + LNot(A) B $

        ])
      )

      #SeperatorLine
      
      === Voll Addierer
      #grid(  
        columns: (auto, 1fr),
        column-gutter: 2mm,
        image("../images/digitaltechnik/va.jpg", height: 1.5cm),
        text(8pt, [
          *Generate/Propergated*

          $G = A dot B \
          P = A xor B \ 
          = A LNot(B) + LNot(A) B $

        ])
      )

      #table(columns: (auto, auto, auto, auto),
        [*$A$*], [*$B$*], [*$C_"out"$*], "",
        $0$, $0$, $0$, raw("KILL"),
        $0$, $1$, $C_"in"$, raw("PROPERGATE"),
        $1$, $0$, $C_"in"$, raw("PROPERGATE"),
        $1$, $1$, $1$, raw("GENERATE"),
      )


      $C_"out" = ((A xor B) dot C_"in") + A B $

      $S = A xor B xor C_"in"$

      #SeperatorLine

      *Ripple Carry Adder*

      $t_"pd"$ dominiert von Carry-Übertrag

      Timing-Worst-Case: \
      - erster VA: #raw("GENERATE") \
        Rest: nur #raw("PROPERGATE")

      Timing-Best-Case: \
      - Alle: #raw("GENERATE") oder #raw("KILL")

      #image("../images/digitaltechnik/rippleAdder.jpg", height: 2cm)
    ]

    #colbreak()
    #bgBlock(fill: colorBoolscheLogic)[
    #subHeading(fill: colorBoolscheLogic)[Logik Gatter]

    #table(columns: (auto, 1fr),
      fill: (x, y) => if (calc.rem(y, 2) == 1) { tableFillLow } else { tableFillHigh },
      align(center, box(image("../images/digitaltechnik/logicGates.jpg", height: 6cm, fit: "cover"), clip: true, height: 6cm/4)),
      align(center+horizon, [*AND* \ $and space dot$]),

      align(center, box(inset: (top: -6cm/4), image("../images/digitaltechnik/logicGates.jpg", height: 6cm, fit: "cover"), clip: true, height: 6cm/4)),
      align(center+horizon, [*OR* \ $or space +$]),

      align(center, box(inset: (top: -6cm/4 * 2), image("../images/digitaltechnik/logicGates.jpg", height: 6cm, fit: "cover"), clip: true, height: 6cm/4)),
      align(center+horizon, [*XOR* \ $xor$]),

      align(center, box(inset: (top: -6cm/4 *3), image("../images/digitaltechnik/logicGates.jpg", height: 6cm, fit: "cover"), clip: true, height: 6cm/4)),
      align(center+horizon, [*NOT* \ $not space LNot(X)$])
    )

    #grid(
      columns: (auto, 1fr),
      column-gutter: 4mm,
      row-gutter: 2.5mm,
      [XOR], $A LNot(B) + LNot(A) B = A xor B$,
      [XNOR], $A B + LNot(A) LNot(B)$,
      [NOR], $LNot(A + B) = LNot(A) dot LNot(B)$,
      [NAND], $LNot(A dot B) = LNot(A) + LNot(B)$,
    
    )

    #truth-table(
      outputs: (
        ("AND",  (0, 0, 0, 1)),
        ("OR",   (0, 1, 1, 1)),
        ("XOR",  (0, 1, 1, 0)),
      ),
      inputs: ("A", "B")
    )

    #truth-table(
      outputs: (
        ("NAND", (1, 1, 1, 0)),
        ("NOR",  (1, 0, 0, 0)),
        ("XNOR", (1, 0, 0, 1)),
      ),
      inputs: ("A", "B")
    )
  ]
  ]
]